Browse Source

bus: add new sd_bus_creds object to encapsulate process credentials

This way we can unify handling of credentials that are attached to
messages, or can be queried for bus name owners or connection peers.

This also adds the ability to extend incomplete credential information
with data from /proc,

Also, provide a convenience call that will automatically determine the
most appropriate credential object for an incoming message, by using the
the attached information if possible, the sending name information if
available and otherwise the peer's credentials.
keep-around/ba91431154ad7bac82ddf0a540ec1b40db62d782
Lennart Poettering 9 years ago
parent
commit
5b12334d35
  1. 1
      .gitignore
  2. 31
      Makefile.am
  3. 8
      src/core/dbus-manager.c
  4. 16
      src/core/dbus.c
  5. 219
      src/core/selinux-access.c
  6. 5
      src/core/service.c
  7. 154
      src/libsystemd-bus/bus-control.c
  8. 30
      src/libsystemd-bus/bus-convenience.c
  9. 796
      src/libsystemd-bus/bus-creds.c
  10. 67
      src/libsystemd-bus/bus-creds.h
  11. 227
      src/libsystemd-bus/bus-dump.c
  12. 2
      src/libsystemd-bus/bus-dump.h
  13. 2
      src/libsystemd-bus/bus-internal.h
  14. 61
      src/libsystemd-bus/bus-kernel.c
  15. 234
      src/libsystemd-bus/bus-message.c
  16. 30
      src/libsystemd-bus/bus-message.h
  17. 31
      src/libsystemd-bus/bus-util.c
  18. 2
      src/libsystemd-bus/bus-util.h
  19. 44
      src/libsystemd-bus/busctl.c
  20. 76
      src/libsystemd-bus/libsystemd-bus.sym
  21. 120
      src/libsystemd-bus/sd-bus.c
  22. 4
      src/libsystemd-bus/test-bus-chat.c
  23. 46
      src/libsystemd-bus/test-bus-creds.c
  24. 18
      src/libsystemd-bus/test-bus-kernel.c
  25. 47
      src/login/logind-dbus.c
  26. 9
      src/login/logind-seat-dbus.c
  27. 23
      src/login/logind-session-dbus.c
  28. 9
      src/login/logind-user-dbus.c
  29. 9
      src/machine/machine-dbus.c
  30. 16
      src/machine/machined-dbus.c
  31. 57
      src/shared/audit.c
  32. 13
      src/shared/util.c
  33. 119
      src/systemd/sd-bus.h

1
.gitignore

@ -90,6 +90,7 @@
/tags
/test-boot-timestamp
/test-bus-chat
/test-bus-creds
/test-bus-error
/test-bus-introspect
/test-bus-kernel

31
Makefile.am

@ -769,7 +769,9 @@ libsystemd_shared_la_SOURCES = \
src/shared/errno-list.c \
src/shared/errno-list.h \
src/shared/syscall-list.c \
src/shared/syscall-list.h
src/shared/syscall-list.h \
src/shared/audit.c \
src/shared/audit.h
nodist_libsystemd_shared_la_SOURCES = \
src/shared/errno-from-name.h \
@ -842,14 +844,6 @@ libsystemd_capability_la_CFLAGS = \
libsystemd_capability_la_LIBADD = \
$(CAP_LIBS)
# ------------------------------------------------------------------------------
noinst_LTLIBRARIES += \
libsystemd-audit.la
libsystemd_audit_la_SOURCES = \
src/shared/audit.c \
src/shared/audit.h
# ------------------------------------------------------------------------------
if HAVE_ACL
noinst_LTLIBRARIES += \
@ -1013,7 +1007,6 @@ libsystemd_core_la_LIBADD = \
libsystemd-capability.la \
libsystemd-units.la \
libsystemd-label.la \
libsystemd-audit.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libudev-internal.la \
@ -1957,6 +1950,8 @@ libsystemd_bus_la_SOURCES = \
src/libsystemd-bus/bus-container.h \
src/libsystemd-bus/bus-message.c \
src/libsystemd-bus/bus-message.h \
src/libsystemd-bus/bus-creds.c \
src/libsystemd-bus/bus-creds.h \
src/libsystemd-bus/bus-signature.c \
src/libsystemd-bus/bus-signature.h \
src/libsystemd-bus/bus-type.c \
@ -2035,6 +2030,7 @@ tests += \
test-bus-introspect \
test-bus-objects \
test-bus-error \
test-bus-creds \
test-event
bin_PROGRAMS += \
@ -2119,6 +2115,17 @@ test_bus_error_LDADD = \
libsystemd-daemon-internal.la \
libsystemd-shared.la
test_bus_creds_SOURCES = \
src/libsystemd-bus/test-bus-creds.c
test_bus_creds_LDADD = \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \
libsystemd-shared.la \
libsystemd-bus-dump.la \
libsystemd-capability.la
test_bus_match_SOURCES = \
src/libsystemd-bus/test-bus-match.c
@ -3086,7 +3093,6 @@ nodist_libsystemd_journal_core_la_SOURCES = \
libsystemd_journal_core_la_LIBADD = \
libsystemd-journal-internal.la \
libudev-internal.la \
libsystemd-audit.la \
libsystemd-capability.la \
libsystemd-label.la \
libsystemd-daemon-internal.la \
@ -3778,7 +3784,6 @@ libsystemd_machine_core_la_SOURCES = \
libsystemd_machine_core_la_LIBADD = \
libsystemd-label.la \
libsystemd-audit.la \
libsystemd-daemon-internal.la \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
@ -3933,7 +3938,6 @@ libsystemd_logind_core_la_SOURCES = \
libsystemd_logind_core_la_LIBADD = \
libsystemd-label.la \
libsystemd-capability.la \
libsystemd-audit.la \
libsystemd-daemon-internal.la \
libsystemd-id128-internal.la \
libsystemd-bus-internal.la \
@ -4073,7 +4077,6 @@ pam_systemd_la_LDFLAGS = \
pam_systemd_la_LIBADD = \
libsystemd-capability.la \
libsystemd-audit.la \
libsystemd-bus-internal.la \
libsystemd-id128-internal.la \
libsystemd-daemon-internal.la \

