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
|
|
|
*/
|
|
|
|
|
2022-08-20 23:22:58 +03:00
|
|
|
#include <Kernel/API/POSIX/sys/limits.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$seteuid(UserID new_euid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-12-19 16:10:45 +03:00
|
|
|
if (new_euid == (uid_t)-1)
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_euid != credentials->uid() && new_euid != credentials->suid() && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
credentials->gid(),
|
|
|
|
new_euid,
|
|
|
|
credentials->egid(),
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2021-03-11 15:13:05 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->euid() != new_euid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setegid(GroupID new_egid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-12-19 16:10:45 +03:00
|
|
|
if (new_egid == (uid_t)-1)
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_egid != credentials->gid() && new_egid != credentials->sgid() && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
2020-12-25 20:27:42 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
credentials->gid(),
|
|
|
|
credentials->euid(),
|
|
|
|
new_egid,
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->egid() != new_egid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setuid(UserID new_uid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-12-19 16:10:45 +03:00
|
|
|
if (new_uid == (uid_t)-1)
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_uid != credentials->uid() && new_uid != credentials->euid() && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
2020-12-25 20:27:42 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
new_uid,
|
|
|
|
credentials->gid(),
|
|
|
|
new_uid,
|
|
|
|
credentials->egid(),
|
|
|
|
new_uid,
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->euid() != new_uid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setgid(GroupID new_gid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-12-19 16:10:45 +03:00
|
|
|
if (new_gid == (uid_t)-1)
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_gid != credentials->gid() && new_gid != credentials->egid() && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
2020-12-25 20:27:42 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
new_gid,
|
|
|
|
credentials->euid(),
|
|
|
|
new_gid,
|
|
|
|
credentials->suid(),
|
|
|
|
new_gid,
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->egid() != new_gid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setreuid(UserID new_ruid, UserID new_euid)
|
2021-04-09 14:13:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2021-04-09 14:13:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_ruid == (uid_t)-1)
|
|
|
|
new_ruid = credentials->uid();
|
|
|
|
if (new_euid == (uid_t)-1)
|
|
|
|
new_euid = credentials->euid();
|
2021-04-09 14:13:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto ok = [&credentials](UserID id) { return id == credentials->uid() || id == credentials->euid() || id == credentials->suid(); };
|
|
|
|
if (!ok(new_ruid) || !ok(new_euid))
|
|
|
|
return EPERM;
|
2021-04-09 14:13:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_ruid < (uid_t)-1 || new_euid < (uid_t)-1)
|
|
|
|
return EINVAL;
|
2021-04-09 14:13:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
new_ruid,
|
|
|
|
credentials->gid(),
|
|
|
|
new_euid,
|
|
|
|
credentials->egid(),
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->euid() != new_euid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2021-04-09 14:13:15 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setresuid(UserID new_ruid, UserID new_euid, UserID new_suid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_ruid == (uid_t)-1)
|
|
|
|
new_ruid = credentials->uid();
|
|
|
|
if (new_euid == (uid_t)-1)
|
|
|
|
new_euid = credentials->euid();
|
|
|
|
if (new_suid == (uid_t)-1)
|
|
|
|
new_suid = credentials->suid();
|
|
|
|
|
|
|
|
auto ok = [&credentials](UserID id) { return id == credentials->uid() || id == credentials->euid() || id == credentials->suid(); };
|
|
|
|
if ((!ok(new_ruid) || !ok(new_euid) || !ok(new_suid)) && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
new_ruid,
|
|
|
|
credentials->gid(),
|
|
|
|
new_euid,
|
|
|
|
credentials->egid(),
|
|
|
|
new_suid,
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-21 13:18:26 +03:00
|
|
|
|
|
|
|
if (credentials->euid() != new_euid)
|
|
|
|
protected_data.dumpable = false;
|
|
|
|
|
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:47:38 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setregid(GroupID new_rgid, GroupID new_egid)
|
|
|
|
{
|
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
|
|
|
TRY(require_promise(Pledge::id));
|
|
|
|
|
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
|
|
|
if (new_rgid == (gid_t)-1)
|
|
|
|
new_rgid = credentials->gid();
|
|
|
|
if (new_egid == (gid_t)-1)
|
|
|
|
new_egid = credentials->egid();
|
|
|
|
|
|
|
|
auto ok = [&credentials](GroupID id) { return id == credentials->gid() || id == credentials->egid() || id == credentials->sgid(); };
|
|
|
|
if (!ok(new_rgid) || !ok(new_egid))
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
new_rgid,
|
|
|
|
credentials->euid(),
|
|
|
|
new_egid,
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-10-01 15:47:38 +03:00
|
|
|
|
|
|
|
if (credentials->egid() != new_egid)
|
|
|
|
protected_data.dumpable = false;
|
|
|
|
|
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setresgid(GroupID new_rgid, GroupID new_egid, GroupID new_sgid)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (new_rgid == (gid_t)-1)
|
|
|
|
new_rgid = credentials->gid();
|
|
|
|
if (new_egid == (gid_t)-1)
|
|
|
|
new_egid = credentials->egid();
|
|
|
|
if (new_sgid == (gid_t)-1)
|
|
|
|
new_sgid = credentials->sgid();
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto ok = [&credentials](GroupID id) { return id == credentials->gid() || id == credentials->egid() || id == credentials->sgid(); };
|
|
|
|
if ((!ok(new_rgid) || !ok(new_egid) || !ok(new_sgid)) && !credentials->is_superuser())
|
|
|
|
return EPERM;
|
2020-12-25 20:27:42 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
auto new_credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
new_rgid,
|
|
|
|
credentials->euid(),
|
|
|
|
new_egid,
|
|
|
|
credentials->suid(),
|
|
|
|
new_sgid,
|
2022-12-19 23:21:51 +03:00
|
|
|
credentials->extra_gids(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
if (credentials->egid() != new_egid)
|
|
|
|
protected_data.dumpable = false;
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
protected_data.credentials = move(new_credentials);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:25:54 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$setgroups(size_t count, Userspace<GroupID const*> user_gids)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-20 19:36:47 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::id));
|
2022-08-20 19:25:54 +03:00
|
|
|
|
2022-08-20 23:22:58 +03:00
|
|
|
if (count > NGROUPS_MAX)
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-08-21 13:18:26 +03:00
|
|
|
return with_mutable_protected_data([&](auto& protected_data) -> ErrorOr<FlatPtr> {
|
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
|
|
|
if (!credentials->is_superuser())
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
if (!count) {
|
|
|
|
protected_data.credentials = TRY(Credentials::create(
|
|
|
|
credentials->uid(),
|
|
|
|
credentials->gid(),
|
|
|
|
credentials->euid(),
|
|
|
|
credentials->egid(),
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
{},
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2022-08-21 13:18:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<GroupID> new_extra_gids;
|
|
|
|
TRY(new_extra_gids.try_resize(count));
|
|
|
|
TRY(copy_n_from_user(new_extra_gids.data(), user_gids, count));
|
|
|
|
|
|
|
|
HashTable<GroupID> unique_extra_gids;
|
|
|
|
for (auto& extra_gid : new_extra_gids) {
|
|
|
|
if (extra_gid != credentials->gid())
|
|
|
|
TRY(unique_extra_gids.try_set(extra_gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
new_extra_gids.clear_with_capacity();
|
|
|
|
for (auto extra_gid : unique_extra_gids) {
|
|
|
|
TRY(new_extra_gids.try_append(extra_gid));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected_data.credentials = TRY(Credentials::create(
|
2022-08-20 19:25:54 +03:00
|
|
|
credentials->uid(),
|
|
|
|
credentials->gid(),
|
|
|
|
credentials->euid(),
|
|
|
|
credentials->egid(),
|
|
|
|
credentials->suid(),
|
|
|
|
credentials->sgid(),
|
2022-12-19 23:21:51 +03:00
|
|
|
new_extra_gids.span(),
|
|
|
|
credentials->sid(),
|
|
|
|
credentials->pgid()));
|
2020-07-31 00:38:15 +03:00
|
|
|
return 0;
|
2022-08-21 13:18:26 +03:00
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|