Skip to contentSkip to conversation

mmap.2: Document danger of mappings larger than PTRDIFF_MAX

PATCH In review Jann Horn 2 participants 3 messages Apr 29, 2025 – May 2, 2025

Subsystems (2)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH v2 1/1 mmap.2: Document danger of mappings larger than PTRDIFF_MAX

References:
- C99 draft: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
section "6.5.6 Additive operators", paragraph 9
- object size restriction in GCC:
https://gcc.gnu.org/legacy-ml/gcc/2011-08/msg00221.html
- glibc malloc restricts object size to <=PTRDIFF_MAX in checked_request2size() since glibc v2.30 (released in 2019, as pointed out by Jakub Wilk):
https://sourceware.org/cgit/glibc/commit/?id=9bf8e29ca136094f

---
 man/man2/mmap.2 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
man/man2/mmap.2 +19 -0
index caf822103..4bb15699d 100644
@@ -785,6 +785,25 @@ correspond to added or removed regions of the file is unspecified.
 An application can determine which pages of a mapping are
 currently resident in the buffer/page cache using
 .BR mincore (2).
+.P
+Unlike typical
+.BR malloc (3)
+implementations,
+.BR mmap ()
+does not prevent creating objects larger than
+.BR PTRDIFF_MAX .
+Objects that are larger than
+.B PTRDIFF_MAX
+only work in limited ways in standard C
+(in particular, pointer subtraction results in undefined behavior if the
+result would be bigger than
+.BR PTRDIFF_MAX ).
+On top of that, GCC also assumes that no object is bigger than
+.BR PTRDIFF_MAX .
+.B PTRDIFF_MAX
+is usually half of the address space size;
+so for 32-bit processes,
+it is usually 0x7fffffff (almost 2 GiB).
 .\"
 .SS Using MAP_FIXED safely
 The only safe use for

base-commit: 4c4d9f0f5148caf1271394018d0f7381c1b8b400

Signature · 1 line
2.49.0.901.g37484f566f-goog
#1 lore raw

Hi Jann,

Quoted 9 lines · Jann Horn

References:
- C99 draft: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
section "6.5.6 Additive operators", paragraph 9
- object size restriction in GCC:

   https://gcc.gnu.org/legacy-ml/gcc/2011-08/msg00221.html
 - glibc malloc restricts object size to <=PTRDIFF_MAX in
   checked_request2size() since glibc v2.30 (released in 2019, as pointed
   out by Jakub Wilk):
   https://sourceware.org/cgit/glibc/commit/?id=9bf8e29ca136094f

Thanks! I've applied the patch. See some comments below.
<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=7e5756fdeba1a4729f817079c64f0d87fdcdadfa>

Have a lovely day!
Alex

man/man2/mmap.2 correspond to added or removed regions of the file is unspecified. · line 788

Quoted 13 lines
---
 man/man2/mmap.2 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
man/man2/mmap.2 +1 -0
index caf822103..4bb15699d 100644
@@ -785,6 +785,25 @@ correspond to added or removed regions of the file is unspecified.
 An application can determine which pages of a mapping are
 currently resident in the buffer/page cache using
 .BR mincore (2).
+.P

I've moved the paragraph to a new CAVEATS section.

man/man2/mmap.2 correspond to added or removed regions of the file is unspecified. · line 797

Quoted 9 lines
man/man2/mmap.2 +9 -0
+Unlike typical
+.BR malloc (3)
+implementations,
+.BR mmap ()
+does not prevent creating objects larger than
+.BR PTRDIFF_MAX .
+Objects that are larger than
+.B PTRDIFF_MAX
+only work in limited ways in standard C

I've removed 'standard', since in any C it is problematic.

Is it okay to you? (We're still in time to amend if you prefer something else.)

Quoted 17 lines
man/man2/mmap.2 +9 -0
+(in particular, pointer subtraction results in undefined behavior if the
+result would be bigger than
+.BR PTRDIFF_MAX ).
+On top of that, GCC also assumes that no object is bigger than
+.BR PTRDIFF_MAX .
+.B PTRDIFF_MAX
+is usually half of the address space size;
+so for 32-bit processes,
+it is usually 0x7fffffff (almost 2 GiB).
 .\"
 .SS Using MAP_FIXED safely
 The only safe use for

base-commit: 4c4d9f0f5148caf1271394018d0f7381c1b8b400

Signature · 1 line
2.49.0.901.g37484f566f-goog
Signature · 1 line
<https://www.alejandro-colomar.es/>
author
#2 lore raw
Quoted 11 lines · Alejandro Colomar
Quoted 9 lines · Jann Horn

References:
- C99 draft: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
section "6.5.6 Additive operators", paragraph 9
- object size restriction in GCC:

   https://gcc.gnu.org/legacy-ml/gcc/2011-08/msg00221.html
 - glibc malloc restricts object size to <=PTRDIFF_MAX in
   checked_request2size() since glibc v2.30 (released in 2019, as pointed
   out by Jakub Wilk):
   https://sourceware.org/cgit/glibc/commit/?id=9bf8e29ca136094f
Quoted 2 lines

Thanks! I've applied the patch. See some comments below.
<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=7e5756fdeba1a4729f817079c64f0d87fdcdadfa>

Thanks!

Quoted 12 lines

man/man2/mmap.2 correspond to added or removed regions of the file is unspecified. · line 797

Quoted 9 lines
man/man2/mmap.2 +9 -0
+Unlike typical
+.BR malloc (3)
+implementations,
+.BR mmap ()
+does not prevent creating objects larger than
+.BR PTRDIFF_MAX .
+Objects that are larger than
+.B PTRDIFF_MAX
+only work in limited ways in standard C

I've removed 'standard', since in any C it is problematic.

Quoted 2 lines

Is it okay to you? (We're still in time to amend if you prefer something else.)

Sounds good to me.