Kernel: Make cloning of FileDescriptions OOM safe

This commit is contained in:
Brian Gianforcaro 2021-08-13 01:37:07 -07:00 committed by Andreas Kling
parent e7fb70b05c
commit 296452a981
Notes: sideshowbarker 2024-07-18 07:02:15 +09:00
2 changed files with 13 additions and 5 deletions

View file

@ -631,18 +631,23 @@ public:
class ScopedDescriptionAllocation;
class FileDescriptions {
AK_MAKE_NONCOPYABLE(FileDescriptions);
friend class Process;
public:
ALWAYS_INLINE const FileDescriptionAndFlags& operator[](size_t i) const { return at(i); }
ALWAYS_INLINE FileDescriptionAndFlags& operator[](size_t i) { return at(i); }
FileDescriptions& operator=(const Kernel::Process::FileDescriptions& other)
KResult try_clone(const Kernel::Process::FileDescriptions& other)
{
ScopedSpinLock lock(m_fds_lock);
ScopedSpinLock lock_other(other.m_fds_lock);
m_fds_metadatas = other.m_fds_metadatas;
return *this;
if (!try_resize(other.m_fds_metadatas.size()))
return ENOMEM;
for (size_t i = 0; i < other.m_fds_metadatas.size(); ++i) {
m_fds_metadatas[i] = other.m_fds_metadatas[i];
}
return KSuccess;
}
const FileDescriptionAndFlags& at(size_t i) const;

View file

@ -25,7 +25,10 @@ KResultOr<FlatPtr> Process::sys$fork(RegisterState& regs)
child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
child->m_veil_state = m_veil_state;
child->m_unveiled_paths = m_unveiled_paths.deep_copy();
child->m_fds = m_fds;
if (auto result = child->m_fds.try_clone(m_fds); result.is_error())
return result.error();
child->m_pg = m_pg;
{