-
Notifications
You must be signed in to change notification settings - Fork 159
Description
I was wondering why connecting to IPv4 addresses sometimes worked.. but IPv6 doesn't. Sometimes I got an errno 97 (address family not supported), sometimes it was just hanging... and adding a couple dbg! statements made it work?
All of this smells a lot like memory unsafety.. and it is!
Here, a Connect struct is created, moving a SockAddr into it:
tokio-uring/src/driver/connect.rs
Lines 17 to 30 in 1c0a2bf
| Op::submit_with( | |
| Connect { | |
| fd: fd.clone(), | |
| socket_addr, | |
| }, | |
| |connect| { | |
| opcode::Connect::new( | |
| types::Fd(connect.fd.raw_fd()), | |
| connect.socket_addr.as_ptr(), | |
| connect.socket_addr.len(), | |
| ) | |
| .build() | |
| }, | |
| ) |
Then, in submit_with, the data is moved into an Op struct, the callback is called to actually configure the SQE from the given data...
Lines 74 to 78 in 1c0a2bf
| // Create the operation | |
| let mut op = Op::new(data, inner, inner_rc); | |
| // Configure the SQE | |
| let sqe = f(op.data.as_mut().unwrap()).user_data(op.index as _); |
And then op is dropped (and the data with it). We now have an op in the submission queue that refers to some userspace address that's been freed (somewhere on the stack, presumably). This probably works well in tests because immediately after attempting to connect, it sleeps. But we now have the kernel using some memory after we've freed it.
A potential fix would be to move the data into the Lifecycle::Submitted variant - but that of course raises the question of: should it be boxed? Probably not right? So there's only so much data we can stuff in there - maybe connect is one of the rare ops that needs something like this, so maybe it should itself contain an enum?
At any rate, that explains the random failures I've been getting.