LWN: Comments on "MAP_FIXED_SAFE" https://lwn.net/Articles/741369/ This is a special feed containing comments posted to the individual LWN article titled "MAP_FIXED_SAFE". en-us Thu, 11 Sep 2025 02:45:07 +0000 Thu, 11 Sep 2025 02:45:07 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net the problem isn't MAP_FIXED... https://lwn.net/Articles/742005/ https://lwn.net/Articles/742005/ HelloWorld <div class="FormattedComment"> ...but its weird replacing behaviour when a mapping already exists. The question is: is there any program that relies on this behaviour to function? I'd be surprised if that were the case.<br> </div> Thu, 21 Dec 2017 05:42:58 +0000 How about this? https://lwn.net/Articles/741979/ https://lwn.net/Articles/741979/ mdenton <div class="FormattedComment"> I have previously worked on a filesystem that provided transactional memory; we needed MAP_FIXED to keep internal addresses the same when a transactional "file" was mapped in multiple processes. We could've of course used relative addresses, but that comes with a not insignificant performance cost, and would've required some C++ magic we didn't really want to deal with.<br> </div> Wed, 20 Dec 2017 22:24:14 +0000 Terminology wisdom https://lwn.net/Articles/741830/ https://lwn.net/Articles/741830/ ortalo <div class="FormattedComment"> Yeah! +1 - love it (the last one) ..._AND_SECURE2<br> </div> Tue, 19 Dec 2017 09:56:32 +0000 Terminology wisdom https://lwn.net/Articles/741828/ https://lwn.net/Articles/741828/ ortalo <div class="FormattedComment"> In the long term, okay. But let's not let economics discussions distract us from the security concerns...<br> <p> </div> Tue, 19 Dec 2017 09:53:37 +0000 Terminology wisdom https://lwn.net/Articles/741827/ https://lwn.net/Articles/741827/ ortalo <div class="FormattedComment"> Well, in my humble opinion, the plot end is more due to the environment than to terminological issues. Let's hope there are not too many well-meaning advisors in the development team and historical partisan disputes have been banned.<br> That's the appropriate time of year for dreaming, no?<br> </div> Tue, 19 Dec 2017 09:50:46 +0000 Terminology wisdom https://lwn.net/Articles/741708/ https://lwn.net/Articles/741708/ shane <div class="FormattedComment"> Fair enough, although I do think that it should be called MAP_FIXED_SAFER, because calling it "SAFE" is almost going to guarantee an exploit at some point, and then we will need MAP_FIXED_REALLY_SAFE, and probably MAP_FIXED_SAFE_FOR_REALZ_THIS_TIME...<br> </div> Mon, 18 Dec 2017 13:46:35 +0000 How about this? https://lwn.net/Articles/741631/ https://lwn.net/Articles/741631/ dtalen <div class="FormattedComment"> FYI, I believe position independent builds have only just become default for the Gentoo distribution.<br> </div> Sat, 16 Dec 2017 14:47:54 +0000 How about this? https://lwn.net/Articles/741630/ https://lwn.net/Articles/741630/ epa <div class="FormattedComment"> The idea was that if you see MAP_FIXED then you know that this particular bit of code hasn't yet been audited and changed to either SAFE or UNSAFE. So checking your codebase is a simple grep. I guess as you say you can "#define MAP_FIXED_UNSAFE MAP_FIXED" and then use the new keyword.<br> </div> Sat, 16 Dec 2017 13:01:05 +0000 Terminology wisdom https://lwn.net/Articles/741626/ https://lwn.net/Articles/741626/ cpitrat <div class="FormattedComment"> Unfortunately I'm pretty sure they'll all end-up dead ...<br> </div> Sat, 16 Dec 2017 10:58:44 +0000 How about this? https://lwn.net/Articles/741625/ https://lwn.net/Articles/741625/ cpitrat <div class="FormattedComment"> I think the idea is to help people clarify their intent in their code. They can easily do that with a #define though so I don't see a real benefit in this proposition.<br> </div> Sat, 16 Dec 2017 10:57:19 +0000 Terminology wisdom https://lwn.net/Articles/741615/ https://lwn.net/Articles/741615/ mogendavido <div class="FormattedComment"> Note though that in spite of Juliet's argument, both she and Romeo ended up dead. Hopefully the same won't happen to the participants in this fight ;-)<br> </div> Sat, 16 Dec 2017 00:56:56 +0000 Terminology wisdom https://lwn.net/Articles/741539/ https://lwn.net/Articles/741539/ ortalo <div class="FormattedComment"> "What's in a name? that which we call a rose<br> by any other word would smell as sweet;"<br> <p> <p> </div> Fri, 15 Dec 2017 10:48:40 +0000 How about this? https://lwn.net/Articles/741477/ https://lwn.net/Articles/741477/ ballombe <div class="FormattedComment"> This is true, however there is a difference if you use vm.overcommit=2.<br> Using MAP_FIXED allows the memory not to be counted as committed.<br> Sometime this is useful.<br> </div> Thu, 14 Dec 2017 19:30:40 +0000 MAP_FIXED is necessary for almost all execve() and dlopen() https://lwn.net/Articles/741476/ https://lwn.net/Articles/741476/ roc <div class="FormattedComment"> rr is another example of a program-manipulating program that absolutely needs MAP_FIXED.<br> </div> Thu, 14 Dec 2017 19:14:24 +0000 How about this? https://lwn.net/Articles/741468/ https://lwn.net/Articles/741468/ zlynx <div class="FormattedComment"> Some programmers prefer to mmap a large block and then mprotect the pieces of it instead of using MAP_FIXED.<br> </div> Thu, 14 Dec 2017 17:16:26 +0000 How about this? https://lwn.net/Articles/741446/ https://lwn.net/Articles/741446/ magfr <div class="FormattedComment"> MMAP_FIXED is part of POSIX so if you change the semantics of it then I would expect libc to add a wrapper that works around the broken kernel implementation.<br> </div> Thu, 14 Dec 2017 15:58:02 +0000 How about this? https://lwn.net/Articles/741447/ https://lwn.net/Articles/741447/ Sesse <div class="FormattedComment"> Who do you propose should go through all code that exists in the world (open and closed source alike) and do this audit?<br> </div> Thu, 14 Dec 2017 15:56:41 +0000 How about this? https://lwn.net/Articles/741413/ https://lwn.net/Articles/741413/ epa <div class="FormattedComment"> A new name MAP_FIXED_UNSAFE should be added which is an alias of the existing MAP_FIXED. Then existing code can be audited and changed to the safe variant if possible, or explicitly marked as requiring the unsafe call.<br> </div> Thu, 14 Dec 2017 08:53:16 +0000 MAP_FIXED is necessary for almost all execve() and dlopen() https://lwn.net/Articles/741410/ https://lwn.net/Articles/741410/ jreiser <div class="FormattedComment"> <font class="QuotedText">&gt; In 2017 I don't think there is any good use for MAP_FIXED.</font><br> <p> MAP_FIXED is necessary for the proper mapping by execve() of any native ET_EXEC file. The addresses are fixed, after all. There are ET_EXEC files which are several percent smaller and run several percent faster than the corresponding ET_DYN. Some environments have done a good risk-vs-benefit analysis, and for them ET_EXEC is worth it.<br> <p> MAP_FIXED is necessary for the proper mapping by execve() and dlopen() of any native ET_DYN file which has more than one PT_LOAD, which is nearly all of them. The second and subsequent PT_LOADs must have fixed offsets from the first PT_LOAD. In most cases the best strategy is to use mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, FD_ANON, 0) to obtain enough contiguous space to hold the convex hull of all PT_LOAD, then mmap() each PT_LOAD into that space using MAP_FIXED.<br> <p> MAP_FIXED is essential for program-manipulating programs such as valgrind and upx.<br> <p> </div> Thu, 14 Dec 2017 05:58:26 +0000 How about this? https://lwn.net/Articles/741405/ https://lwn.net/Articles/741405/ TheJH <div class="FormattedComment"> <font class="QuotedText">&gt; In 2017 I don't think there is any good use for MAP_FIXED.</font><br> <p> MAP_FIXED is how you properly, safely allocate virtually contiguous VMAs. This includes:<br> - guard pages for thread stacks<br> - the different sections for library mappings (you need separate VMAs for code, readonly data, copy-on-write data and zero-initialized data)<br> <p> Try running strace on any binary that is dynamically linked and/or uses threads, and you should see MAP_FIXED.<br> </div> Thu, 14 Dec 2017 01:49:35 +0000 How about this? https://lwn.net/Articles/741403/ https://lwn.net/Articles/741403/ TheJH <div class="FormattedComment"> Because that would break existing, perfectly legitimate users of MAP_FIXED.<br> <p> Look at, for example, how glibc allocates memory for threads:<br> <p> $ cat thread.c<br> #include &lt;pthread.h&gt;<br> void *testfn(void *arg) { return (void*)0; }<br> int main(void) {<br> pthread_t thread;<br> pthread_create(&amp;thread, NULL, testfn, (void*)0);<br> return 0;<br> }<br> $ gcc -o thread thread.c -Wall -pthread<br> $ strace ./thread<br> [...]<br> mmap(NULL, 3795360, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9b24276000<br> mprotect(0x7f9b2440b000, 2097152, PROT_NONE) = 0<br> mmap(0x7f9b2460b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x195000) = 0x7f9b2460b000<br> mmap(0x7f9b24611000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f9b24611000<br> [...]<br> <p> MAP_FIXED is perfectly safe as long as you only pass in address ranges that you have allocated yourself - just like other APIs, like munmap() or mprotect(), which you also shouldn't call on address ranges that haven't been allocated to you. Unlike MAP_FIXED, the suggested MAP_FIXED_SAFE is for a very uncommon usecase and actually requires more care to get right. (Which is also why MAP_FIXED_SAFE isn't exactly a good name - it suggests that it is a "safer" alternative to MAP_FIXED, which it simply isn't in what I think is probably the majority of usecases.)<br> </div> Thu, 14 Dec 2017 01:44:00 +0000 How about this? https://lwn.net/Articles/741404/ https://lwn.net/Articles/741404/ wahern <div class="FormattedComment"> s/hardware failures/filesystem corruption/. At various times hardware corruption was believed to be the primary culprit.<br> <p> </div> Thu, 14 Dec 2017 01:35:12 +0000 How about this? https://lwn.net/Articles/741402/ https://lwn.net/Articles/741402/ wahern <div class="FormattedComment"> In 2017 I don't think there is any good use for MAP_FIXED. Not only has position independence been best practice for decades, for both normal toolchains as well as bespoke applications, these days it's all but mandatory to be position independent to avoid being an exploit target.<br> <p> I once wrote a Bayesian SPAM token database that self-repaired file corruption. (The devices it ran on were seeing substantial hardware failures, which years later was tracked down to XFS bugs.) It used a red-black tree and thus internal pointers, and I very briefly considered MAP_FIXED; but even circa 2003 it just wasn't worth baking in such an anachronism, and even when disregarding the headaches it would impose to utilize multiple databases concurrently.<br> <p> But the reason the default semantics can't be changed is because of the Linux backwards compatibility guarantee. Also, while I've never used MAP_FIXED nor see myself ever using it, I don't find the semantics of silent replacement odd. dup2 has the same semantics, and for the same reasons the semantics seem quite natural and intuitive to me. It's just that back when people regularly mmap'd stuff to fixed addresses sbrk() was the primary (if not only) means of growing the heap, and static linking was the normal (if not only) method of linking. <br> <p> </div> Thu, 14 Dec 2017 01:33:14 +0000 How about this? https://lwn.net/Articles/741394/ https://lwn.net/Articles/741394/ pr1268 <blockquote><font class="QuotedText">Some had suggested adding a separate flag to modify the behavior of <tt>MAP_FIXED</tt>, so that applications would pass something like <tt>MAP_FIXED|MAP_SAFE</tt> to mmap(). The problem with that approach is that mmap() is one of those system calls that never checked for unknown flags.</font></blockquote> <p>Why not do the converse of that, i.e. make <tt>MAP_FIXED</tt> <em>default</em> to the <tt>MAP_SAFE</tt> behavior as defined and implemented by Mr. Hocko's patch, and add the separate flag, e.g. <tt>MAP_RIGHT_HERE</tt> for the legacy behavior?</p> <p>As an aside, I've used <tt><a href="http://man7.org/linux/man-pages/man2/mmap.2.html">mmap(2)</a></tt> for years, and <b>not once</b> have I ever needed to use <tt>MAP_FIXED</tt>. (But I'm sure there's a good use for it somewhere...)</p> Thu, 14 Dec 2017 00:39:42 +0000