News for hackers. Stuff that matters.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pjmlp on Thursday May 28, 2020 @07:24AM (#23336031)
And the winners are:

- 2x double-free

- 8x NULL pointer dereference

- 6x general protection

- 6x slab-out-of-bounds access

- 14x use-after-free access

Naturally it was only due to the current shortage of those mythical C developers that never make memory corruption mistakes.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gsnedders on Thursday May 28, 2020 @08:25AM (#23336418)
In many ways I'm most interested in the Windows bugs: Windows 8+ has a model-checked USB stack written in P (https://github.com/p-org/P). Or maybe that's just the USB 3.0 stack? Either way, would be interested in whether they're in the integration code or bugs in the P compiler.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by MaxBarraclough on Thursday May 28, 2020 @07:44AM (#23336156)
Hypothesis: the more fuzzing tools we develop, the fewer infallible C programmers remain.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by Gravityloss on Thursday May 28, 2020 @08:11AM (#23336319)
I'm reminded of this apocryphal story of a revered machinist from Tampere. The city is on an isthmus between two lakes. Their shop was right next to the rapids between the lakes. They were famous, having never made any mistakes.

During some construction event, the city closed off the rapids and no water was flowing there, it was completely dry. There was a massive amount of partially machined objects on the bottom. The machinist had been tossing them out of the window into the water, every time when they made a mistake.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by MaxBarraclough on Thursday May 28, 2020 @08:44AM (#23336583)
So the machinist had been using rigorous testing, catching all defects prior to release, rather than using a correct-by-construction methodology. Either approach seems fine.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by bluGill on Thursday May 28, 2020 @10:09AM (#23337393)
Neither is perfect. Tests are only as good as your imagination of what to test. Proof by construction is only as good as your ability to have the right axioms
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by MaxBarraclough on Thursday May 28, 2020 @01:17PM (#23339877)
> Tests are only as good as your imagination of what to test.

A lot of work has been done on code-coverage, boundary-analysis, etc. Testing can never be complete, though.

> Proof by construction is only as good as your ability to have the right axioms

That's not a good summary of the challenges that formal methods face with respect to correctness. Defects can creep in at various points.

See p. 46 of the PDF linked from https://www.adacore.com/tokeneer

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by monadic2 on Thursday May 28, 2020 @04:31PM (#23342184)
Well, apparently it didn’t catch the defect that mattered. Typical engineer trying to justify an engineering mistake!
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by hawski on Thursday May 28, 2020 @03:19PM (#23341248)
He never had problem with use-after-free, because he was leaking memory.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by melling on Thursday May 28, 2020 @08:25AM (#23336422)
I’m not a Rust programmer but usually one interjects to say that Rust would have prevented [some of] these issues.

Does Rust in fact prevent most of these at compile time? Any that it does not?

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @09:51AM (#23337205)
- 2x double-free

Yes, it would be very weird to manage to do this in rust. It would require screwing up badly in unsafe code. I guess the most likely way to fuck up unsafe code in this way would be to screw up a custom datastructure like a custom reference counted pointer (but the obvious solution is to just use the standard ones).

- 8x NULL pointer dereference

Yes, rust tells you when pointers might be Null (which is represented by saying the pointer is Option<PointerType> instead of just PointerType), and doesn't let you use the pointer in a way that implicitly assumes it isn't null.

- 6x general protection

These are generally memory errors, and are certainly undefined behavior, so yes.

- 6x slab-out-of-bounds access

"Yes" with a small caveat, depending on how you are using the slab it is likely that Rust doesn't force you to explicitly think about the out of bounds case and turns just silently turns the security vulnerability into a runtime error, which might be handled farther up the stack or might cause the kernel to halt.

- 14x use-after-free access

Yes, like double frees. The range of possible fuckups in unsafe code that cause this is probably slightly larger than the equivalent range for double frees.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @10:10AM (#23337410)
So the same argument that is made against the "mythical C programmer that never makes memory corruption mistakes" is also valid for the mythical Rust developer that never makes mistakes in unsafe code... It's two sides of the same coin.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by spieglt on Thursday May 28, 2020 @10:29AM (#23337684)
But overall the two situations aren't nearly equivalent. Writing C, you're constantly at risk of making these mistakes. Writing Rust, you can keep the vast majority of your code safe and more closely examine the smaller unsafe area for memory errors. (In driver development, you may have to use more "unsafe" than for application development, but this doesn't negate the benefits. Also the compiler output is much more helpful.)
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @10:43AM (#23337879)
> Writing C, you're constantly at risk of making these mistakes. Writing Rust, you can keep the vast majority of your code safe and more closely examine the smaller unsafe area for memory errors.

I think this is a fallacy...The majority of C code doesn't manipulate pointers either. The point is, the moment that you have _any_ unsafe code (C or Rust), it's a question of time before you will have some bugs, especially if you have a very large number of people working on the same code base...you may be extra careful, maybe the next guy is not...Rust is not going to magically solve these problems for unsafe code...

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by toyg on Thursday May 28, 2020 @10:53AM (#23337975)
I think you guys are debating whether perfect is the enemy of the good.

Rust will not make your code magically bugfree (this is basically impossible by definition), but it will undoubtedly reduce the number of bugs in a very significant way and nudge you towards better code.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by spieglt on Thursday May 28, 2020 @10:57AM (#23338027)
Exactly, it's a difference in degree, not in kind. Rust isn't perfect with regard to memory errors, it's just massively better than C. I don't think pml1 will be convinced, but I've been working on a C project lately and I've never appreciated Rust's memory virtues so much as when using valgrind to debug my string and hashmap implementations. Chased a memory leak for two evenings that was just having free() a few lines off in a function. Stupidly simple error, but my eyes just glazed over the code from having read it so many times. In Rust, it never would've happened because the borrow checker would've dropped the memory at the proper time.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by MaxBarraclough on Friday May 29, 2020 @04:32AM (#23347655)
> The majority of C code doesn't manipulate pointers either

But it's not just pointers. C is unsafe at every turn. Assignments can give undefined behaviour, as can the arithmetic operators, as can varargs... the list goes on.

    int i;
    int j = i; // undefined behavior

    int m = 0;
    int n = 42 / m; // undefined behavior

    int x = INT_MIN;
    int y = -1;
    int z = x / y; // undefined behavior (assuming two's complement)
 
    printf("%d"); // undefined behaviour
> Rust is not going to magically solve these problems for unsafe code...

It doesn't need to. By providing a language where only a tiny fraction of a well-designed codebase needs to use the unsafe features, the number of safety-related bugs can be vastly reduced.

Rust does not aim for perfection. If you want that, your best bet is formal methods.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by samsari on Thursday May 28, 2020 @10:25AM (#23337636)
Except in rust unsafe code is very much not the norm, and much easier keep an eye on. Anyone writing rust worth their salt should be paying 5 times as close attention to every line of unsafe code for exactly this reason.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by monocasa on Thursday May 28, 2020 @10:17AM (#23337518)
Except unsafe blocks are rare and heavily marked as "this is where the unsafeness is".
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @10:22AM (#23337597)
It won't be rare in driver code.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by ComputerGuru on Thursday May 28, 2020 @03:51PM (#23341748)
I’ve written bare metal code for rust that does networking and had only a handful of `unsafe` usages, and none after startup/init was completed. It’s actually really incredible how far “simple” shared-read-vs-exclusive-write bound checking and a very strict (but capable/likely Turing complete) type system can take you.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by monocasa on Thursday May 28, 2020 @10:28AM (#23337670)
It absolutely will, particularly in USB code. USB devices other than host controllers don't have memory mapped registers for instance, it's basically a network protocol.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by _pmf_ on Thursday May 28, 2020 @10:09AM (#23337394)
> These are generally memory errors, and are certainly undefined behavior, so yes.

USB registers might be in DMA buffers or another memory location that might get mapped/unmapped at any point. Not everything fits a simplistic linear memory model.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by asveikau on Thursday May 28, 2020 @10:49AM (#23337946)
This is an interesting point. Rust's borrow checker or bounds checks probably also can't know about a page table change happening underneath it. Anyone want to correct me on that?
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @11:17AM (#23338323)
There's no fundamental reason why you couldn't write an abstraction around the page table that the rust borrow checker understands.

The rust borrow checker works on the principle of ensuring that if you have a unique pointer (&mut) to something, nothing else can access it. If you have a shared pointer (&) to something, nothing else is mutating it except where internal mutability is explicitly marked (UnsafeCell, and abstractions using UnsafeCell such as Cell, RefCell, Mutex, RwLock, and so on).

To mutate a page table entry you would need an &mut reference to it, to access a page you would need an & reference to the page table entry (from the &PageTableEntry you would get a &[u8] pointing to the data which the borrow checker would guarantee you drop before you drop the &PageTableEntry before anything mutates the PageTableEntry).

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by steveklabnik on Thursday May 28, 2020 @11:20AM (#23338362)
This isn't exactly what you're asking about, but I really really love https://os.phil-opp.com/paging-implementation/

Shows you how you might implement paging, and how much unsafe you need to do so.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by monocasa on Thursday May 28, 2020 @11:05AM (#23338160)
It's pretty easy to wrap those constructs in RAII wrappers to replace or augment the normal reference counting that C code would be using to keep those buffers mapped, along with associating the lifetime of the relevant buffers with that refcnt.

So it won't be perfect, but you can add safety versus what you get in C. You can even add safety versus what you'd get in C++ because of the lifetimes you can associate.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by asveikau on Thursday May 28, 2020 @11:18AM (#23338341)
I know it's possible to reference count a page table mapping, in any language. My question is has anybody really attempted it in a rust kernel to make the sort of automatic safety measures we know rust for mean anything at all. It seems like if you really want correctness, every allocation must bump such a recount, which is very expensive.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @11:24AM (#23338398)
So the general trick with reference counted pointers in rust, is that you don't have to touch the allocation count when creating a new pointer as long as you already have a pointer that you know lives for longer than your new pointer, and the rust type system will check that you didn't make a mistake when you thought you did.

I.e. say I have a `x: Rc<[u8]>`, that is a ref counted pointer to some memory, and a length of that memory. I can do `let y: &[u8] = &x;`. `y` is now a not-ref counted pointer to the same memory (with the same length), that's guaranteed to be dropped before `x` is so the memory won't be freed from under it. I can also do `let z: &u8 = &x[5]`. `z` is now a pointer to a byte in `x`. Like `y` it's not ref counted and the compiler will force us to drop it before we drop `x`.

You can make a whole allocator in this fashion (people have, even in the standard library I believe). If you get really clever you can probably even make an allocator in this fashion where the allocator doesn't use any unsafe code, you can easily make one where the users of the allocator don't need any unsafe code.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by josephcsible on Thursday May 28, 2020 @10:49AM (#23337945)
I don't think Rust really makes NULL dereferences any better. In practice, a NULL dereference in C is almost always "just" a crash that can't be turned into something worse (unlike the rest of these kinds of bugs), and Rust makes it really easy to call "unwrap", which if your Option is None... crashes.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @11:01AM (#23338086)
Explicit `unwrap` (rust) is really orders of magnitude better than implicit `unwrap` (c dereference if you're lucky and the compiler hasn't optimized based on the assumption that the pointer is non-null). There aren't actually many explicit unwrap's in practical code, and they're something you notice when auditing or reviewing the code. The change in type means both the caller and the callee almost always agree on whether or not the contract is that "this pointer can be null" or "this pointer isn't null".
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by cesarb on Thursday May 28, 2020 @11:01AM (#23338095)
> In practice, a NULL dereference in C is almost always "just" a crash that can't be turned into something worse

No, in C a NULL pointer dereference is not a crash, it's undefined behavior (which yes, can manifest as a crash), which is much more unpredictable.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by Tobu on Thursday May 28, 2020 @03:22PM (#23341305)
Indeed.

For anyone still believing a NULL dereference (or any of the other UB that is in the C spec) is just a segfault, "What Every C Programmer Should Know About Undefined Behavior" is still required reading:

http://blog.llvm.org/2011/05/what-every-c-programmer-should-...

Here's how triggering UB ends up as a vulnerability:

https://lwn.net/Articles/342330/

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by suyjuris on Thursday May 28, 2020 @05:16PM (#23342760)
> Here's how triggering UB ends up as a vulnerability

To be fair, dereferencing NULL ended up as a vulnerability due to

1. the optimiser removing the subsequent NULL check, and

2. the zero page being mapped.

While 1. may happen, depending on how sophisticated the compiler is, 2. was already a security issue and is 'almost always' not the case.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by elwes5 on Thursday May 28, 2020 @08:46AM (#23336594)
I would argue that Rust removes a class of bugs. Not because the language is better, but because the compiler just does not allow it. It will not let you build your executable if that type of bug exists. For example you could still do something like a SQL injection attack using something compiled with Rust. The C compilers should be doing the same as Rust (some are, but usually at the warn level). Static analysis is not a new thing.

Usually the best thing any C/C++ developer can do is crank the warn levels as high as they can go. Then set the project to error out on warn. A good code smell for a project is when you see one that has turned off warnings. Usually the reason is 'too noisy'. I always tell my junior devs the same thing 'the compiler is trying to tell you something all you have to do is listen'.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by nitrogen on Thursday May 28, 2020 @09:30AM (#23336986)
The next level after fixing all compiler warnings is running under Valgrind's different modes to catch many more memory and threading bugs.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by sanxiyn on Thursday May 28, 2020 @09:22AM (#23336900)
Yes it does. Not most of these, but all of them.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by cesarb on Thursday May 28, 2020 @09:34AM (#23337028)
No, that's untrue. For instance, Rust doesn't prevent an out-of-bounds access at compile time; it just converts an out-of-bounds access into a panic at run time. That is, it reliably aborts the program (or the thread), instead of reading or writing out of the bounds of the object.

The same for "NULL pointer dereference"; the Rust equivalent (without using raw pointers, which require "unsafe") is calling .unwrap() on an Option<T> which has a None, which once again is not prevented at compile time, only converted into a panic at run time.

Edit: note, however, that idiomatic Rust can help prevent these bugs, by using iterators instead of indexed access, and match/if-let statements instead of unwrap/expect.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by jjnoakes on Thursday May 28, 2020 @02:41PM (#23340803)
There's quite a huge difference between undefined behavior (dereferencing NULL or indexing out of bounds) which can lead to remote code execution and other super nasty bugs, and runtime checks which panic and abort the process reliably and in a well-defined way.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @09:47AM (#23337160)
really ? You've checked all the code in question and you are a 100% sure that you would have required _zero_ unsafe code ?

Drivers by their very nature require a lot of unsafe pointer passing...Having worked on a lot of embedded Linux driver code, I'm not convinced that you could do without a ton of unsafe code...which basically negates all of Rust's safety guarantees...The current architecture just doesn't lend itself well to Rust (in my opinion), so you would have to basically rewrite very large parts of the plumbing, which by its very nature would introduce a ton of new bugs...

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by Tobu on Thursday May 28, 2020 @03:40PM (#23341611)
Rust has the concept of "interior unsafe".

The idea is that you build an abstraction and wrap away the unsafety. That abstraction has to be built defensively, which is enabled by the type system enforcing very powerful invariants across the program. You can safely encode the way your structures may or may not be used across threads, enforce exclusive vs shared access, control mutability.

Unsafe is not a "do whatever" card, you use it with the rest of the language as one more tool that helps build safe programs.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @10:03AM (#23337318)
For the most part rust handles pointer chasing in safe code just fine.

Drivers are a fundamentally unsafe concept, like an ffi call, you're talking to hardware the compiler doesn't understand and assuming it's going to do what you want. So there will of course be some unsafe. In a well implemented driver it will also be very limited in scope and relatively easy to check.

I can't vouch for the code quality (I just don't know how good it is, I had no hand in writing it, and it's not used in a production system), but I believe you can find a usb driver implementation written in rust here: https://gitlab.redox-os.org/redox-os/drivers/-/tree/master/x...

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @10:17AM (#23337510)
>So there will of course be some unsafe. In a well implemented driver it will also be very limited in scope and relatively easy to check.

I'm sure that is what the C developer thought as well...I'm not trying to be snarky, but that same arguments that are made against C code holds equally true for unsafe code...

I don't think it is a reasonable position to suggest that unsafe Rust code is somehow safer than C code...

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @10:56AM (#23338019)
The point isn't that unsafe rust code is safer than C code, but that there is many orders of magnitude less unsafe rust code than c code in a driver of the same size.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by teknopaul on Thursday May 28, 2020 @06:21PM (#23343563)
Unsafe rust is safer than C for two reasons.

You can limit what comes in from safe rust.

Unsafe rust still has more compiler checks than C. Unsafe rust is not as permissive as C. Its rust still, with certain things permitted.

Its unsafe{} not nosafe{}

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by smallstepforman on Thursday May 28, 2020 @03:07PM (#23341093)
Rust didnt exist when these kernels were written. A mature kernel written in Rust still doesn’t exist.

Modern toolchains can effectively warn about nemory/pointer issues. I cant wait for a C++2x proposal to add Rust like memory semantics to the language - primarily to shut Rust fanboys up. Also, Rust does not warn about threading race conditions, you’d need a reference capabilities like language (Pony without ORCA) for that.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by teknopaul on Thursday May 28, 2020 @06:04PM (#23343361)
Rust does warn about threading race conditions. Kinda magic but it does. It prevents them.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by 0xffff2 on Thursday May 28, 2020 @06:56PM (#23343996)
Could you elaborate? AFAIK, Rust entirely prevents data races between threads, but it doesn't do anything special at all for other kinds of race conditions.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by eeZah7Ux on Thursday May 28, 2020 @10:35AM (#23337777)
RIIR spam is not useful. People on HN are already aware of Rust.

Some are aware that many other langs are memory safe and yet that's not a reason enough to rewrite a kernel.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @11:52AM (#23338728)
You're the first one in this thread to bring up the idea of rewriting the linux kernel in rust... the rest of us are just having a productive discussion on the degree to which a language solves a problem.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by iforgotpassword on Thursday May 28, 2020 @07:32AM (#23336081)
That would be me, unfortunately I don't have time currently to work on the kernel.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by serf on Thursday May 28, 2020 @10:33PM (#23345722)
>Naturally it was only due to the current shortage of those mythical C developers that never make memory corruption mistakes.

the repetition of this cutesy line lately almost feels like there is some programming language astro-turfing campaign going on somewhere that I haven't become fully aware of yet.

i'm not saying that it's wrong -- safe languages producer safe(r) products -- but to be upset about the unsafe nature of C is, in my opinion, to be upset about the very thing that makes C a powerful language.

The host of foot-gun abstractions and abilities that come along with C are exactly the kinds of reasons why someone would choose C for a project in the first place : flexibility and a sort of 'freedom of expression' that is unmatched everywhere else except for maybe assembly language.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pjmlp on Friday May 29, 2020 @01:39AM (#23346599)
"A consequence of this principle is that every occurrence of every subscript of every subscripted variable was on every occasion checked at run time against both the upper and the lower declared bounds of the array. Many years later we asked our customers whether they wished us to provide an option to switch off these checks in the interests of efficiency on production runs. Unanimously, they urged us not to--they already knew how frequently subscript errors occur on production runs where failure to detect them could be disastrous. I note with fear and horror that even in 1980 language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law."

-- C. A. R. Hoare, Turing award lecture in 1981,

http://www.labouseur.com/projects/codeReckon/papers/The-Empe...

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by nsajko on Thursday May 28, 2020 @10:23AM (#23337606)
You have no proof that writing a Linux-like kernel in Rust would result in fewer bugs, or even that it is feasible at all (what with writing even linked lists being difficult).
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by jabedude on Thursday May 28, 2020 @10:32AM (#23337736)
>or even that it is feasible at all (what with writing even linked lists being difficult).

You're right, a Rust OS kernel probably wouldn't use linked lists but I'm not sure why that is a barrier to making a kernel. The redox[0] kernel is written entirely in Rust.

[0] https://www.redox-os.org/

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by nsajko on Thursday May 28, 2020 @10:43AM (#23337884)
That is why I said "Linux-like". Redox has a microkernel design, which presumably makes writing the drivers in Rust much easier at the expense of some throughput and latency.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by Someone1234 on Thursday May 28, 2020 @11:09AM (#23338222)
I mean it is obviously possible. Rust can do anything that C can do, literally.

Your question is mixing safe and unsafe Rust as if they're one and the same. What I think you meant to ask is "Can Linux be implemented in safe Rust?" And my answer would be "likely not, but the goal is to limit the number/size of unsafe sections, so that they can be QA-ed harder and bugs discovered."

When you write C/C++, 100% of that codebase is unsafe. When you write Rust, even a kernel, should be 80/20 safe/unsafe or less. That's where the improvements come from.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by nsajko on Thursday May 28, 2020 @06:12PM (#23343465)
You are oversimplifying. Both of the languages being Turing-complete does not imply Rust being as good a choice for talking to hardware as C is. What I am getting at is: the imaginary Rust replacement for Linux may have ten times as much code as Linux does and be unmaintainable.

Rust's safe mode eliminates the possibility for some classes of bugs, but this still does not imply the hypothetical Rust kernel would be more reliable than Linux. What I am getting at is: Rust is a quite different language than C, and the hypothetical Linux replacement in Rust could be buggier than Linux.

Regarding the above, my message is not that I know that Rust is worse than C for some applications; but rather that Rust fans and C haters are not even addresing those points. pjmlp deftly avoided saying anything explicitly, but still he should for the sake of the discussion and decency at least try to address those points while making such distasteful comments as "Naturally it was only due to the current shortage of those mythical C developers that never make memory corruption mistakes.".

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pml1 on Thursday May 28, 2020 @05:50PM (#23343183)
> When you write C/C++, 100% of that codebase is unsafe.

Don't you think that statement is a bit over-the-top ? Large parts of C/C++ codebases are just as safe as the equivalent Rust code, as it doesn't do any pointer/memory manipulation.

For example, how is making a os system call in Rust any safer than the equivalent call in C ?

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by gpm on Thursday May 28, 2020 @06:08PM (#23343418)
If you mean literally writing

    asm!("
        mov eax,1  ; system call number (sys_exit)
        int 0x80  ; call kernel
    ")
Obviously both are equally unsafe, also obviously both are extremely rare and not "large parts" of any program whatsoever (unless you're writing your program directly in assembly).

If you mean using the wrapper libraries that typically wrap system calls. Rust removes all sorts of possible fuckups. For instance in C one might write

    ssize_t bytes = read(some_file, buf, nbyte);
and if buf is null or a dangling pointer or nbyte is greater than the size of the allocation of buf you get undefined behavior. Or if afterwards you write

    printf("%s", buf);
and you forgot that read doesn't necessarily return a null terminated string you get undefined behavior. And so on and so forth.

In rust you would write something like

    let mut buf = [0; nbyte];
    let bytes = some_file.read(&mut buf)?;
and buf can't possibly be null or dangling and you can't possibly have messed up the length of the array because the abstractions checked that for you.

Afterwards if we were to write

    println!("{}", buf);
Well it will fail to compile... because buf isn't a string... and doesn't otherwise implement Display. But if we were to try to match the C code exactly and write one of

    stdout().write_all(buf)?;
    println!("{}", str::from_utf(&buf)?);
the second one would return an error if it wasn't a utf8 string, but neither would ever result in undefined behavior/security vulnerabilities.

(And yes, both of those are probably bugs in most programs, since you probably want to print buf[.. bytes] not buf. But bugs aren't security vulnerabilities. Also in real code I expect you would use read_to_string() if you were going to print it, which eliminates this potential bug too)

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by monocasa on Thursday May 28, 2020 @10:56AM (#23338021)
Linked lists are difficult, but can be abstracted behind a library.

Here's a no_std intrusive linked list library I've used in kernel environments. https://docs.rs/intrusive-collections/0.9.0/intrusive_collec...

It's not like you reimplement linked lists in every driver in the Linux kernel either; you include linux/list.h like everyone else.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by black_puppydog on Thursday May 28, 2020 @10:35AM (#23337778)
But "use rust" seems to be obvious enough as a solution that you simply assumed that's the direction GP is going? :)

Honest question though: shouldn't it be possible to only write single kernel modules (like drivers) in rust, without switching out the entire OS at once?

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by black_puppydog on Thursday May 28, 2020 @10:47AM (#23337924)
Answering myself here: of course someone else already did that. :)

https://github.com/fishinabarrel/linux-kernel-module-rust

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pjmlp on Thursday May 28, 2020 @10:40AM (#23337851)
No I don't, but there are surely proofs in Ada, PL/I, PL/S, NEWP.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by AnimalMuppet on Thursday May 28, 2020 @05:02PM (#23342575)
Well... I'm going to guess that the largest kernel written in PL/I was at least one order of magnitude smaller than Linux, and probably two orders of magnitude smaller. I'm also going to guess that it was never subject to a 2020 state-of-the-art fuzzer. (I'm even going to guess that it never had USB support.) So, while you may have a point, your comparison is hardly apples-to-apples.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pjmlp on Friday May 29, 2020 @01:43AM (#23346619)
I bet IBM i does have a couple of USB ports.

And Unisys sells ClearPath MCP to three letter agencies over UNIX for a reason.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by bluescarni on Thursday May 28, 2020 @10:47AM (#23337923)
It's worth noting that many of these issues likely wouldn't have been possible with the use of RAII semantics.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by Someone1234 on Thursday May 28, 2020 @11:04AM (#23338148)
So you're saying if the code was higher quality it would have fewer bugs? I mean, well, yeah.

The problem with solutions like RAII is that it doubles down on programmer infallibility, from "good programmers don't write bugs" to "good programmers correctly use RAII."

You haven't really solved the actual problem unless you have tooling available that won't let non-RAII be checked in at all. Does that exist?

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by bluescarni on Friday May 29, 2020 @06:34AM (#23348362)
> So you're saying if the code was higher quality it would have fewer bugs? I mean, well, yeah.

That's not what I am saying, at all. You cannot do RAII in C.

My point was that I believe that most of the bugs under discussion here would not have happened if a language that provides RAII capabilities had been used.

> The problem with solutions like RAII is that it doubles down on programmer infallibility, from "good programmers don't write bugs" to "good programmers correctly use RAII." > You haven't really solved the actual problem unless you have tooling available that won't let non-RAII be checked in at all. Does that exist?

I don't understand what you are trying to say honestly.

If you think that RAII requires the same level of attention and care as manual malloc()/free()/new/delete, then this makes me wonder if you even know what the point of RAII is. Given the same "programmer quality" (for the lack of a better term), the use of RAII will certainly reduce the occurrence of memory mismanagement bugs.

For the tooling, you can use clang-tidy in C++ to enforce the use of smart pointers, make_unique, etc. E.g., see:

https://www.bfilipek.com/2017/12/why-uniqueptr.html

(I am sure there are other static checkers around that can spot the use of manual memory management functions, it's an easy thing to check for).

So, please don't put words in my mouth. The claim was never that RAII gives you the mathematical certainty of the absence of certain classes of bugs. Rather the claim is that in practice the use of RAII would have prevented most of the bugs presented here.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by pjmlp on Friday May 29, 2020 @07:10AM (#23348582)
However RAII does not prevent use-after-move, which is how use-after-free happens with std::unique_ptr.

And sadly, most recent C++ surveys place the use of static anaylsis tooling at around 50%.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by tinus_hn on Thursday May 28, 2020 @10:35AM (#23337786)
I think part of the problem is the attitude that if your hardware is compromised so is the software, so you don’t need to account for the more insane inputs. After all, at least it doesn’t make much sense to have a device send special packets to cause DOS when it can for instance just blow up the system by causing electrical overload.

Still better to have these issues fixed.

Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by sanxiyn on Thursday May 28, 2020 @07:27AM (#23336058)
Naturally. Where are they? They should be the ones developing operating systems, not these charlatans.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by nineteen999 on Friday May 29, 2020 @03:55AM (#23347403)
Too busy bragging about their pet language on HN, instead of off proving their point with it.
Re:New fuzzing tool finds USB bugs in Linux, Windows, macOS,... by rlpb on Thursday May 28, 2020 @10:00AM (#23337292)
> Naturally it was only due to the current shortage of those mythical C developers that never make memory corruption mistakes.

Ah - you'd prefer to use the that mythical equivalent kernel written in Rust instead?