10 ms·
There’s a Hole in the Bottom of the C: Effectiveness of Allocation Protection [pdf]
- Animats 8y agoShort version: yet another hokey scheme for fixing buffer overruns in C fails for typedef struct { char buf[124]; void* fptr; } mystruct; because if you overrun "buf", you can overstore "fptr" without going outside the bounds of "struct". Exploit for Ngnix found. We've got to get more code out of C and into something with subscript checking. None of the kludges for fixing C work.
- kragen 8y agoI don't know why they didn't put this in the abstract.
- nneonneo 8y agoI don’t think they actually found an exploit for Nginx: they state that “we assume such a memory bug exists”, rather than saying outright what bug they exploited. It almost sounds like they planted a bug and then exploited it in a binary that was hardened (using this non-standard hardening technique, i.e. not exploiting a binary that was ever used in a real deployment of Nginx).
- fulafel 8y agoExploit != vulnerability. You develop an exploit to take advantage of a specific vulnerability. An exploit for a hypothetical vulnerability is a valid concept, and their conjecture that such a vulnerability may exist is justified in the paper.
- deleted 8y ago[deleted]
- pjmlp 8y agoThat will never happen with UNIX based OSes around, so what we really need, as much as I would like to nuke C, is to have developers accept some variant of "Safe C". Solaris has it thanks to SPARC tagged memory. Problem is when would we ever get it as mainstream, always enabled, in other platforms.
- Animats 8y agoRust is getting some traction, and much web infrastructure has moved away from C/C++ to C# or Go or Java. After decades of little progress, we're seeing forward motion.
- pjmlp 8y agoTrue, myself have moved definitely into Java/.NET worlds back in 2006, although C++ is still an option for what is unavoidable to do without it. Maybe in the future their canonical implementations can even be fully bootstraped or have replaced the C++ layer with something else. But even if all userspace developers move to safer languages, we still have the UNIX derived kernels, PIC and other microcontrollers, routers and plenty of other embedded stuff to worry about. Even Microsoft with their Azure Sphere project, which is supposed to be all about secure IoT, still bundles a Linux kernel with it. Hence why we really need to fix C somehow.
- andrewflnr 8y agoThere's no way to "fix C" in a way that saves you from the cost of fixing all those broken programs written in C. The best a language-level solution can do for an existing project is make it easier to figure out what specifically needs fixing. Or blow up your code and run time with checks, I guess, but if that was technically feasible, to say nothing of political challenges, those projects wouldn't have been in C to start. I feel like Elrond at the council of Rivendell: "you have only one choice." Except throwing a ring into a hole is a comparatively well-defined task relative to rewriting an unknown number of crappy pieces of software. Ed: also there's no conveniently situated group of people to whom to assign the project.
- pjmlp 8y agoBasically, until the world gets rid of UNIX derived OSes, we call it a defeat and go back home.
- 8y ago
- mirekrusin 8y agoI always thought this is example of basic buffer overflow in C - what's different with this example? If somebody asked me to write buffer overflow example in C I'd write something that looks exactly like this, this must have been known for years, what am I missing here?
- pcwalton 8y agoThe "classic" buffer overflow involves overwriting the return address by overflowing an array on stack. Low-fat pointers can protect against that problem [1]. [1]: https://www.comp.nus.edu.sg/~gregory/papers/ndss17stack.pdf https://www.comp.nus.edu.sg/~gregory/papers/ndss17stack.pdf
- writepub 8y agoor you could move all pointers to the beginning of the struct, and could even make the pointer a const (read only). typedef struct { const void* fptr; char buf[124]; } mystruct;
- roblabla 8y agoWhat if mystruct ends up in an array? buf[125] will overwite fptr all the same. Furthermore, what if you need two arrays in your struct?
- BagOfPistchios 8y agoThe const keyword really doesn't mean anything. It is not checked at runtime, so a buffer-overrun could change the const member regardless
- gpderetta 8y agoand even if it were, it is in the wrong position (as defined, fptr is a mutable pointer to const data).
- nneonneo 8y agoActually the structure in Nginx that the authors attacked is laid out exactly in this way: struct ngx_http_request_s { uint32_t signature; ngx_connection_t connection; ... ngx_http_log_handler_pt log_handler; ... u_char lowcase_header[NGX_HTTP_LC_HEADER_LEN]; ... unsigned http_minor:16; unsigned http_major:16; }; They underflow lowcase_header (or rather, assume the existence of an underflow bug) to overwrite log_handler, a function pointer that is called when an error occurs. I do find the authors' presentation a little disingenuous, because the purported underflow bug does not actually exist - the authors assume it does, then proceed as if they have full control over the function pointer (including the ASLR leak necessary to obtain proper gadget addresses).
- ElBarto 8y agoIn such cases you can insert a barrier with a magic value to detect any array overflow. The other thing to do is to enforce checks on array indices.
- nneonneo 8y agoYou're referring to canaries, which are special values used to detect if overflows occur. Canaries are usually checked on heap deallocation (resp. stack canaries are checked on stack frame deallocation, i.e. function return). Checking them at any other time is expensive and incurs high overhead. They also don't catch all overflows - an overflow with an attacker-controlled index could easily skip the canary and overwrite whatever lies past it. The problem of memory bounds checking is solvable if you decide to forego memory, processing time, and/or compatibility with existing code. Trying to achieve memory safety while maintaining those three properties is very hard.
- ElBarto 8y agoThere's no way around it. What I described is what higher-level languages do if they provide this sort of protection and it's what must do done if such protection is required.
- lvs 8y agoNo, it's more than that. While that struct might be a generic overrun, the question is how you check bounds on filling buf. The paper is discussing that schemes to constrain buffer size using a global hardware or software address-based size constraints ("low-fat") as bounds checks are less safe than claimed.
- amelius 8y agoWouldn't Valgrind catch this one?
- jcranmer 8y agoTo my knowledge, valgrind doesn't catch overflow errors that overflow inside of an object.
- tedunangst 8y agoValgrind is basically an implementation of the defense they are saying doesn't work. It works on an allocation basis.
- kevin_thibedeau 8y agoIt would be nice if the betterC mode of Dlang would get traction. That fixes the worst weaknesses of C without having to create a whole new ecosystem.
- MaxBarraclough 8y ago> without having to create a whole new ecosystem But it does, doesn't it? You're suggesting we replace C, which isn't necessarily a bad idea, but there are considerable hurdles, as it's a lingua franca. C has an excellent suite of compilers for every platform you can name, excellent development tools, every serious programmer already knows the language, and it easily hooks in to platform-specific functionality. If we want to replace C, we have a few languages to choose from: BetterC, Zig, Rust. If BetterC were a strict subset of C, things would be quite different, but it's not, so it has the just another language problem. If we want to go the subset route, we already have MISRA C, which blocks various categories of bugs and undefined behaviour. Crucially, it's possible to mechanically verify that a program is MISRA-compliant.
- derf_ 8y agoOne (non-generalizable) solution is to avoid using writable function pointers, to help maintain control flow integrity. For example, in libopus we have platform-specific SIMD optimizations that are chosen at runtime. The common solution is just to have a function pointer that points to the accelerated routine. But there are only a limited number of choices for each routine. So we put pointers to them in a static const array, which gets stored on a read-only page. Then instead of storing a pointer, we just store an index into this array. This index is chosen based on the available instruction sets detected at runtime. This lets us use the same index for every accelerated function. Then, we pad the array size out to a power of two. To call an accelerated function, we mask off the upper bits of the index. Even if the index gets corrupted, we still call one of the functions in the array. So there's only so much damage it can do. Obviously this doesn't apply if the set of functions you need to call is open-ended (e.g., a callback, like nginx was using). But it seems like a good pattern to follow when it applies.
- chocolatebunny 8y agoIs there a reason why you did that that way other than to avoid writeable function pointers? Also, that just sounds like how switch/case is sometimes implemented. Have you considered just using a switch/case statement instead of manually managing function pointers and the like?
- derf_ 8y agoThat was the main reason. It also means that you don't need to pass around (a pointer to) a big table of function pointers throughout all of your functions that need to use accelerated routines. Instead you just pass a single integer index. But that is pretty minor. It can be pretty similar to a switch in practice, but I think there are a few differences. One is function argument handling. Each version of an accelerated routine has to be an independent function, compiled in a separate compilation unit, because you have to limit the instruction sets available. So you will duplicate all of the function call setup overhead in each branch of the switch. This probably gets optimized away by a decent compiler. Even if it does, you still wind up essentially duplicating the function table at every call site. That does not seem likely to get optimized away. You could dispatch to a common thunk which selects the accelerated routine to call, but now you have two function calls instead of one. I am also not sure that switch statements handle the default case as cheaply as doing a single bitwise AND with a small, compile-time constant. But if you have a use of function pointers that can be replaced by a simple switch statement, that is probably the better approach.
- rawoke083600 8y agoHonest question: Can't you just swap around the order of fptr and buf ?
- z92 8y agoWon't work if you have more pointers and buffers in the structure.
- dataking 8y ago> We've got to get more code out of C and into something with subscript checking. None of the kludges for fixing C work. shameless plug: https://www.c2rust.com https://www.c2rust.com
- duneroadrunner 8y agoequally shameless: https://github.com/duneroadrunner/SaferCPlusPlus-AutoTranslation https://github.com/duneroadrunner/SaferCPlusPlus-AutoTransla...
- ndesaulniers 8y ago> None of the kludges for fixing C work. Turn on ASAN in production. HWASAN may make that more palatable.
- nneonneo 8y agotl;dr: the authors overflow from one struct field into another struct field, thus avoiding any allocation protection scheme that protects individual heap allocations from each other. Most of the paper is just spent implementing basic ROP-style gadget hopping. I’m not actually clear on what’s new or novel here. Allocation protection mechanisms exist to prevent one allocated block from overflowing into another allocated block. They usually don’t protect allocated blocks from themselves.
- WalterBright 8y agoHere's my proposal for fixing buffer overflows in C: https://www.digitalmars.com/articles/b44.html https://www.digitalmars.com/articles/b44.html It's simple and it works, we've got 15 years experience with this in D.
- baybal2 8y agoI wish somebody could bring Walter into C standard committee
- WalterBright 8y agoI remember when C added function prototypes. It was an unmitigated and welcome improvement. If I was to add two features with the most impact, simple to implement, and no backwards compatibility issues, it would be: 1. phat pointers for arrays 2. nested functions (Nested functions have a surprising ability to untangle ugly control flow, including getting rid of most uses of goto's.)
- anticensor 8y agoThey do not want to break single-pass compilers. Nested functions with no forward declarations will require a multi-pass compiler. I think your D compiler is multi-pass already.
- naasking 8y ago> They do not want to break single-pass compilers. Nested functions with no forward declarations will require a multi-pass compiler. Why is that exactly?
- WalterBright 8y agoNested functions are done in one pass. As you might infer, they cannot be forward referenced within the function.
- pcwalton 8y ago
- acomjean 8y agoThere's a hole in the bucket... dear liza dear liza. I suspect the circular never ending problem song might not have been what they were going for (or maybe it was). https://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket https://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket
- xvilka 8y agoJust simply slowly substitute C code, piece by piece into Rust. Building userspace less depending from C ecosystem.
- carlmr 8y agoYes, at least as long as you're working on a target supported by the Rust compiler.
- bnastic 8y agoHard to read a paper that repeats “legacy languages” many times just on the first page.
- rurban 8y ago> In this paper, we have illustrated a new type of attack that can bypass bounds checking spatial memory safety techniques that protect allocations. No they have not. Bounds checking only outer structs doesnt do bounds checks on inner buffers. They haven't analyzed traditional bounds checks in C: asan, fortify, c11 Annex K, Intel MPX, only a broken application, which does none of these, and uses a wrong bounds check.
- deleted 8y ago[deleted]
- srfilipek 8y agoWithout protection function pointers themselves, this only covers a small part of the issue. I'm more interested in seeing how CPI and CPS pans out in the end: https://dslab.epfl.ch/pubs/cpi.pdf https://dslab.epfl.ch/pubs/cpi.pdf