2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$seteuid(uid_t new_euid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_euid != uid() && new_euid != suid() && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (euid() != new_euid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
2021-03-11 15:13:05 +03:00
|
|
|
|
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
|
|
|
|
m_euid = new_euid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setegid(gid_t new_egid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_egid != gid() && new_egid != sgid() && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (egid() != new_egid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_egid = new_egid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setuid(uid_t new_uid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_uid != uid() && new_uid != euid() && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (euid() != new_uid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_uid = new_uid;
|
|
|
|
m_euid = new_uid;
|
|
|
|
m_suid = new_uid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setgid(gid_t new_gid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_gid != gid() && new_gid != egid() && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (egid() != new_gid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_gid = new_gid;
|
|
|
|
m_egid = new_gid;
|
|
|
|
m_sgid = new_gid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setreuid(uid_t new_ruid, uid_t new_euid)
|
2021-04-09 14:13:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-04-09 14:13:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
|
|
|
if (new_ruid == (uid_t)-1)
|
|
|
|
new_ruid = uid();
|
|
|
|
if (new_euid == (uid_t)-1)
|
|
|
|
new_euid = euid();
|
|
|
|
|
|
|
|
auto ok = [this](uid_t id) { return id == uid() || id == euid() || id == suid(); };
|
|
|
|
if (!ok(new_ruid) || !ok(new_euid))
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
if (new_ruid < (uid_t)-1 || new_euid < (uid_t)-1)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (euid() != new_euid)
|
|
|
|
set_dumpable(false);
|
|
|
|
|
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_uid = new_ruid;
|
|
|
|
m_euid = new_euid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setresuid(uid_t new_ruid, uid_t new_euid, uid_t new_suid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_ruid == (uid_t)-1)
|
|
|
|
new_ruid = uid();
|
|
|
|
if (new_euid == (uid_t)-1)
|
|
|
|
new_euid = euid();
|
|
|
|
if (new_suid == (uid_t)-1)
|
|
|
|
new_suid = suid();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
auto ok = [this](uid_t id) { return id == uid() || id == euid() || id == suid(); };
|
|
|
|
if ((!ok(new_ruid) || !ok(new_euid) || !ok(new_suid)) && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (euid() != new_euid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_uid = new_ruid;
|
|
|
|
m_euid = new_euid;
|
|
|
|
m_suid = new_suid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setresgid(gid_t new_rgid, gid_t new_egid, gid_t new_sgid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (new_rgid == (gid_t)-1)
|
|
|
|
new_rgid = gid();
|
|
|
|
if (new_egid == (gid_t)-1)
|
|
|
|
new_egid = egid();
|
|
|
|
if (new_sgid == (gid_t)-1)
|
|
|
|
new_sgid = sgid();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
auto ok = [this](gid_t id) { return id == gid() || id == egid() || id == sgid(); };
|
|
|
|
if ((!ok(new_rgid) || !ok(new_egid) || !ok(new_sgid)) && !is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
if (egid() != new_egid)
|
2020-12-25 20:27:42 +03:00
|
|
|
set_dumpable(false);
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_gid = new_rgid;
|
|
|
|
m_egid = new_egid;
|
|
|
|
m_sgid = new_sgid;
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$setgroups(size_t count, Userspace<const gid_t*> user_gids)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-31 00:38:15 +03:00
|
|
|
REQUIRE_PROMISE(id);
|
|
|
|
if (!is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
if (!count) {
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_extra_gids.clear();
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:59:46 +03:00
|
|
|
Vector<gid_t> new_extra_gids;
|
2021-04-30 13:10:55 +03:00
|
|
|
if (!new_extra_gids.try_resize(count))
|
|
|
|
return ENOMEM;
|
2021-03-10 21:59:46 +03:00
|
|
|
if (!copy_n_from_user(new_extra_gids.data(), user_gids, count))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
HashTable<gid_t> unique_extra_gids;
|
2021-03-10 21:59:46 +03:00
|
|
|
for (auto& extra_gid : new_extra_gids) {
|
|
|
|
if (extra_gid != gid())
|
|
|
|
unique_extra_gids.set(extra_gid);
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:13:05 +03:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
2021-04-30 13:10:55 +03:00
|
|
|
if (!m_extra_gids.try_resize(unique_extra_gids.size()))
|
|
|
|
return ENOMEM;
|
2020-07-31 00:38:15 +03:00
|
|
|
size_t i = 0;
|
2021-03-10 21:59:46 +03:00
|
|
|
for (auto& extra_gid : unique_extra_gids) {
|
|
|
|
if (extra_gid == gid())
|
2020-07-31 00:38:15 +03:00
|
|
|
continue;
|
2021-03-11 15:13:05 +03:00
|
|
|
m_extra_gids[i++] = extra_gid;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|