Debian Codegraph @

src/shared/notify-recv.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4
5 #include "alloc-util.h"
6 #include "async.h"
7 #include "constants.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "fdset.h"
11 #include "log.h"
12 #include "notify-recv.h"
13 #include "pidref.h"
14 #include "process-util.h"
15 #include "socket-util.h"
16 #include "strv.h"
17
18 int notify_socket_prepare_full(
19 sd_event *event,
20 int64_t priority,
21 sd_event_io_handler_t handler,
22 void *userdata,
23 bool accept_fds,
24 char **ret_path,
25 sd_event_source **ret_event_source) {
26
27 int r;
28
29 assert(event);
30
31 /* This creates an autobind AF_UNIX socket and adds an IO event source for the socket, which helps
32 * prepare the notification socket used to communicate with worker processes. */
33
34 _cleanup_close_ int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
35 if (fd < 0)
36 return log_debug_errno(errno, "Failed to create notification socket: %m");
37
38 _cleanup_free_ char *path = NULL;
39 r = socket_autobind(fd, &path);
40 if (r < 0)
41 return log_debug_errno(r, "Failed to bind notification socket: %m");
42
43 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
44 if (r < 0)
45 return log_debug_errno(r, "Failed to enable SO_PASSCRED on notification socket: %m");
46
47 /* SO_PASSPIDFD is supported since kernel v6.5. */
48 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
49 if (r < 0)
50 log_debug_errno(r, "Failed to enable SO_PASSPIDFD on notification socket, ignoring: %m");
51
52 if (!accept_fds) {
53 /* since kernel v6.16 */
54 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false);
55 if (r < 0)
56 log_debug_errno(r, "Failed to disable SO_PASSRIGHTS on notification socket, ignoring: %m");
57 }
58
59 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
60 r = sd_event_add_io(event, &s, fd, EPOLLIN, handler, userdata);
61 if (r < 0)
62 return log_debug_errno(r, "Failed to create notification event source: %m");
63
64 r = sd_event_source_set_priority(s, priority);
65 if (r < 0)
66 return log_debug_errno(r, "Failed to set priority to notification event source: %m");
67
68 r = sd_event_source_set_io_fd_own(s, true);
69 if (r < 0)
70 return log_debug_errno(r, "Failed to make notification event source own file descriptor: %m");
71 TAKE_FD(fd);
72
73 (void) sd_event_source_set_description(s, "notify-socket");
74
75 if (ret_event_source)
76 *ret_event_source = TAKE_PTR(s);
77 else {
78 r = sd_event_source_set_floating(s, true);
79 if (r < 0)
80 return log_debug_errno(r, "Failed to make notification event source floating: %m");
81 }
82
83 if (ret_path)
84 *ret_path = TAKE_PTR(path);
85
86 return 0;
87 }
88
89 int notify_recv_with_fds(
90 int fd,
91 char **ret_text,
92 struct ucred *ret_ucred,
93 PidRef *ret_pidref,
94 FDSet **ret_fds) {
95
96 char buf[NOTIFY_BUFFER_MAX];
97 struct iovec iovec = {
98 .iov_base = buf,
99 .iov_len = sizeof(buf),
100 };
101 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) +
102 CMSG_SPACE(sizeof(int)) + /* SCM_PIDFD */
103 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)) control;
104 struct msghdr msghdr = {
105 .msg_iov = &iovec,
106 .msg_iovlen = 1,
107 .msg_control = &control,
108 .msg_controllen = sizeof(control),
109 };
110 ssize_t n;
111 int r;
112
113 assert(fd >= 0);
114
115 /* Receives a $NOTIFY_SOCKET message (aka sd_notify()). Does various validations.
116 *
117 * Returns -EAGAIN on recoverable errors (e.g. in case an invalid message is received, following
118 * the logic that an invalid message shall be ignored, and treated like no message at all). */
119
120 n = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
121 if (ERRNO_IS_NEG_TRANSIENT(n))
122 return -EAGAIN;
123 if (n == -ECHRNG) {
124 log_warning_errno(n, "Got message with truncated control data (%s fds sent?), ignoring.",
125 ret_fds ? "too many" : "unexpected");
126 return -EAGAIN;
127 }
128 if (n == -EXFULL) {
129 log_warning_errno(n, "Got message with truncated payload data, ignoring.");
130 return -EAGAIN;
131 }
132 if (n < 0)
133 return log_error_errno(n, "Failed to receive notification message: %m");
134
135 const struct ucred *ucred = NULL;
136 _cleanup_close_ int pidfd = -EBADF;
137 int *fd_array = NULL;
138 size_t n_fds = 0;
139
140 struct cmsghdr *cmsg;
141 CMSG_FOREACH(cmsg, &msghdr) {
142 if (cmsg->cmsg_level != SOL_SOCKET)
143 continue;
144
145 switch (cmsg->cmsg_type) {
146
147 case SCM_RIGHTS:
148 assert(!fd_array && n_fds == 0);
149
150 fd_array = CMSG_TYPED_DATA(cmsg, int);
151 n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
152 break;
153
154 case SCM_PIDFD:
155 assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
156 assert(pidfd < 0);
157
158 pidfd = *CMSG_TYPED_DATA(cmsg, int);
159 break;
160
161 case SCM_CREDENTIALS:
162 assert(cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)));
163 assert(!ucred);
164
165 ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
166 break;
167 }
168 }
169
170 _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
171 if (n_fds > 0) {
172 assert(fd_array);
173
174 if (!ret_fds) {
175 log_debug("Received fds via notification while none is expected, closing all.");
176 asynchronous_close_many(fd_array, n_fds);
177 } else {
178 r = fdset_new_array(&fds, fd_array, n_fds);
179 if (r < 0) {
180 asynchronous_close_many(fd_array, n_fds);
181 log_warning_errno(r, "Failed to collect fds from notification, ignoring message: %m");
182 return -EAGAIN;
183 }
184 }
185 }
186
187 if ((ret_ucred || ret_pidref) && (!ucred || !pid_is_valid(ucred->pid)))
188 return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
189 "Got notification datagram lacking valid credential information, ignoring.");
190
191 /* As extra safety check, let's make sure the string we get doesn't contain embedded NUL bytes.
192 * We permit one trailing NUL byte in the message, but don't expect it. */
193 if (n > 1 && memchr(buf, 0, n - 1))
194 return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN), "Got notification message with embedded NUL, ignoring.");
195
196 if (ret_text) {
197 char *s = memdup_suffix0(buf, n);
198 if (!s) {
199 log_oom_warning();
200 return -EAGAIN;
201 }
202
203 *ret_text = s;
204 }
205
206 if (ret_ucred)
207 *ret_ucred = *ucred;
208
209 if (ret_pidref) {
210 if (pidfd >= 0)
211 *ret_pidref = (PidRef) {
212 .pid = ucred->pid,
213 .fd = TAKE_FD(pidfd),
214 };
215 else
216 *ret_pidref = PIDREF_MAKE_FROM_PID(ucred->pid);
217 }
218
219 if (ret_fds)
220 *ret_fds = TAKE_PTR(fds);
221
222 return 0;
223 }
224
225 int notify_recv_with_fds_strv(
226 int fd,
227 char ***ret_list,
228 struct ucred *ret_ucred,
229 PidRef *ret_pidref,
230 FDSet **ret_fds) {
231
232 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
233 _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
234 struct ucred ucred = UCRED_INVALID;
235 _cleanup_free_ char *text = NULL;
236 int r;
237
238 r = notify_recv_with_fds(
239 fd,
240 ret_list ? &text : NULL,
241 ret_ucred ? &ucred : NULL,
242 ret_pidref ? &pidref : NULL,
243 ret_fds ? &fds : NULL);
244 if (r < 0)
245 return r;
246
247 if (ret_list) {
248 char **l = strv_split_newlines(text);
249 if (!l) {
250 log_oom_warning();
251 return -EAGAIN;
252 }
253
254 *ret_list = l;
255 }
256
257 if (ret_ucred)
258 *ret_ucred = ucred;
259 if (ret_pidref)
260 *ret_pidref = TAKE_PIDREF(pidref);
261 if (ret_fds)
262 *ret_fds = TAKE_PTR(fds);
263
264 return 0;
265 }