8
src/core/dbus-manager.c

@ -338,7 +338,13 @@ static int method_get_unit_by_pid(sd_bus *bus, sd_bus_message *message, void *us
return r;
if (pid == 0) {
r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
if (r < 0)
return r;
r = sd_bus_creds_get_pid(creds, &pid);
if (r < 0)
return r;
}

16
src/core/dbus.c

@ -247,9 +247,14 @@ static int selinux_filter(sd_bus *bus, sd_bus_message *message, void *userdata,
}
if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
pid_t pid;
r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
if (r < 0)
return 0;
r = sd_bus_creds_get_pid(creds, &pid);
if (r < 0)
return 0;
@ -300,6 +305,7 @@ static int find_unit(Manager *m, sd_bus *bus, const char *path, Unit **unit, sd_
assert(path);
if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
sd_bus_message *message;
pid_t pid;
@ -307,9 +313,13 @@ static int find_unit(Manager *m, sd_bus *bus, const char *path, Unit **unit, sd_
if (!message)
return 0;
r = sd_bus_get_owner_pid(bus, sd_bus_message_get_sender(message), &pid);
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
if (r < 0)
return 0;
return r;
r = sd_bus_creds_get_pid(creds, &pid);
if (r < 0)
return r;
u = manager_get_unit_by_pid(m, pid);
} else {

219
src/core/selinux-access.c

@ -41,91 +41,16 @@
#include "audit.h"
#include "selinux-util.h"
#include "audit-fd.h"
#include "strv.h"
static bool initialized = false;
struct auditstruct {
struct audit_info {
sd_bus_creds *creds;
const char *path;
char *cmdline;
uid_t loginuid;
uid_t uid;
gid_t gid;
const char *cmdline;
};
static int bus_get_selinux_security_context(
sd_bus *bus,
const char *name,
sd_bus_error *error,
char **ret) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
const void *p;
size_t sz;
char *b;
int r;
assert(bus);
assert(name);
assert(ret);
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"GetConnectionSELinuxSecurityContext",
error, &m,
"s", name);
if (r < 0)
return r;
r = sd_bus_message_read_array(m, 'y', &p, &sz);
if (r < 0)
return r;
b = strndup(p, sz);
if (!b)
return -ENOMEM;
*ret = b;
return 0;
}
static int bus_get_audit_data(
sd_bus *bus,
const char *name,
struct auditstruct *audit) {
pid_t pid;
int r;
assert(bus);
assert(name);
assert(audit);
r = sd_bus_get_owner_pid(bus, name, &pid);
if (r < 0)
return r;
r = audit_loginuid_from_pid(pid, &audit->loginuid);
if (r < 0)
return r;
r = get_process_uid(pid, &audit->uid);
if (r < 0)
return r;
r = get_process_gid(pid, &audit->gid);
if (r < 0)
return r;
r = get_process_cmdline(pid, 0, true, &audit->cmdline);
if (r < 0)
return r;
return 0;
}
/*
Any time an access gets denied this callback will be called
with the aduit data. We then need to just copy the audit data into the msgbuf.
@ -136,19 +61,19 @@ static int audit_callback(
char *msgbuf,
size_t msgbufsize) {
struct auditstruct *audit = (struct auditstruct *) auditdata;
const struct audit_info *audit = auditdata;
uid_t uid = 0, login_uid = 0;
gid_t gid = 0;
sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid);
sd_bus_creds_get_uid(audit->creds, &uid);
sd_bus_creds_get_gid(audit->creds, &gid);
snprintf(msgbuf, msgbufsize,
"auid=%d uid=%d gid=%d%s%s%s%s%s%s",
audit->loginuid,
audit->uid,
audit->gid,
(audit->path ? " path=\"" : ""),
strempty(audit->path),
(audit->path ? "\"" : ""),
(audit->cmdline ? " cmdline=\"" : ""),
strempty(audit->cmdline),
(audit->cmdline ? "\"" : ""));
login_uid, uid, gid,
audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "");
msgbuf[msgbufsize-1] = 0;
@ -164,13 +89,12 @@ static int audit_callback(
_printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_AUDIT
if (get_audit_fd() >= 0) {
_cleanup_free_ char *buf = NULL;
int r;
va_start(ap, fmt);
r = vasprintf(&buf, fmt, ap);
va_end(ap);
@ -178,10 +102,10 @@ _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
audit_log_user_avc_message(get_audit_fd(), AUDIT_USER_AVC, buf, NULL, NULL, NULL, 0);
return 0;
}
va_start(ap, fmt);
}
#endif
va_start(ap, fmt);
log_metav(LOG_USER | LOG_INFO, __FILE__, __LINE__, __FUNCTION__, fmt, ap);
va_end(ap);
@ -238,76 +162,6 @@ void selinux_access_free(void) {
initialized = false;
}
static int get_audit_data(
sd_bus *bus,
sd_bus_message *message,
struct auditstruct *audit) {
struct ucred ucred;
const char *sender;
socklen_t len;
int r, fd;
sender = sd_bus_message_get_sender(message);
if (sender)
return bus_get_audit_data(bus, sender, audit);
fd = sd_bus_get_fd(bus);
if (fd < 0)
return fd;
len = sizeof(ucred);
r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
if (r < 0)
return -errno;
audit->uid = ucred.uid;
audit->gid = ucred.gid;
r = audit_loginuid_from_pid(ucred.pid, &audit->loginuid);
if (r < 0)
return r;
r = get_process_cmdline(ucred.pid, 0, true, &audit->cmdline);
if (r < 0)
return r;
return 0;
}
/*
This function returns the security context of the remote end of the dbus
connections. Whether it is on the bus or a local connection.
*/
static int get_calling_context(
sd_bus *bus,
sd_bus_message *message,
sd_bus_error *error,
security_context_t *ret) {
const char *sender;
int r, fd;
/*
If sender exists then
if sender is NULL this indicates a local connection. Grab the fd
from dbus and do an getpeercon to peers process context
*/
sender = sd_bus_message_get_sender(message);
if (sender)
return bus_get_selinux_security_context(bus, sender, error, ret);
fd = sd_bus_get_fd(bus);
if (fd < 0)
return fd;
r = getpeercon(fd, ret);
if (r < 0)
return -errno;
return 0;
}
/*
This function communicates with the kernel to check whether or not it should
allow the access.
@ -321,9 +175,12 @@ int selinux_generic_access_check(
const char *permission,
sd_bus_error *error) {
security_context_t scon = NULL, fcon = NULL;
const char *tclass = NULL;
struct auditstruct audit;
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
const char *tclass = NULL, *scon = NULL;
struct audit_info audit_info = {};
_cleanup_free_ char *cl = NULL;
security_context_t fcon = NULL;
char **cmdline = NULL;
int r = 0;
assert(bus);
@ -338,12 +195,16 @@ int selinux_generic_access_check(
if (r < 0)
return r;
audit.uid = audit.loginuid = (uid_t) -1;
audit.gid = (gid_t) -1;
audit.cmdline = NULL;
audit.path = path;
r = sd_bus_query_sender_creds(
message,
SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|
SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_AUDIT_LOGIN_UID|
SD_BUS_CREDS_SELINUX_CONTEXT,
&creds);
if (r < 0)
goto finish;
r = get_calling_context(bus, message, error, &scon);
r = sd_bus_creds_get_selinux_context(creds, &scon);
if (r < 0)
goto finish;
@ -367,21 +228,23 @@ int selinux_generic_access_check(
tclass = "system";
}
get_audit_data(bus, message, &audit);
sd_bus_creds_get_cmdline(creds, &cmdline);
cl = strv_join(cmdline, " ");
audit_info.creds = creds;
audit_info.path = path;
audit_info.cmdline = cl;
errno = 0;
r = selinux_check_access(scon, fcon, tclass, permission, &audit);
r = selinux_check_access((security_context_t) scon, fcon, tclass, permission, &audit_info);
if (r < 0)
r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "SELinux policy denies access.");
log_debug("SELinux access check scon=%s tcon=%s tclass=%s perm=%s path=%s cmdline=%s: %i", scon, fcon, tclass, permission, path, audit.cmdline, r);
log_debug("SELinux access check scon=%s tcon=%s tclass=%s perm=%s path=%s cmdline=%s: %i", scon, fcon, tclass, permission, path, cl, r);
finish:
free(audit.cmdline);
freecon(scon);
freecon(fcon);
if (r && security_getenforce() != 1) {
if (r < 0 && security_getenforce() != 1) {
sd_bus_error_free(error);
r = 0;
}

5
src/core/service.c

@ -3673,11 +3673,14 @@ static void service_bus_name_owner_change(
s->state == SERVICE_RUNNING ||
s->state == SERVICE_RELOAD)) {
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
pid_t pid;
/* Try to acquire PID from bus service */
r = sd_bus_get_owner_pid(u->manager->api_bus, name, &pid);
r = sd_bus_get_owner_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
if (r >= 0)
r = sd_bus_creds_get_pid(creds, &pid);
if (r >= 0) {
log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);

154
src/libsystemd-bus/bus-control.c

@ -260,80 +260,114 @@ _public_ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
return 0;
}
_public_ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
_public_ int sd_bus_get_owner_creds(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **creds) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
uint32_t u;
_cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
pid_t pid = 0;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!uid)
return -EINVAL;
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
assert_return(bus, -EINVAL);
assert_return(name, -EINVAL);
assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
assert_return(creds, -EINVAL);
assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD);
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
c = bus_creds_new();
if (!c)
return -ENOMEM;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
if ((mask & SD_BUS_CREDS_PID) ||
mask & ~(SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_SELINUX_CONTEXT)) {
uint32_t u;
*uid = (uid_t) u;
return 0;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixProcessID",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
_public_ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
uint32_t u;
int r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!pid)
return -EINVAL;
if (!BUS_IS_OPEN(bus->state))
return -ENOTCONN;
if (bus_pid_changed(bus))
return -ECHILD;
pid = u;
if (mask & SD_BUS_CREDS_PID) {
c->pid = u;
c->mask |= SD_BUS_CREDS_PID;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixProcessID",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
reply = sd_bus_message_unref(reply);
}
if (mask & SD_BUS_CREDS_UID) {
uint32_t u;
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
c->uid = u;
c->mask |= SD_BUS_CREDS_UID;
reply = sd_bus_message_unref(reply);
}
if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
const void *p;
size_t sz;
r = sd_bus_call_method(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionSELinuxSecurityContext",
NULL,
&reply,
"s",
name);
if (r < 0)
return r;
r = sd_bus_message_read_array(reply, 'y', &p, &sz);
if (r < 0)
return r;
c->label = strndup(p, sz);
if (!c->label)
return -ENOMEM;
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
}
r = sd_bus_message_read(reply, "u", &u);
r = bus_creds_add_more(c, mask, pid, 0);
if (r < 0)
return r;
if (u == 0)
return -EIO;
*creds = c;
c = NULL;
*pid = (uid_t) u;
return 0;
}

