1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* Embedded Linux library
* Copyright (C) 2011-2014 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "log.h"
#include "util.h"
#include "idle.h"
#include "test-private.h"
#include "dbus.h"
static void do_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
l_info("%s%s", prefix, str);
}
static int create_unix_socket(const char *path)
{
struct sockaddr_un addr;
int fd;
unlink(path);
fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (fd < 0) {
perror("Failed to create bus socket");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Failed to bind to bus socket");
close(fd);
return -1;
}
if (listen(fd, 5) < 0) {
perror("Failed to listen on bus socket");
close(fd);
return -1;
}
return fd;
}
static pid_t fork_broker(int ctl_fd, int log_fd)
{
char *dbus_broker_exec;
pid_t pid;
pid = fork();
if (pid < 0) {
perror("Failed to fork new process");
return -1;
}
dbus_broker_exec = getenv("DBUS_BROKER") ?: "/usr/bin/dbus-broker";
if (pid == 0) {
char **argv, **envp;
char ctl_str[12], log_str[12];
int pos, val;
prctl(PR_SET_PDEATHSIG, SIGTERM);
prctl(PR_SET_DUMPABLE, 0L);
val = fcntl(ctl_fd, F_GETFD);
fcntl(ctl_fd, F_SETFD, val & ~FD_CLOEXEC);
val = fcntl(log_fd, F_GETFD);
fcntl(log_fd, F_SETFD, val & ~FD_CLOEXEC);
snprintf(ctl_str, sizeof(ctl_str), "%d", ctl_fd);
snprintf(log_str, sizeof(log_str), "%d", log_fd);
argv = alloca(sizeof(char *) * 12);
pos = 0;
argv[pos++] = dbus_broker_exec;
argv[pos++] = "--controller";
argv[pos++] = ctl_str;
if (log_fd >= 0) {
argv[pos++] = "--log";
argv[pos++] = log_str;
}
argv[pos++] = "--machine-id";
argv[pos++] = "0123456789abcdef0123456789abcdef";
argv[pos++] = NULL;
envp = alloca(sizeof(char *) * 4);
pos = 0;
envp[pos++] = NULL;
printf("Running command %s\n", argv[0]);
execve(argv[0], argv, envp);
exit(EXIT_FAILURE);
}
close(ctl_fd);
close(log_fd);
return pid;
}
struct cb_data {
start_dbus_cb_t cb;
void *ud;
};
static void add_listener_reply(struct l_dbus_message *reply, void *user_data)
{
struct cb_data *cbd = user_data;
if (l_dbus_message_is_error(reply)) {
l_info("Failed to add listener");
return;
}
l_info("Bus socket added successfully");
if (cbd->cb)
cbd->cb(cbd->ud);
}
static void builder_append(struct l_dbus_message_builder *builder,
const char *signature, ...)
{
va_list args;
va_start(args, signature);
l_dbus_message_builder_append_from_valist(builder, signature, args);
va_end(args);
}
#define POLICY_T_BATCH "bt" "a(btbs)" "a(btssssuutt)" "a(btssssuutt)"
#define POLICY_T "a(u(" POLICY_T_BATCH "))" \
"a(buu(" POLICY_T_BATCH "))" \
"a(ss)" "bs"
static void add_listener(struct l_dbus *dbus, int fd, const char *type,
struct cb_data *cbd)
{
struct l_dbus_message *msg;
struct l_dbus_message_builder *bld;
msg = l_dbus_message_new_method_call(dbus, NULL,
"/org/bus1/DBus/Broker",
"org.bus1.DBus.Broker", "AddListener");
bld = l_dbus_message_builder_new(msg);
builder_append(bld, "oh", "/org/bus1/DBus/Listener/0", fd);
l_dbus_message_builder_enter_variant(bld, "(" POLICY_T ")");
l_dbus_message_builder_enter_struct(bld, POLICY_T);
/* per-uid batches */
l_dbus_message_builder_enter_array(bld, "(u(" POLICY_T_BATCH "))");
l_dbus_message_builder_enter_struct(bld, "u(" POLICY_T_BATCH ")");
/* Fall-back UID */
builder_append(bld, "u", (uint32_t) -1);
l_dbus_message_builder_enter_struct(bld, POLICY_T_BATCH);
/* Default test policy:
* - allow all connections
* - allow everyone to own names
* - allow all sends
* - allow all recvs */
builder_append(bld, POLICY_T_BATCH,
true, UINT64_C(1),
1, true, UINT64_C(1), true, "",
1, true, UINT64_C(1), "", "", "", "", 0, 0,
UINT64_C(0), UINT64_MAX,
1, true, UINT64_C(1), "", "", "", "", 0, 0,
UINT64_C(0), UINT64_MAX);
l_dbus_message_builder_leave_struct(bld);
l_dbus_message_builder_leave_struct(bld);
l_dbus_message_builder_leave_array(bld);
/* per-gid and uid-range batches */
l_dbus_message_builder_enter_array(bld, "(buu(" POLICY_T_BATCH "))");
l_dbus_message_builder_leave_array(bld);
/* empty SELinux policy */
l_dbus_message_builder_enter_array(bld, "(ss)");
l_dbus_message_builder_leave_array(bld);
/* disable AppArmor */
builder_append(bld, "b", false);
/* mark as system or session bus */
builder_append(bld, "s", type);
l_dbus_message_builder_leave_struct(bld);
l_dbus_message_builder_leave_variant(bld);
msg = l_dbus_message_builder_finalize(bld);
l_dbus_message_builder_destroy(bld);
l_dbus_send_with_reply(dbus, msg, add_listener_reply, cbd, l_free);
}
pid_t start_dbus(const char *address, start_dbus_cb_t cb, void *user_data,
bool enable_debug)
{
struct cb_data *cbd;
struct l_dbus *ctl_dbus;
int err, bus_fd, ctl_fds[2];
pid_t pid;
if (!address)
return -1;
err = socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
0, ctl_fds);
if (err < 0) {
perror("Failed to create controller socket pair");
return -1;
}
ctl_dbus = l_dbus_new_private(ctl_fds[0]);
if (!ctl_dbus) {
close(ctl_fds[0]);
return -1;
}
if (enable_debug)
l_dbus_set_debug(ctl_dbus, do_debug, "[dbus-broker] ", NULL);
pid = fork_broker(ctl_fds[1], -1);
if (pid < 0) {
close(ctl_fds[0]);
l_dbus_destroy(ctl_dbus);
return -1;
}
if (strlen(address) < 11 || strncmp(address, "unix:path=", 10)) {
kill(pid, SIGKILL);
close(ctl_fds[0]);
l_dbus_destroy(ctl_dbus);
return -1;
}
bus_fd = create_unix_socket(address + 10);
if (bus_fd < 0) {
kill(pid, SIGKILL);
close(ctl_fds[0]);
l_dbus_destroy(ctl_dbus);
ctl_dbus = NULL;
return -1;
}
if (enable_debug)
l_info("Policy signature: %s", "(" POLICY_T ")");
cbd = l_new(struct cb_data, 1);
cbd->cb = cb;
cbd->ud = user_data;
add_listener(ctl_dbus, bus_fd, "test", cbd);
close(bus_fd);
return pid;
}
|