mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:07:36 +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
|
@ -49,7 +49,7 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
|||
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 new_fd = fd_or_error.release_value();
|
||||
RefPtr<Custody> base;
|
||||
if (dirfd == AT_FDCWD) {
|
||||
base = current_directory();
|
||||
|
@ -73,8 +73,8 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
|||
return ENXIO;
|
||||
|
||||
u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
||||
m_fds[fd].set(move(description), fd_flags);
|
||||
return fd;
|
||||
m_fds[new_fd.fd].set(move(description), fd_flags);
|
||||
return new_fd.fd;
|
||||
}
|
||||
|
||||
KResultOr<FlatPtr> Process::sys$close(int fd)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue