2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-06-28 20:59:35 +02:00
|
|
|
KResultOr<FlatPtr> Process::sys$pledge(Userspace<const Syscall::SC_pledge_params*> user_params)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-09-05 17:51:37 +02:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
|
if (params.promises.length > 1024 || params.execpromises.length > 1024)
|
2021-03-01 13:49:16 +01:00
|
|
|
return E2BIG;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-07-23 09:14:35 -07:00
|
|
|
OwnPtr<KString> promises;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (params.promises.characters) {
|
2021-07-23 09:14:35 -07:00
|
|
|
auto promises_or_error = try_copy_kstring_from_user(params.promises);
|
|
|
|
|
if (promises_or_error.is_error())
|
|
|
|
|
return promises_or_error.error();
|
|
|
|
|
promises = promises_or_error.release_value();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 09:14:35 -07:00
|
|
|
OwnPtr<KString> execpromises;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (params.execpromises.characters) {
|
2021-07-23 09:14:35 -07:00
|
|
|
auto execpromises_or_error = try_copy_kstring_from_user(params.execpromises);
|
|
|
|
|
if (execpromises_or_error.is_error())
|
|
|
|
|
return execpromises_or_error.error();
|
|
|
|
|
execpromises = execpromises_or_error.release_value();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 09:14:35 -07:00
|
|
|
auto parse_pledge = [&](auto pledge_spec, u32& mask) {
|
2020-07-30 23:38:15 +02:00
|
|
|
auto parts = pledge_spec.split_view(' ');
|
|
|
|
|
for (auto& part : parts) {
|
|
|
|
|
#define __ENUMERATE_PLEDGE_PROMISE(x) \
|
2021-07-23 09:31:08 -07:00
|
|
|
if (part == StringView { #x }) { \
|
2020-07-30 23:38:15 +02:00
|
|
|
mask |= (1u << (u32)Pledge::x); \
|
|
|
|
|
continue; \
|
|
|
|
|
}
|
|
|
|
|
ENUMERATE_PLEDGE_PROMISES
|
|
|
|
|
#undef __ENUMERATE_PLEDGE_PROMISE
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-11 13:13:05 +01:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
2021-03-10 22:50:00 +01:00
|
|
|
|
2021-07-23 09:21:43 -07:00
|
|
|
u32 new_promises = 0;
|
2021-07-23 09:14:35 -07:00
|
|
|
if (promises) {
|
|
|
|
|
if (!parse_pledge(promises->view(), new_promises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-08-07 22:30:06 +03:00
|
|
|
if (m_protected_values.has_promises && (new_promises & ~m_protected_values.promises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 09:21:43 -07:00
|
|
|
u32 new_execpromises = 0;
|
2021-07-23 09:14:35 -07:00
|
|
|
if (execpromises) {
|
|
|
|
|
if (!parse_pledge(execpromises->view(), new_execpromises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-08-07 22:30:06 +03:00
|
|
|
if (m_protected_values.has_execpromises && (new_execpromises & ~m_protected_values.execpromises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-07-23 09:21:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only apply promises after all validation has occurred, this ensures
|
|
|
|
|
// we don't introduce logic bugs like applying the promises, and then
|
|
|
|
|
// erroring out when parsing the exec promises later. Such bugs silently
|
|
|
|
|
// leave the caller in an unexpected state.
|
|
|
|
|
|
|
|
|
|
if (promises) {
|
2021-08-07 22:30:06 +03:00
|
|
|
m_protected_values.has_promises = true;
|
|
|
|
|
m_protected_values.promises = new_promises;
|
2021-07-23 09:21:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (execpromises) {
|
2021-08-07 22:30:06 +03:00
|
|
|
m_protected_values.has_execpromises = true;
|
|
|
|
|
m_protected_values.execpromises = new_execpromises;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|