rust-vsock / vsock-rs

Virtio socket support for Rust
https://crates.io/crates/vsock
Apache License 2.0
25 stars 19 forks source link

VsockStream and VsockListener should probably not implement Clone #31

Closed danielverkamp closed 9 months ago

danielverkamp commented 1 year ago

Currently VsockListener and VsockStream have automatically derived impls of Clone, but since they contain only a RawFd (which is an alias for c_int, not an owned file descriptor), the clone implementation will "clone" the file descriptor by simply copying the integer. This will result in two streams/listeners containing the same file descriptor, so whichever one is dropped first will close() that fd, and the other will have a dangling fd which won't work anymore (or could even refer to a newly-opened file from elsewhere in the program).

Probably the correct thing to do is to just remove the #[derive(Clone)] and replace the try_clone() implementation with one that actually duplicates the file descriptor (e.g. dup() or fcntl(F_DUPFD_CLOEXEC)).

Alternatively, maybe it would be better to use OwnedFd instead of RawFd and use its try_clone() implementation, although that would bump the minimum Rust version to 1.63.0.