5 ms·
Much ado about NULL: An introduction to virtual memory
- saagarjha 5y agoNote that Linux has since added a "mmap_min_addr" tunable that is intended to prevent applications from mapping in the zero page and exploiting NULL dereferences.
- gpvos 5y agoIt is already mentioned in the article, or maybe in part 2.
- bhaavan 5y agoThe follow up post: https://blogs.oracle.com/linux/post/much-ado-about-null-exploiting-a-kernel-null-dereference https://blogs.oracle.com/linux/post/much-ado-about-null-expl...
- gpvos 5y ago^^^ This is where the actually interesting bits are, and it's not directly linked from part 1.
- unwind 5y agoI drives me slightly crazy that an article about a fantastically technical subject such as "NULL pointer dereferences in the (Linux) kernel" immediately uses incorrect C code with undefined behavior to illustrate something. This: printf("The argv pointer = %d\n", argv); is not okay, you cannot pass a pointer-sized value when you tell printf() you're passing an integer. It needs to be: printf("The argv pointer = %p\n", (void *) argv); You should use %p which is for printing pointers, and cast to void pointer. Edit1: Oh, and I realize it "worked" for the author, no nasal demons [1] appeared and the code printed a number. That's just being lucky, and it's not a very nice foundation to base an argument on. Edit2: Okay, now I read a little bit further and I'm pretty sure the author lives in the "pointers are just integers, and NULL is zero" world, which is of course not a world that co-exists with our own. There is no guarantee that the in-memory representation of a NULL pointer is "all bits zero", that is implementation-defined and completely up the the architecture you're running on (and the compiler). I realize that in practice, on common hardware, NULL is typically implemented as all-bits-zero, but that is not defined by the language. I don't even know if I make sense, obviously I'm a C buff and take these things rather seriously. [1]: http://catb.org/jargon/html/N/nasal-demons.html http://catb.org/jargon/html/N/nasal-demons.html
- saagarjha 5y agoIn the context of vulnerability analysis it is often more convenient to mark undefined behavior with something implementation-defined. This is why you can say "a buffer overflow will overwrite the return address" or "this integer will wrap to be negative", or, in this context, NULL is represented with the zero address/bit pattern.
- vlovich123 5y agoThe article is from 2010 (missing tag) so the author likely is talking about 32 bit x86 machines. Also compilers from that era were less likely to exploit UB if at all. Certainly knowledge about it had only started trickling out in the general industry at the time as compilers started exploiting it with Chris Partner’s blog about it following about a year later [1]. [1] http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- guerrilla 5y ago> Certainly knowledge about it had only started trickling out in the general industry at the time as compilers started exploiting it It was common knowledge among C programmers long before then, for both security and portability reasons but optimization was also done. Old MacOS even used the higher 8-bits of pointers for MM flags. Other popular systems did equally weird things (think VAX.) That discussion linked to by the Jargon file is from 1992.
- Taniwha 5y agoI've programmed all these, old Macs certainly did weird stuff with the upper bits of pointers, Vaxen though have quite ordinary virtual address spaces, they do use upper bits to choose between page tables but that's about it
- tialaramex 5y ago> Also compilers from that era were less likely to exploit UB if at all No. Here's an LWN discussion of a CERT advisory in 2008 trying this nonsense, claiming it's a problem that GCC 4.2 elides futile late NULL checks. https://lwn.net/Articles/278137/ https://lwn.net/Articles/278137/ In fact several other compilers already had this optimisation, and CERT had to go back and revise the advisory because in practice all that's going on is, as now Real Programmers think their undefined program ought to do whatever it was they intended, and that's not how programming works.
- da_chicken 5y agoNote that this article is from 2010. Title should probably include a "(2010)".
- twoodfin 5y agoHP-UX actually had an executable attribute bit that would control whether dereferencing a pointer to address 0 would accvio or result in the value 0. I learned this fact when at least one library (OpenSSL? Kerberos?) shipped with the OS relied on the non-crashing behavior, unconditionally dereferencing an optional pointer-to-struct argument. That was a fun debugging session.
- nickcw 5y ago> NULL is only slightly special as a pointer value: if we look in stddef.h, we can see that it's just defined to be the pointer with value 0. Your C compiler is at liberty to use a different value than 0 for the actual NULL pointer stored in memory or in a register. It has to pretend to you that it is 0, but could be 0xDEADBEEF in reality. Before ANSI C, NULL was #define-d as the actual bit value required. In modern C NULL is always 0. In all the modern architectures I can think of a NULL pointer is represented by a value with all bits 0, but there are lots of examples of older architectures where this wasn't true. There is a section in the comp.lang.c FAQ about this: http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html
- Filligree 5y agoNeeding to care about a wart that's only there due to 40 year old, dead architectures is yet another problem with C. At least in this case the standard goes to some length to make sure you don't have to care.
- SAI_Peregrinus 5y ago40-year old dead architectures like Android on 64-bit ARM?[1] Pointer tagging isn't that rare, and as memory expands in practice I'd expect we might even end up wanting >64-bit systems to allow for >48-bit real address spaces and/or >16-bit tags. The C standard means you don't have to care that there might actually be 2^8 NULL pointers since you're running on ARM with Top Byte Ignore in use. [1]https://source.android.com/devices/tech/debug/tagged-pointers https://source.android.com/devices/tech/debug/tagged-pointer...