1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +00:00

Kernel+LibC: Introduce a "dumpable" flag for processes

This new flag controls two things:
- Whether the kernel will generate core dumps for the process
- Whether the EUID:EGID should own the process's files in /proc

Processes are automatically made non-dumpable when their EUID or EGID is
changed, either via syscalls that specifically modify those ID's, or via
sys$execve(), when a set-uid or set-gid program is executed.

A process can change its own dumpable flag at any time by calling the
new sys$prctl(PR_SET_DUMPABLE) syscall.

Fixes #4504.
This commit is contained in:
Andreas Kling 2020-12-25 18:27:42 +01:00
parent 3c9bd911b8
commit 82f86e35d6
13 changed files with 199 additions and 7 deletions

View file

@ -363,19 +363,25 @@ int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Ve
auto old_suid = m_suid;
auto old_egid = m_egid;
auto old_sgid = m_sgid;
auto was_dumpable = is_dumpable();
ArmedScopeGuard cred_restore_guard = [&] {
m_euid = old_euid;
m_suid = old_suid;
m_egid = old_egid;
m_sgid = old_sgid;
set_dumpable(was_dumpable);
};
if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
if (main_program_metadata.is_setuid())
if (main_program_metadata.is_setuid()) {
set_dumpable(false);
m_euid = m_suid = main_program_metadata.uid;
if (main_program_metadata.is_setgid())
}
if (main_program_metadata.is_setgid()) {
set_dumpable(false);
m_egid = m_sgid = main_program_metadata.gid;
}
}
auto load_result_or_error = load(main_program_description, interpreter_description);

46
Kernel/Syscalls/prctl.cpp Normal file
View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Kernel/Process.h>
#include <LibC/sys/prctl_numbers.h>
namespace Kernel {
int Process::sys$prctl(int option, FlatPtr arg1, [[maybe_unused]] FlatPtr arg2)
{
switch (option) {
case PR_GET_DUMPABLE:
return is_dumpable();
case PR_SET_DUMPABLE:
set_dumpable(arg1);
return 0;
default:
return -EINVAL;
}
return 0;
}
}

View file

@ -35,6 +35,8 @@ int Process::sys$seteuid(uid_t euid)
if (euid != m_uid && euid != m_suid && !is_superuser())
return -EPERM;
if (m_euid != euid)
set_dumpable(false);
m_euid = euid;
return 0;
}
@ -46,6 +48,9 @@ int Process::sys$setegid(gid_t egid)
if (egid != m_gid && egid != m_sgid && !is_superuser())
return -EPERM;
if (m_egid != egid)
set_dumpable(false);
m_egid = egid;
return 0;
}
@ -57,6 +62,9 @@ int Process::sys$setuid(uid_t uid)
if (uid != m_uid && uid != m_euid && !is_superuser())
return -EPERM;
if (m_euid != uid)
set_dumpable(false);
m_uid = uid;
m_euid = uid;
m_suid = uid;
@ -70,6 +78,9 @@ int Process::sys$setgid(gid_t gid)
if (gid != m_gid && gid != m_egid && !is_superuser())
return -EPERM;
if (m_egid != gid)
set_dumpable(false);
m_gid = gid;
m_egid = gid;
m_sgid = gid;
@ -91,6 +102,9 @@ int Process::sys$setresuid(uid_t ruid, uid_t euid, uid_t suid)
if ((!ok(ruid) || !ok(euid) || !ok(suid)) && !is_superuser())
return -EPERM;
if (m_euid != euid)
set_dumpable(false);
m_uid = ruid;
m_euid = euid;
m_suid = suid;
@ -112,6 +126,9 @@ int Process::sys$setresgid(gid_t rgid, gid_t egid, gid_t sgid)
if ((!ok(rgid) || !ok(egid) || !ok(sgid)) && !is_superuser())
return -EPERM;
if (m_egid != egid)
set_dumpable(false);
m_gid = rgid;
m_egid = egid;
m_sgid = sgid;