1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 08:45:09 +00:00

Kernel/ProcFS: Allow a process directory to have a null Process pointer

In case we are about to delete the PID directory, we clear the Process
pointer. If someone still holds a reference to the PID directory (by
opening it), we still need to delete the process, but we can't delete
the directory, so we will keep it alive, but any operation on it will
fail by propogating the error to userspace about that the Process was
deleted and therefore there's no meaning to trying to do operations on
the directory.

Fixes #8576.
This commit is contained in:
Liav A 2021-07-10 00:24:47 +03:00 committed by Andreas Kling
parent d1028f8aed
commit bee75c1f24
4 changed files with 87 additions and 24 deletions

View file

@ -59,8 +59,6 @@ void ProcFSComponentRegistry::unregister_process(Process& deleted_process)
process_folder->prepare_for_deletion(); process_folder->prepare_for_deletion();
process_folder->m_list_node.remove(); process_folder->m_list_node.remove();
dbgln_if(PROCFS_DEBUG, "ProcFSExposedDirectory ref_count now: {}", process_folder->ref_count()); dbgln_if(PROCFS_DEBUG, "ProcFSExposedDirectory ref_count now: {}", process_folder->ref_count());
// Note: Let's ensure we are the last holder of the ProcFSProcessDirectory object before it can be deleted for good
VERIFY(process_folder->ref_count() == 1);
} }
RefPtr<ProcFS> ProcFS::create() RefPtr<ProcFS> ProcFS::create()

View file

@ -160,7 +160,9 @@ KResult ProcFSProcessInformation::refresh_data(FileDescription& description) con
auto parent_folder = const_cast<ProcFSProcessInformation&>(*this).m_parent_folder.strong_ref(); auto parent_folder = const_cast<ProcFSProcessInformation&>(*this).m_parent_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return KResult(EINVAL); return KResult(EINVAL);
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (!process)
return KResult(ESRCH);
process->ptrace_lock().lock(); process->ptrace_lock().lock();
if (!process->is_dumpable()) { if (!process->is_dumpable()) {
process->ptrace_lock().unlock(); process->ptrace_lock().unlock();

View file

@ -142,19 +142,21 @@ class ProcFSProcessDirectory final
public: public:
static NonnullRefPtr<ProcFSProcessDirectory> create(const Process&); static NonnullRefPtr<ProcFSProcessDirectory> create(const Process&);
NonnullRefPtr<Process> associated_process() { return m_associated_process; } RefPtr<Process> associated_process() { return m_associated_process; }
virtual uid_t owner_user() const override { return m_associated_process->uid(); } virtual uid_t owner_user() const override { return m_associated_process ? m_associated_process->uid() : 0; }
virtual gid_t owner_group() const override { return m_associated_process->gid(); } virtual gid_t owner_group() const override { return m_associated_process ? m_associated_process->gid() : 0; }
virtual KResult refresh_data(FileDescription&) const override; virtual KResult refresh_data(FileDescription&) const override;
virtual RefPtr<ProcFSExposedComponent> lookup(StringView name) override; virtual RefPtr<ProcFSExposedComponent> lookup(StringView name) override;
virtual void prepare_for_deletion() override;
private: private:
void on_attach(); void on_attach();
IntrusiveListNode<ProcFSProcessDirectory, RefPtr<ProcFSProcessDirectory>> m_list_node; IntrusiveListNode<ProcFSProcessDirectory, RefPtr<ProcFSProcessDirectory>> m_list_node;
explicit ProcFSProcessDirectory(const Process&); explicit ProcFSProcessDirectory(const Process&);
NonnullRefPtr<Process> m_associated_process; RefPtr<Process> m_associated_process;
}; };
class ProcFSBusDirectory : public ProcFSExposedDirectory { class ProcFSBusDirectory : public ProcFSExposedDirectory {
@ -227,8 +229,26 @@ public:
virtual KResultOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* description) const override; virtual KResultOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* description) const override;
virtual uid_t owner_user() const override { return m_parent_folder.strong_ref()->m_associated_process->uid(); } virtual uid_t owner_user() const override
virtual gid_t owner_group() const override { return m_parent_folder.strong_ref()->m_associated_process->gid(); } {
auto parent_folder = m_parent_folder.strong_ref();
if (!parent_folder)
return false;
auto process = parent_folder->associated_process();
if (!process)
return false;
return process->uid();
}
virtual gid_t owner_group() const override
{
auto parent_folder = m_parent_folder.strong_ref();
if (!parent_folder)
return false;
auto process = parent_folder->associated_process();
if (!process)
return false;
return process->gid();
}
protected: protected:
ProcFSProcessInformation(StringView name, const ProcFSProcessDirectory& process_folder) ProcFSProcessInformation(StringView name, const ProcFSProcessDirectory& process_folder)

