r/programming • • Apr 14 '21

[RFC] Rust support for Linux Kernel

https://lkml.org/lkml/2021/4/14/1023
730 Upvotes

308 comments sorted by

View all comments

Show parent comments

115

u/Nicksaurus Apr 14 '21

I assume he would prefer an error code at runtime on an out-of-bounds access

16

u/RepliesOnlyToIdiots Apr 15 '21

Could force array access to include a default, which is either fine by itself or a sentinel to be checked on return.

17

u/steveklabnik1 Apr 15 '21

The more Rust-y way is the .get() method, which returns an Option.

3

u/[deleted] Apr 15 '21 edited Apr 15 '21

force array access to include a default, which is either fine by itself or a sentinel to be checked on return.

Or Rust's existing mechanism for exactly this scenario https://doc.rust-lang.org/std/result/

0

u/[deleted] Apr 14 '21

[deleted]

80

u/wahoo63 Apr 14 '21

Even in release mode, out of bounds array accesses panic in rust

26

u/merlinsbeers Apr 15 '21

Yeah, that's okay in a process, but in the kernel it's completely bad.

25

u/censored_username Apr 15 '21

If it indicates a violation of kernel assumptions, a panic is fine, that's what BUG() exists for as well after all. If it's possible due to external input it should of course use something like the .get() apis instead.

Just cause its a kernel doesn't mean you want to ignore oit of bounds, that's how you get security bugs.

13

u/merlinsbeers Apr 15 '21

You don't want to ignore it, but if you're linking code that panics wnen it detects of oob access, that is a security hole allowing vectors for denial of service. If that's Rust's method for dealing with oob access, then Rust code shouldn't go in the kernel. It should be changed to do something less drastic.

15

u/jonathansharman Apr 15 '21

Panicking cannot possibly be worse than UB, by definition.

7

u/IceSentry Apr 15 '21

No, but linus isn't asking for UB he's asking for having an actual return error code instead of a panic. In that context a panic is indeed worse.

0

u/i-can-sleep-for-days Apr 15 '21

Hm. I don't even agree that out of bounds access, unsafe casts from int to float (ie quake fast inverse square root), should automatically cause a panic.

Let the panics come from the hardware, not from your language. One is truly fatal, the other is we-don't-like-undefined-behavior-so-lets-just-panic-and-say-we-dont-have-undefined-behavior-in-our-language

4

u/electrogravity Apr 15 '21 edited Apr 15 '21

Let the panics come from the hardware, not from your language. One is truly fatal, the other is we-don't-like-undefined-behavior-so-lets-just-panic-and-say-we-dont-have-undefined-behavior-in-our-language

I’m with Linus that Rust should be “fixed” with a mode where we ban any sort of runtime “panic” from being compiled in (outside of very explicitly controlled exceptional circumstances), but there is no reason achieving this needs to entail the massive reliability/stability cost of undefined behavior.

There is no good reason to want undefined behavior (of which hardware-sensitive ”panic” conditions is one), since it not only makes writing reliable bug-free software needlessly difficult in general, but opens the door to endless bugs and severe security vulnerabilities.

So, the only remaining question is why and when we would ever need any undefined behavior to write high performance software, whether high or low level.

Rust answers this. It proves we no longer need (nor should ever desire, obviously) undefined behavior and memory unsafety as the default state of our programming language in order to write highly readable, efficient software (both system level and user level).

And the benefits of safe-by-default are massive, and should be the obviously correct choice in an industry where extremely dangerous and harmful security vulnerabilities are shamefully pervasive — a sad state which is arguably a direct consequence of stubbornly holding to incorrect ideas that we somehow need undefined behavior to prevent sacrificing performance or other nebulous unsubstantiated concerns.

→ More replies (0)

4

u/7h4tguy Apr 15 '21

DOA is the least worrisome security vuln. Fail fast is worlds better than potentially exploitable buffer overflow.

2

u/[deleted] Apr 15 '21

I'm not so sure you want to fail fast in a kernel, however. In face, if I'm not mistaken, that's been Linus's long-standing policy - "whatever you do, don't crash user space".

3

u/lelanthran Apr 15 '21

If it indicates a violation of kernel assumptions, a panic is fine,

Not in release, it is not. You log it and move on. I'd prefer my OS to produce an out of bounds warning, letting me save my work before rebooting than unconditionally deciding for me that my work is less important than the OS.

3

u/matthieum Apr 15 '21

I'd prefer my OS to produce an out of bounds warning, letting me save my work before rebooting than unconditionally deciding for me that my work is less important than the OS.

But logging is not the alternative.

The alternative, in C, is that the OS just read from or wrote to outside the memory area it was supposed to access.

If your developer is conscientious enough to check and log in C, then they're conscientious enough to check and log in Rust -- the panic is an alternative to "UB", not an alternative to smooth handling of edge cases.

9

u/LicensedProfessional Apr 15 '21

That seems like something a compiler flag could fix. OSes have some weird requirements

2

u/matthieum Apr 15 '21

Rust allows specifying the panic handler, however it cannot return (and resume execution) because there's no code at the call site to handle the error.

A panic is a bug, and that's not something a flag can fix.

-2

u/[deleted] Apr 15 '21

Jokes aren't allowed here

0

u/[deleted] Apr 15 '21

[deleted]

1

u/[deleted] Apr 15 '21

I think it hurts their eyes