603 points · by pjmlp · 7y ago · 322 comments
This is because one of the sources of very hard to reproduce bugs is a set of race conditions aligning just right.
[0] https://en.wikipedia.org/wiki/Metastability_%28electronics%2...
The inputs to "software" finite state machines are typically inputs in the form of variables. Those inputs define which branches within the routine will be taken during processing.
You can model a subroutine as an FSM for which its "outputs" are its state, the FSM takes inputs, processes them, and sets new outputs or a new state.
We use fuzzing to expose the state machine to all possible combinations of inputs and this identifies all possible exit states from all possible input states, but assumes the input states are stable.
Multi-processing introduces the possibility of race conditions. In a race condition, one input state is present when the state machine is entered, but during execution the race completes which changes the input value.
We use 'stable' to define an input that has the same value across the entire duration of the state machine's state execution.
During a race condition, an input may change value one or more times across the time interval of the state machine's state execution. Thus at any instant in time the input has a singular value, during an interval in time that value may have many different values. These inputs are meta-stable.
And yes, you can fix meta-stability in software with things like mutexes and execution exclusion. Just like you can fix meta-stability bugs in hardware signals by add a clock synchronization domain that spans the widest period of meta-stability possible for a signal.
One of the things that makes race condition bugs so hard to debug is that your typical tracing facility assumes that the inputs passed to a function are stable and don't change. Thus you'll see a trace record of a function call, with its parameters, and walk through the code and say "Wait, with those parameters this code could never do what it just did." Or conversely, that variables that have shared write status across execution domains will be the same throughout a function.
Does that make is clearer what I was talking about? Race conditions suck :-)
The Zebra test is a good friend.
https://software.rajivprab.com/2019/04/28/rethinking-softwar...
Does this automatically generate fixes too, or does someone need to investigate time for each by hand?
>kcsan-with-fixes: Contains KCSAN with various bugfixes for races detected; the commit messages for those bugfixes include the KCSAN report as-is.
Which to me seems to imply some sort of automatic mitigation. Maybe I'm reading too much into it
Still, I think from my interest point of view it would be interesting to not only understand the Linux kernel better, but also other OS design paradigms.
Of course, it's not much of an option for anything I require proprietary software.
Also it’s pretty easy to copy the C stuff to new OSes as well for portability.
Like... Windows?
Can run Linux without virtualization (native performance via syscall translation) in a zone if I hit any issues, which I’ve not yet (simple web stack).
Google around for some of Bryan Cantrill’s talks on smartos. Solaris derivative. He’s been quite critical of several linux engineering decisions and what he frames as sloppy thinking in the kernel community.
I found this helpful/inspiring - https://timboudreau.com/blog/smartos/read - if you follow his example be sure to change dataset_uuid to image_uuid in the zone manifest.
I have both macOS and Ubuntu on my MacBook. I have always predominantly used macOS for all my day-to-day uses and only use Ubuntu to test software that needs to run on Linux before releasing.
I have known many people who use FreeBSD or OpenBSD daily and I have even heard some people installing Haiku as a secondary OS because they have now found it 'very interesting' to use.
I don't know anyone using MINIX though, but I believe that Fuchsia looks like one of the most technically interesting new OSes to be developed in terms of OS design paradigms.
I use macOS all the time. My use-case is systems programming.
I also uses iOS on my phone.
Or is that not what you meant?
"Mac OS X is sort of microkernelish. Inside, it consists of Berkeley UNIX riding on top of a modified version of the Mach microkernel. Since all of it runs in kernel mode (to get that little extra bit of performance) it is not a true microkernel, but Carnegie Mellon University had Berkeley UNIX running on Mach in user space years ago, so it probably could be done again, albeit with a small amount of performance loss, as with L4Linux. Work is underway to port the Apple BSD code (Darwin) to L4 to make it a true microkernel system." [1]
And many many others.
EDIT: windows famously had bugs and had to add special code to preserve the buggy behaviour to keep certain applications that relied on them working https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost...
Part of the job of a kernel is to resolve races. Requests come to it from multiple threads and processes in a parallel fashion. Sometimes your timing will be one way and get you one result, sometimes it will go another way. That's OK. It's the nature of the beast.
Even if there is parallelism and multi-core, it is still a question of ordering, and from there we can use language like who gets there "first" [indeed "race" suggests this]. Or, did this read of a machine word see somebody else's write, etc.
Perhaps a read not seeing someone else's write in a timely fashion is OK in your situation. Perhaps you will resolve conflicts later using proper synchronization.
You can even have race conditions in hardware. Multi-threading or distributed software are excellent recipes for the introduction of race conditions, sometimes so subtle that the code looks good and the only proof that there is something bad going on is the once-in-a-fortnight system lockup.
In a - properly implemented - micro kernel you will have each of those drivers isolated in its own user process, and if there is an issue (memory corruption, crash, logic error or race condition) the possibility of its effects spreading is limited.
Isolation is half the battle when dealing with issues such as these.
Less facetiously, some micro kernels have been proven formally correct. You need a small code to prove formally correct.
And even on an intuitive level, if the code is small I can "hold" it all in my head - at least really grock it in a way you can 1E6 loc.
Would also be cool for auto-complete suggestions. I'm thinking more along the lines of extracting larger patterns than for loops, such as stubbing out classes that follow a projects existing style. For example, adding an auth check at the start of a web request handler if that's what it sees elsewhere.
Yes, these are both much higher level than C, but if what I'm reading among other comments is correct, Rust accomplishes an equivalent improvement in compile time checks while still being a systems language.
It is a heck of a lot easier to not make them in Rust than in someting like C and C++. But Rust is not a panacea for asynchronous code.
But the distinction between "runtime" and "compile-time" is not entirely binary. What you call "compile time" checks can be viewed as an abstract interpretation [1] of the program, or as running the program in some abstract domain -- e.g., where a concrete value could be 3, its abstract value could be called `int`, and the + operation could be interpreted as `int + int = int`; type inference could be viewed as abstract interpretation. The problem is that sound abstract interpretation (i.e., one without false negatives -- if it tells you your programs is "safe" then it will definitely be safe, for some appropriate definition of safe) is limited, and often comes with big tradeoffs, e.g. either false positives given by sound static analyzers or the pain something like Rust's borrow checker sometimes causes -- these are both instances of the same underlying difficulty with abstract interpretation.
One of the most promising directions in formal methods research is "concolic testing" [2], which means a combination of concrete (i.e. non-abstract, or "runtime") execution and symbolic execution (an instance of abstract interpretation). It is not sound, but can be made quite safe, and at the same time it can be very powerful and flexible, checking properties that can be either impossible or extremely tedious, to the point of infeasibility, with abstract methods like types. It might prove to be a good sweet spot between the power, expressivity and cost of concrete (AKA dynamic, AKA runtime) methods and the soundness of abstract (AKA static, AKA compile-time) methods.
Even dynamic methods are often "stronger" (more sound) than just testing assertions during testing. For example, fuzzers can sample the program's state space, offering more coverage, either by analyzing the program (whitebox) or just randomly (blackbox), and various sanitizers sample the scheduling space by fuzzing thread scheduleing.
However, one way to avoid such a problem was basically settling for less accurate, but still strict subproblems. I.e. Rust doesn't stop all race conditions but solves them for a subset of data races.
In other words you reject some valid programs (false positive?) in order to make sure all valid programs are really valid (false negative?).
Here some resources:
The Wikipedia page: https://en.wikipedia.org/wiki/ATS_(programming_language)
"A (Not So Gentle) Introduction To Systems Programming In ATS" by Aditya Siram at the StrangeLoop 2017: https://www.youtube.com/watch?v=zt0OQb1DBko
Introduction to ATS, a series of screen casts: https://www.youtube.com/playlist?list=PL6BIXG1a4elsauhh56i5n...
I do not program in ATS because I am not doing anything that needs to be as efficient, so that I have the luxury to enjoy programming and learning Haskell, in the hope that by the time when I would need to write some super secure and performant program that has to run without interruption, and is used by more users than myself, well, I hope that by that time Haskell will evolve enough to allow me to write such a program. Otherwise I might end up using ATS, though honestly the syntax is so ugly.
So, not only can you do that: it's standard practice for some developers already in and out of commercial space. Mostly commercial developers doing it that I see, though.
Note: Linking to Saturn since it's old work by small team. I theorized big investment could achieve even more. Facebook's acquisition and continued investment with excellent results proved it.
If tech and money is the limit, tell me why Google hasn't straight-up bought the companies behind RV-Match and Mayhem to turn their tech on all their code plus open-source dependencies. Even if over-priced, it might actually turn out cheaper than all the bug bounties adding up. Maybe re-sell it cheap as a service Amazon-style. The precedent is that Facebook bought one that built a scalable tool they're applying to their codebase. Then, they were nice enough to open-source it. Google, Microsoft, Apple, etc could get one, too.
What's Google's excuse? They sure aren't broke or dumb. Gotta be cultural. Likewise, Microsoft built lots of internal tools. They do use at least two static analyzers: one for drivers, one for software. I heard they even ship 2nd one in VS. They don't use most of the MS Research tools, though. The reason is cultural: Operations side knows they'll keep making money regardless of crap security.
Far as FOSS developers, most don't care about security. Those that do tend to follow both what takes little effort (naturally) and whats trending. The trending topics are sanitizers, fuzzers, and reproducible builds. Hence, you see them constantly instead of people learning how to build and scale program analyzers, provers, etc with much better track record. Note that I'm not against the others being built to complement the better methods. I just think, if this is rational vs emotional, you'd see most volunteer and corporate effort going to what has had highest payoff with most automation so far. For a positive example, a slice of academics are going all-in on adaptive fuzzers for that reason, too. They're getting great results like Eclipser.
Also, not all data races can be exploited into serious bugs (in some cases, some stats might just be incorrect, for example).
That doesn't make the tool useless, of course! Just that one should take the numbers with a grain of salt.
2) data races are undefined behavior in C, but:
3) they don't necessarily translate to bugs in practice. for example:
this is a data race, but in practice everything works as expected.
in cases like this, fixing the data race adds overhead without giving us much benefit.
HN is a community. Users needn't use their real name, but do need some identity for others to relate to. Otherwise we may as well have no usernames and no community, and that would be a different kind of forum. https://hn.algolia.com/?sort=byDate&dateRange=all&type=comme...
Anyway, back on topic: would fixing these race conditions be only beneficial to stability, or would this also improve performance/responsiveness?
Edit: it goes without saying, but it's much better to fix the race conditions, regardless of what it means for performance. Some people seem to think I'm advocating for not fixing them.
For example, from dvyukov's KTSAN wiki [1]:
Given a sufficiently expressive atomic API and a good
implementation, you pay only for what you really need (if
you pay just a bit less, generated code becomes incorrect).
So performance is not an argument here.
[1] https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONC...Just to be pedantic... in some contexts better brakes actually allow you to go faster. Consider racing on an oval track... a car with better braking ability can maintain speed longer as it approaches a corner, then scrub off speed more quickly to navigate the corner. A car with inferior brakes has to start slowing down sooner or risk crashing by taking the corner too fast. So better brakes can lead directly to faster lap times.
It's the price to be paid for security. No more branch prediction, no more cutting corners with UB. It's time to do things the right way once and for all and that means setting back performance a good 10-20 years.
Will the fixes consist of adding more synchronization mechanisms (mutexes, semaphores, etc) to prevent potential race conditions?
I think my follow on comment is to just suggest that they fork it and maintain the project themselves.
EDIT: It was meant as a joke as to how media manages to make something that's good into a problem.
It's a team fault, meant that maybe the PO didnt spec correctly, then the developer implemented something wrong, then this was missed in the code review, then missed in team testing, then missed in testing before going live. Missed in the PO sign off before live...
If anything I would hope that stuff like this by Google helps to encourage orgs to build better processes. If you succeed as a team, you fail as a team. No scapegoats!
Yes, and in my physics classes I learned a lot about spherical, frictionless cows in a perfect vacuum.
In the real world, the shape of cows is not a sphere, nor even a closed-form equation. And real organization care about blame, and as the saying goes, shit rolls downhill.
I've shipped more than my share of bugs live. I've never felt like I didn't have a team behind me. Obviously I shipped a lot of things that work, as well.
Working someplace humane and rational doesn't just happen, it requires work.
I expect anyone with "Senior" in their job title to do that work, both in terms of setting team culture and establishing post-mortem policies with management.
The difference is that in the real world, organizations exist that don't merely blame the developer, and aren't really that rare. To give you an idea, I simply don't work for managers that are focused on blame. I probe this during any interview I have, or for any position I'm considering. It's on my list of "Life is too short to put up with this."
I don't have trouble finding jobs.
“Google is uncovering millions of gender preference in literature...” /sarcasm
It would be nice to see an effort to migrate some of the kernel core, but I can't see Rust gaining widespread acceptance in the kernel development community any time soon.
I don't even think that a migration effort from C -> Rust for the Linux Kernel is even feasible or even worth it, given the scale of the project. At this point, you might as well start from scratch.
Google is already experimenting with Rust for OS development with their new Fuchsia operating system [0], which has some drivers written in Rust with a capability based security model.
Similarly, Firefox's Project Quantum seeks to bring Rust to Firefox from the more experimental Servo project.
There are 2 reasons the Linux kernel in particular uses C and will continue to use C: 1) it was C originally and 2) it should compile without requiring some other compiler.
Anyone who sets out to write a new OS kernel (or similar) today for platforms where Rust is available, would probably/hopefully use Rust over C. That doesn't mean it's a good idea to start making existing code bases "hybrids". No one wants a huge codebase where to build it you need N compilers for languages that were popular in various times during the code bases hisory, and where you need to be an expert in N languages to maintain it.
And IMHO, such large number of patches / fixes on Linux should always use a decent open audit process.
The tool is released as open source.
> such large number of patches / fixes on Linux should always use a decent open audit process
All contributions to the Linux kernel are audited by the kernel development team. Look at kernel.org, you'll see that contributions are always signed off by at least one other kernel dev, and often also Linus himself.
> For those wanting to learn more, the code at least for now is being hosted on GitHub.
Google basically announced their project and published the code. That's it. In their email to the LKML (https://lkml.org/lkml/2019/9/20/394) they specifically mention:
In the coming weeks we're planning to:
* Attempt to send fixes for some races upstream […]
There are a few open questions:
* How/when to upstream KCSAN?
So they intend to follow the standard contribution process of sending fixes upstream.How is this a sufficient point of confusion to lead to this comment chain? It’s a link. Click on it and see.