LWN: Comments on "Fun with NULL pointers, part 1" https://lwn.net/Articles/342330/ This is a special feed containing comments posted to the individual LWN article titled "Fun with NULL pointers, part 1". en-us Mon, 15 Sep 2025 10:01:34 +0000 Mon, 15 Sep 2025 10:01:34 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Fun with NULL pointers, part 1 https://lwn.net/Articles/347254/ https://lwn.net/Articles/347254/ gvy <div class="FormattedComment"> Yeah, previously one had to introduce a rootkit. Now it's half-done by prominent distributors. :(<br> </div> Sat, 15 Aug 2009 09:01:39 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/345836/ https://lwn.net/Articles/345836/ jschrod <div class="FormattedComment"> I'll have to say that I don't find PolicyKit's documentation very good. I don't know how it is for a developer, but for me as a long-time Unix admin it was frustrating when I had to delve into it a few weeks ago for the first time.<br> <p> The document linked by you was one of the 1st that I read, btw. It's not that I didn't understand what PolicyKit was supposed to be doing, that was quite clear from the start. And not even that I couldn't read the config files -- while being rather chatty, they were understandable.<br> <p> Where I didn't succeed, is finding concise information about the overall architecture: how PolicyKit / ConsoleKit / HAL / D-Bus / pam / login processes, both X and console, are *supposed* to work together. That would have delivered pointers to information sources that could have answered questions like Where are the names in the PolicyKit XML files from? Which possible names exist on a given system? Which daemons are started and by whom? What are common PolicyKit authorizations, since it is all about policy and nothing about content? The output of polkit-auth --show-obtainable is not sufficient, IMHO. E.g., I'd need to know what org.freedesktop.hal.lock is, and googling it on site:freedesktop.org does _not_ provide an adequate answer, at least not for me. (And yes, I've read <a href="http://people.freedesktop.org/~david/hal-spec/hal-spec.html">http://people.freedesktop.org/~david/hal-spec/hal-spec.html</a>.)<br> <p> It was not easy to find the answers to such questions in reasonable time frames because the overall structure had to be laborously reverse engineered by yours humbly. Well, maybe my Google foo may simply be not good enough.<br> <p> Just my 0.02 EUR. :-)<br> <p> Joachim<br> </div> Fri, 07 Aug 2009 12:21:44 +0000 Optimizations and undefined behavior https://lwn.net/Articles/344720/ https://lwn.net/Articles/344720/ hozelda <div class="FormattedComment"> I know the conversation is partly about what developers should depend on or not, but keep in mind that "undefined behavior" and "implementation-defined behavior" mean something precise and completely different from each other in C99. See Chapter 3 (and 4) and informative Appendix J <a rel="nofollow" href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1336.pdf">http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1336.pdf</a><br> <p> </div> Fri, 31 Jul 2009 14:28:03 +0000 Optimizations and undefined behavior https://lwn.net/Articles/344584/ https://lwn.net/Articles/344584/ nix <div class="FormattedComment"> fsync(), brought to you by the same people who thought up 'volatile', <br> another equally impossible-to-define-except-by-reference-to-implementation <br> feature.<br> </div> Thu, 30 Jul 2009 21:31:23 +0000 Optimizations and undefined behavior https://lwn.net/Articles/344437/ https://lwn.net/Articles/344437/ foom <div class="FormattedComment"> <font class="QuotedText">&gt; I remember GCC taking out tests like if(x+n&gt;x)</font><br> <p> Still does. You can use -fwrapv if you want to tell it that you want signed number overflow to be <br> defined as wrapping.<br> </div> Thu, 30 Jul 2009 15:04:31 +0000 Optimizations and undefined behavior https://lwn.net/Articles/344428/ https://lwn.net/Articles/344428/ forthy <p>The problem with fsync() is that it's semantics resemble whery much the one of "PLEASE" in INTERCAL, which means it is a joke. fsync() basically has <b>no</b> semantics, except "make it so" (make it persistent now). Now, all file system operations are persistent, anyway, just not made persistent <b>now</b>. You can't properly test (that is: automated), because to test if there's a missing fsync(), you have to force an unexpected reboot, and then check if there's any missing data. What's worse: A number of popular Unix programming languages don't even have fsync(), starting with all kinds of shell scripts. fsync() is a dirty hack introduced into Unix because of broken (but extremely fast) file system implementations.</p> <p>We know that <a href="http://www.asandler.com/jokes/computer/c.shtml">Unix is a joke</a> for quite some time, but parts of the API like fsync() show that this is not so far away from the truth ;-). From the kernel development side it is always "easier" to maintain a sloppy specification and blame the loser, but that's the wrong thinking. You are providing a service. Same thing for GCC: Compiler writers provide a service. Using a sloppy specification for questionable "optimizations" is wrong, as well. If the compiler writer can't know that the code really will break when accessing the NULL pointer, then he can't take the test out after having accessed an object. I remember GCC taking out tests like if(x+n>x), because overflows are said to be unspecified in the C language, but compiling code to a machine where overflows were very specifically handled as wraparounds in two's complement representation. This is all wrong thinking.</p> Thu, 30 Jul 2009 13:38:30 +0000 Optimizations and undefined behavior https://lwn.net/Articles/344432/ https://lwn.net/Articles/344432/ lysse <div class="FormattedComment"> <font class="QuotedText">&gt; "Implementation dependent" != `cat /dev/urandom` </font><br> <p> Wasn't that where Bruce came in...? ;)<br> </div> Thu, 30 Jul 2009 13:31:05 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343462/ https://lwn.net/Articles/343462/ nix <div class="FormattedComment"> Well, you can't access a structure at address zero on such a platform <br> without either disabling all optimizations that involve knowing which <br> pointers are null (as the kernel now is) and taking great care to ensure <br> that you never need anything that can point to said structure to be NULL <br> at any time, or defining the null pointer to be other than all-bits-zero <br> (allowed, but weird, about as rare as platforms with strange word sizes).<br> <p> I'd say that trying to access structures at address zero, MMU or no MMU, <br> is extremely unusual and not really sane to handle in a general-purpose <br> compiler. (GCC goes further than I would expect in actually having a <br> switch that makes it possible to use such a barmy thing.)<br> <p> </div> Sun, 26 Jul 2009 18:27:32 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343434/ https://lwn.net/Articles/343434/ PaXTeam <div class="FormattedComment"> <font class="QuotedText">&gt; Data structures at address zero do not exist on any sane C platform.</font><br> <p> so platforms without an MMU are not sane?<br> </div> Sat, 25 Jul 2009 23:18:38 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343400/ https://lwn.net/Articles/343400/ nix <div class="FormattedComment"> GCC doesn't ignore the standard: you simply can't have data structures <br> that reside at address zero. Shaving off 1/2^32 or less of the address <br> space, and disabling an optimization in the one place that cares about <br> this (the kernel) does not seem like a terrible cost to me.<br> <p> Data structures at address zero do not exist on any sane C platform.<br> <p> </div> Sat, 25 Jul 2009 12:49:12 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343395/ https://lwn.net/Articles/343395/ spitzak <div class="FormattedComment"> That won't work, plenty of code assumes that comparing two NULL pointers for equality will return <br> true.<br> <p> </div> Sat, 25 Jul 2009 09:37:55 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343376/ https://lwn.net/Articles/343376/ giraffedata <blockquote> GCC certainly doesn't insert code checking if a pointer is NULL before every pointer dereference. </blockquote> <p> Sure, but that wouldn't improve standards compliance anyway. I asked if GCC generates extra code to comply with the C99 requirement that a null pointer not be equal to any non-null one (while still allowing the existence of pointers to a data structure that resides at address 0). Thinking about it now, though, I don't see how any such code is possible since a null pointer still has to compare equal to another null pointer. <p> <blockquote> That kernel space has to work when the lower part of its address space is effectively under the control of a hostile attacker is a unique problem which it is really not worth changing the C standard for, </blockquote> <p> There has been no proposal to deal with this by changing the standard, which GCC apparently ignores anyhow. And objection to GCC's conflation of null pointers and zero-address pointers wasn't that it's a security problem but that it's a basic correctness problem. Even without a hostile page 0, unless you proclaim data structures at address 0 don't exist, this optimization breaks code. Sat, 25 Jul 2009 02:51:47 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/343374/ https://lwn.net/Articles/343374/ mikov <div class="FormattedComment"> I am with you. <br> In my mind the key to reasonable security is simplicity. SUID is extremely simple. Compare changing a user's password using passwd to what has to happen in Windows.<br> <p> <p> </div> Sat, 25 Jul 2009 02:13:23 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343336/ https://lwn.net/Articles/343336/ nix <div class="FormattedComment"> GCC certainly doesn't insert code checking if a pointer is NULL before <br> every pointer dereference. Considering how common pointer dereferencing is <br> in C and related languages, this would be a substantial slowdown for no <br> real gain, given that multiuser OSes invariably trap such things, and <br> non-multiuser OSes are specialist environments in which attacks by hostile <br> local users are not so common (yet).<br> <p> That kernel space has to work when the lower part of its address space is <br> effectively under the control of a hostile attacker is a unique problem <br> which it is really not worth changing the C standard for, nor imposing <br> vast overheads on all userspace code. -fno-delete-null-pointer-checks does <br> the job.<br> </div> Fri, 24 Jul 2009 22:06:06 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/343318/ https://lwn.net/Articles/343318/ giraffedata <blockquote> It would be possible to keep 0x0 for the null pointer while respecting the C99 standard: the compiler would need to put in a couple of extra instructions for every pointer comparison making sure that 0x0 != P for any value of P. </blockquote> <p> Do we know Gcc doesn't do this? Seems like it would have to, to be C99 compliant. <blockquote> Optionally, it could also put in a check before every pointer dereference making sure the pointer is not 0x0 and causing a SIGSEGV if it is (since the memory layout can no longer be relied on to guarantee that). </blockquote> <p> But then how would you represent a pointer to a data structure that resides at address 0? A pointer should be able to do that. <p> It would of course be unrealistically expensive on typical machines to represent a pointer with anything but a simple address, but pointer comparisons are rare enough that a few extra instructions for them seems worthwhile to maintain the null pointer concept. <p> Regardless of how the compiler chooses to represent pointers (null or otherwise), the optimization in question is logically sound. C99 says a dereference of a null pointer causes undefined behavior, so either a) tun is non-null and !tun must be false or b) tun is null and !tun can be anything, including false. Fri, 24 Jul 2009 20:38:29 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/343281/ https://lwn.net/Articles/343281/ rogue@autistici.org <div class="FormattedComment"> And, may you be glorified for your find as much as spender is for his exploit.<br> </div> Fri, 24 Jul 2009 16:06:49 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342968/ https://lwn.net/Articles/342968/ johnflux <div class="FormattedComment"> I hope not. Good developers will realize that fsync is a complete overkill there. They don't need wait for the changes to actually be made to disk before continuing - they only need to make sure that the changes happen in the right order.<br> </div> Thu, 23 Jul 2009 05:07:21 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342966/ https://lwn.net/Articles/342966/ johnflux <div class="FormattedComment"> You, sir, are a coding god.<br> </div> Thu, 23 Jul 2009 05:01:18 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342926/ https://lwn.net/Articles/342926/ nix <div class="FormattedComment"> Ew, no. Utterly un-awklike and doing awk-like transformations with XSLT is <br> really quite painful. (And yes, you can do awklike languages for things <br> other than text streams: see gvpr(1) for example.)<br> <p> (One of many problems is XSLT's heavy use of &lt;&gt;, which makes it very <br> annoying to use from the shell prompt. Another is its astonishing <br> verbosity. Another is its total lack of good taste in design... also the <br> functional nature of it, while one of its nicer aspects, fits very badly <br> with the shell in my experience.)<br> </div> Wed, 22 Jul 2009 22:00:39 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342925/ https://lwn.net/Articles/342925/ nix <div class="FormattedComment"> I think one is essential if PolicyKit can be considered safe to use: we <br> have to be able to do automated audits and yell at unexpected quiet <br> changes, lest we miss attacks or accidental fumbles opening up holes.<br> <p> You're quite right on the fugliness of the UID-setting syscalls in Unix, <br> but this is not helped by introducing *more* :/ again, userv did all this <br> better (IMNSHO) many years ago. Why more people don't use it I have no <br> idea.<br> </div> Wed, 22 Jul 2009 21:59:08 +0000 RISC can do that https://lwn.net/Articles/342887/ https://lwn.net/Articles/342887/ klossner <div class="FormattedComment"> PowerPC silently drops the two low bits when loading an address into the PC, so a branch to 1 becomes a branch to 0. The misaligned-address exception occurs only for load/store instructions.<br> <p> <p> </div> Wed, 22 Jul 2009 18:13:58 +0000 #pragma and GCC https://lwn.net/Articles/342844/ https://lwn.net/Articles/342844/ zeekec <div class="FormattedComment"> Wouldn't that be a great response to a NULL dereference in the kernel.<br> </div> Wed, 22 Jul 2009 15:46:25 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342822/ https://lwn.net/Articles/342822/ mezcalero <div class="FormattedComment"> pa 0.9.16 is not setuid anymore, because the introduction of rtkit makes that unnecessary.<br> </div> Wed, 22 Jul 2009 14:24:48 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342810/ https://lwn.net/Articles/342810/ cortana <div class="FormattedComment"> It's not GNOME-specific. It just uses GTK+. Yes, it's unfortunate that there is no command-line equivalent, but it shouldn't be too difficult for one to be written--you are just querying the PolicyKit object after all.<br> </div> Wed, 22 Jul 2009 12:49:10 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342797/ https://lwn.net/Articles/342797/ nix <div class="FormattedComment"> So the only way you can see what actions can be run on a system with privilege is... to run a GNOME-specific GUI application?<br> <p> *sigh*<br> </div> Wed, 22 Jul 2009 11:19:46 +0000 xml awk https://lwn.net/Articles/342796/ https://lwn.net/Articles/342796/ nix <div class="FormattedComment"> That looks nice. Not very awkish though...<br> </div> Wed, 22 Jul 2009 11:18:59 +0000 A zero pointer is not a null pointer https://lwn.net/Articles/342780/ https://lwn.net/Articles/342780/ epa <div class="FormattedComment"> It would be possible to keep 0x0 for the null pointer while respecting the C99 standard: the compiler would need to put in a couple of extra instructions for every pointer comparison making sure that 0x0 != P for any value of P. Optionally, it could also put in a check before every pointer dereference making sure the pointer is not 0x0 and causing a SIGSEGV if it is (since the memory layout can no longer be relied on to guarantee that).<br> </div> Wed, 22 Jul 2009 09:02:41 +0000 xml awk https://lwn.net/Articles/342767/ https://lwn.net/Articles/342767/ Frej <div class="FormattedComment"> xml cli tool <br> <a href="http://xmlstar.sourceforge.net/">http://xmlstar.sourceforge.net/</a><br> <p> </div> Wed, 22 Jul 2009 07:08:00 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342753/ https://lwn.net/Articles/342753/ mjg59 <div class="FormattedComment"> Good developers aren't going to have to - good operating systems will provide guarantees above and beyond POSIX. Operating systems that don't will end up poorly supported and gradually become irrelevant.<br> </div> Wed, 22 Jul 2009 04:16:05 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342748/ https://lwn.net/Articles/342748/ BrucePerens Sigh. Good developers are still going to do create temp, write, fsync file, link to permanent name, unlink temp. Fsync on the directory, though, shouldn't be necessary. Wed, 22 Jul 2009 03:28:40 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342737/ https://lwn.net/Articles/342737/ Baylink <div class="FormattedComment"> Am I the only person who read "has checks for this" and thought "a wart is a crocky feature"?<br> </div> Wed, 22 Jul 2009 02:18:52 +0000 Fun with NULL pointers, part 1 https://lwn.net/Articles/342736/ https://lwn.net/Articles/342736/ Baylink <div class="FormattedComment"> "vulnerability surface".<br> <p> Wow. That's an altogether neat concept. Thanks.<br> </div> Wed, 22 Jul 2009 02:14:12 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342733/ https://lwn.net/Articles/342733/ mjg59 <div class="FormattedComment"> If a system left truly random numbers in uninitialised variables and applications started making use of that functionality (even if undocumented), then I think you'd need a very good reason to change that behaviour. But since I can't imagine any sane system ever doing that, no, I'm not proposing that we need a user contract on that point.<br> <p> Contrast that to the ext3 behaviour. While, yes, the behaviour of fsync() on ext3 did result in people being even less likely to use it, the fact that ext3 also made it possible to overwrite a file without having to go through a cumbersome sequence of fsync()ing both the data and the directory made it attractive to application writers. That behaviour dates back far beyond Firefox 3, as demonstrated by people's long-term complaints about XFS leaving their files full of zeros after a crash. ext4 now provides the same ease of use because people made it clear that they weren't going to use it otherwise. Future Linux filesystems are effectively forced to provide the same semantics, which is a good thing.<br> </div> Wed, 22 Jul 2009 01:39:50 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342732/ https://lwn.net/Articles/342732/ BrucePerens Matt, you aren't seriously proposing that we provide some sort of user contract regarding the contents of uninitialized variables being reliable sources of entropy.<p>Regarding ext3, this problem came up because fsync() was implemented as a performance pig, at least in ext3, and rather than fix it we trained application developers that they'd be safe without it. Had fsync() been repaired when the mozilla problem came up, nobody would be arguing about it today. Wed, 22 Jul 2009 01:28:41 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342711/ https://lwn.net/Articles/342711/ mjg59 <div class="FormattedComment"> The role of software is to be useful to its consumers. Software that fails in this will tend to end up being ignored in the long run.<br> </div> Tue, 21 Jul 2009 22:39:48 +0000 #pragma and GCC https://lwn.net/Articles/342691/ https://lwn.net/Articles/342691/ BrucePerens Some time in the 80's, when RMS was still coding GCC, the response to #pragma in C was explicitly stated to be undefined, so that your compiler could have whatever magic happen that it wished.<p> So, RMS coded GCC to start the game "rogue" when it saw #pragma. Tue, 21 Jul 2009 21:48:48 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342668/ https://lwn.net/Articles/342668/ BrucePerens The recent ext3 fsync() snafu is a good example of implementation dependent behavior becoming taken as an implicit guarantee. And then the developer had to reduce the scope of the promise for performance reasons. He ended up regretting that he had ever made that feature visible.<p> Anyway, there was no such guarantee in this case. Tue, 21 Jul 2009 20:55:17 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342664/ https://lwn.net/Articles/342664/ martinfick <i>"Implementation dependent" means that the folks who change the compiler, the C library, the kernel, and any stack-smashing detection/prevention code that you are using have the right to change the behavior at any time without documenting the particular side-effect that your code is depending upon. </i> <p> No, it does not always mean this. In this particular case it may, but it is perfectly reasonable for a specific C compiler to specify its behavior when the standard says that it is undefined, that is the point of "undefined" and "Implementation dependent". </p> "Implementation dependent" != `cat /dev/urandom` Tue, 21 Jul 2009 20:11:02 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342662/ https://lwn.net/Articles/342662/ BrucePerens "Implementation dependent" means that the folks who change the compiler, the C library, the kernel, and any stack-smashing detection/prevention code that you are using have the right to change the behavior at any time without documenting the particular side-effect that your code is depending upon.<p> Especially in the context of the various stack-smashing prevention hacks going around, you have no reason to believe that an uninitialized variable would not actually be initialized to a fixed value.<p> Undefined stuff is not a service that you can count on. Ever. Tue, 21 Jul 2009 20:04:38 +0000 Optimizations and undefined behavior https://lwn.net/Articles/342658/ https://lwn.net/Articles/342658/ martinfick <i>it means "implementation dependent".</i> <p> which as others have pointed out, is the purpose of undefined in the C spec, which does not mean that it has <i>"No correct cases."</i>, but rather, that you'd better be familiar with the implementation you are using. It makes no sense to say: "Here is a feature not available elsewhere, that we think it valuable, but you should never use it!" does it? </p> Tue, 21 Jul 2009 19:42:21 +0000