View file

@ -93,7 +93,10 @@ KResultOr<size_t> ProcFSProcessStacks::entries_count() const
auto parent_folder = m_process_folder.strong_ref(); auto parent_folder = m_process_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return KResult(EINVAL); return KResult(EINVAL);
return parent_folder->m_associated_process->thread_count(); auto process = parent_folder->associated_process();
if (process.is_null())
return KResult(ESRCH);
return process->thread_count();
} }
KResult ProcFSProcessStacks::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const KResult ProcFSProcessStacks::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
@ -105,7 +108,9 @@ KResult ProcFSProcessStacks::traverse_as_directory(unsigned fsid, Function<bool(
callback({ ".", { fsid, component_index() }, 0 }); callback({ ".", { fsid, component_index() }, 0 });
callback({ "..", { fsid, parent_folder->component_index() }, 0 }); callback({ "..", { fsid, parent_folder->component_index() }, 0 });
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return KResult(ESRCH);
process->for_each_thread([&](const Thread& thread) { process->for_each_thread([&](const Thread& thread) {
int tid = thread.tid().value(); int tid = thread.tid().value();
InodeIdentifier identifier = { fsid, thread.global_procfs_inode_index() }; InodeIdentifier identifier = { fsid, thread.global_procfs_inode_index() };
@ -120,7 +125,9 @@ RefPtr<ProcFSExposedComponent> ProcFSProcessStacks::lookup(StringView name)
auto parent_folder = m_process_folder.strong_ref(); auto parent_folder = m_process_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return nullptr; return nullptr;
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return nullptr;
RefPtr<ProcFSThreadStack> procfd_stack; RefPtr<ProcFSThreadStack> procfd_stack;
// FIXME: Try to exit the loop earlier // FIXME: Try to exit the loop earlier
process->for_each_thread([&](const Thread& thread) { process->for_each_thread([&](const Thread& thread) {
@ -195,7 +202,10 @@ KResultOr<size_t> ProcFSProcessFileDescriptions::entries_count() const
auto parent_folder = m_process_folder.strong_ref(); auto parent_folder = m_process_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return KResult(EINVAL); return KResult(EINVAL);
return parent_folder->m_associated_process->fds().open_count(); auto process = parent_folder->associated_process();
if (process.is_null())
return KResult(ESRCH);
return process->fds().open_count();
} }
KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
{ {
@ -206,7 +216,9 @@ KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Func
callback({ ".", { fsid, component_index() }, 0 }); callback({ ".", { fsid, component_index() }, 0 });
callback({ "..", { fsid, parent_folder->component_index() }, 0 }); callback({ "..", { fsid, parent_folder->component_index() }, 0 });
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return KResult(ESRCH);
size_t count = 0; size_t count = 0;
process->fds().enumerate([&](auto& file_description_metadata) { process->fds().enumerate([&](auto& file_description_metadata) {
if (!file_description_metadata.is_valid()) { if (!file_description_metadata.is_valid()) {
@ -225,7 +237,9 @@ RefPtr<ProcFSExposedComponent> ProcFSProcessFileDescriptions::lookup(StringView
auto parent_folder = m_process_folder.strong_ref(); auto parent_folder = m_process_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return nullptr; return nullptr;
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return nullptr;
RefPtr<ProcFSProcessFileDescription> procfd_fd; RefPtr<ProcFSProcessFileDescription> procfd_fd;
// FIXME: Try to exit the loop earlier // FIXME: Try to exit the loop earlier
size_t count = 0; size_t count = 0;
@ -259,7 +273,9 @@ private:
auto parent_folder = m_parent_folder.strong_ref(); auto parent_folder = m_parent_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return false;
JsonObjectSerializer obj { builder }; JsonObjectSerializer obj { builder };
#define __ENUMERATE_PLEDGE_PROMISE(x) \ #define __ENUMERATE_PLEDGE_PROMISE(x) \
if (process->has_promised(Pledge::x)) { \ if (process->has_promised(Pledge::x)) { \
@ -295,8 +311,11 @@ private:
auto parent_folder = m_parent_folder.strong_ref(); auto parent_folder = m_parent_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
auto process = parent_folder->associated_process();
if (process.is_null())
return false;
JsonArraySerializer array { builder }; JsonArraySerializer array { builder };
for (auto& unveiled_path : parent_folder->m_associated_process->unveiled_paths()) { for (auto& unveiled_path : process->unveiled_paths()) {
if (!unveiled_path.was_explicitly_unveiled()) if (!unveiled_path.was_explicitly_unveiled())
continue; continue;
auto obj = array.add_object(); auto obj = array.add_object();
@ -337,7 +356,9 @@ private:
auto parent_folder = m_parent_folder.strong_ref(); auto parent_folder = m_parent_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return false;
if (!process->perf_events()) { if (!process->perf_events()) {
dbgln("ProcFS: No perf events for {}", process->pid()); dbgln("ProcFS: No perf events for {}", process->pid());
return false; return false;
@ -364,7 +385,9 @@ private:
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
JsonArraySerializer array { builder }; JsonArraySerializer array { builder };
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return false;
if (process->fds().open_count() == 0) { if (process->fds().open_count() == 0) {
array.finish(); array.finish();
return true; return true;
@ -414,7 +437,10 @@ private:
auto parent_folder = m_parent_process_directory.strong_ref(); auto parent_folder = m_parent_process_directory.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
builder.append_bytes(parent_folder->m_associated_process->root_directory_relative_to_global_root().absolute_path().to_byte_buffer()); auto process = parent_folder->associated_process();
if (process.is_null())
return false;
builder.append_bytes(process->root_directory_relative_to_global_root().absolute_path().to_byte_buffer());
return true; return true;
} }
WeakPtr<ProcFSProcessDirectory> m_parent_process_directory; WeakPtr<ProcFSProcessDirectory> m_parent_process_directory;
@ -437,7 +463,9 @@ private:
auto parent_folder = m_parent_folder.strong_ref(); auto parent_folder = m_parent_folder.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
auto process = parent_folder->m_associated_process; auto process = parent_folder->associated_process();
if (process.is_null())
return false;
JsonArraySerializer array { builder }; JsonArraySerializer array { builder };
{ {
ScopedSpinLock lock(process->space().get_lock()); ScopedSpinLock lock(process->space().get_lock());
@ -500,7 +528,10 @@ private:
auto parent_folder = m_parent_process_directory.strong_ref(); auto parent_folder = m_parent_process_directory.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
builder.append_bytes(parent_folder->m_associated_process->current_directory().absolute_path().bytes()); auto process = parent_folder->associated_process();
if (process.is_null())
return false;
builder.append_bytes(process->current_directory().absolute_path().bytes());
return true; return true;
} }
@ -519,7 +550,10 @@ public:
auto parent_folder = m_parent_process_directory.strong_ref(); auto parent_folder = m_parent_process_directory.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
if (!parent_folder->m_associated_process->executable()) auto process = parent_folder->associated_process();
if (process.is_null())
return false;
if (!process->executable())
return 0; return 0;
return ProcFSExposedComponent::required_mode(); return ProcFSExposedComponent::required_mode();
} }
@ -535,7 +569,10 @@ private:
auto parent_folder = m_parent_process_directory.strong_ref(); auto parent_folder = m_parent_process_directory.strong_ref();
if (parent_folder.is_null()) if (parent_folder.is_null())
return false; return false;
auto* custody = parent_folder->m_associated_process->executable(); auto process = parent_folder->associated_process();
if (process.is_null())
return false;
auto* custody = process->executable();
if (!custody) if (!custody)
return false; return false;
builder.append(custody->absolute_path().bytes()); builder.append(custody->absolute_path().bytes());
@ -582,6 +619,12 @@ NonnullRefPtr<ProcFSProcessDirectory> ProcFSProcessDirectory::create(const Proce
return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessDirectory(process)).release_nonnull(); return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessDirectory(process)).release_nonnull();
} }
void ProcFSProcessDirectory::prepare_for_deletion()
{
ProcFSExposedDirectory::prepare_for_deletion();
m_associated_process.clear();
}
ProcFSProcessDirectory::ProcFSProcessDirectory(const Process& process) ProcFSProcessDirectory::ProcFSProcessDirectory(const Process& process)
: ProcFSExposedDirectory(String::formatted("{:d}", process.pid().value()), ProcFSComponentRegistry::the().root_folder()) : ProcFSExposedDirectory(String::formatted("{:d}", process.pid().value()), ProcFSComponentRegistry::the().root_folder())
, m_associated_process(process) , m_associated_process(process)