From: Ulrich Drepper <drepper@redhat.com>
To: Linux Kernel <linux-kernel@vger.kernel.org>,
Linus Torvalds <torvalds@osdl.org>
Subject: mremap() use is racy
Date: Tue, 23 Aug 2005 12:53:18 -0700 [thread overview]
Message-ID: <430B7EAE.6020001@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1613 bytes --]
Not the mremap() implementation itself, so don't worry.
If mremap() is to be used without the MREMAP_MAYMOVE flag the call will
only succeed of the address space after the block which is to be
remapped is empty. This is rarely the case since there are many users
of mmap and memory is allocated consecutively in many cases.
So what programs have to do is to make sure ahead of time that the
mremap() call can succeed. The best way to do this is using an
anonymous, unused, unusable mapping. Code like this:
p = mmap(NULL, 65536, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
mmap(p, 16384, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
Then when the mapping has to be extended one should be able to use mremap():
mremap(p, 16384, 32768, 0);
But this is not possible since mremap() respects the anonymous mapping.
So one has to use
munmap((char*)p + 16384, 16384);
before the mremap() call. But this is where the race comes in. Some
other thread might allocate these blocks before the mremap() call can do it.
One possible solution would be to add a flag to mremap() which allows
mremap() to steal memory. In general that would be too dangerous but we
could limit it to private, anonymous mappings which have no access
permissions (i.e., PROT_NONE with MAP_PRIVATE and MAP_ANON). One
explicitly has to allocate such blocks, they don't appear naturally.
And the program in any case knows about the address space layout.
So, how about adding MREMAP_MAPOVERNONE or so?
--
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 251 bytes --]
next reply other threads:[~2005-08-23 19:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-23 19:53 Ulrich Drepper [this message]
2005-08-23 20:45 ` mremap() use is racy Hugh Dickins
2005-08-23 20:56 ` Ulrich Drepper
2005-08-23 21:28 ` Linus Torvalds
2005-08-23 22:08 ` Ulrich Drepper
2005-08-23 23:46 ` Hugh Dickins
2005-08-24 0:08 ` Linus Torvalds
2005-08-24 12:00 ` Hugh Dickins
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=430B7EAE.6020001@redhat.com \
--to=drepper@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@osdl.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.