From d16d805d96d08685e7259569b4ea8875a8124488 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 3 Mar 2023 15:47:39 +0200 Subject: [PATCH] Kernel: Merge {get,set}_process_name syscalls to the prctl syscall It makes much more sense to have these actions being performed via the prctl syscall, as they both require 2 plain arguments to be passed to the syscall layer, and in contrast to most syscalls, we don't get in these removed syscalls an automatic representation of Userspace, but two FlatPtr(s) to perform casting on them in the prctl syscall which is suited to what has been done in the removed syscalls. Also, it makes sense to have these actions in the prctl syscall, because they are strongly related to the process control concept of the prctl syscall. --- Kernel/API/Syscall.h | 2 -- Kernel/API/prctl_numbers.h | 2 ++ Kernel/Process.h | 2 -- Kernel/Syscalls/prctl.cpp | 32 ++++++++++++++++++- Kernel/Syscalls/process.cpp | 29 ----------------- .../DevTools/UserspaceEmulator/Emulator.h | 1 - .../UserspaceEmulator/Emulator_syscalls.cpp | 26 --------------- Userland/Libraries/LibC/unistd.cpp | 5 +-- Userland/Utilities/strace.cpp | 8 ----- 9 files changed, 36 insertions(+), 71 deletions(-) diff --git a/Kernel/API/Syscall.h b/Kernel/API/Syscall.h index 2ca0751713f..8e19dbd2be1 100644 --- a/Kernel/API/Syscall.h +++ b/Kernel/API/Syscall.h @@ -85,7 +85,6 @@ enum class NeedsBigProcessLock { S(ftruncate, NeedsBigProcessLock::No) \ S(futex, NeedsBigProcessLock::Yes) \ S(get_dir_entries, NeedsBigProcessLock::Yes) \ - S(get_process_name, NeedsBigProcessLock::No) \ S(get_root_session_id, NeedsBigProcessLock::No) \ S(get_stack_bounds, NeedsBigProcessLock::No) \ S(get_thread_name, NeedsBigProcessLock::No) \ @@ -159,7 +158,6 @@ enum class NeedsBigProcessLock { S(sendfd, NeedsBigProcessLock::No) \ S(sendmsg, NeedsBigProcessLock::Yes) \ S(set_mmap_name, NeedsBigProcessLock::Yes) \ - S(set_process_name, NeedsBigProcessLock::No) \ S(set_thread_name, NeedsBigProcessLock::No) \ S(setegid, NeedsBigProcessLock::No) \ S(seteuid, NeedsBigProcessLock::No) \ diff --git a/Kernel/API/prctl_numbers.h b/Kernel/API/prctl_numbers.h index 4c5d1603e3e..20976f2abd0 100644 --- a/Kernel/API/prctl_numbers.h +++ b/Kernel/API/prctl_numbers.h @@ -11,3 +11,5 @@ #define PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS 3 #define PR_GET_NO_NEW_SYSCALL_REGION_ANNOTATIONS 4 #define PR_SET_COREDUMP_METADATA_VALUE 5 +#define PR_SET_PROCESS_NAME 6 +#define PR_GET_PROCESS_NAME 7 diff --git a/Kernel/Process.h b/Kernel/Process.h index a5e1e113415..12027a4c978 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -293,8 +293,6 @@ public: ErrorOr sys$yield(); ErrorOr sys$sync(); ErrorOr sys$beep(int tone); - ErrorOr sys$get_process_name(Userspace buffer, size_t buffer_size); - ErrorOr sys$set_process_name(Userspace user_name, size_t user_name_length); ErrorOr sys$create_inode_watcher(u32 flags); ErrorOr sys$inode_watcher_add_watch(Userspace user_params); ErrorOr sys$inode_watcher_remove_watch(int fd, int wd); diff --git a/Kernel/Syscalls/prctl.cpp b/Kernel/Syscalls/prctl.cpp index c76cec85806..b152e51d119 100644 --- a/Kernel/Syscalls/prctl.cpp +++ b/Kernel/Syscalls/prctl.cpp @@ -9,7 +9,7 @@ namespace Kernel { -ErrorOr Process::sys$prctl(int option, FlatPtr arg1, [[maybe_unused]] FlatPtr arg2) +ErrorOr Process::sys$prctl(int option, FlatPtr arg1, FlatPtr arg2) { VERIFY_NO_PROCESS_BIG_LOCK(this); return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr { @@ -49,6 +49,36 @@ ErrorOr Process::sys$prctl(int option, FlatPtr arg1, [[maybe_unused]] F TRY(set_coredump_property(move(key), move(value))); return 0; } + case PR_SET_PROCESS_NAME: { + TRY(require_promise(Pledge::proc)); + Userspace buffer = arg1; + int user_buffer_size = static_cast(arg2); + if (user_buffer_size < 0) + return EINVAL; + if (user_buffer_size > 256) + return ENAMETOOLONG; + size_t buffer_size = static_cast(user_buffer_size); + auto name = TRY(try_copy_kstring_from_user(buffer, buffer_size)); + // NOTE: Reject empty and whitespace-only names, as they only confuse users. + if (name->view().is_whitespace()) + return EINVAL; + set_name(move(name)); + return 0; + } + case PR_GET_PROCESS_NAME: { + TRY(require_promise(Pledge::stdio)); + Userspace buffer = arg1; + int user_buffer_size = arg2; + if (user_buffer_size < 0) + return EINVAL; + size_t buffer_size = static_cast(arg2); + TRY(m_name.with([&buffer, buffer_size](auto& name) -> ErrorOr { + if (name->length() + 1 > buffer_size) + return ENAMETOOLONG; + return copy_to_user(buffer, name->characters(), name->length() + 1); + })); + return 0; + } } return EINVAL; diff --git a/Kernel/Syscalls/process.cpp b/Kernel/Syscalls/process.cpp index af4baea8201..1f165eb2ef3 100644 --- a/Kernel/Syscalls/process.cpp +++ b/Kernel/Syscalls/process.cpp @@ -23,33 +23,4 @@ ErrorOr Process::sys$getppid() return ppid().value(); } -ErrorOr Process::sys$get_process_name(Userspace buffer, size_t buffer_size) -{ - VERIFY_NO_PROCESS_BIG_LOCK(this); - TRY(require_promise(Pledge::stdio)); - - TRY(m_name.with([&buffer, buffer_size](auto& name) -> ErrorOr { - if (name->length() + 1 > buffer_size) - return ENAMETOOLONG; - - return copy_to_user(buffer, name->characters(), name->length() + 1); - })); - - return 0; -} - -ErrorOr Process::sys$set_process_name(Userspace user_name, size_t user_name_length) -{ - VERIFY_NO_PROCESS_BIG_LOCK(this); - TRY(require_promise(Pledge::proc)); - if (user_name_length > 256) - return ENAMETOOLONG; - auto name = TRY(try_copy_kstring_from_user(user_name, user_name_length)); - // Empty and whitespace-only names only exist to confuse users. - if (name->view().is_whitespace()) - return EINVAL; - set_name(move(name)); - return 0; -} - } diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.h b/Userland/DevTools/UserspaceEmulator/Emulator.h index 307015578fd..404468cff8d 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.h +++ b/Userland/DevTools/UserspaceEmulator/Emulator.h @@ -171,7 +171,6 @@ private: int virt$ftruncate(int fd, FlatPtr length_addr); int virt$futex(FlatPtr); int virt$get_dir_entries(int fd, FlatPtr buffer, ssize_t); - int virt$get_process_name(FlatPtr buffer, int size); int virt$get_stack_bounds(FlatPtr, FlatPtr); int virt$getcwd(FlatPtr buffer, size_t buffer_size); gid_t virt$getegid(); diff --git a/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp b/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp index f7ba559d3ab..fba7caef925 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp +++ b/Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp @@ -100,8 +100,6 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3) return virt$futex(arg1); case SC_get_dir_entries: return virt$get_dir_entries(arg1, arg2, arg3); - case SC_get_process_name: - return virt$get_process_name(arg1, arg2); case SC_get_stack_bounds: return virt$get_stack_bounds(arg1, arg2); case SC_getcwd: @@ -210,8 +208,6 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3) return virt$sendmsg(arg1, arg2, arg3); case SC_set_mmap_name: return virt$set_mmap_name(arg1); - case SC_set_process_name: - return virt$set_process_name(arg1, arg2); case SC_set_thread_name: return virt$set_thread_name(arg1, arg2, arg3); case SC_setgid: @@ -571,28 +567,6 @@ int Emulator::virt$set_mmap_name(FlatPtr params_addr) return 0; } -int Emulator::virt$get_process_name(FlatPtr buffer, int size) -{ - if (size < 0) - return -EINVAL; - auto host_buffer_result = ByteBuffer::create_zeroed((size_t)size); - if (host_buffer_result.is_error()) - return -ENOMEM; - auto& host_buffer = host_buffer_result.value(); - int rc = syscall(SC_get_process_name, host_buffer.data(), host_buffer.size()); - mmu().copy_to_vm(buffer, host_buffer.data(), host_buffer.size()); - return rc; -} - -int Emulator::virt$set_process_name(FlatPtr user_buffer, int size) -{ - if (size < 0) - return -EINVAL; - auto host_buffer = mmu().copy_buffer_from_vm(user_buffer, size); - auto name = DeprecatedString::formatted("(UE) {}", StringView { host_buffer.data(), host_buffer.size() }); - return syscall(SC_set_process_name, name.characters(), name.length()); -} - int Emulator::virt$lseek(int fd, FlatPtr offset_addr, int whence) { off_t offset; diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index c1b7531766f..ef5f299853b 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -969,13 +970,13 @@ void dump_backtrace() int get_process_name(char* buffer, int buffer_size) { - int rc = syscall(SC_get_process_name, buffer, buffer_size); + int rc = syscall(SC_prctl, PR_GET_PROCESS_NAME, buffer, buffer_size); __RETURN_WITH_ERRNO(rc, rc, -1); } int set_process_name(char const* name, size_t name_length) { - int rc = syscall(SC_set_process_name, name, name_length); + int rc = syscall(SC_prctl, PR_SET_PROCESS_NAME, name, name_length); __RETURN_WITH_ERRNO(rc, rc, -1); } diff --git a/Userland/Utilities/strace.cpp b/Userland/Utilities/strace.cpp index b026cac11b0..a51e9035992 100644 --- a/Userland/Utilities/strace.cpp +++ b/Userland/Utilities/strace.cpp @@ -690,11 +690,6 @@ static void format_dbgputstr(FormattedSyscallBuilder& builder, char* characters, builder.add_argument(StringArgument { { characters, size }, "\0\n"sv }); } -static void format_get_process_name(FormattedSyscallBuilder& builder, char* buffer, size_t buffer_size) -{ - builder.add_argument(StringArgument { { buffer, buffer_size }, "\0"sv }); -} - static ErrorOr format_syscall(FormattedSyscallBuilder& builder, Syscall::Function syscall_function, syscall_arg_t arg1, syscall_arg_t arg2, syscall_arg_t arg3, syscall_arg_t res) { enum ResultType { @@ -730,9 +725,6 @@ static ErrorOr format_syscall(FormattedSyscallBuilder& builder, Syscall::F format_chdir(builder, (char const*)arg1, (size_t)arg2); result_type = Int; break; - case SC_get_process_name: - format_get_process_name(builder, (char*)arg1, (size_t)arg2); - break; case SC_getrandom: format_getrandom(builder, (void*)arg1, (size_t)arg2, (unsigned)arg3); break;