sd-bus: use SO_PEERGROUPS when available to identify groups of peer

This commit is contained in:
Lennart Poettering 2017-12-30 15:19:15 +01:00
parent 2fce06b0d6
commit 18ac4643cb
4 changed files with 31 additions and 3 deletions

View file

@ -555,15 +555,16 @@ _public_ int sd_bus_get_name_creds(
static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) { static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL; _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
pid_t pid = 0; pid_t pid = 0;
bool do_label; bool do_label, do_groups;
int r; int r;
assert(bus); assert(bus);
do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT); do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
do_groups = bus->n_groups != (size_t) -1 && (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS);
/* Avoid allocating anything if we have no chance of returning useful data */ /* Avoid allocating anything if we have no chance of returning useful data */
if (!bus->ucred_valid && !do_label) if (!bus->ucred_valid && !do_label && !do_groups)
return -ENODATA; return -ENODATA;
c = bus_creds_new(); c = bus_creds_new();
@ -595,6 +596,16 @@ static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **
c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT; c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
} }
if (do_groups) {
c->supplementary_gids = newdup(gid_t, bus->groups, bus->n_groups);
if (!c->supplementary_gids)
return -ENOMEM;
c->n_supplementary_gids = bus->n_groups;
c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
}
r = bus_creds_add_more(c, mask, pid, 0); r = bus_creds_add_more(c, mask, pid, 0);
if (r < 0) if (r < 0)
return r; return r;

View file

@ -261,6 +261,8 @@ struct sd_bus {
struct ucred ucred; struct ucred ucred;
char *label; char *label;
gid_t *groups;
size_t n_groups;
uint64_t creds_mask; uint64_t creds_mask;

View file

@ -603,14 +603,27 @@ static void bus_get_peercred(sd_bus *b) {
int r; int r;
assert(b); assert(b);
assert(!b->ucred_valid);
assert(!b->label);
assert(b->n_groups == (size_t) -1);
/* Get the peer for socketpair() sockets */ /* Get the peer for socketpair() sockets */
b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0; b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
/* Get the SELinux context of the peer */ /* Get the SELinux context of the peer */
r = getpeersec(b->input_fd, &b->label); r = getpeersec(b->input_fd, &b->label);
if (r < 0 && r != -EOPNOTSUPP) if (r < 0 && !IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
log_debug_errno(r, "Failed to determine peer security context: %m"); log_debug_errno(r, "Failed to determine peer security context: %m");
/* Get the list of auxiliary groups of the peer */
r = getpeergroups(b->input_fd, &b->groups);
if (r < 0) {
if (!IN_SET(r, -EOPNOTSUPP, -ENOPROTOOPT))
log_debug_errno(r, "Failed to determine peer groups list: %m");
b->n_groups = (size_t) -1;
} else
b->n_groups = (size_t) r;
} }
static int bus_socket_start_auth_client(sd_bus *b) { static int bus_socket_start_auth_client(sd_bus *b) {

View file

@ -134,6 +134,7 @@ static void bus_free(sd_bus *b) {
bus_close_fds(b); bus_close_fds(b);
free(b->label); free(b->label);
free(b->groups);
free(b->rbuffer); free(b->rbuffer);
free(b->unique_name); free(b->unique_name);
free(b->auth_buffer); free(b->auth_buffer);
@ -185,6 +186,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
r->hello_flags |= KDBUS_HELLO_ACCEPT_FD; r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
r->attach_flags |= KDBUS_ATTACH_NAMES; r->attach_flags |= KDBUS_ATTACH_NAMES;
r->original_pid = getpid_cached(); r->original_pid = getpid_cached();
r->n_groups = (size_t) -1;
assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0); assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);