Debian Codegraph @

src/dbus/sasl.c

raw
1 /*
2 * Server-Side SASL Parser
3 *
4 * This wraps the SASL state machine. Only the EXTERNAL mechanism is supported.
5 * The broker knows the UID of a peer from the peer credentials obtained from its
6 * socket, which is used for authenticating. For the purposes of the broker SASL
7 * is a no-op needed only for compatibility.
8 *
9 * The SASL exchange does not need to be synchronous, so a client can typically
10 * implement SASL by simply prepending the string
11 * "\0AUTH EXTERNAL\r\nDATA\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n"
12 * to the first message it sends, and discarding the first
13 * strlen("DATA\r\nOK 0123456789abcdef0123456789abcdef\r\nAGREE_UNIX_FD\r\n")
14 * bytes from the beginning of the first message it receives.
15 */
16
17 #include <c-stdaux.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include "dbus/sasl.h"
22 #include "util/error.h"
23 #include "util/string.h"
24
25 static void sasl_split(const char *input, size_t n_input,
26 const char **cmd, size_t *n_cmd,
27 const char **arg, size_t *n_arg) {
28 /*
29 * Split @cmd into a command and argument. This splits after the
30 * first occurrence of whitespace characters. If the argument is empty
31 * it is treated as non-existant (which is what legacy D-Bus
32 * applications expect).
33 */
34
35 *cmd = input;
36 /* memchr(NULL, ..) is UB */
37 *arg = !n_input ? NULL : memchr(input, ' ', n_input);
38
39 if (*arg) {
40 *n_cmd = *arg - input;
41 *n_arg = n_input - *n_cmd;
42
43 do {
44 ++*arg;
45 --*n_arg;
46 } while (*n_arg && **arg == ' ');
47 } else {
48 *n_cmd = n_input;
49 *n_arg = 0;
50 }
51 }
52
53 /**
54 * sasl_client_init() - initialize SASL client
55 * @sasl: the client to operate on
56 *
57 * This initializes the SASL client object @sasl.
58 */
59 void sasl_client_init(SASLClient *sasl) {
60 sasl->state = SASL_CLIENT_STATE_INIT;
61 }
62
63 /**
64 * sasl_client_deinit() - deinitialize SASL client
65 * @sasl: the client to operate on
66 *
67 * This deinitializes the SASL client object @sasl. It is safe to call deinit several times.
68 */
69 void sasl_client_deinit(SASLClient *sasl) {
70 *sasl = (SASLClient){};
71 }
72
73 /**
74 * sasl_client_dispatch() - dispatch SASL client
75 * @sasl: the client to dispatch
76 * @input: the input to consume
77 * @n_input: the length of the input
78 * @outputp: return pointer to the output
79 * @n_outputp: return pointer to the size of the output
80 *
81 * This dispatches the SASL object, consuming the given input string and providing a pointer to
82 * an output string to send, if applicable. Neither input nor output strings are nul-terminated.
83 *
84 * We hardcode the EXTERNAL mechanism, relying on the UDS providing the credentials, so not
85 * attaching them ourselves. Note that we do not necessarily know the UID the remote end will
86 * see us as, in the case of crossing a namespace boundary, so providing an UID is not
87 * straight-forward. We also hard-code the requirement for unix FD support.
88 *
89 * If either the EXTERNAL mechanism or FD support is not supported by the remote side, we fail
90 * the negotiation, we do not fall back to anything else.
91 *
92 * Return: SASL_E_FAILURE if the server did not support what we requested,
93 * SASL_E_PROTOCOL_VIOLATION if the server violated the protocol, or
94 * 0 on success.
95 */
96 int sasl_client_dispatch(SASLClient *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp) {
97 static const char request[] = {
98 "\0AUTH EXTERNAL\r\n"
99 "DATA\r\n"
100 "NEGOTIATE_UNIX_FD\r\n"
101 "BEGIN"
102 };
103 const char *cmd, *arg, *output = NULL;
104 size_t n_cmd, n_arg, n_output = 0;
105
106 sasl_split(input, n_input, &cmd, &n_cmd, &arg, &n_arg);
107
108 switch (sasl->state) {
109 case SASL_CLIENT_STATE_INIT:
110 if (cmd)
111 return SASL_E_PROTOCOL_VIOLATION;
112
113 output = request;
114 n_output = sizeof(request) - 1;
115
116 sasl->state = SASL_CLIENT_STATE_AUTH;
117 break;
118
119 case SASL_CLIENT_STATE_AUTH:
120 if (!cmd)
121 break;
122
123 if (n_cmd != strlen("DATA") || strncmp(cmd, "DATA", n_cmd))
124 return SASL_E_FAILURE;
125 if (n_arg)
126 return SASL_E_PROTOCOL_VIOLATION;
127
128 sasl->state = SASL_CLIENT_STATE_DATA;
129 break;
130
131 case SASL_CLIENT_STATE_DATA:
132 if (!cmd)
133 break;
134
135 if (n_cmd != strlen("OK") || strncmp(cmd, "OK", n_cmd))
136 return SASL_E_FAILURE;
137 if (n_arg != strlen("0123456789abcdef0123456789abcdef"))
138 return SASL_E_PROTOCOL_VIOLATION;
139
140 sasl->state = SASL_CLIENT_STATE_UNIX_FD;
141 break;
142
143 case SASL_CLIENT_STATE_UNIX_FD:
144 if (!cmd)
145 break;
146
147 if (n_cmd != strlen("AGREE_UNIX_FD") || strncmp(cmd, "AGREE_UNIX_FD", n_cmd))
148 return SASL_E_FAILURE;
149 if (n_arg)
150 return SASL_E_PROTOCOL_VIOLATION;
151
152 sasl->state = SASL_CLIENT_STATE_DONE;
153 break;
154
155 default:
156 return error_origin(-ENOTRECOVERABLE);
157 }
158
159 *outputp = output;
160 *n_outputp = n_output;
161 return 0;
162 }
163
164 /**
165 * sasl_server_init() - initialize SASL server
166 * @sasl: the server to operate on
167 * @uid: the UID of the connecting client
168 * @guid: the GUID associated with the listening socket
169 *
170 * This initializes the SASL server object @sasl, for authenticating with UID
171 * @uid. The GUID @guid is used to identify the connection. This should be
172 * unique per listening socket, but shared beteewn all connections to the same
173 * socket.
174 */
175 void sasl_server_init(SASLServer *sasl, uid_t uid, const char *guid) {
176 *sasl = (SASLServer){};
177 sasl->uid = uid;
178 sasl->ok_response[0] = 'O';
179 sasl->ok_response[1] = 'K';
180 sasl->ok_response[2] = ' ';
181 string_to_hex(guid, 16, &sasl->ok_response[3]);
182 };
183
184 /**
185 * sasl_server_deinit() - deinitialize SASL server
186 * @sasl: the server to operate on
187 *
188 * This deinitializes the SASL server object @sasl. It is safe to call deinit several times.
189 */
190 void sasl_server_deinit(SASLServer *sasl) {
191 *sasl = (SASLServer){};
192 };
193
194 static void sasl_server_handle_data(SASLServer *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp) {
195 char hexbuf[2 * C_DECIMAL_MAX(uint32_t) + 1];
196 char uidbuf[C_DECIMAL_MAX(uint32_t) + 1];
197 bool failed = false;
198 int n;
199
200 /*
201 * The EXTERNAL mechanism requires the UID to authenticate as as
202 * argument. If omitted, the server deduces the UID from the socket, in
203 * which case we rely on the kernel to verify its correctness.
204 */
205 if (n_input) {
206 n = snprintf(uidbuf, sizeof(uidbuf), "%" PRIu32, sasl->uid);
207 c_assert(n >= 0 && (size_t)n < sizeof(uidbuf));
208
209 string_to_hex(uidbuf, n, hexbuf);
210 if (n_input != 2 * (size_t)n || memcmp(input, hexbuf, 2 * n))
211 failed = true;
212 }
213
214 if (failed) {
215 *outputp = "REJECTED EXTERNAL";
216 *n_outputp = strlen("REJECTED EXTERNAL");
217 sasl->state = SASL_SERVER_STATE_AUTH;
218 } else {
219 *outputp = sasl->ok_response;
220 *n_outputp = sizeof(sasl->ok_response);
221 sasl->state = SASL_SERVER_STATE_AUTHENTICATED;
222 }
223 }
224
225 static void sasl_server_handle_auth(SASLServer *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp) {
226 const char *protocol, *arg;
227 size_t n_protocol, n_arg;
228
229 sasl_split(input, n_input, &protocol, &n_protocol, &arg, &n_arg);
230
231 if (n_protocol == strlen("EXTERNAL") && !strncmp(protocol, "EXTERNAL", n_protocol)) {
232 if (n_arg)
233 return sasl_server_handle_data(sasl, arg, n_arg, outputp, n_outputp);
234
235 *outputp = "DATA";
236 *n_outputp = strlen("DATA");
237 sasl->state = SASL_SERVER_STATE_CHALLENGE;
238 } else {
239 *outputp = "REJECTED EXTERNAL";
240 *n_outputp = strlen("REJECTED EXTERNAL");
241 sasl->state = SASL_SERVER_STATE_AUTH;
242 }
243 }
244
245 /**
246 * sasl_server_dispatch() - dispatch SASL server
247 * @sasl: the server to dispatch
248 * @input: the input to consume
249 * @n_input: the length of the input
250 * @outputp: return pointer to the output
251 * @n_outputp: return pointer to the size of the output
252 *
253 * This dispatches the SASL object, consuming the given input string and providing a pointer to
254 * an output string to send, if applicable. Neither input nor output strings are nul-terminated.
255 *
256 * Only the EXTERNAL mechanism is supported, so if the client tries to use anything else, we
257 * reject that and inform them that only EXTERNAL is supported. If the client provides their
258 * UID, then we verify that this is as expected, but if it is ommitted, we use the expected
259 * one.
260 *
261 * We will optionally allow unix FD support to be negotiated if the client requests that.
262 *
263 * Note that even though each output string has a fixed size, the server will potentially
264 * produce an unbounded amount of output over all, as it will only terminate the exchange
265 * in case it succeeded or the protocol was violated.
266 *
267 * Return: SASL_E_PROTOCOL_VIOLATION if the client violated the protocol, or 0 on success.
268 */
269 int sasl_server_dispatch(SASLServer *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp) {
270 const char *cmd, *arg;
271 size_t n_cmd, n_arg;
272
273 sasl_split(input, n_input, &cmd, &n_cmd, &arg, &n_arg);
274
275 switch (sasl->state) {
276 case SASL_SERVER_STATE_INIT:
277 /* first byte must be the zero-byte */
278 if (n_cmd < 1 || *cmd)
279 return SASL_E_PROTOCOL_VIOLATION;
280
281 --n_cmd;
282 ++cmd;
283 /* fallthrough */
284 case SASL_SERVER_STATE_AUTH:
285 if (n_cmd == strlen("AUTH") && !strncmp(cmd, "AUTH", n_cmd)) {
286 sasl_server_handle_auth(sasl, arg, n_arg, outputp, n_outputp);
287 } else if (n_cmd == strlen("ERROR") && !strncmp(cmd, "ERROR", n_cmd)) {
288 *outputp = "REJECTED EXTERNAL";
289 *n_outputp = strlen("REJECTED EXTERNAL");
290 sasl->state = SASL_SERVER_STATE_AUTH;
291 } else if (n_cmd == strlen("BEGIN") && !strncmp(cmd, "BEGIN", n_cmd) && !n_arg) {
292 return SASL_E_PROTOCOL_VIOLATION;
293 } else {
294 *outputp = "ERROR";
295 *n_outputp = strlen("ERROR");
296 }
297
298 break;
299
300 case SASL_SERVER_STATE_CHALLENGE:
301 if (n_cmd == strlen("DATA") && !strncmp(cmd, "DATA", n_cmd)) {
302 sasl_server_handle_data(sasl, arg, n_arg, outputp, n_outputp);
303 } else if ((n_cmd == strlen("ERROR") && !strncmp(cmd, "ERROR", n_cmd)) ||
304 (n_cmd == strlen("CANCEL") && !strncmp(cmd, "CANCEL", n_cmd) && !n_arg)) {
305 *outputp = "REJECTED EXTERNAL";
306 *n_outputp = strlen("REJECTED EXTERNAL");
307 sasl->state = SASL_SERVER_STATE_AUTH;
308 } else if (n_cmd == strlen("BEGIN") && !strncmp(cmd, "BEGIN", n_cmd) && !n_arg) {
309 return SASL_E_PROTOCOL_VIOLATION;
310 } else {
311 *outputp = "ERROR";
312 *n_outputp = strlen("ERROR");
313 }
314
315 break;
316
317 case SASL_SERVER_STATE_AUTHENTICATED:
318 case SASL_SERVER_STATE_NEGOTIATED_FDS:
319 if (n_cmd == strlen("NEGOTIATE_UNIX_FD") && !strncmp(cmd, "NEGOTIATE_UNIX_FD", n_cmd) && !n_arg) {
320 *outputp = "AGREE_UNIX_FD";
321 *n_outputp = strlen("AGREE_UNIX_FD");
322 sasl->fds_allowed = true;
323 sasl->state = SASL_SERVER_STATE_NEGOTIATED_FDS;
324 } else if (n_cmd == strlen("BEGIN") && !strncmp(cmd, "BEGIN", n_cmd) && !n_arg) {
325 *outputp = NULL;
326 *n_outputp = 0;
327 sasl->state = SASL_SERVER_STATE_DONE;
328 } else if ((n_cmd == strlen("ERROR") && !strncmp(cmd, "ERROR", n_cmd)) ||
329 (n_cmd == strlen("CANCEL") && !strncmp(cmd, "CANCEL", n_cmd) && !n_arg)) {
330 *outputp = "REJECTED EXTERNAL";
331 *n_outputp = strlen("REJECTED EXTERNAL");
332 sasl->fds_allowed = false;
333 sasl->state = SASL_SERVER_STATE_AUTH;
334 } else {
335 *outputp = "ERROR";
336 *n_outputp = strlen("ERROR");
337 }
338
339 break;
340
341 default:
342 return error_origin(-ENOTRECOVERABLE);
343 }
344
345 return 0;
346 }