From d8ebcaede8fd968ea81f34a9b8fe164103ebf450 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 6 Jan 2023 09:13:40 +0200 Subject: Kernel: Add helper function to check if a Process is in jail Use this helper function in various places to replace the old code of acquiring the SpinlockProtected> of a Process to do that validation. --- Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp | 9 +++------ .../SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp | 10 ++++------ .../SysFS/Subsystems/Kernel/Variables/StringVariable.cpp | 9 +++------ 3 files changed, 10 insertions(+), 18 deletions(-) (limited to 'Kernel/FileSystem') diff --git a/Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp b/Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp index f05b32d903..3d2c1f45da 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp +++ b/Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp @@ -46,12 +46,9 @@ ErrorOr SysFSPowerStateSwitchNode::truncate(u64 size) ErrorOr SysFSPowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*) { - TRY(Process::current().jail().with([&](auto const& my_jail) -> ErrorOr { - // Note: If we are in a jail, don't let the current process to change the power state. - if (my_jail) - return Error::from_errno(EPERM); - return {}; - })); + // Note: If we are in a jail, don't let the current process to change the power state. + if (Process::current().is_currently_in_jail()) + return Error::from_errno(EPERM); if (Checked::addition_would_overflow(offset, count)) return Error::from_errno(EOVERFLOW); if (offset > 0) diff --git a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp index 7486e8f2c5..67f52e93ab 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp +++ b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp @@ -23,12 +23,10 @@ ErrorOr SysFSSystemBooleanVariable::write_bytes(off_t, size_t count, Use char value = 0; TRY(buffer.read(&value, 1)); - TRY(Process::current().jail().with([&](auto& my_jail) -> ErrorOr { - // Note: If we are in a jail, don't let the current process to change the variable. - if (my_jail) - return Error::from_errno(EPERM); - return {}; - })); + // NOTE: If we are in a jail, don't let the current process to change the variable. + if (Process::current().is_currently_in_jail()) + return Error::from_errno(EPERM); + if (count != 1) return Error::from_errno(EINVAL); if (value == '0') { diff --git a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp index 8064589aab..62bf635dd5 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp +++ b/Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp @@ -25,12 +25,9 @@ ErrorOr SysFSSystemStringVariable::write_bytes(off_t, size_t count, User auto new_value = TRY(KString::try_create_uninitialized(count, value)); TRY(buffer.read(value, count)); auto new_value_without_possible_newlines = TRY(KString::try_create(new_value->view().trim("\n"sv))); - TRY(Process::current().jail().with([&](auto& my_jail) -> ErrorOr { - // Note: If we are in a jail, don't let the current process to change the variable. - if (my_jail) - return Error::from_errno(EPERM); - return {}; - })); + // NOTE: If we are in a jail, don't let the current process to change the variable. + if (Process::current().is_currently_in_jail()) + return Error::from_errno(EPERM); set_value(move(new_value_without_possible_newlines)); return count; } -- cgit v1.2.3