Debian Codegraph @

src/broker/main.c

raw
1 /*
2 * D-Bus Broker Main Entry
3 */
4
5 #include <c-stdaux.h>
6 #include <getopt.h>
7 #include <limits.h>
8 #include <stdlib.h>
9 #include <sys/prctl.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
12 #include "broker/broker.h"
13 #include "broker/main.h"
14 #include "util/audit.h"
15 #include "util/error.h"
16 #include "util/log.h"
17 #include "util/selinux.h"
18 #include "util/string.h"
19
20 bool main_arg_audit = false;
21 int main_arg_controller = 3;
22 int main_arg_log = -1;
23 const char *main_arg_machine_id = NULL;
24 uint64_t main_arg_max_bytes = 512 * 1024 * 1024;
25 uint64_t main_arg_max_fds = 128;
26 uint64_t main_arg_max_matches = 16 * 1024;
27 uint64_t main_arg_max_objects = 16 * 1024 * 1024;
28
29 static void help(void) {
30 printf("%s [GLOBALS...] ...\n\n"
31 "Linux D-Bus Message Broker\n\n"
32 " -h --help Show this help\n"
33 " --version Show package version\n"
34 " --audit Log to the audit subsystem\n"
35 " --controller FD Specify controller file-descriptor\n"
36 " --log FD Provide logging socket\n"
37 " --machine-id MACHINE_ID Machine ID of the current machine\n"
38 " --max-bytes BYTES Maximum number of bytes each user may allocate in the broker\n"
39 " --max-fds FDS Maximum number of file descriptors each user may allocate in the broker\n"
40 " --max-matches MATCHES Maximum number of match rules each user may allocate in the broker\n"
41 " --max-objects OBJECTS Maximum total number of names, peers, pending replies, etc each user may allocate in the broker\n"
42 , program_invocation_short_name);
43 }
44
45 static int parse_argv(int argc, char *argv[]) {
46 enum {
47 ARG_VERSION = 0x100,
48 ARG_AUDIT,
49 ARG_CONTROLLER,
50 ARG_LOG,
51 ARG_MACHINE_ID,
52 ARG_MAX_BYTES,
53 ARG_MAX_FDS,
54 ARG_MAX_MATCHES,
55 ARG_MAX_OBJECTS,
56 };
57 static const struct option options[] = {
58 { "help", no_argument, NULL, 'h' },
59 { "version", no_argument, NULL, ARG_VERSION },
60 { "audit", no_argument, NULL, ARG_AUDIT },
61 { "controller", required_argument, NULL, ARG_CONTROLLER },
62 { "log", required_argument, NULL, ARG_LOG },
63 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
64 { "max-bytes", required_argument, NULL, ARG_MAX_BYTES },
65 { "max-fds", required_argument, NULL, ARG_MAX_FDS },
66 { "max-matches", required_argument, NULL, ARG_MAX_MATCHES },
67 { "max-objects", required_argument, NULL, ARG_MAX_OBJECTS },
68 {}
69 };
70 int r, c;
71
72 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
73 switch (c) {
74 case 'h':
75 help();
76 return MAIN_EXIT;
77
78 case ARG_VERSION:
79 printf("dbus-broker %d\n", PACKAGE_VERSION);
80 return MAIN_EXIT;
81
82 case ARG_AUDIT:
83 main_arg_audit = true;
84 break;
85
86 case ARG_CONTROLLER: {
87 unsigned long vul;
88 char *end;
89
90 errno = 0;
91 vul = strtoul(optarg, &end, 10);
92 if (errno != 0 || *end || optarg == end || vul > INT_MAX) {
93 fprintf(stderr, "%s: invalid controller file-descriptor -- '%s'\n", program_invocation_name, optarg);
94 return MAIN_FAILED;
95 }
96
97 main_arg_controller = vul;
98 break;
99 }
100
101 case ARG_LOG: {
102 unsigned long vul;
103 char *end;
104
105 errno = 0;
106 vul = strtoul(optarg, &end, 10);
107 if (errno != 0 || *end || optarg == end || vul > INT_MAX) {
108 fprintf(stderr, "%s: invalid log file-descriptor -- '%s'\n", program_invocation_name, optarg);
109 return MAIN_FAILED;
110 }
111
112 main_arg_log = vul;
113 break;
114 }
115
116 case ARG_MACHINE_ID: {
117 if (strlen(optarg) != 32) {
118 fprintf(stderr, "%s: invalid machine ID -- '%s'\n", program_invocation_name, optarg);
119 return MAIN_FAILED;
120 }
121
122 main_arg_machine_id = optarg;
123 break;
124 }
125
126 case ARG_MAX_BYTES:
127 r = util_strtou64(&main_arg_max_bytes, optarg);
128 if (r) {
129 fprintf(stderr, "%s: invalid max number of bytes -- '%s'\n", program_invocation_name, optarg);
130 return MAIN_FAILED;
131 }
132
133 break;
134
135 case ARG_MAX_FDS:
136 r = util_strtou64(&main_arg_max_fds, optarg);
137 if (r) {
138 fprintf(stderr, "%s: invalid max number of fds -- '%s'\n", program_invocation_name, optarg);
139 return MAIN_FAILED;
140 }
141
142 break;
143
144 case ARG_MAX_MATCHES:
145 r = util_strtou64(&main_arg_max_matches, optarg);
146 if (r) {
147 fprintf(stderr, "%s: invalid max number of matches -- '%s'\n", program_invocation_name, optarg);
148 return MAIN_FAILED;
149 }
150
151 break;
152
153 case ARG_MAX_OBJECTS:
154 r = util_strtou64(&main_arg_max_objects, optarg);
155 if (r) {
156 fprintf(stderr, "%s: invalid max number of objects -- '%s'\n", program_invocation_name, optarg);
157 return MAIN_FAILED;
158 }
159
160 break;
161
162 case '?':
163 /* getopt_long() prints warning */
164 return MAIN_FAILED;
165
166 default:
167 return error_origin(-EINVAL);
168 }
169 }
170
171 if (optind != argc) {
172 fprintf(stderr, "%s: invalid arguments -- '%s'\n", program_invocation_name, argv[optind]);
173 return MAIN_FAILED;
174 }
175
176 /*
177 * Verify that the passed FDs exist. Preferably, we would not care
178 * and simply fail later on. However, the FD-number might be
179 * used by one of our other FDs (signalfd, epollfd, ...), and thus we
180 * might trigger assertions on their behavior, which we better avoid.
181 */
182
183 /* verify log-fd is DGRAM or STREAM */
184 if (main_arg_log >= 0) {
185 socklen_t n;
186 int v1, v2;
187
188 n = sizeof(v1);
189 r = getsockopt(main_arg_log, SOL_SOCKET, SO_DOMAIN, &v1, &n);
190 n = sizeof(v2);
191 r = r ?: getsockopt(main_arg_log, SOL_SOCKET, SO_TYPE, &v2, &n);
192
193 if (r < 0) {
194 if (errno != EBADF && errno != ENOTSOCK)
195 return error_origin(-errno);
196
197 fprintf(stderr, "%s: log file-descriptor not a socket -- '%d'\n", program_invocation_name, main_arg_log);
198 return MAIN_FAILED;
199 } else if (v1 != AF_UNIX || (v2 != SOCK_DGRAM && v2 != SOCK_STREAM)) {
200 fprintf(stderr, "%s: socket type of log file-descriptor not supported -- '%d'\n", program_invocation_name, main_arg_log);
201 return MAIN_FAILED;
202 }
203 }
204
205 /* verify controller-fd is STREAM */
206 {
207 socklen_t n;
208 int v1, v2;
209
210 n = sizeof(v1);
211 r = getsockopt(main_arg_controller, SOL_SOCKET, SO_DOMAIN, &v1, &n);
212 n = sizeof(v2);
213 r = r ?: getsockopt(main_arg_controller, SOL_SOCKET, SO_TYPE, &v2, &n);
214
215 if (r < 0) {
216 if (errno != EBADF && errno != ENOTSOCK)
217 return error_origin(-errno);
218
219 fprintf(stderr, "%s: controller file-descriptor not a socket -- '%d'\n", program_invocation_name, main_arg_controller);
220 return MAIN_FAILED;
221 } else if (v1 != AF_UNIX || v2 != SOCK_STREAM) {
222 fprintf(stderr, "%s: socket type of controller file-descriptor not supported -- '%d'\n", program_invocation_name, main_arg_controller);
223 return MAIN_FAILED;
224 }
225 }
226
227 /* verify that a machine ID was passed */
228 {
229 if (!main_arg_machine_id) {
230 fprintf(stderr, "%s: the machine ID argument is mandatory\n", program_invocation_name);
231 return MAIN_FAILED;
232 }
233 }
234
235 return 0;
236 }
237
238 static int setup(Log *logp) {
239 socklen_t z;
240 int r, log_type;
241
242 /*
243 * We never spawn external applications from within the broker itself,
244 * so clear the ambient set, as it is never needed. This is meant as
245 * safety measure to guarantee our capabilities are not inherited by
246 * possible exploits.
247 */
248 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
249 if (r < 0)
250 return error_origin(-errno);
251
252 if (main_arg_log >= 0) {
253 z = sizeof(log_type);
254 r = getsockopt(main_arg_log, SOL_SOCKET, SO_TYPE, &log_type, &z);
255 if (r < 0)
256 return error_origin(-errno);
257 }
258
259 if (main_arg_log < 0)
260 log_init(logp);
261 else if (log_type == SOCK_STREAM)
262 log_init_stderr(logp, main_arg_log);
263 else if (log_type == SOCK_DGRAM)
264 log_init_journal(logp, main_arg_log);
265 else
266 return error_origin(-ENOTRECOVERABLE);
267
268 /* XXX: make this run-time optional */
269 log_set_lossy(logp, true);
270
271 return 0;
272 }
273
274 static int run(Log *log) {
275 _c_cleanup_(broker_freep) Broker *broker = NULL;
276 int r;
277
278 r = broker_new(&broker, log, main_arg_machine_id, main_arg_controller, main_arg_max_bytes, main_arg_max_fds, main_arg_max_matches, main_arg_max_objects);
279 if (!r)
280 r = broker_run(broker);
281
282 return error_trace(r);
283 }
284
285 int main(int argc, char **argv) {
286 Log log = LOG_NULL;
287 int r;
288
289 r = parse_argv(argc, argv);
290 if (r)
291 goto exit;
292
293 r = setup(&log);
294 if (r)
295 goto exit;
296
297 if (main_arg_audit) {
298 r = util_audit_init_global();
299 if (r) {
300 r = error_fold(r);
301 goto exit;
302 }
303 }
304
305 r = bus_selinux_init_global(&log);
306 if (r) {
307 r = error_fold(r);
308 goto exit;
309 }
310
311 r = run(&log);
312
313 exit:
314 bus_selinux_deinit_global();
315 util_audit_deinit_global();
316 log_deinit(&log);
317
318 r = error_trace(r);
319 return (r == 0 || r == MAIN_EXIT) ? 0 : 1;
320 }