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
|
|
|
{
|
|
|
|
Syscall::SC_pledge_params params;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
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
|
|
|
|
|
|
|
String promises;
|
|
|
|
if (params.promises.characters) {
|
2020-09-17 15:06:26 +02:00
|
|
|
promises = copy_string_from_user(params.promises);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (promises.is_null())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String execpromises;
|
|
|
|
if (params.execpromises.characters) {
|
2020-09-11 21:11:07 -06:00
|
|
|
execpromises = copy_string_from_user(params.execpromises);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (execpromises.is_null())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EFAULT;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto parse_pledge = [&](auto& pledge_spec, u32& mask) {
|
|
|
|
auto parts = pledge_spec.split_view(' ');
|
|
|
|
for (auto& part : parts) {
|
|
|
|
#define __ENUMERATE_PLEDGE_PROMISE(x) \
|
|
|
|
if (part == #x) { \
|
|
|
|
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
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!promises.is_null()) {
|
2021-01-26 17:47:20 +01:00
|
|
|
u32 new_promises = 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!parse_pledge(promises, new_promises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-07-08 20:29:46 +02:00
|
|
|
if (m_has_promises && (new_promises & ~m_promises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-03-10 22:50:00 +01:00
|
|
|
|
2021-03-11 13:13:05 +01:00
|
|
|
m_has_promises = true;
|
|
|
|
m_promises = new_promises;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!execpromises.is_null()) {
|
2021-01-26 17:47:20 +01:00
|
|
|
u32 new_execpromises = 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!parse_pledge(execpromises, new_execpromises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-07-08 20:29:46 +02:00
|
|
|
if (m_has_execpromises && (new_execpromises & ~m_execpromises))
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-03-11 13:13:05 +01:00
|
|
|
m_has_execpromises = true;
|
|
|
|
m_execpromises = new_execpromises;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|