1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:47:35 +00:00

Kernel+Userland: Remove the {get,set}_thread_name syscalls

These syscalls are not necessary on their own, and they give the false
impression that a caller could set or get the thread name of any process
in the system, which is not true.

Therefore, move the functionality of these syscalls to be options in the
prctl syscall, which makes it abundantly clear that these operations
could only occur from a running thread in a process that sees other
threads in that process only.
This commit is contained in:
Liav A 2023-08-19 21:10:25 +03:00 committed by Tim Schumacher
parent 1458849850
commit 1c0aa51684
14 changed files with 54 additions and 63 deletions

View file

@ -91,7 +91,6 @@ enum class NeedsBigProcessLock {
S(get_dir_entries, NeedsBigProcessLock::No) \
S(get_root_session_id, NeedsBigProcessLock::No) \
S(get_stack_bounds, NeedsBigProcessLock::No) \
S(get_thread_name, NeedsBigProcessLock::No) \
S(getcwd, NeedsBigProcessLock::No) \
S(getegid, NeedsBigProcessLock::No) \
S(geteuid, NeedsBigProcessLock::No) \
@ -162,7 +161,6 @@ enum class NeedsBigProcessLock {
S(sendfd, NeedsBigProcessLock::No) \
S(sendmsg, NeedsBigProcessLock::Yes) \
S(set_mmap_name, NeedsBigProcessLock::No) \
S(set_thread_name, NeedsBigProcessLock::No) \
S(setegid, NeedsBigProcessLock::No) \
S(seteuid, NeedsBigProcessLock::No) \
S(setgid, NeedsBigProcessLock::No) \

View file

@ -13,3 +13,5 @@
#define PR_SET_COREDUMP_METADATA_VALUE 5
#define PR_SET_PROCESS_NAME 6
#define PR_GET_PROCESS_NAME 7
#define PR_SET_THREAD_NAME 8
#define PR_GET_THREAD_NAME 9

View file

@ -9,7 +9,7 @@
namespace Kernel {
ErrorOr<FlatPtr> Process::sys$prctl(int option, FlatPtr arg1, FlatPtr arg2)
ErrorOr<FlatPtr> Process::sys$prctl(int option, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
@ -71,6 +71,31 @@ ErrorOr<FlatPtr> Process::sys$prctl(int option, FlatPtr arg1, FlatPtr arg2)
}));
return 0;
}
case PR_SET_THREAD_NAME: {
TRY(require_promise(Pledge::stdio));
int thread_id = static_cast<int>(arg1);
Userspace<char const*> buffer = arg2;
size_t buffer_size = static_cast<size_t>(arg3);
Thread::Name thread_name {};
TRY(try_copy_name_from_user_into_fixed_string_buffer(buffer, thread_name, buffer_size));
auto thread = TRY(get_thread_from_thread_list(thread_id));
thread->set_name(thread_name.representable_view());
return 0;
}
case PR_GET_THREAD_NAME: {
TRY(require_promise(Pledge::thread));
int thread_id = static_cast<int>(arg1);
Userspace<char*> buffer = arg2;
size_t buffer_size = static_cast<size_t>(arg3);
auto thread = TRY(get_thread_from_thread_list(thread_id));
TRY(thread->name().with([&](auto& thread_name) -> ErrorOr<void> {
VERIFY(!thread_name.representable_view().is_null());
return copy_fixed_string_buffer_including_null_char_to_user(buffer, buffer_size, thread_name);
}));
return 0;
}
}
return EINVAL;

View file

