mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 13:27:34 +00:00
Kernel: Track allocated FileDescriptionAndFlag elements in each Process
The way the Process::FileDescriptions::allocate() API works today means that two callers who allocate back to back without associating a FileDescription with the allocated FD, will receive the same FD and thus one will stomp over the other. Naively tracking which FileDescriptions are allocated and moving onto the next would introduce other bugs however, as now if you "allocate" a fd and then return early further down the control flow of the syscall you would leak that fd. This change modifies this behavior by tracking which descriptions are allocated and then having an RAII type to "deallocate" the fd if the association is not setup the end of it's scope.
This commit is contained in:
parent
ba03b6ad02
commit
4b2651ddab
11 changed files with 104 additions and 45 deletions
|
@ -21,7 +21,7 @@ KResultOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
|
|||
auto fd_or_error = m_fds.allocate();
|
||||
if (fd_or_error.is_error())
|
||||
return fd_or_error.error();
|
||||
auto fd = fd_or_error.value();
|
||||
auto inode_watcher_fd = fd_or_error.release_value();
|
||||
|
||||
auto watcher_or_error = InodeWatcher::create();
|
||||
if (watcher_or_error.is_error())
|
||||
|
@ -31,15 +31,15 @@ KResultOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
|
|||
if (description_or_error.is_error())
|
||||
return description_or_error.error();
|
||||
|
||||
m_fds[fd].set(description_or_error.release_value());
|
||||
m_fds[fd].description()->set_readable(true);
|
||||
m_fds[inode_watcher_fd.fd].set(description_or_error.release_value());
|
||||
m_fds[inode_watcher_fd.fd].description()->set_readable(true);
|
||||
|
||||
if (flags & static_cast<unsigned>(InodeWatcherFlags::Nonblock))
|
||||
m_fds[fd].description()->set_blocking(false);
|
||||
m_fds[inode_watcher_fd.fd].description()->set_blocking(false);
|
||||
if (flags & static_cast<unsigned>(InodeWatcherFlags::CloseOnExec))
|
||||
m_fds[fd].set_flags(m_fds[fd].flags() | FD_CLOEXEC);
|
||||
m_fds[inode_watcher_fd.fd].set_flags(m_fds[inode_watcher_fd.fd].flags() | FD_CLOEXEC);
|
||||
|
||||
return fd;
|
||||
return inode_watcher_fd.fd;
|
||||
}
|
||||
|
||||
KResultOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall::SC_inode_watcher_add_watch_params*> user_params)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue