blob: ac3cb642d7c51f8b5d92710661be48b0474fd07d [file] [log] [blame]
Willy Tarreau0d06df62020-08-28 15:10:11 +02001/*
2 * SOCK_UNIX socket management
3 *
4 * Copyright 2000-2020 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
Willy Tarreaueb8cfe62020-09-16 22:15:40 +020014#include <errno.h>
Willy Tarreau0d06df62020-08-28 15:10:11 +020015#include <string.h>
Willy Tarreau1e0a8602020-09-02 17:14:29 +020016#include <unistd.h>
Willy Tarreau0d06df62020-08-28 15:10:11 +020017
18#include <sys/param.h>
19#include <sys/socket.h>
20#include <sys/types.h>
21
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
27#include <haproxy/api.h>
Willy Tarreau1e0a8602020-09-02 17:14:29 +020028#include <haproxy/errors.h>
29#include <haproxy/fd.h>
30#include <haproxy/global.h>
Willy Tarreau0d06df62020-08-28 15:10:11 +020031#include <haproxy/listener.h>
Willy Tarreau1e0a8602020-09-02 17:14:29 +020032#include <haproxy/receiver-t.h>
Willy Tarreau0d06df62020-08-28 15:10:11 +020033#include <haproxy/namespace.h>
Willy Tarreau1e0a8602020-09-02 17:14:29 +020034#include <haproxy/sock.h>
Willy Tarreau0d06df62020-08-28 15:10:11 +020035#include <haproxy/sock_unix.h>
36#include <haproxy/tools.h>
37
38
Willy Tarreaub0254cb2020-09-04 08:07:11 +020039struct proto_fam proto_fam_unix = {
40 .name = "unix",
41 .sock_domain = PF_UNIX,
42 .sock_family = AF_UNIX,
43 .sock_addrlen = sizeof(struct sockaddr_un),
44 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
45 .addrcmp = sock_unix_addrcmp,
46 .bind = sock_unix_bind_receiver,
47 .get_src = sock_get_src,
48 .get_dst = sock_get_dst,
49};
50
Willy Tarreau0d06df62020-08-28 15:10:11 +020051/* PLEASE NOTE for functions below:
52 *
53 * The address family SHOULD always be checked. In some cases a function will
54 * be used in a situation where the address family is guaranteed (e.g. protocol
55 * definitions), so the test may be avoided. This special case must then be
56 * mentioned in the comment before the function definition.
57 */
58
59
60/* Compares two AF_UNIX sockaddr addresses. Returns 0 if they match or non-zero
61 * if they do not match. It also supports ABNS socket addresses (those starting
62 * with \0). For regular UNIX sockets however, this does explicitly support
63 * matching names ending exactly with .XXXXX.tmp which are newly bound sockets
64 * about to be replaced; this suffix is then ignored. Note that our UNIX socket
65 * paths are always zero-terminated.
66 */
67int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
68{
69 const struct sockaddr_un *au = (const struct sockaddr_un *)a;
70 const struct sockaddr_un *bu = (const struct sockaddr_un *)b;
71 int idx, dot, idx2;
72
73 if (a->ss_family != b->ss_family)
74 return -1;
75
76 if (a->ss_family != AF_UNIX)
77 return -1;
78
79 if (au->sun_path[0] != bu->sun_path[0])
80 return -1;
81
82 if (au->sun_path[0] == 0)
83 return memcmp(au->sun_path, bu->sun_path, sizeof(au->sun_path));
84
85 idx = 1; dot = 0;
86 while (au->sun_path[idx] == bu->sun_path[idx]) {
87 if (au->sun_path[idx] == 0)
88 return 0;
89 if (au->sun_path[idx] == '.')
90 dot = idx;
91 idx++;
92 }
93
94 /* Now we have a difference. It's OK if they are within or after a
95 * sequence of digits following a dot, and are followed by ".tmp".
Aurelien DARRAGON2a7903b2023-02-21 17:33:50 +010096 *
97 * make sure to perform the check against tempname if the compared
98 * string is in "final" format (does not end with ".XXXX.tmp").
99 *
100 * Examples:
101 * /tmp/test matches with /tmp/test.1822.tmp
102 * /tmp/test.1822.tmp matches with /tmp/test.XXXX.tmp
Willy Tarreau0d06df62020-08-28 15:10:11 +0200103 */
Aurelien DARRAGON2a7903b2023-02-21 17:33:50 +0100104 if (au->sun_path[idx] == 0 || bu->sun_path[idx] == 0) {
105 if (au->sun_path[idx] == '.' || bu->sun_path[idx] == '.')
106 dot = idx; /* try to match against temp path */
107 else
108 return -1; /* invalid temp path */
109 }
110
Willy Tarreau0d06df62020-08-28 15:10:11 +0200111 if (!dot)
112 return -1;
113
114 /* First, check in path "a" */
115 if (au->sun_path[idx] != 0) {
Willy Tarreau1c34b882020-08-29 06:44:37 +0200116 for (idx2 = dot + 1; idx2 && isdigit((unsigned char)au->sun_path[idx2]);)
Willy Tarreau0d06df62020-08-28 15:10:11 +0200117 idx2++;
118 if (strcmp(au->sun_path + idx2, ".tmp") != 0)
119 return -1;
120 }
121
122 /* Then check in path "b" */
123 if (bu->sun_path[idx] != 0) {
Willy Tarreau1c34b882020-08-29 06:44:37 +0200124 for (idx2 = dot + 1; idx2 && isdigit((unsigned char)bu->sun_path[idx2]); idx2++)
Willy Tarreau0d06df62020-08-28 15:10:11 +0200125 ;
126 if (strcmp(bu->sun_path + idx2, ".tmp") != 0)
127 return -1;
128 }
129
130 /* OK that's a match */
131 return 0;
132}
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200133
Willy Tarreau233ad282020-10-15 21:45:15 +0200134/* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback and
135 * context, respectively, with ->bind_thread as the thread mask. Returns an
136 * error code made of ERR_* bits on failure or ERR_NONE on success. On failure,
137 * an error message may be passed into <errmsg>.
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200138 */
Willy Tarreau233ad282020-10-15 21:45:15 +0200139int sock_unix_bind_receiver(struct receiver *rx, char **errmsg)
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200140{
141 char tempname[MAXPATHLEN];
142 char backname[MAXPATHLEN];
143 struct sockaddr_un addr;
144 const char *path;
145 int maxpathlen;
146 int fd, err, ext, ret;
147
148 /* ensure we never return garbage */
149 if (errmsg)
150 *errmsg = 0;
151
152 err = ERR_NONE;
153
154 if (rx->flags & RX_F_BOUND)
155 return ERR_NONE;
156
157 /* if no FD was assigned yet, we'll have to either find a compatible
158 * one or create a new one.
159 */
160 if (rx->fd == -1)
161 rx->fd = sock_find_compatible_fd(rx);
162
163 path = ((struct sockaddr_un *)&rx->addr)->sun_path;
164 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
165
166 /* if the listener already has an fd assigned, then we were offered the
167 * fd by an external process (most likely the parent), and we don't want
168 * to create a new socket. However we still want to set a few flags on
169 * the socket.
170 */
171 fd = rx->fd;
172 ext = (fd >= 0);
173 if (ext)
174 goto fd_ready;
175
176 if (path[0]) {
177 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
178 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
179 err |= ERR_FATAL | ERR_ALERT;
180 memprintf(errmsg, "name too long for UNIX socket (limit usually 97)");
181 goto bind_return;
182 }
183
184 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
185 if (ret < 0 || ret >= maxpathlen) {
186 err |= ERR_FATAL | ERR_ALERT;
187 memprintf(errmsg, "name too long for UNIX socket (limit usually 97)");
188 goto bind_return;
189 }
190
191 /* 2. clean existing orphaned entries */
192 if (unlink(tempname) < 0 && errno != ENOENT) {
193 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200194 memprintf(errmsg, "error when trying to unlink previous UNIX socket (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200195 goto bind_return;
196 }
197
198 if (unlink(backname) < 0 && errno != ENOENT) {
199 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200200 memprintf(errmsg, "error when trying to unlink previous UNIX socket (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200201 goto bind_return;
202 }
203
204 /* 3. backup existing socket */
205 if (link(path, backname) < 0 && errno != ENOENT) {
206 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200207 memprintf(errmsg, "error when trying to preserve previous UNIX socket (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200208 goto bind_return;
209 }
210
211 /* Note: this test is redundant with the snprintf one above and
212 * will never trigger, it's just added as the only way to shut
213 * gcc's painfully dumb warning about possibly truncated output
214 * during strncpy(). Don't move it above or smart gcc will not
215 * see it!
216 */
217 if (strlen(tempname) >= sizeof(addr.sun_path)) {
218 err |= ERR_FATAL | ERR_ALERT;
219 memprintf(errmsg, "name too long for UNIX socket (limit usually 97)");
220 goto bind_return;
221 }
222
223 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
224 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
225 }
226 else {
227 /* first char is zero, it's an abstract socket whose address
228 * is defined by all the bytes past this zero.
229 */
230 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
231 }
232 addr.sun_family = AF_UNIX;
233
234 /* WT: shouldn't we use my_socketat(rx->netns) here instead ? */
Willy Tarreauf1f66092020-09-04 08:15:31 +0200235 fd = socket(rx->proto->fam->sock_domain, rx->proto->sock_type, rx->proto->sock_prot);
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200236 if (fd < 0) {
237 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200238 memprintf(errmsg, "cannot create receiving socket (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200239 goto bind_return;
240 }
241
242 fd_ready:
Willy Tarreau145b17f2023-01-11 10:59:52 +0100243 if (ext && fd < global.maxsock && fdtab[fd].owner) {
244 /* This FD was already bound so this means that it was already
245 * known and registered before parsing, hence it's an inherited
246 * FD. The only reason why it's already known here is that it
247 * has been registered multiple times (multiple listeners on the
248 * same, or a "shards" directive on the line). There cannot be
249 * multiple listeners on one FD but at least we can create a
250 * new one from the original one. We won't reconfigure it,
251 * however, as this was already done for the first one.
252 */
253 fd = dup(fd);
254 if (fd == -1) {
255 err |= ERR_RETRYABLE | ERR_ALERT;
256 memprintf(errmsg, "cannot dup() receiving socket (%s)", strerror(errno));
257 goto bind_return;
258 }
259 }
260
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200261 if (fd >= global.maxsock) {
262 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
263 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
264 goto bind_close_return;
265 }
266
Willy Tarreau38247432022-04-26 10:24:14 +0200267 if (fd_set_nonblock(fd) == -1) {
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200268 err |= ERR_FATAL | ERR_ALERT;
269 memprintf(errmsg, "cannot make socket non-blocking");
270 goto bind_close_return;
271 }
272
273 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
274 /* note that bind() creates the socket <tempname> on the file system */
275 if (errno == EADDRINUSE) {
276 /* the old process might still own it, let's retry */
277 err |= ERR_RETRYABLE | ERR_ALERT;
278 memprintf(errmsg, "cannot bind UNIX socket (already in use)");
279 goto bind_close_return;
280 }
281 else {
282 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200283 memprintf(errmsg, "cannot bind UNIX socket (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200284 goto bind_close_return;
285 }
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200286 }
287
288 /* <uid> and <gid> different of -1 will be used to change the socket owner.
289 * If <mode> is not 0, it will be used to restrict access to the socket.
290 * While it is known not to be portable on every OS, it's still useful
291 * where it works. We also don't change permissions on abstract sockets.
292 */
293 if (!ext && path[0] &&
294 (((rx->settings->ux.uid != -1 || rx->settings->ux.gid != -1) &&
295 (chown(tempname, rx->settings->ux.uid, rx->settings->ux.gid) == -1)) ||
296 (rx->settings->ux.mode != 0 && chmod(tempname, rx->settings->ux.mode) == -1))) {
297 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200298 memprintf(errmsg, "cannot change UNIX socket ownership (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200299 goto err_unlink_temp;
300 }
301
302 /* Point of no return: we are ready, we'll switch the sockets. We don't
303 * fear losing the socket <path> because we have a copy of it in
304 * backname. Abstract sockets are not renamed.
305 */
306 if (!ext && path[0] && rename(tempname, path) < 0) {
307 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau3cd58bf2020-09-17 08:35:38 +0200308 memprintf(errmsg, "cannot switch final and temporary UNIX sockets (%s)", strerror(errno));
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200309 goto err_rename;
310 }
311
312 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
313 if (!ext && path[0])
314 unlink(backname);
315
316 rx->fd = fd;
317 rx->flags |= RX_F_BOUND;
318
Willy Tarreau9464bb12022-07-05 05:16:13 +0200319 fd_insert(fd, rx->owner, rx->iocb, rx->bind_tgroup, rx->bind_thread);
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200320
321 /* for now, all regularly bound TCP listeners are exportable */
322 if (!(rx->flags & RX_F_INHERITED))
Willy Tarreau9063a662021-04-06 18:09:06 +0200323 HA_ATOMIC_OR(&fdtab[fd].state, FD_EXPORTED);
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200324
325 return err;
326
327 err_rename:
328 ret = rename(backname, path);
329 if (ret < 0 && errno == ENOENT)
330 unlink(path);
331 err_unlink_temp:
332 if (!ext && path[0])
333 unlink(tempname);
334 close(fd);
335 err_unlink_back:
336 if (!ext && path[0])
337 unlink(backname);
338 bind_return:
339 if (errmsg && *errmsg) {
Aurelien DARRAGONde63efb2023-02-06 19:23:40 +0100340 if (!ext) {
341 char *path_str;
342
343 path_str = sa2str((struct sockaddr_storage *)&rx->addr, 0, 0);
344 memprintf(errmsg, "%s [%s]", *errmsg, ((path_str) ? path_str : ""));
345 ha_free(&path_str);
346 }
Willy Tarreau1e0a8602020-09-02 17:14:29 +0200347 else
348 memprintf(errmsg, "%s [fd %d]", *errmsg, fd);
349 }
350 return err;
351
352 bind_close_return:
353 close(fd);
354 goto bind_return;
355}