| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
#include "sd-future.h" |
| 7 |
|
| 8 |
#include "bus-dump.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "pidref.h" |
| 11 |
#include "process-util.h" |
| 12 |
#include "socket-util.h" |
| 13 |
#include "tests.h" |
| 14 |
#include "user-util.h" |
| 15 |
|
| 16 |
static bool gid_list_contained(const gid_t *a, size_t n, const gid_t *b, size_t m) { |
| 17 |
ASSERT_TRUE(a || n == 0); |
| 18 |
ASSERT_TRUE(b || m == 0); |
| 19 |
|
| 20 |
/* Checks if every entry in a[] is also in b[] */ |
| 21 |
|
| 22 |
for (size_t i = 0; i < n; i++) { |
| 23 |
size_t j; |
| 24 |
|
| 25 |
for (j = 0; j < m; j++) |
| 26 |
if (a[i] == b[j]) |
| 27 |
break; |
| 28 |
|
| 29 |
if (j >= m) |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
static bool gid_list_same(const gid_t *a, size_t n, const gid_t *b, size_t m) { |
| 37 |
return gid_list_contained(a, n, b, m) && |
| 38 |
gid_list_contained(b, m, a, n); |
| 39 |
} |
| 40 |
|
| 41 |
static int server(void *userdata) { |
| 42 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 43 |
_cleanup_close_ int listen_fd = PTR_TO_INT(userdata), fd = -EBADF; |
| 44 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL; |
| 45 |
_cleanup_free_ char *our_comm = NULL; |
| 46 |
sd_id128_t id; |
| 47 |
int r; |
| 48 |
|
| 49 |
ASSERT_OK(sd_id128_randomize(&id)); |
| 50 |
|
| 51 |
ASSERT_OK(fd = sd_fiber_accept(listen_fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK)); |
| 52 |
|
| 53 |
ASSERT_OK(sd_bus_new(&bus)); |
| 54 |
ASSERT_OK(sd_bus_set_fd(bus, fd, fd)); |
| 55 |
TAKE_FD(fd); |
| 56 |
ASSERT_OK(sd_bus_set_server(bus, true, id)); |
| 57 |
ASSERT_OK(sd_bus_negotiate_creds(bus, 1, SD_BUS_CREDS_EUID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_DESCRIPTION|SD_BUS_CREDS_PIDFD|SD_BUS_CREDS_SUPPLEMENTARY_GIDS)); |
| 58 |
ASSERT_OK(sd_bus_start(bus)); |
| 59 |
ASSERT_OK(sd_bus_get_owner_creds(bus, SD_BUS_CREDS_EUID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_DESCRIPTION|SD_BUS_CREDS_PIDFD|SD_BUS_CREDS_SUPPLEMENTARY_GIDS, &c)); |
| 60 |
|
| 61 |
bus_creds_dump(c, /* f= */ NULL, /* terse= */ false); |
| 62 |
|
| 63 |
uid_t u; |
| 64 |
ASSERT_OK(sd_bus_creds_get_euid(c, &u)); |
| 65 |
ASSERT_EQ(u, getuid()); |
| 66 |
|
| 67 |
gid_t g; |
| 68 |
ASSERT_OK(sd_bus_creds_get_egid(c, &g)); |
| 69 |
ASSERT_EQ(g, getgid()); |
| 70 |
|
| 71 |
pid_t pid; |
| 72 |
ASSERT_OK(sd_bus_creds_get_pid(c, &pid)); |
| 73 |
ASSERT_EQ(pid, getpid_cached()); |
| 74 |
|
| 75 |
int pidfd = -EBADF; |
| 76 |
if (sd_bus_creds_get_pidfd_dup(c, &pidfd) >= 0) { |
| 77 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 78 |
|
| 79 |
ASSERT_OK(pidref_set_pidfd_take(&pidref, pidfd)); |
| 80 |
ASSERT_TRUE(pidref_is_self(&pidref)); |
| 81 |
} |
| 82 |
|
| 83 |
const gid_t *gl = NULL; |
| 84 |
int n = sd_bus_creds_get_supplementary_gids(c, &gl); |
| 85 |
|
| 86 |
if (n >= 0) { |
| 87 |
_cleanup_free_ gid_t *gg = NULL; |
| 88 |
ASSERT_OK(r = getgroups_alloc(&gg)); |
| 89 |
ASSERT_TRUE(gid_list_same(gl, n, gg, r)); |
| 90 |
} |
| 91 |
|
| 92 |
const char *comm; |
| 93 |
ASSERT_OK(sd_bus_creds_get_comm(c, &comm)); |
| 94 |
ASSERT_OK(pid_get_comm(0, &our_comm)); |
| 95 |
ASSERT_STREQ(comm, our_comm); |
| 96 |
|
| 97 |
const char *description; |
| 98 |
ASSERT_OK(sd_bus_creds_get_description(c, &description)); |
| 99 |
ASSERT_STREQ(description, "wuffwuff"); |
| 100 |
|
| 101 |
for (;;) { |
| 102 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 103 |
|
| 104 |
ASSERT_OK(r = sd_bus_process(bus, &m)); |
| 105 |
|
| 106 |
if (r == 0) { |
| 107 |
ASSERT_OK(sd_bus_wait(bus, UINT64_MAX)); |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if (m && sd_bus_message_is_method_call(m, "foo.foo", "Foo") > 0) { |
| 112 |
ASSERT_OK(sd_bus_reply_method_return(m, "s", "bar")); |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
static int client(void *userdata) { |
| 121 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 122 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 123 |
const char *z; |
| 124 |
|
| 125 |
ASSERT_OK(sd_bus_new(&bus)); |
| 126 |
ASSERT_OK(sd_bus_set_description(bus, "wuffwuff")); |
| 127 |
ASSERT_OK(sd_bus_set_address(bus, userdata)); |
| 128 |
ASSERT_OK(sd_bus_attach_event(bus, sd_fiber_get_event(), 0)); |
| 129 |
ASSERT_OK(sd_bus_start(bus)); |
| 130 |
|
| 131 |
ASSERT_OK(sd_bus_call_method(bus, "foo.foo", "/foo", "foo.foo", "Foo", NULL, &reply, "s", "foo")); |
| 132 |
|
| 133 |
ASSERT_OK(sd_bus_message_read(reply, "s", &z)); |
| 134 |
ASSERT_STREQ(z, "bar"); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
TEST(description) { |
| 140 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 141 |
_cleanup_(sd_future_unrefp) sd_future *f_server = NULL, *f_client = NULL; |
| 142 |
_cleanup_free_ char *a = NULL; |
| 143 |
_cleanup_close_ int fd = -EBADF; |
| 144 |
union sockaddr_union sa = { |
| 145 |
.un.sun_family = AF_UNIX, |
| 146 |
}; |
| 147 |
socklen_t salen; |
| 148 |
|
| 149 |
ASSERT_OK_ERRNO(fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)); |
| 150 |
ASSERT_OK_ERRNO(bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path))); /* force auto-bind */ |
| 151 |
ASSERT_OK_ERRNO(listen(fd, 1)); |
| 152 |
|
| 153 |
salen = sizeof(sa); |
| 154 |
ASSERT_OK_ERRNO(getsockname(fd, &sa.sa, &salen)); |
| 155 |
ASSERT_GE(salen, offsetof(struct sockaddr_un, sun_path)); |
| 156 |
ASSERT_EQ(sa.un.sun_path[0], 0); |
| 157 |
|
| 158 |
ASSERT_OK(asprintf(&a, "unix:abstract=%s", sa.un.sun_path + 1)); |
| 159 |
|
| 160 |
ASSERT_OK(sd_event_new(&e)); |
| 161 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 162 |
|
| 163 |
ASSERT_OK(sd_fiber_new(e, "server", server, INT_TO_PTR(fd), /* destroy= */ NULL, &f_server)); |
| 164 |
TAKE_FD(fd); |
| 165 |
|
| 166 |
ASSERT_OK(sd_fiber_new(e, "client", client, a, /* destroy= */ NULL, &f_client)); |
| 167 |
|
| 168 |
ASSERT_OK(sd_event_loop(e)); |
| 169 |
|
| 170 |
ASSERT_OK(sd_future_result(f_server)); |
| 171 |
ASSERT_OK(sd_future_result(f_client)); |
| 172 |
} |
| 173 |
|
| 174 |
DEFINE_TEST_MAIN(LOG_INFO); |