@ -46,7 +46,7 @@ ErrorOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<Sys
auto thread = TRY(Thread::create(*this));
// We know this thread is not the main_thread,
// So give it a unique name until the user calls $set_thread_name on it
// So give it a unique name until the user calls $prctl with the PR_SET_THREAD_NAME option on it
auto new_thread_name = TRY(name().with([&](auto& process_name) {
return KString::formatted("{} [{}]", process_name.representable_view(), thread->tid().value());
}));
@ -189,45 +189,6 @@ ErrorOr<FlatPtr> Process::sys$kill_thread(pid_t tid, int signal)
return 0;
}
ErrorOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<char const*> user_name, size_t user_name_length)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::stdio));
Thread::Name thread_name {};
TRY(try_copy_name_from_user_into_fixed_string_buffer<64>(user_name, thread_name, user_name_length));
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return ESRCH;
thread->set_name(thread_name.representable_view());
return 0;
}
ErrorOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::thread));
if (buffer_size == 0)
return EINVAL;
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return ESRCH;
TRY(thread->name().with([&](auto& thread_name) -> ErrorOr<void> {
VERIFY(!thread_name.representable_view().is_null());
auto thread_name_view = thread_name.representable_view();
if (thread_name_view.length() + 1 > buffer_size)
return ENAMETOOLONG;
return copy_to_user(buffer, thread_name_view.characters_without_null_termination(), thread_name_view.length() + 1);
}));
return 0;
}
ErrorOr<FlatPtr> Process::sys$gettid()
{
VERIFY_NO_PROCESS_BIG_LOCK(this);

View file

@ -639,6 +639,19 @@ size_t Process::OpenFileDescriptions::open_count() const
return count;
}
ErrorOr<NonnullRefPtr<Thread>> Process::get_thread_from_thread_list(pid_t tid)
{
if (tid < 0)
return ESRCH;
return m_thread_list.with([tid](auto& list) -> ErrorOr<NonnullRefPtr<Thread>> {
for (auto& thread : list) {
if (thread.tid() == tid)
return thread;
}
return ESRCH;
});
}
ErrorOr<Process::ScopedDescriptionAllocation> Process::OpenFileDescriptions::allocate(int first_candidate_fd)
{
for (size_t i = first_candidate_fd; i < max_open(); ++i) {

View file

@ -434,8 +434,6 @@ public:
[[noreturn]] void sys$exit_thread(Userspace<void*>, Userspace<void*>, size_t);
ErrorOr<FlatPtr> sys$join_thread(pid_t tid, Userspace<void**> exit_value);
ErrorOr<FlatPtr> sys$detach_thread(pid_t tid);
ErrorOr<FlatPtr> sys$set_thread_name(pid_t tid, Userspace<char const*> buffer, size_t buffer_size);
ErrorOr<FlatPtr> sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size);
ErrorOr<FlatPtr> sys$kill_thread(pid_t tid, int signal);
ErrorOr<FlatPtr> sys$rename(Userspace<Syscall::SC_rename_params const*>);
ErrorOr<FlatPtr> sys$mknod(Userspace<Syscall::SC_mknod_params const*>);
@ -459,7 +457,7 @@ public:
ErrorOr<FlatPtr> sys$sysconf(int name);
ErrorOr<FlatPtr> sys$disown(ProcessID);
ErrorOr<FlatPtr> sys$allocate_tls(Userspace<char const*> initial_data, size_t);
ErrorOr<FlatPtr> sys$prctl(int option, FlatPtr arg1, FlatPtr arg2);
ErrorOr<FlatPtr> sys$prctl(int option, FlatPtr arg1, FlatPtr arg2, FlatPtr arg3);
ErrorOr<FlatPtr> sys$anon_create(size_t, int options);
ErrorOr<FlatPtr> sys$statvfs(Userspace<Syscall::SC_statvfs_params const*> user_params);
ErrorOr<FlatPtr> sys$fstatvfs(int fd, statvfs* buf);
@ -901,6 +899,7 @@ private:
SpinlockProtected<Thread::ListInProcess, LockRank::None> const& thread_list() const { return m_thread_list; }
ErrorOr<NonnullRefPtr<Thread>> get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode);
ErrorOr<NonnullRefPtr<Thread>> get_thread_from_thread_list(pid_t tid);
SpinlockProtected<Thread::ListInProcess, LockRank::None> m_thread_list {};