Skip to contentSkip to conversation

mmap.2: Document danger of mappings larger than PTRDIFF_MAX

PATCH Superseded Jann Horn 3 participants 5 messages Apr 9, 2025 – Apr 10, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 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()
---
I'm not sure if we can reasonably do anything about this in the kernel, given that the kernel does not really have any idea of what userspace object sizes look like, or whether userspace even wants C semantics. But we can at least document it...

@man-pages maintainer: Please wait a few days before applying this; I imagine there might be some discussion about this.

 man/man2/mmap.2 | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
man/man2/mmap.2 +17 -0
index caf822103..9cb7dacf3 100644
@@ -785,6 +785,23 @@ 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
+.B 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
+.B PTRDIFF_MAX).
+On top of that, GCC also assumes that no object is bigger than
+.B 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.504.g3bcea36a83-goog
#2 lore raw
In reply to Jann Horn

Hi Jan,

Quoted 11 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()
---
I'm not sure if we can reasonably do anything about this in the kernel, given that the kernel does not really have any idea of what userspace object sizes look like,

Hmmm. Maybe it could reject PTRDIFF_MAX within the kernel, which would at least work for cases where user-space ptrdiff_t matches the kernel's ptrdiff_t? Then only users where they don't match would be unprotected, but those are hopefully extra careful.

Quoted 1 line

or whether userspace even wants C semantics.

I guess any language will have to link to C at some point, or have inherent limitations similar to those of C.

Quoted 1 line

But we can at least document it...

Yep. Most people are unaware of this, and believe they can get SIZE_MAX.

Quoted 3 lines

@man-pages maintainer: Please wait a few days before applying this; I imagine there might be some discussion about this.

Okay; see some minor comments below.

Quoted 9 lines
 man/man2/mmap.2 | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
man/man2/mmap.2 +0 -0
index caf822103..9cb7dacf3 100644

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

Quoted 10 lines
man/man2/mmap.2 +7 -0
@@ -785,6 +785,23 @@ 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
+.B PTRDIFF_MAX.

.BR PTRDIFF_MAX .

(since you want the '.' not bold, but roman)

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

Quoted 3 lines
man/man2/mmap.2 +3 -0
+Objects that are larger than
+.B PTRDIFF_MAX
+only work in limited ways in standard C (in particular, pointer subtraction

Please break the line also before the '('.

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

Quoted 2 lines
man/man2/mmap.2 +2 -0
+results in undefined behavior if the result would be bigger than
+.B PTRDIFF_MAX).

.BR PTRDIFF_MAX ).

(same reasons)

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

Quoted 2 lines
man/man2/mmap.2 +2 -0
+On top of that, GCC also assumes that no object is bigger than
+.B PTRDIFF_MAX.

.BR PTRDIFF_MAX .

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

Quoted 2 lines
man/man2/mmap.2 +2 -0
+.B PTRDIFF_MAX
+is usually half of the address space size; so for 32-bit processes, it is

Please break the line after ';' and after ',' (and not after 'is').

See also man-pages(7):

$ MANWIDTH=72 man man-pages | sed -n '/Use semantic newlines/,/^$/p'
   Use semantic newlines
     In the source of a manual page, new sentences should be started on
     new lines, long sentences should be split  into  lines  at  clause
     breaks  (commas,  semicolons, colons, and so on), and long clauses
     should be split at phrase boundaries.  This convention,  sometimes
     known as "semantic newlines", makes it easier to see the effect of
     patches, which often operate at the level of individual sentences,
     clauses, or phrases.

Have a lovely night!
Alex

Quoted 9 lines
man/man2/mmap.2 +1 -0
+usually 0x7fffffff (almost 2 GiB).
 .\"
 .SS Using MAP_FIXED safely
 The only safe use for

base-commit: 4c4d9f0f5148caf1271394018d0f7381c1b8b400

Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Signature · 1 line
<https://www.alejandro-colomar.es/>
author
#3 lore raw
Quoted 13 lines · Alejandro Colomar
Quoted 11 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()
---
I'm not sure if we can reasonably do anything about this in the kernel, given that the kernel does not really have any idea of what userspace object sizes look like,

Quoted 4 lines

Hmmm. Maybe it could reject PTRDIFF_MAX within the kernel, which would at least work for cases where user-space ptrdiff_t matches the kernel's ptrdiff_t? Then only users where they don't match would be unprotected, but those are hopefully extra careful.

Perhaps. But then some tricky things are:

1. How many existing users would we be breaking with such a change? Probably _someone_ out there is deliberately mapping files over 2G into 32-bit processes and it sorta worked until now...
2. We don't really have a concept of object size in the kernel, and it might be hard to reason about whether mmap() is used logically to create a new object or extend an existing object. I guess we could limit VMA sizes for 32-bit userspace to 0x7ffff000 and enforce a 1-page gap around mappings that are at least half that size, or something like that, but that would probably get a bit ugly on the kernel side...

The first point is really the main concern for me - we might end up breaking existing users.

Quoted 4 lines
Quoted 1 line

or whether userspace even wants C semantics.

I guess any language will have to link to C at some point, or have inherent limitations similar to those of C.

This limitation is really a result of deciding to make pointer subtraction return a signed value, so that you can subtract a bigger pointer from a smaller pointer. I don't know whether other languages do that.

Quoted 10 lines
Quoted 1 line

But we can at least document it...

Yep. Most people are unaware of this, and believe they can get SIZE_MAX.

Quoted 3 lines

@man-pages maintainer: Please wait a few days before applying this; I imagine there might be some discussion about this.

Okay; see some minor comments below.

Thanks. (I'll probably be out for the next two weeks or so, I'll probably get back to this afterwards.)

#4 lore raw

Hi Jann,

Quoted 18 lines · Jann Horn
Quoted 4 lines

Hmmm. Maybe it could reject PTRDIFF_MAX within the kernel, which would at least work for cases where user-space ptrdiff_t matches the kernel's ptrdiff_t? Then only users where they don't match would be unprotected, but those are hopefully extra careful.

Perhaps. But then some tricky things are:

1. How many existing users would we be breaking with such a change? Probably _someone_ out there is deliberately mapping files over 2G into 32-bit processes and it sorta worked until now...
2. We don't really have a concept of object size in the kernel, and it might be hard to reason about whether mmap() is used logically to create a new object or extend an existing object. I guess we could limit VMA sizes for 32-bit userspace to 0x7ffff000 and enforce a 1-page gap around mappings that are at least half that size, or something like that, but that would probably get a bit ugly on the kernel side...

Quoted 2 lines

The first point is really the main concern for me - we might end up breaking existing users.

Hmmm, okay. If it ends up being too complex, it also would be bad. It's easier for careful programmers to just check the size before the call. So it's fine to not do the check in the kernel.

Quoted 16 lines
Quoted 4 lines
Quoted 1 line

or whether userspace even wants C semantics.

I guess any language will have to link to C at some point, or have inherent limitations similar to those of C.

This limitation is really a result of deciding to make pointer subtraction return a signed value, so that you can subtract a bigger pointer from a smaller pointer. I don't know whether other languages do that.

Quoted 6 lines
Quoted 1 line

But we can at least document it...

Yep. Most people are unaware of this, and believe they can get SIZE_MAX.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

@man-pages maintainer: Please wait a few days before applying this; I imagine there might be some discussion about this.

Okay; see some minor comments below.

Thanks. (I'll probably be out for the next two weeks or so, I'll probably get back to this afterwards.)

Okay, no problem

Signature · 1 line
<https://www.alejandro-colomar.es/>