process-util: introduce trivial proc_set_nnp() helper

This commit is contained in:
Lennart Poettering
2026-07-13 15:39:16 +02:00
parent 0e077d3eea
commit 3ac3632e28
6 changed files with 24 additions and 13 deletions
+4
View File
@@ -2292,3 +2292,7 @@ int prctl_safe(int op, unsigned long arg2, unsigned long arg3, unsigned long arg
int proc_set_comm(const char *comm) {
return prctl_safe(PR_SET_NAME, (unsigned long) comm, 0, 0, 0);
}
int proc_set_nnp(void) {
return prctl_safe(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
}
+1
View File
@@ -259,3 +259,4 @@ int read_errno(int errno_fd);
int prctl_safe(int op, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
int proc_set_comm(const char *comm);
int proc_set_nnp(void);
+5 -3
View File
@@ -6479,11 +6479,13 @@ int exec_invoke(
}
}
if (context_has_no_new_privileges(context))
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
if (context_has_no_new_privileges(context)) {
r = proc_set_nnp();
if (r < 0) {
*exit_status = EXIT_NO_NEW_PRIVILEGES;
return log_error_errno(errno, "Failed to disable new privileges: %m");
return log_error_errno(r, "Failed to disable new privileges: %m");
}
}
#if HAVE_SECCOMP
r = apply_address_families(context, params);
+3 -2
View File
@@ -2739,9 +2739,10 @@ static int initialize_runtime(
}
if (arg_no_new_privs) {
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
r = proc_set_nnp();
if (r < 0) {
*ret_error_message = "Failed to disable new privileges";
return log_struct_errno(LOG_EMERG, errno,
return log_struct_errno(LOG_EMERG, r,
LOG_MESSAGE("Failed to disable new privileges: %m"),
LOG_MESSAGE_ID(SD_MESSAGE_CORE_DISABLE_PRIVILEGES_STR));
}
+6 -5
View File
@@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sched.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "sd-event.h"
@@ -81,8 +80,9 @@ int import_fork_tar_x(int tree_fd, int userns_fd, PidRef *ret_pid) {
if (r < 0)
log_debug_errno(r, "Failed to drop capabilities, ignoring: %m");
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
log_warning_errno(errno, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
r = proc_set_nnp();
if (r < 0)
log_warning_errno(r, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
if (tar_x(pipefd[0], tree_fd, flags) < 0)
_exit(EXIT_FAILURE);
@@ -141,8 +141,9 @@ int import_fork_tar_c(int tree_fd, int userns_fd, PidRef *ret_pid) {
if (r < 0)
log_debug_errno(r, "Failed to drop capabilities, ignoring: %m");
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
log_warning_errno(errno, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
r = proc_set_nnp();
if (r < 0)
log_warning_errno(r, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
if (tar_c(tree_fd, pipefd[1], /* filename= */ NULL, flags) < 0)
_exit(EXIT_FAILURE);
+5 -3
View File
@@ -3593,9 +3593,11 @@ static int inner_child(
if (r < 0)
return log_error_errno(r, "Dropping capabilities failed: %m");
if (arg_no_new_privileges)
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
return log_error_errno(errno, "Failed to disable new privileges: %m");
if (arg_no_new_privileges) {
r = proc_set_nnp();
if (r < 0)
return log_error_errno(r, "Failed to disable new privileges: %m");
}
/* LXC sets container=lxc, so follow the scheme here */
envp[n_env++] = strjoina("container=", arg_container_service_name);