News for hackers. Stuff that matters.
Re:Rust in Linux kernel is just a beginning by JoeAltmaier on Tuesday October 25, 2022 @05:31AM (#33328249)
Confused by that bug. Adding 2 to a U8 causing overflow makes it smaller, not larger. It will copy too little? not too much.
Re:Rust in Linux kernel is just a beginning by planede on Tuesday October 25, 2022 @05:44AM (#33328335)
"The subsequent memcpy" is this:

  memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
Re:Rust in Linux kernel is just a beginning by abainbridge on Tuesday October 25, 2022 @09:27AM (#33330184)
How would Rust fix this? Here's a Rust program that overflows a u8, with no compile error or warning. https://godbolt.org/z/ME3e9KeMe

You can turn on runtime overflow checking with the Rust compiler, but you can do that with gcc compiling C too.

I guess the answer is that you wouldn't do the memcpy like that in idiomatic Rust - you'd use some higher level construct that gives the compiler more chance to catch your errors. Could anyone comment on how this works in a case like this?

Re:Rust in Linux kernel is just a beginning by j-krieger on Tuesday October 25, 2022 @10:30AM (#33330932)
That is because you're using a normal add instead of `checked_add`. There are discussions about forcing the compiler to only allow `checked_x` and `wrapping_x` operations for any mathematical operation within your code.
Re:Rust in Linux kernel is just a beginning by laweijfmvo on Tuesday October 25, 2022 @10:32AM (#33330962)
I thought the idea was that Rust’s behavior in cases of overflow was defined, whereas c/c++ is not defined by the language.
Re:Rust in Linux kernel is just a beginning by tialaramex on Tuesday October 25, 2022 @11:37AM (#33332046)
The C language says the unsigned integers wrap. That is, C's 8-bit unsigned integer (e.g. uint8_t or in Linux source u8) behaves the same as Rust's Wrapping<u8> type.

In Rust the u8 type wraps in default release builds, but panics on overflow in debug builds, but this is kernel code so it will definitely be built in release mode with wrapping.

Re:Rust in Linux kernel is just a beginning by tialaramex on Tuesday October 25, 2022 @10:37AM (#33331028)
In C when you use an inappropriate type (such as u8 here for a size) it's just coerced. In Rust the wrong type doesn't compile. So, I think the programmer is less likely to choose u8 in the first place.
Re:Rust in Linux kernel is just a beginning by cesarb on Tuesday October 25, 2022 @10:41AM (#33331078)
> How would Rust fix this? [...] I guess the answer is that you wouldn't do the memcpy like that in idiomatic Rust - you'd use some higher level construct that gives the compiler more chance to catch your errors.

Yes, that's the trick: it will be caught not in the u8 overflow, but either in the memcpy equivalent (which is "dst.copy_from_slice(src)"), or in the slice manipulation before it. What happens is that slices in Rust are represented by a "fat pointer", a pair of the starting address and the length, and both the "copy_from_slice" method and the index/index_mut operator checks the bounds before doing the operation.

(You could do things using "unsafe" and raw pointers, which have only the starting address without the length, but in idiomatic Rust you'd use slices most of the time.)