Skip to content

Use-after free in Op::connect #125

@fasterthanlime

Description

@fasterthanlime

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:

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...

// 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions