News for hackers. Stuff that matters.
Re:I love coding in C by raverbashing on Thursday December 26, 2019 @09:34AM (#21884028)
C is simple. But not ergonomic. Or safe. It's bad to the point of being almost broken.

"Oh but it's powerful", yes, a chainsaw is powerful but even that has an emergency brake. C has none of that.

(Not to mention compilers that take "undefined behaviour" as a code word for "bamboozle time")

Re:I love coding in C by andrepd on Thursday December 26, 2019 @09:41AM (#21884088)
Undefined behaviour is a tool to generate faster code, by shifting some burden of correctness to the programmer. For example, accessing past the array size is undefined behaviour. This lets the compiler skip the bounds check, an expensive check+branch on each array access. The trade-off is that now the programmer has to ensure correctness manually (or use an alternative interface that wraps a bounds check) else bad (TM) things will happen.
Re:I love coding in C by AaronFriel on Thursday December 26, 2019 @09:55AM (#21884189)
Despite this, tools will often not warn of undefined behavior or these premature optimizations. Quite the opposite, the compilers are almost user hostile for trying to ensure safety and correctness. I cannot see into the minds of the GCC and LLVM teams responsible for the C frontends, but to the extent that you'd think the compiler should do anything here, they seem to think it's largely not their job to improve safety or correctness. That's the job of static analysis tools (which can cost a lot of money), runtime analysis tools (which can cost a lot of time in writing tests that exercise all code paths), and fuzzers (yet more time).

Punting all of this extremely important reasoning to "the programmer" is ridiculous when it's been proven over and over again that even incredibly smart C programmers get this wrong.

Re:I love coding in C by saagarjha on Thursday December 26, 2019 @10:02AM (#21884231)
Compilers could certainly do a better job when performing optimizations that “abuse” undefined behavior, but in general this isn’t a trivial problem. The way optimization passes are set up can often make it difficult to generate warnings that have a low false-positive rate.
Re:I love coding in C by AaronFriel on Thursday December 26, 2019 @10:05AM (#21884254)
Do you think it might be a problem that undefined behavior optimizations and/or pattern matches are so prolific in C that reducing the "false-positive rate" of ... actually bad undefined behavior (?) would be the hard thing?

Why not disallow undefined behavior and instead have the compiler emit: "Hey, this branch is never taken, if you want us not to compile it remove the dead code". Seems like that's exactly the same sort of putting the onus on the programmer that andrepd mentioned, but without the side effect of millions of device drivers are zero day remote code execution vulnerabilities waiting to happen?

Re:I love coding in C by cyphar on Thursday December 26, 2019 @10:27AM (#21884399)
The array access example shows why this isn't generally possible -- the programmer hasn't written any bounds-checking code or assertions (they just wrote x[y]). Should the compiler emit bounds-checks? If yes, your code is now slower but there isn't an easy way to disable this "feature" (if x is heap-allocated or passed as a pointers, how will the compiler know what kind of bounds check is sufficient?). If no, you have undefined behaviour and crashes.

Even Rust made a mistake with restricting undefined behaviour -- they accidentally allowed creating references to fields in packed structures (resulting in possibly unaligned references which is undefined behaviour in Rust) in safe code. These days you get a warning telling you to use unsafe or copy the value, but they have yet to make it an error. For comparison, C also gives you a warning for the same problem (or an error with -Werror).

I also want to point out that modern compilers have different sanitisers (-fsanitize=...) which cause your program to abort() on memory unsafety or other violations. It's effectively a better valgrind which is compiled into your program.

Re:I love coding in C by AaronFriel on Thursday December 26, 2019 @11:10AM (#21884724)
I would argue that most of the time, yes, the compiler should emit bounds checks and only in performance sensitive code should they be removed (or where the compiler can prove the bounds check holds for a range).

I don't know how anyone who has studied the history of vulnerabilities and security issues and code would think this is a hard tradeoff:

> If yes, your code is now slower but there isn't an easy way to disable this "feature" (if x is heap-allocated or passed as a pointers, how will the compiler know what kind of bounds check is sufficient?).

> If no, you have undefined behaviour and crashes.

The latter choice has resulted in how many millions or billions in losses, perhaps even lives lost?

Re:I love coding in C by cyphar on Sunday December 29, 2019 @07:24PM (#21909697)
The problem is that if you want to permit users to do things like dereference pointers and do pointer arithmetic (a requirement for a C-like language) there is no way for the compiler to ensure (even at runtime, because the pointer might not be to memory managed by the language) that the pointer dereference is valid.

Okay, maybe you can add a small overhead to array accesses (which is what Rust did), but you can't stop undefined behaviour entirely. Rust has plenty of undefined behaviour (most can only be triggered through unsafe -- which is the whole argument behind the language -- but as I mentioned there are some cases where even they made a mistake and made an operation safe when it should've have been).

Re:I love coding in C by AaronFriel on Thursday January 02, 2020 @06:21PM (#21941064)
The benefit of "unsafe" blocks is that the surface area to audit is dramatically reduced, so that yes, if you want high performance pointer mangling and arithmetic, you can do it, but your code will require more attention.

In C/C++, undefined behavior and unsafe operations abound and it is nigh impossible to isolate what is "important" to review from a security standpoint.

Re:I love coding in C by saagarjha on Thursday December 26, 2019 @12:12PM (#21885163)
Address Sanitizer is actually a worse Valgrind, because it misses certain bugs and makes up for it by being faster.
Re:I love coding in C by DSMan195276 on Thursday December 26, 2019 @10:53AM (#21884612)
> Why not disallow undefined behavior and instead have the compiler emit ...

You can't "disallow" all undefined behavior in C without fundamentally changing the language to something different (Which would also likely make it useless for it's intended use-case). Some of the details related to undefined behavior are simply not possible for the compiler to detect without extra information, hence why they are undefined. `Rust` has the same thing, you can invoke undefined behavior anywhere in the program through use of `unsafe` anywhere else. And Ex. How is the compiler supposed to determine if a particular function is ever called with a `NULL` parameter? How does it know if a particular signed addition may overflow? How does it know if two parameters to a function alias in an invalid way? How does it know if a particular pointer lacks a `const` and is actually backed by read-only memory? And most importantly, how does it determine all of this at compile time?

With that not all undefined behavior-based optimizations are unexpected or warrant a warning or error (In-fact, I would say at the very least the majority of them don't). Undefined behavior is much more complicated then just dead-code removal. Even then though, you don't always want dead-code removal warned about - Imagine getting a warning about every inline function or macro that results in some dead-code.

To be clear, I'm not saying there aren't obvious problems that have come from this approach, and I think some of the choices for undefined-behavior are clear mistakes now even though they made some sense in the 80s or 90s (And some of those can usually be turned off, thankfully). And I think there are also some additions that could be made to the language that could solve a lot of the existing problems (Like non-null pointers, or fat pointers, though I'm not exactly holding my breath...), but the existence of undefined-behavior is not in-and-of-itself the actual problem with the language, and "disallowing" it outright is simply not possible. I would actually wager undefined-behavior exists in some form in a lot more languages than you'd expect, just less documented. Rust certainly still has it, despite it's numerous safety-focused features. And even if you add things like fat pointers to C that would clearly reduce possible out-of-bounds mistakes, the language would still need to provide some way to create one from a size and a pointer, and that still allows for undefined-behavior cases to happen - my point being, while adding such a thing is still clearly better, it does not actually remove the undefined-behavior possibility.

Re:I love coding in C by imtringued on Thursday December 26, 2019 @10:30AM (#21884421)
>an expensive check+branch on each array access

It's not very expensive because the branch will never be taken except when it results in an out of bounds access. When that out of bound access happens you care more about the security benefits than performance.

Re:I love coding in C by saagarjha on Thursday December 26, 2019 @10:58AM (#21884643)
It has a code size penalty as well.
Re:I love coding in C by kjhrklewiou on Thursday December 26, 2019 @10:31AM (#21884431)
> an expensive check+branch on each array access

I'm curious if this still stands in general today, given today's advanced branch predictors and the fact that the CPU tends to be memory-bandwidth-bound, thus you have more "free computation" while waiting for memory (what GPU programmers call compute to memory ratio).

Re:I love coding in C by jacquesm on Thursday December 26, 2019 @09:35AM (#21884038)
The C emergency brake is called valgrind.
Re:I love coding in C by pjmlp on Thursday December 26, 2019 @10:14AM (#21884308)
Except it doesn't break on all platforms.

So I have to hope for the best when some piece of hardware, coded in C, gets plugged into the network.

Morris Worm is 30+ year old, and yet best we can do is having platforms like CHERI, Solaris SPARC ADI, ARM MTE, which also happen to be quite specific.

Re:I love coding in C by jacquesm on Thursday December 26, 2019 @10:20AM (#21884340)
C has a bad rep, and deservedly so in the context of anything related to security. We have not been able to sanitize the language because the ability to surprise the writer of some bit of code is almost a core concept in the language. This sucks. But at the same time the way newer languages are held up as the manna from heaven is rather tiresome as well. By the time they will be deployed in the quantities that C code is today there will be exploits in those languages because it is mostly the programmers that are fallible. Now, some languages - again, C - make it easier to shoot yourself in the foot than others. But I've seen plenty of code in Java that was exploitable even though memory safety in Java is arguably at a higher level than say Rust.

So you'll be hoping for the best for some time to come, no matter what the stuff you install was written in.

Re:I love coding in C by msla on Thursday December 26, 2019 @11:07AM (#21884703)
It is possible to foreclose on some kinds of error entirely, and if you compare like-for-like, those benefits become obvious. If, on the other hand, you take the productivity gains of a higher-level language as a reason to write more ambitious programs, well, new functionality can find new problems.
Re:I love coding in C by pjmlp on Thursday December 26, 2019 @12:48PM (#21885428)
It all boils down to:

Σ UB + Σ memory_corruption + Σ logical_errors ≥ Σ logical_errors

Re:I love coding in C by klingonopera on Thursday December 26, 2019 @05:51PM (#21887541)
Very nice, but I'd nitpick memory corruption and expand it to hardware errors, or add that category.

Sending digital signals over analog medium (read: always) can fail, and may do so regularly depending on hardware itself and environment conditions.

Simple examples: Digital 3.3V signals sent over 10m, high-capacity lines (lose a bit here and then), or an overclocked CPU undervolting just at the right time, etc...

EDIT: I'd also argue that UB is a type of logical_error. Not the fact that UB exists within the language, but in that the logic failed to account for the scenario?

Re:I love coding in C by pjmlp on Friday December 27, 2019 @07:02AM (#21890840)
Except that unless we are talking about hardware design languages, no programming language accounts for hardware errors as such.

UB is its own kind of error, specially since ISO C documents over 200 use cases, and unless you are using static analysers, most likely won't be able to find out that are failing into such scenarios as no human is able to know all of them by heart, whereas logical errors are relatively easy to track down, even by code review.

Re:I love coding in C by jacquesm on Thursday December 26, 2019 @01:56PM (#21885842)
Very true.

But every language that can reach the hardware will have the ability to wreck things in spectacular and hard to predict ways. Every new language ever was touted as the one that would finally solve all our problems. For Java and COBOL the historical record borders on the comical. I have no doubt that the same will go for every other language that we just haven't found the warts in yet. Two steps forward, one step back, that seems to be the way of the world in the programming kingdom.

Re:I love coding in C by varjag on Thursday December 26, 2019 @10:29AM (#21884414)
Nearly every piece of networked hardware has critical software running written in C, and the consequences are not nearly as disastrous as reading HN would make us believe.
Re:I love coding in C by pjmlp on Thursday December 26, 2019 @12:39PM (#21885371)
The CVE database proves otherwise.

Unfortunately liability is not yet a thing across the industry.

Re:I love coding in C by rightbyte on Thursday December 26, 2019 @01:25PM (#21885629)
What does that site prove?

https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=stack 3496 entries https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=pointer 2389 entries

https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=java 2034 entries

A possible outcome is that you would trade pointer bugs etc. for "Java bugs" if Java embedded were used everywhere. Embedding a complete runtime increases the attack surface alot.

Re:I love coding in C by pjmlp on Thursday December 26, 2019 @01:37PM (#21885725)
Contrary to urban myths, C has a runtime as well.

Many of those Java exploits are on C and C++ written code layer, yet another reason to get rid of them in security critical code.

According to Microsoft Security Research Center and Google's driven Linux Kernel Self Protection project, the industry losses due to memory corruptions in C written software goes up to billions of dollars per year.

Several reports are available.

Re:I love coding in C by jstimpfle on Thursday December 26, 2019 @03:03PM (#21886278)
> Contrary to urban myths, C has a runtime as well.

Would you like to tell us how it compares to e.g. Java's runtime as well?

Re:I love coding in C by pjmlp on Thursday December 26, 2019 @03:13PM (#21886345)
For starters, easier to exploit.

Then, unless we are speaking about a non-conformant ISO C implementation for bare metal deployments, it provides the initialization before main() starts, floating point emulation, handling of signals on non-UNIX OSes, VLAs.

Re:I love coding in C by jstimpfle on Thursday December 26, 2019 @04:54PM (#21887144)
So basically all the stuff that noone cares about? Maybe it's only a runtime if you really want to win a pointless argument?
Re:I love coding in C by pjmlp on Friday December 27, 2019 @07:05AM (#21890853)
How Apple, Google, Microsoft, Sony, ARM are now steering the industry regarding their OS SDKs has already proven who is on the right side.

I have been at this since the BBS days, I don't care about Internet brownie points.

The only thing left is actually having some liabily in place for business damages caused by exploits, I am fairly confident that it will eventually happen, even if it takes a couple of more years or decades to arrive there.

Re:I love coding in C by jstimpfle on Friday December 27, 2019 @07:43AM (#21891001)
Claiming that a little process startup code (that isn't really part of a particular language, but is much more part of the OS ABI) was easier to exploit than an entire JRE is just dishonest.

I would never think of "floating point emulation, handling of signals on non-UNIX OSes, VLAs" as anything resembling a "runtime". These are mostly irrelevant anyway, but apart from that they are just little library nuggets or a few assembly instructions that get inserted as part of the regular compilation.

By "runtime", I believe most people mean a runtime system (like the JRE), and that is an entirely different world. It runs your "compiled" byte code because that can't run on its own.

Re:I love coding in C by pjmlp on Friday December 27, 2019 @07:55AM (#21891057)
I care about computer science definitions, not what most people think.

Dishonest is selling C to write any kind of quality software, specially anything conected to the Internet, unless one's metrics about quality are very low.

Re:I love coding in C by jstimpfle on Friday December 27, 2019 @08:00AM (#21891078)
So "computer science" defines a little process startup code to be equal to the JRE? If that is so, I admit defeat to your infallible logic.
Re:I love coding in C by pjmlp on Friday December 27, 2019 @08:02AM (#21891089)
Computer science defines any kind of code used to support language features as a language runtime.

Assume whatever you feel like.

Re:I love coding in C by jstimpfle on Friday December 27, 2019 @08:06AM (#21891103)
So that seems to be about how balanced and deep you want discussions to go. Thanks for the propaganda anyway.
Re:I love coding in C by pjmlp on Friday December 27, 2019 @03:05PM (#21894086)
Propaganda goes both ways.
Re:I love coding in C by varjag on Thursday December 26, 2019 @02:24PM (#21886012)
Most of the notorious security breaches in practice (Stuxnet, NSA, IoT mega-botnets…) had nothing to do with buffer overflows.
Re:I love coding in C by pjmlp on Thursday December 26, 2019 @03:12PM (#21886338)
A few stars don't hide the impact of billions of dollars in fixing buffer overflows and their related exploits.

> 70% of the vulnerabilities Microsoft assigns a CVE each year continue to be memory safety issues

https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-s...

If you are going to argue that is 'cause Windows, there are similar reports from Google regarding Linux.

Re:I love coding in C by saagarjha on Thursday December 26, 2019 @09:59AM (#21884213)
Or some of the newer sanitizers.