summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2023-01-06 09:13:40 +0200
committerLinus Groh <mail@linusgroh.de>2023-01-06 17:29:47 +0100
commitd8ebcaede8fd968ea81f34a9b8fe164103ebf450 (patch)
tree4426206cbbe1d94d335c36377ce8e2a2dad00dca /Kernel
parent6b3688147fd74b301944b368a896767e49915faa (diff)
downloadserenity-d8ebcaede8fd968ea81f34a9b8fe164103ebf450.zip
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<RefPtr<Jail>> of a Process to do that validation.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.cpp9
-rw-r--r--Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.cpp10
-rw-r--r--Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/StringVariable.cpp9
-rw-r--r--Kernel/Process.h5
4 files changed, 15 insertions, 18 deletions
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<void> SysFSPowerStateSwitchNode::truncate(u64 size)
ErrorOr<size_t> SysFSPowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*)
{
- TRY(Process::current().jail().with([&](auto const& my_jail) -> ErrorOr<void> {
- // 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<off_t>::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<size_t> 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<void> {
- // 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<size_t> 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<void> {
- // 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;
}
diff --git a/Kernel/Process.h b/Kernel/Process.h
index 90b3e3ed76..67f257636d 100644
--- a/Kernel/Process.h
+++ b/Kernel/Process.h
@@ -240,6 +240,11 @@ public:
SpinlockProtected<RefPtr<Jail>, LockRank::Process>& jail() { return m_attached_jail; }
+ bool is_currently_in_jail() const
+ {
+ return m_attached_jail.with([&](auto& jail) -> bool { return !jail.is_null(); });
+ }
+
NonnullRefPtr<Credentials> credentials() const;
bool is_dumpable() const