Merge pull request #30578 from bluca/polkit-varlink
varlink: add glue to allow authenticating varlink connections via polkit
This commit is contained in:
@@ -32,6 +32,10 @@ struct sockaddr_vm {
|
||||
#define SO_PEERGROUPS 59
|
||||
#endif
|
||||
|
||||
#ifndef SO_PEERPIDFD
|
||||
#define SO_PEERPIDFD 77
|
||||
#endif
|
||||
|
||||
#ifndef SO_BINDTOIFINDEX
|
||||
#define SO_BINDTOIFINDEX 62
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
#ifndef P_PIDFD
|
||||
#define P_PIDFD 3
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "missing_wait.h"
|
||||
#include "parse-util.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
@@ -302,6 +303,44 @@ bool pidref_is_self(const PidRef *pidref) {
|
||||
return pidref->pid == getpid_cached();
|
||||
}
|
||||
|
||||
int pidref_wait(const PidRef *pidref, siginfo_t *ret, int options) {
|
||||
int r;
|
||||
|
||||
if (!pidref_is_set(pidref))
|
||||
return -ESRCH;
|
||||
|
||||
if (pidref->pid == 1 || pidref->pid == getpid_cached())
|
||||
return -ECHILD;
|
||||
|
||||
siginfo_t si = {};
|
||||
|
||||
if (pidref->fd >= 0) {
|
||||
r = RET_NERRNO(waitid(P_PIDFD, pidref->fd, &si, options));
|
||||
if (r >= 0) {
|
||||
if (ret)
|
||||
*ret = si;
|
||||
return r;
|
||||
}
|
||||
if (r != -EINVAL) /* P_PIDFD was added in kernel 5.4 only */
|
||||
return r;
|
||||
}
|
||||
|
||||
r = RET_NERRNO(waitid(P_PID, pidref->pid, &si, options));
|
||||
if (r >= 0 && ret)
|
||||
*ret = si;
|
||||
return r;
|
||||
}
|
||||
|
||||
int pidref_wait_for_terminate(const PidRef *pidref, siginfo_t *ret) {
|
||||
int r;
|
||||
|
||||
for (;;) {
|
||||
r = pidref_wait(pidref, ret, WEXITED);
|
||||
if (r != -EINTR)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
static void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
|
||||
siphash24_compress_typesafe(pidref->pid, state);
|
||||
}
|
||||
|
||||
+13
-1
@@ -55,7 +55,19 @@ int pidref_new_from_pid(pid_t pid, PidRef **ret);
|
||||
|
||||
int pidref_kill(const PidRef *pidref, int sig);
|
||||
int pidref_kill_and_sigcont(const PidRef *pidref, int sig);
|
||||
int pidref_sigqueue(const PidRef *pidfref, int sig, int value);
|
||||
int pidref_sigqueue(const PidRef *pidref, int sig, int value);
|
||||
|
||||
int pidref_wait(const PidRef *pidref, siginfo_t *siginfo, int options);
|
||||
int pidref_wait_for_terminate(const PidRef *pidref, siginfo_t *ret);
|
||||
|
||||
static inline void pidref_done_sigkill_wait(PidRef *pidref) {
|
||||
if (!pidref_is_set(pidref))
|
||||
return;
|
||||
|
||||
(void) pidref_kill(pidref, SIGKILL);
|
||||
(void) pidref_wait_for_terminate(pidref, NULL);
|
||||
pidref_done(pidref);
|
||||
}
|
||||
|
||||
int pidref_verify(const PidRef *pidref);
|
||||
|
||||
|
||||
@@ -730,6 +730,82 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pid_get_start_time(pid_t pid, uint64_t *ret) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
const char *p;
|
||||
int r;
|
||||
|
||||
assert(pid >= 0);
|
||||
|
||||
p = procfs_file_alloca(pid, "stat");
|
||||
r = read_one_line_file(p, &line);
|
||||
if (r == -ENOENT)
|
||||
return -ESRCH;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* Let's skip the pid and comm fields. The latter is enclosed in () but does not escape any () in its
|
||||
* value, so let's skip over it manually */
|
||||
|
||||
p = strrchr(line, ')');
|
||||
if (!p)
|
||||
return -EIO;
|
||||
|
||||
p++;
|
||||
|
||||
unsigned long llu;
|
||||
|
||||
if (sscanf(p, " "
|
||||
"%*c " /* state */
|
||||
"%*u " /* ppid */
|
||||
"%*u " /* pgrp */
|
||||
"%*u " /* session */
|
||||
"%*u " /* tty_nr */
|
||||
"%*u " /* tpgid */
|
||||
"%*u " /* flags */
|
||||
"%*u " /* minflt */
|
||||
"%*u " /* cminflt */
|
||||
"%*u " /* majflt */
|
||||
"%*u " /* cmajflt */
|
||||
"%*u " /* utime */
|
||||
"%*u " /* stime */
|
||||
"%*u " /* cutime */
|
||||
"%*u " /* cstime */
|
||||
"%*i " /* priority */
|
||||
"%*i " /* nice */
|
||||
"%*u " /* num_threads */
|
||||
"%*u " /* itrealvalue */
|
||||
"%lu ", /* starttime */
|
||||
&llu) != 1)
|
||||
return -EIO;
|
||||
|
||||
if (ret)
|
||||
*ret = llu;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pidref_get_start_time(const PidRef *pid, uint64_t *ret) {
|
||||
uint64_t t;
|
||||
int r;
|
||||
|
||||
if (!pidref_is_set(pid))
|
||||
return -ESRCH;
|
||||
|
||||
r = pid_get_start_time(pid->pid, ret ? &t : NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = pidref_verify(pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (ret)
|
||||
*ret = t;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_process_umask(pid_t pid, mode_t *ret) {
|
||||
_cleanup_free_ char *m = NULL;
|
||||
const char *p;
|
||||
@@ -1650,6 +1726,30 @@ int safe_fork_full(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pidref_safe_fork_full(
|
||||
const char *name,
|
||||
const int stdio_fds[3],
|
||||
const int except_fds[],
|
||||
size_t n_except_fds,
|
||||
ForkFlags flags,
|
||||
PidRef *ret_pid) {
|
||||
|
||||
pid_t pid;
|
||||
int r, q;
|
||||
|
||||
assert(!FLAGS_SET(flags, FORK_WAIT));
|
||||
|
||||
r = safe_fork_full(name, stdio_fds, except_fds, n_except_fds, flags, &pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
q = pidref_set_pid(ret_pid, pid);
|
||||
if (q < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
|
||||
*ret_pid = PIDREF_MAKE_FROM_PID(pid);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int namespace_fork(
|
||||
const char *outer_name,
|
||||
const char *inner_name,
|
||||
|
||||
@@ -54,6 +54,8 @@ int get_process_cwd(pid_t pid, char **ret);
|
||||
int get_process_root(pid_t pid, char **ret);
|
||||
int get_process_environ(pid_t pid, char **ret);
|
||||
int get_process_ppid(pid_t pid, pid_t *ret);
|
||||
int pid_get_start_time(pid_t pid, uint64_t *ret);
|
||||
int pidref_get_start_time(const PidRef* pid, uint64_t *ret);
|
||||
int get_process_umask(pid_t pid, mode_t *ret);
|
||||
|
||||
int container_get_leader(const char *machine, pid_t *pid);
|
||||
@@ -191,6 +193,18 @@ static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
|
||||
return safe_fork_full(name, NULL, NULL, 0, flags, ret_pid);
|
||||
}
|
||||
|
||||
int pidref_safe_fork_full(
|
||||
const char *name,
|
||||
const int stdio_fds[3],
|
||||
const int except_fds[],
|
||||
size_t n_except_fds,
|
||||
ForkFlags flags,
|
||||
PidRef *ret_pid);
|
||||
|
||||
static inline int pidref_safe_fork(const char *name, ForkFlags flags, PidRef *ret_pid) {
|
||||
return pidref_safe_fork_full(name, NULL, NULL, 0, flags, ret_pid);
|
||||
}
|
||||
|
||||
int namespace_fork(const char *outer_name, const char *inner_name, const int except_fds[], size_t n_except_fds, ForkFlags flags, int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd, pid_t *ret_pid);
|
||||
|
||||
int set_oom_score_adjust(int value);
|
||||
|
||||
@@ -956,6 +956,21 @@ int getpeergroups(int fd, gid_t **ret) {
|
||||
return (int) n;
|
||||
}
|
||||
|
||||
int getpeerpidfd(int fd) {
|
||||
socklen_t n = sizeof(int);
|
||||
int pidfd = -EBADF;
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
|
||||
return -errno;
|
||||
|
||||
if (n != sizeof(int))
|
||||
return -EIO;
|
||||
|
||||
return pidfd;
|
||||
}
|
||||
|
||||
ssize_t send_many_fds_iov_sa(
|
||||
int transport_fd,
|
||||
int *fds_array, size_t n_fds_array,
|
||||
|
||||
@@ -152,6 +152,7 @@ bool address_label_valid(const char *p);
|
||||
int getpeercred(int fd, struct ucred *ucred);
|
||||
int getpeersec(int fd, char **ret);
|
||||
int getpeergroups(int fd, gid_t **ret);
|
||||
int getpeerpidfd(int fd);
|
||||
|
||||
ssize_t send_many_fds_iov_sa(
|
||||
int transport_fd,
|
||||
|
||||
Reference in New Issue
Block a user