30
src/libsystemd-bus/bus-convenience.c

@ -410,3 +410,33 @@ _public_ int sd_bus_set_property(
return sd_bus_call(bus, m, 0, error, NULL);
}
_public_ int sd_bus_query_sender_creds(sd_bus_message *call, uint64_t mask, sd_bus_creds **creds) {
sd_bus_creds *c;
assert_return(call, -EINVAL);
assert_return(call->sealed, -EPERM);
assert_return(call->bus && BUS_IS_OPEN(call->bus->state), -ENOTCONN);
assert_return(!bus_pid_changed(call->bus), -ECHILD);
c = sd_bus_message_get_creds(call);
/* All data we need? */
if (c && (mask & ~c->mask) == 0) {
*creds = sd_bus_creds_ref(c);
return 0;
}
/* No data passed? Or not enough data passed to retrieve the missing bits? */
if (!c || !(c->mask & SD_BUS_CREDS_PID)) {
/* We couldn't read anything from the call, let's try
* to get it from the sender or peer */
if (call->sender)
return sd_bus_get_owner_creds(call->bus, call->sender, mask, creds);
else
return sd_bus_get_peer_creds(call->bus, mask, creds);
}
return sd_bus_creds_extend(c, mask, creds);
}

796
src/libsystemd-bus/bus-creds.c

