[master] a50db76fa Grossly hack the (debug only) persistent stevedore to cope with ASLR

Poul-Henning Kamp phk at FreeBSD.org
Wed May 4 07:07:06 UTC 2022


commit a50db76fa6c5f8092a1a7f0a63a2a8860ec81e8c
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed May 4 07:06:48 2022 +0000

    Grossly hack the (debug only) persistent stevedore to cope with ASLR

diff --git a/bin/varnishd/storage/mgt_storage_persistent.c b/bin/varnishd/storage/mgt_storage_persistent.c
index 77e8049a2..22c58ab19 100644
--- a/bin/varnishd/storage/mgt_storage_persistent.c
+++ b/bin/varnishd/storage/mgt_storage_persistent.c
@@ -143,7 +143,8 @@ smp_mgt_init(struct stevedore *parent, int ac, char * const *av)
 	struct smp_sc		*sc;
 	struct smp_sign		sgn;
 	void *target;
-	int i;
+	int i, mmap_flags;
+	uintptr_t up;
 
 	ASSERT_MGT();
 
@@ -205,12 +206,38 @@ smp_mgt_init(struct stevedore *parent, int ac, char * const *av)
 	else
 		target = NULL;
 
+	mmap_flags = MAP_NOCORE | MAP_NOSYNC | MAP_SHARED;
+	if (target) {
+		mmap_flags |= MAP_FIXED;
+#ifdef MAP_EXCL
+		mmap_flags |= MAP_EXCL;
+#endif
+	} else {
+		/*
+		 * I guess the people who came up with ASLR never learned
+		 * that virtual memory can have benficial uses, because they
+		 * added no facility for realiably and portably allocing
+		 * stable address-space.
+		 * This stevedore is only for testing these days, so we
+		 * can get away with just hacking something up: 16M below
+		 * the break seems to work on FreeBSD.
+		 */
+		up = (uintptr_t)sbrk(0);
+		up -= 1ULL<<24;
+		up -= sc->mediasize;
+		up &= ~(getpagesize() - 1ULL);
+		target = (void *)up;
+
+#ifdef MAP_ALIGNED_SUPER
+		mmap_flags |= MAP_ALIGNED_SUPER;
+#endif
+	}
 	sc->base = (void*)mmap(target, sc->mediasize, PROT_READ|PROT_WRITE,
-	    MAP_NOCORE | MAP_NOSYNC | MAP_SHARED, sc->fd, 0);
+	    mmap_flags, sc->fd, 0);
 
 	if (sc->base == MAP_FAILED)
-		ARGV_ERR("(-spersistent) failed to mmap (%s)\n",
-		    VAS_errtxt(errno));
+		ARGV_ERR("(-spersistent) failed to mmap (%s) @%p\n",
+		    VAS_errtxt(errno), target);
 	if (target != NULL && sc->base != target)
 		fprintf(stderr, "WARNING: Persistent silo lost to ASLR %s\n",
 		    sc->filename);


More information about the varnish-commit mailing list