10 ms·
Fearless Security: Memory Safety
- pmoriarty 8y ago"Some languages (like C) require programmers to manually manage memory by specifying when to allocate resources, how much to allocate, and when to free the resources." This is not required of programmers in C, because the programmer could choose to delegate memory management to a memory management library, such as the Boehm-Demers-Weiser conservative garbage collector. [1] [1] - http://www.hboehm.info/gc/ http://www.hboehm.info/gc/
- staticassertion 8y agoNeat. Doesn't really change anything, except that you aren't required to manually manage memory but, practically speaking, you will end up manually managing memory. Especially if you're choosing C for performance requirements.
- pmoriarty 8y agoNot necessarily. My first experience with the boehm-gc was a long time ago, when I was using a very performance-intensive AI library. As an experiment, I modified it to use the boehm-gc and, surprisingly, it actually became faster. I've since learned that such a speed improvement when manual memory management is replaced with the boehm-gc is not uncommon.
- monocasa 8y ago'Performance' isn't just raw averaged throughout, but often includes worse case latency. There's a lot of perf sensitive applications where a GC isn't a great fit.
- chaosite 8y agoThis is true, but a lot of those latency sensitive applications are hard latency requirements, where you have to do something in X time, but doing it faster than X isn't actually useful. There are realtime GCs that can meet these hard requirements.
- monocasa 8y agoThose hard real time GCs everytime I've seen them come with overall throughput compromises. Many times real time constraints and overall throughput aren't mutually exclusive. And even on the soft real time side, like rendering modern GUI, GC pauses causing frameskips (you have ~16ms to render each frame) makes your app look janky.
- staticassertion 8y agoTotally, that could absolutely be the case. But it probably won't be. The reality is that most C programs do use manual memory management, by a very wide margin.
- barbecue_sauce 8y agoYou're still required to manage memory allocation. Even having to explicitly delegate memory management to a garbage collector is arguably manual memory management compared to other languages (It's a choice you have to make and adhere to, not a decision that's already been made for you).
- monocasa 8y agoConservative garbage collection like Boehm inevitably leads to memory leaks in long running applications. It's awesome for some use cases but isn't a complete solution by any means.
- rurban 8y agoWrong. Only if the system failed to identify a root, which is a grave developers mistake, but in the general case boehm-gc does not generate any leaks. Unlike manual memory collection which inevitably leads to memory leaks.
- yorwba 8y agoFailing to identify a root would cause the GC to free too much memory. Memory leaks happen when too little memory is freed. Boehm GC is called conservative because it can't distinguish pointers from other memory content, so it will determine some allocations to be reachable even if there's no pointer to them, because some random integer looks like a pointer into that allocation.
- rurban 8y agoI see, that's what you meant. You are right. You need to use boehmgc in precise mode, which is not the default. You need to tag your pointers. Also use incremental mode for shorter pause times.
- naasking 8y agoConservative collection is unfortunately terrible when you're dealing with data that looks random, like cryptography. It generates many false "pointer live" positives, which ends up retaining a lot more memory which looks like it's leaking.
- YouAreGreat 8y agoTrue if you don't cooperate at all with the collector, but easily avoided in many cases. E.g., when using Boehm, try using gc_malloc_atomic() for data that you know cannot contain pointers, like your encryption buffers.
- baby 8y agoUnfortunately the defaults are insecure, and so are most of the programs written in C.
- zozbot123 8y agoIn related news, the C/C++ development community is finally catching up to what Java has been providing for over 20 years.
- jchw 8y agoWhat is it that the C/C++ development community is catching up to? (And also, I'm not convinced that programming languages should be treated as having their own isolated developer communities, considering there is often a lot of overlap. Are we talking individual users? Companies? Language designers? Etc.)
- pjmlp 8y agoGood support for multi-core programming comparable to what java.lang.concurrent, language thread and network async IO offer. C++11 introduced std::thread, with a couple of issues retified in later revisions, and apparently executors just failed C++20, delaying the introduction of a major part of async networking. Language safety as well. For me, in spite of the safety improvements in C++, I see the language being tailored for specific niches and no longer a full stack language, similar to how it is handled on modern desktop and mobile OSes. And C will never catch up in security.
- tedivm 8y agoThis is not really a good take. There's reasons why Java was able to do what C/C++ couldn't, and there were also performance implications that kept people from moving away from C/C++.
- _underfl0w_ 8y agoInteresting that your metaphor involves comparisons of speed...
- monocasa 8y agoI mean, this post is more about Rust than C and C++.
- baby 8y agoJava is the worst language ever invented. Go and Erlang both have annihilated it. C and C++ are definitely being replaced by Rust now.
- sifoobar 8y agoI have a feeling this will end in disaster one way or the other. Conditioning an army of ignorant script kiddies into believing they're suddenly system programmers who know better than everyone else isn't exactly a recipe for success. Most of them couldn't write a page of decent C if their life depended on it. I wish they would choose a different world for their fearless adventures.
- xiaodai 8y agoWhen your life depends on something, nerves will kick in and prevent you from doing well. Ok? I can't find the article on google now, but the experiment was done in India where a worker will be given the equivalent of one year of salary if they can perform a simple tasks. Before being told the stakes were so highly, many could complete, but after being told about the potential reward, almost none can do it.
- dooglius 8y agoIt's an English idiom, see https://idioms.thefreedictionary.com/if+his+life+depended+on+it https://idioms.thefreedictionary.com/if+his+life+depended+on...
- xiaodai 8y agoI know. Just pointing out that it's not scientific. People use that to mean it's important, and hence will bring out your best. But the opposite is true scientifically.
- devwastaken 8y agoCurrent systems programming in unsafe languages has already resulted in many disasters. Rust is too complex to be the language of script kiddies, and neither does the article infer it's direction towards it. Perhaps if devs of old changes with the times we would already had rust 10 years ago.
- sifoobar 8y ago
- insertcredit 8y agoNot bad but a little bit of a publicity stunt. A major source of vulnerabilities is (still) the Javascript engine and that's (still) written in C++. Even worse, as far as I know, Mozilla has no plans to rewrite even parts of Spidermonkey in Rust. For some recent examples: https://usn.ubuntu.com/3688-1/ https://usn.ubuntu.com/3688-1/ https://usn.ubuntu.com/3749-1/ https://usn.ubuntu.com/3749-1/
- gpm 8y ago> Mozilla has no plans to rewrite even parts of Spidermonkey in Rust. Here's a substantial part being rewritten in rust by Mozilla: https://github.com/CraneStation/cranelift/blob/master/spidermonkey.md#phase-2-ionmonkey https://github.com/CraneStation/cranelift/blob/master/spider...
- insertcredit 8y agoI am not sure I would call cranelift "substantial" in terms of exposure/usage. From what I gather, it's not used at all for normal, everyday Javascript. I stand corrected though, every little bit helps. Here's hope they'll start using Rust in more places where it counts.
- gpm 8y agoI suppose "substantial" is subjective, but I really do thing it counts. Certainly their are unfortunately frequent vulnerabilities in the code it intends to replace. For example: https://bugzilla.mozilla.org/show_bug.cgi?id=1493900 https://bugzilla.mozilla.org/show_bug.cgi?id=1493900 https://bugzilla.mozilla.org/show_bug.cgi?id=1493903 https://bugzilla.mozilla.org/show_bug.cgi?id=1493903 To be fair I'm not actually sure rust would fix either of the CVEs I linked. Both being about problems in the generated code (as I understand them from a glance), which is something inherently unsafe to do. Edit: I realized you might be picking out the word "ARM" on that page. I know Crainlift also works on x86, and I assume it's intended to replace IonMonkey everywhere, not just on ARM chips.
- devwastaken 8y ago
- nine_k 8y agoFor completeness sake, there are C programs that never allocate memory dynamically, using press-allocated variables only. Your microwave likely runs one. There are also programs that allocate but never we-allocate memory. They are fast-terminating programs, ranging from a CLI utility to onboard software that controls a surface-to-air rocket. But many programs still need more complex memory management done safely.
- staticassertion 8y agoI think it's worth noting that there's plenty of memory unsafety to be had without dynamic allocation.
- baby 8y agoUnfortunately, that does not completely prevent memory corruption bugs in C. But it's a step in the right direction. For example BearSSL is a C TLS library that does not use malloc.
- hannob 8y agoBuffer overflow without any dynamic allocation: #include <string.h> #include <stdio.h> int main() { char hello[5]; strcpy(hello, "hello"); printf("%s\n", hello); } (gcc warns about, interestingly clang does not)
- wahern 8y agoRust recently had an integer overflow in str::repeat permitting a buffer overflow. It was a classic bug, the kind that gave C a bad reputation. And the error(s) in arithmetic occurred outside any unsafe block. If we want to be pedantic, all this talk of "fearless" wrt Rust is dangerous hyperbole. Once you move past juggling scalar values things get dicey, especially in C but also in so-called "safe" languages like Rust. And that, I think, was the previous poster's point--as you move away from dynamic allocation you tend to move toward using scalar (fixed-sized) objects. One of the original points of emphasis of Rust was to favor scalar values rather than pointers and even references. To a limited but useful extent you can mimic this in C. Rust examples that simply use Vec miss the point--1) the str::repeat bug overflowed a Vec, and 2) just because C doesn't come with a built-in Vec doesn't mean you can't write one or use one.
- nickpsecurity 8y ago"Another type of problem that can appear is memory leakage... This is a memory-related problem, but one that can’t be addressed by programming languages." https://www.fos.kuis.kyoto-u.ac.jp/~tanki/papers/memoryleak.pdf https://www.fos.kuis.kyoto-u.ac.jp/~tanki/papers/memoryleak.... Well, that took about a minute in DuckDuckGo. :) Key words were "memory leaks" and "type system." For those wanting to find CompSci, adding type system in quotes to a property is a reliable way to find language work on that property. The word language can help, too, but the wording in PDF's varies on that more.
- rwmj 8y agoWhen talking about Garbage Collection they claim that "Even languages with highly optimized garbage collectors can’t match the performance of non-GC’d languages" and link to this paper: http://greenlab.di.uminho.pt/wp-content/uploads/2017/09/paperSLE.pdf http://greenlab.di.uminho.pt/wp-content/uploads/2017/09/pape... However I cannot see where this paper supports the assertion (although Rust comes out well). The particular problem is that malloc/free is not free of charge. Different allocators have complex internal implementations, plus you lose easy sharing of complex structures and compaction. So if you're using a GC you probably program in a different way, eg using more shared immutable structures, exploiting the advantages of GC. Edit: A bit later they brush off this old favourite for GC-haters: https://people.cs.umass.edu/~emery/pubs/gcvsmalloc.pdf https://people.cs.umass.edu/~emery/pubs/gcvsmalloc.pdf claiming that GC requires 5x as much memory. The problem with this paper is it assumes that malloc/free is cost-free and instantaneous, and that your programmer always calls free at the exact moment that the memory is no longer needed, both of which are extremely unrealistic.
- whyever 8y ago> that your programmer always calls free at the exact moment that the memory is no longer needed I think this is not so unrealistic if you use RAII.
- naasking 8y agoEven then. There's nothing stopping you from placing a loop in the same scope as an object that's not used in or after the loop.
- pjmlp 8y agoHerb Sutter has a nice talk about how RAII can cause stack overflows or stop-the-world pauses if used badly. https://www.youtube.com/watch?v=JfmTagWcqoE https://www.youtube.com/watch?v=JfmTagWcqoE
- mrec 8y agoAgreed. I suspect a more useful distinction would be "...can't match the performance of languages that facilitate stack allocation" - the main advantage comes from locality of reference rather than tighter heap management. There's a separate point that could be made about the memory headroom needed for GC to work well, but (sadly) I don't think many people think of memory-efficiency as a performance metric these days. I'm not sure about your > So if you're using a GC you probably program in a different way, eg using more shared immutable structures This is definitely not the case for mainstream GC languages like Java, which have truly awful support for immutability, to the point of having to write a separate type if you want it.
- deleted 8y ago[deleted]