@ -0,0 +1,796 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include "util.h"
#include "cgroup-util.h"
#include "fileio.h"
#include "audit.h"
#include "bus-message.h"
#include "bus-util.h"
#include "time-util.h"
#include "bus-creds.h"
enum {
CAP_OFFSET_INHERITABLE = 0,
CAP_OFFSET_PERMITTED = 1,
CAP_OFFSET_EFFECTIVE = 2,
CAP_OFFSET_BOUNDING = 3
};
void bus_creds_done(sd_bus_creds *c) {
assert(c);
/* For internal bus cred structures that are allocated by
* something else */
free(c->session);
free(c->unit);
free(c->user_unit);
free(c->slice);
free(c->cmdline_array);
}
_public_ sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
assert_return(c, NULL);
if (c->allocated) {
assert(c->n_ref > 0);
c->n_ref++;
} else {
sd_bus_message *m;
/* If this is an embedded creds structure, then
* forward ref counting to the message */
m = container_of(c, sd_bus_message, creds);
sd_bus_message_ref(m);
}
return c;
}
_public_ sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
assert_return(c, NULL);
if (c->allocated) {
assert(c->n_ref > 0);
c->n_ref--;
if (c->n_ref == 0) {
bus_creds_done(c);
free(c->comm);
free(c->tid_comm);
free(c->exe);
free(c->cmdline);
free(c->cgroup);
free(c->capability);
free(c->label);
free(c);
}
} else {
sd_bus_message *m;
m = container_of(c, sd_bus_message, creds);
sd_bus_message_unref(m);
}
return NULL;
}
_public_ uint64_t sd_bus_creds_get_mask(sd_bus_creds *c) {
assert_return(c, 0);
return c->mask;
}
sd_bus_creds* bus_creds_new(void) {
sd_bus_creds *c;
c = new0(sd_bus_creds, 1);
if (!c)
return NULL;
c->allocated = true;
c->n_ref = 1;
return c;
}
_public_ int sd_bus_creds_new_from_pid(pid_t pid, uint64_t mask, sd_bus_creds **ret) {
sd_bus_creds *c;
int r;
assert_return(pid >= 0, -EINVAL);
assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
assert_return(ret, -EINVAL);
if (pid == 0)
pid = getpid();
c = bus_creds_new();
if (!c)
return -ENOMEM;
r = bus_creds_add_more(c, mask, pid, 0);
if (r < 0) {
free(c);
return r;
}
/* Check if the process existed at all, in case we haven't
* figured that out already */
if (kill(pid, 0) < 0 && errno == ESRCH) {
sd_bus_creds_unref(c);
return -ESRCH;
}
*ret = c;
return 0;
}
_public_ int sd_bus_creds_get_uid(sd_bus_creds *c, uid_t *uid) {
assert_return(c, -EINVAL);
assert_return(uid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_UID, -ENODATA);
*uid = c->uid;
return 0;
}
_public_ int sd_bus_creds_get_gid(sd_bus_creds *c, gid_t *gid) {
assert_return(c, -EINVAL);
assert_return(gid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_UID, -ENODATA);
*gid = c->gid;
return 0;
}
_public_ int sd_bus_creds_get_pid(sd_bus_creds *c, pid_t *pid) {
assert_return(c, -EINVAL);
assert_return(pid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_PID, -ENODATA);
assert(c->pid > 0);
*pid = c->pid;
return 0;
}
_public_ int sd_bus_creds_get_tid(sd_bus_creds *c, pid_t *tid) {
assert_return(c, -EINVAL);
assert_return(tid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_TID, -ENODATA);
assert(c->tid > 0);
*tid = c->tid;
return 0;
}
_public_ int sd_bus_creds_get_pid_starttime(sd_bus_creds *c, uint64_t *usec) {
assert_return(c, -EINVAL);
assert_return(usec, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_PID_STARTTIME, -ENODATA);
assert(c->pid_starttime > 0);
*usec = c->pid_starttime;
return 0;
}
_public_ int sd_bus_creds_get_selinux_context(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_SELINUX_CONTEXT, -ENODATA);
assert(c->label);
*ret = c->label;
return 0;
}
_public_ int sd_bus_creds_get_comm(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_COMM, -ENODATA);
assert(c->comm);
*ret = c->comm;
return 0;
}
_public_ int sd_bus_creds_get_tid_comm(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_TID_COMM, -ENODATA);
assert(c->tid_comm);
*ret = c->tid_comm;
return 0;
}
_public_ int sd_bus_creds_get_exe(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_EXE, -ENODATA);
assert(c->exe);
*ret = c->exe;
return 0;
}
_public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) {
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_CGROUP, -ENODATA);
assert(c->cgroup);
*ret = c->cgroup;
return 0;
}
_public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
int r;
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_UNIT, -ENODATA);
assert(c->cgroup);
if (!c->unit) {
r = cg_path_get_unit(c->cgroup, (char**) &c->unit);
if (r < 0)
return r;
}
*ret = c->unit;
return 0;
}
_public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
int r;
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_USER_UNIT, -ENODATA);
assert(c->cgroup);
if (!c->user_unit) {
r = cg_path_get_user_unit(c->cgroup, (char**) &c->user_unit);
if (r < 0)
return r;
}
*ret = c->user_unit;
return 0;
}
_public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
int r;
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_SLICE, -ENODATA);
assert(c->cgroup);
if (!c->slice) {
r = cg_path_get_slice(c->cgroup, (char**) &c->slice);
if (r < 0)
return r;
}
*ret = c->slice;
return 0;
}
_public_ int sd_bus_creds_get_session(sd_bus_creds *c, const char **ret) {
int r;
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_SESSION, -ENODATA);
assert(c->cgroup);
if (!c->session) {
r = cg_path_get_session(c->cgroup, (char**) &c->session);
if (r < 0)
return r;
}
*ret = c->session;
return 0;
}
_public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) {
assert_return(c, -EINVAL);
assert_return(uid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_OWNER_UID, -ENODATA);
assert(c->cgroup);
return cg_path_get_owner_uid(c->cgroup, uid);
}
_public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
size_t n, i;
const char *p;
bool first;
assert_return(c, -EINVAL);
assert_return(c->cmdline, -ESRCH);
assert_return(c->mask & SD_BUS_CREDS_CMDLINE, -ENODATA);
assert(c->cmdline);
for (p = c->cmdline, n = 0; p < c->cmdline + c->cmdline_length; p++)
if (*p == 0)
n++;
*(char***) &c->cmdline_array = new(char*, n + 1);
if (!c->cmdline_array)
return -ENOMEM;
for (p = c->cmdline, i = 0, first = true; p < c->cmdline + c->cmdline_length; p++) {
if (first)
c->cmdline_array[i++] = (char*) p;
first = *p == 0;
}
c->cmdline_array[i] = NULL;
*cmdline = c->cmdline_array;
return 0;
}
_public_ int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessionid) {
assert_return(c, -EINVAL);
assert_return(sessionid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID, -ENODATA);
*sessionid = c->audit_session_id;
return 0;
}
_public_ int sd_bus_creds_get_audit_login_uid(sd_bus_creds *c, uid_t *uid) {
assert_return(c, -EINVAL);
assert_return(uid, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_AUDIT_LOGIN_UID, -ENODATA);
*uid = c->audit_login_uid;
return 0;
}
static int has_cap(sd_bus_creds *c, unsigned offset, int capability) {
size_t sz;
assert(c);
assert(c->capability);
sz = c->capability_size / 4;
if ((size_t) capability >= sz*8)
return 0;
return !!(c->capability[offset * sz + (capability / 8)] & (1 << (capability % 8)));
}
_public_ int sd_bus_creds_has_effective_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_EFFECTIVE_CAPS, -ENODATA);
return has_cap(c, CAP_OFFSET_EFFECTIVE, capability);
}
_public_ int sd_bus_creds_has_permitted_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_PERMITTED_CAPS, -ENODATA);
return has_cap(c, CAP_OFFSET_PERMITTED, capability);
}
_public_ int sd_bus_creds_has_inheritable_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_INHERITABLE_CAPS, -ENODATA);
return has_cap(c, CAP_OFFSET_INHERITABLE, capability);
}
_public_ int sd_bus_creds_has_bounding_cap(sd_bus_creds *c, int capability) {
assert_return(c, -EINVAL);
assert_return(capability >= 0, -EINVAL);
assert_return(c->mask & SD_BUS_CREDS_BOUNDING_CAPS, -ENODATA);
return has_cap(c, CAP_OFFSET_BOUNDING, capability);
}
static int parse_caps(sd_bus_creds *c, unsigned offset, const char *p) {
size_t sz;
unsigned i;
assert(c);
assert(p);
p += strspn(p, WHITESPACE);
sz = strlen(p);
if (sz % 2 != 0)
return -EINVAL;
sz /= 2;
if (!c->capability) {
c->capability = new0(uint8_t, sz * 4);
if (!c->capability)
return -ENOMEM;
c->capability_size = sz * 4;
}
for (i = 0; i < sz; i ++) {
int x, y;
x = unhexchar(p[i*2]);
y = unhexchar(p[i*2+1]);
if (x < 0 || y < 0)
return -EINVAL;
c->capability[offset * sz + (sz - i - 1)] = (uint8_t) x << 4 | (uint8_t) y;
}
return 0;
}
int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
uint64_t missing;
int r;
assert(c);
assert(c->allocated);
missing = mask & ~c->mask;
if (missing == 0)
return 0;
/* Try to retrieve PID from creds if it wasn't passed to us */
if (pid <= 0 && (c->mask & SD_BUS_CREDS_PID))
pid = c->pid;
if (tid <= 0 && (c->mask & SD_BUS_CREDS_TID))
tid = c->pid;
/* Without pid we cannot do much... */
if (pid <= 0)
return 0;
if (missing & (SD_BUS_CREDS_UID | SD_BUS_CREDS_GID |
SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_INHERITABLE_CAPS |
SD_BUS_CREDS_PERMITTED_CAPS | SD_BUS_CREDS_BOUNDING_CAPS)) {
_cleanup_fclose_ FILE *f = NULL;
char line[LINE_MAX];
const char *p;
p = procfs_file_alloca(pid, "status");
f = fopen(p, "re");
if (!f)
return errno == ENOENT ? -ESRCH : -errno;
FOREACH_LINE(line, f, return -errno) {
truncate_nl(line);
if (missing & SD_BUS_CREDS_UID) {
p = startswith(line, "Uid:");
if (p) {
unsigned long uid;
p += strspn(p, WHITESPACE);
if (sscanf(p, "%lu", &uid) != 1)
return -EIO;
c->uid = (uid_t) uid;
c->mask |= SD_BUS_CREDS_UID;
continue;
}
}
if (missing & SD_BUS_CREDS_GID) {
p = startswith(line, "Gid:");
if (p) {
unsigned long gid;
p += strspn(p, WHITESPACE);
if (sscanf(p, "%lu", &gid) != 1)
return -EIO;
c->gid = (uid_t) gid;
c->mask |= SD_BUS_CREDS_GID;
continue;
}
}
if (missing & SD_BUS_CREDS_EFFECTIVE_CAPS) {
p = startswith(line, "CapEff:");
if (p) {
r = parse_caps(c, CAP_OFFSET_EFFECTIVE, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_EFFECTIVE_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_PERMITTED_CAPS) {
p = startswith(line, "CapPrm:");
if (p) {
r = parse_caps(c, CAP_OFFSET_PERMITTED, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_PERMITTED_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_INHERITABLE_CAPS) {
p = startswith(line, "CapInh:");
if (p) {
r = parse_caps(c, CAP_OFFSET_INHERITABLE, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_INHERITABLE_CAPS;
continue;
}
}
if (missing & SD_BUS_CREDS_BOUNDING_CAPS) {
p = startswith(line, "CapBnd:");
if (p) {
r = parse_caps(c, CAP_OFFSET_BOUNDING, p);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_BOUNDING_CAPS;
continue;
}
}
}
}
if (missing & (SD_BUS_CREDS_PID_STARTTIME)) {
unsigned long long st;
r = get_starttime_of_pid(pid, &st);
if (r < 0)
return r;
c->pid_starttime = ((usec_t) st * USEC_PER_SEC) / (usec_t) sysconf(_SC_CLK_TCK);
c->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (missing & SD_BUS_CREDS_SELINUX_CONTEXT) {
const char *p;
p = procfs_file_alloca(pid, "attr/current");
r = read_one_line_file(p, &c->label);
if (r < 0 && r != -ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
}
if (missing & SD_BUS_CREDS_COMM) {
r = get_process_comm(pid, &c->comm);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_COMM;
}
if (missing & SD_BUS_CREDS_EXE) {
r = get_process_exe(pid, &c->exe);
if (r < 0)
return r;
c->mask |= SD_BUS_CREDS_EXE;
}
if (missing & SD_BUS_CREDS_CMDLINE) {
const char *p;
p = procfs_file_alloca(pid, "cmdline");
r = read_full_file(p, &c->cmdline, &c->cmdline_length);
if (r < 0)
return r;
if (c->cmdline_length == 0) {
free(c->cmdline);
c->cmdline = NULL;
} else
c->mask |= SD_BUS_CREDS_CMDLINE;
}
if (tid > 0 && (missing & SD_BUS_CREDS_TID_COMM)) {
_cleanup_free_ char *p = NULL;
if (asprintf(&p, "/proc/%lu/task/%lu/comm", (unsigned long) pid, (unsigned long) tid) < 0)
return -ENOMEM;
r = read_one_line_file(p, &c->tid_comm);
if (r < 0)
return r == -ENOENT ? -ESRCH : r;
c->mask |= SD_BUS_CREDS_TID_COMM;
}
if (missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID)) {
r = cg_pid_get_path(NULL, pid, &c->cgroup);
if (r < 0)
return r;
c->mask |= missing & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID);
}
if (missing & SD_BUS_CREDS_AUDIT_SESSION_ID) {
r = audit_session_from_pid(pid, &c->audit_session_id);
if (r < 0 && r != -ENOTSUP && r != -ENXIO && r != ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
}
if (missing & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
r = audit_loginuid_from_pid(pid, &c->audit_login_uid);
if (r < 0 && r != -ENOTSUP && r != -ENXIO && r != ENOENT)
return r;
else if (r >= 0)
c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
return 0;
}
_public_ int sd_bus_creds_extend(sd_bus_creds *c, uint64_t mask, sd_bus_creds **ret) {
_cleanup_bus_creds_unref_ sd_bus_creds *n = NULL;
int r;
assert_return(c, -EINVAL);
assert_return(ret, -EINVAL);
if ((mask & ~c->mask) == 0) {
/* There's already all data we need. */
*ret = sd_bus_creds_ref(c);
return 0;
}
n = bus_creds_new();
if (!n)
return -ENOMEM;
/* Copy the original data over */
if (c->mask & mask & SD_BUS_CREDS_UID) {
n->uid = c->uid;
n->mask |= SD_BUS_CREDS_UID;
}
if (c->mask & mask & SD_BUS_CREDS_GID) {
n->gid = c->gid;
n->mask |= SD_BUS_CREDS_GID;
}
if (c->mask & mask & SD_BUS_CREDS_PID) {
n->pid = c->pid;
n->mask |= SD_BUS_CREDS_PID;
}
if (c->mask & mask & SD_BUS_CREDS_TID) {
n->tid = c->tid;
n->mask |= SD_BUS_CREDS_TID;
}
if (c->mask & mask & SD_BUS_CREDS_PID_STARTTIME) {
n->pid_starttime = c->pid_starttime;
n->mask |= SD_BUS_CREDS_PID_STARTTIME;
}
if (c->mask & mask & SD_BUS_CREDS_COMM) {
n->comm = strdup(c->comm);
if (!n->comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_COMM;
}
if (c->mask & mask & SD_BUS_CREDS_TID_COMM) {
n->tid_comm = strdup(c->tid_comm);
if (!n->tid_comm)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_TID_COMM;
}
if (c->mask & mask & SD_BUS_CREDS_EXE) {
n->exe = strdup(c->exe);
if (!n->exe)
return -ENOMEM;
n->mask |= SD_BUS_CREDS_EXE;
}
if (c->mask & mask & SD_BUS_CREDS_CMDLINE) {
n->cmdline = memdup(c->cmdline, c->cmdline_length);
if (!n->cmdline)
return -ENOMEM;
n->cmdline_length = c->cmdline_length;
n->mask |= SD_BUS_CREDS_CMDLINE;
}
if (c->mask & mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID)) {
n->cgroup = strdup(c->cgroup);
if (!n->cgroup)
return -ENOMEM;
n->mask |= mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_OWNER_UID);
}
if (c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)) {
n->capability = memdup(c->capability, c->capability_size);
if (!n->capability)
return -ENOMEM;
n->capability_size = c->capability_size;
n->mask |= c->mask & mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS);
}
if (c->mask & mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
n->audit_session_id = c->audit_session_id;
n->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
}
if (c->mask & mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
n->audit_login_uid = c->audit_login_uid;
n->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
}
/* Get more data */
r = bus_creds_add_more(n, mask,
c->mask & SD_BUS_CREDS_PID ? c->pid : 0,
c->mask & SD_BUS_CREDS_TID ? c->tid : 0);
if (r < 0)
return r;
*ret = n;
n = NULL;
return 0;
}

67
src/libsystemd-bus/bus-creds.h

@ -0,0 +1,67 @@