mmap(2) with MAP_ANON honouring offset although it shouldn't

Alexander Best alexbestms at math.uni-muenster.de
Wed Oct 21 15:51:13 UTC 2009


although the mmap(2) manual states in section MAP_ANON:

"The offset argument is ignored."

this doesn't seem to be true. running

printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_NONE, MAP_ANON, -1,
0x12345678));

and

printf("%p\n", mmap((void*)0x1000, 0x1000, PROT_NONE, MAP_ANON, -1, 0));

produces different outputs. i've attached a patch to solve the problem. the
patch is similar to the one proposed in this PR, but should apply cleanly to
CURRENT: http://www.freebsd.org/cgi/query-pr.cgi?pr=kern/71258

cheers.
alex
-------------- next part --------------
--- src/sys/vm/vm_mmap.c	2009-10-21 04:13:24.000000000 +0200
+++ src/sys/vm/vm_mmap.c	2009-10-21 04:13:43.000000000 +0200
@@ -245,15 +245,18 @@
 	}
 
 	/*
-	 * Align the file position to a page boundary,
-	 * and save its page offset component.
+	 * Unless the MAP_ANON flag is set, align the file position
+	 * to a page boundary and save its page offset component.
 	 */
-	pageoff = (pos & PAGE_MASK);
-	pos -= pageoff;
-
-	/* Adjust size for rounding (on both ends). */
-	size += pageoff;			/* low end... */
-	size = (vm_size_t) round_page(size);	/* hi end */
+	if (flags & MAP_ANON) {
+		pageoff = pos = 0;
+	} else {
+		pageoff = (pos & PAGE_MASK);
+		pos -= pageoff;
+		/* Adjust size for rounding (on both ends). */
+		size += pageoff;			/* low end... */
+		size = (vm_size_t) round_page(size);	/* hi end */
+	}
 
 	/*
 	 * Check for illegal addresses.  Watch out for address wrap... Note
@@ -300,7 +303,6 @@
 		handle = NULL;
 		handle_type = OBJT_DEFAULT;
 		maxprot = VM_PROT_ALL;
-		pos = 0;
 	} else {
 		/*
 		 * Mapping file, get fp for validation and


More information about the freebsd-hackers mailing list