Repository navigation
io: add AsyncFd::register and deprecate AsyncFd::new - #8587
Conversation
There was a problem hiding this comment.
nit: I prefer new2 instead of register. I know it looks ugly. However, there are some benefits:
new2still follows the common naming pattern of constructor, less surprise.- Developers already using
AsyncFd::newcan quickly infer from the nameAsyncFd::new2that this is an (almost) drop-in replacement. Otherwise, developers might need a dozen or so seconds to ensure that it is (almost) a drop-in replacement.
|
Or 'newer' 😀 |
Haha, it looks like a bad idea based on the GitHub reaction. Anyway, not a blocker. |
|
Any opinions on whether this is something we should backport to LTS? Potentially backport the new methods, but not the |
I'd skip the backport. LTS only takes patch releases, but new public methods and new deprecation lints are both minor changes (RFC 1105) Since #8540 already mitigates the UB, and the full fix needs new API, so it would be better landing the deprecation annotations and new method in the next minor release. |
|
According to the bug patching policy, LTS releases are specifically intended to allow bug fixes in patch releases. From an outside perspective, backporting this seems justified: this is a soundness issue in an existing API rather than a new feature. I understand the concern around adding Since the new API provides the migration path, would it make sense to backport both the new API and the deprecation? That would let LTS users stay on the LTS line while getting both the fix and a clear indication that they should move away from the problematic API. |
|
I guess part of the concern is that if we file a soundness advisory to RUSTSEC, we have to mark which versions have fixed the soundness issue, and I don't want the LTS branches to be listed as affected by an advisory. |
|
Given these constraints, and the fact that anyone could file a RUSTSEC advisory, it doesn't seem like we can really treat the backport as optional. It seems like we need to backport both the new API and the deprecation so the LTS branches can be considered fixed. |
|
Strictly speaking, As long as the Previously, we have released a breaking change without deprecation cycle and major release (#3345) as the the impact is minimal at that time. However, now, it is pretty hard for us to do similar thing (remove |
|
We're not going to remove |
This comment was marked as outdated.
This comment was marked as outdated.
|
Just to be clear, I'm not trying to block it, just raising the concerns I had. It is fine for me to backport deprecation annotation plus new APIs. |
@djc Would you consider a version to still be affected by a soundness advisory if the unsound API remains available but is deprecated? |
No, I agree with @Darksonn's judgement here (as usual). (For good measure, I might also mark it as |
Tokio's `AsyncFd` was added before the concept of I/O safety was stabilized, so for historical reasons it requires `T: AsRawFd` instead of `T: AsFd`. However, Tokio's IO driver tracks per-IO resource information in an allocated object, and the user data given to epoll is a pointer to said object. This means that if epoll returns a readiness event for an fd after its object is removed, that leads to use-after-free. Unfortunately, this situation can happen if `AsyncFd` is misused in safe code. For example, if the underlying fd is closed while the `AsyncFd` exists. We recently merged tokio-rs#8540, which avoids the UAF in common cases, but it's not a perfect mitigation, and as seen in tokio-rs#8539 it's still possible to trigger UAF today. There are only really two ways to fix this issue: 1. Stop storing pointers in the epoll user data, and instead store a key into a hash map or similar data structure, so that UAF can be detected safely. 2. Adopt I/O safety for `AsyncFd`. This PR takes the second approach. Specifically, `AsyncFd::new` is deprecated, and a new, unsafe `AsyncFd::register` is added to replace it. Note that `AsyncFd::register` has to be an unsafe constructor. It's not enough to just accept `T: AsFd` instead of `T: AsRawFd` because the `AsFd` trait does not promise that every call to `as_fd()` returns the *same* file descriptor. I also considered the option of defining a new `unsafe trait AsyncFdSafe` with a safety requirement that every call to `as_fd()` returns the same file descriptor. However, that's not enough either because of `AsyncFd::get_mut()`, which lets the end-user `mem::swap` the I/O resource for another one. And we can't get rid of `get_mut()`-like methods on `AsyncFd`, because `AsyncFd` fundamentally relies on the user manually invoking `read()` and `write()` directly on the inner I/O resource. Fixes: tokio-rs#8539
|
I think we do want to mark them |
In that case, the documentation for |
AsyncFd::new and related constructors violate I/O safety
2389-research/smasher#52

Tokio's
AsyncFdwas added before the concept of I/O safety was stabilized, so for historical reasons it requiresT: AsRawFdinstead ofT: AsFd. However, Tokio's IO driver tracks per-IO resource information in an allocated object, and the user data given to epoll is a pointer to said object. This means that if epoll returns a readiness event for an fd after its object is removed, that leads to use-after-free.Unfortunately, this situation can happen if
AsyncFdis misused in safe code. For example, if the underlying fd is closed while theAsyncFdexists. We recently merged #8540, which avoids the UAF in common cases, but it's not a perfect mitigation, and as seen in #8539 it's still possible to trigger UAF today.There are only really two ways to fix this issue:
AsyncFd.This PR takes the second approach. Specifically,
AsyncFd::newis deprecated, and a new, unsafeAsyncFd::registeris added to replace it.Note that
AsyncFd::registerhas to be an unsafe constructor. It's not enough to just acceptT: AsFdinstead ofT: AsRawFdbecause theAsFdtrait does not promise that every call toas_fd()returns the same file descriptor.I also considered the option of defining a new
unsafe trait AsyncFdSafewith a safety requirement that every call toas_fd()returns the same file descriptor. However, that's not enough either because ofAsyncFd::get_mut(), which lets the end-usermem::swapthe I/O resource for another one. And we can't get rid ofget_mut()-like methods onAsyncFd, becauseAsyncFdfundamentally relies on the user manually invokingread()andwrite()directly on the inner I/O resource.Fixes: #8539