vmm: wrap a new fd in UnixListener in serial manager

The original code gave an owned fd to UnixListener. That made the same
fd wrapped into two owned files.

When the files were dropped, the same fd would be closed more than once.
A newly introduced check in Rust's stdlib caught that error.

A newly cloned fd should be given to UnixListener.

Fixes: #6485

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2024-05-27 08:23:07 +00:00 committed by Liu Wei
parent a9e41c417a
commit 400837ff99

View File

@ -274,8 +274,10 @@ impl SerialManager {
let mut in_file = self.in_file.try_clone().map_err(Error::FileClone)?;
let serial = self.serial.clone();
let pty_write_out = self.pty_write_out.clone();
//SAFETY: in_file is has a valid fd
let listener = unsafe { UnixListener::from_raw_fd(self.in_file.as_raw_fd()) };
// SAFETY: from_raw_fd is always called with a valid fd
let listener = unsafe {
UnixListener::from_raw_fd(in_file.try_clone().map_err(Error::FileClone)?.into_raw_fd())
};
let mut reader: Option<UnixStream> = None;
let mode = self.mode.clone();