]>
Commit | Line | Data |
---|---|---|
b0a33c1e | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
b0a33c1e | 8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
b0a33c1e | 22 | */ |
d06245b8 | 23 | |
d38dd64a CB |
24 | #ifndef _GNU_SOURCE |
25 | #define _GNU_SOURCE 1 | |
26 | #endif | |
94ac256f CB |
27 | #include <errno.h> |
28 | #include <fcntl.h> | |
29 | #include <stddef.h> | |
ae467c54 CB |
30 | #include <stdio.h> |
31 | #include <stdlib.h> | |
b0a33c1e | 32 | #include <string.h> |
33 | #include <unistd.h> | |
b0a33c1e | 34 | #include <sys/socket.h> |
94ac256f | 35 | #include <sys/syscall.h> |
b0a33c1e | 36 | #include <sys/un.h> |
37 | ||
d38dd64a | 38 | #include "config.h" |
2dcb28a9 | 39 | #include "log.h" |
83c11f1d | 40 | #include "memory_utils.h" |
d7b58715 | 41 | #include "raw_syscalls.h" |
0059379f | 42 | #include "utils.h" |
2dcb28a9 | 43 | |
9de31d5a CB |
44 | #ifndef HAVE_STRLCPY |
45 | #include "include/strlcpy.h" | |
46 | #endif | |
47 | ||
ac2cecc4 | 48 | lxc_log_define(af_unix, lxc); |
b0a33c1e | 49 | |
c62fb5e0 | 50 | static ssize_t lxc_abstract_unix_set_sockaddr(struct sockaddr_un *addr, |
51 | const char *path) | |
b0a33c1e | 52 | { |
ddb17f1f | 53 | size_t len; |
b0a33c1e | 54 | |
c62fb5e0 | 55 | if (!addr || !path) { |
56 | errno = EINVAL; | |
b0a33c1e | 57 | return -1; |
c62fb5e0 | 58 | } |
b0a33c1e | 59 | |
aae93dd3 | 60 | /* Clear address structure */ |
c62fb5e0 | 61 | memset(addr, 0, sizeof(*addr)); |
b0a33c1e | 62 | |
c62fb5e0 | 63 | addr->sun_family = AF_UNIX; |
aae93dd3 | 64 | |
caf3beb0 | 65 | len = strlen(&path[1]); |
c62fb5e0 | 66 | |
caf3beb0 | 67 | /* do not enforce \0-termination */ |
c62fb5e0 | 68 | if (len >= INT_MAX || len >= sizeof(addr->sun_path)) { |
aae93dd3 ÇO |
69 | errno = ENAMETOOLONG; |
70 | return -1; | |
ddb17f1f | 71 | } |
9de31d5a CB |
72 | |
73 | /* do not enforce \0-termination */ | |
c62fb5e0 | 74 | memcpy(&addr->sun_path[1], &path[1], len); |
75 | return len; | |
76 | } | |
77 | ||
78 | int lxc_abstract_unix_open(const char *path, int type, int flags) | |
79 | { | |
80 | int fd, ret; | |
81 | ssize_t len; | |
82 | struct sockaddr_un addr; | |
83 | ||
ad9429e5 | 84 | fd = socket(PF_UNIX, type | SOCK_CLOEXEC, 0); |
c62fb5e0 | 85 | if (fd < 0) |
86 | return -1; | |
87 | ||
88 | if (!path) | |
89 | return fd; | |
90 | ||
91 | len = lxc_abstract_unix_set_sockaddr(&addr, path); | |
92 | if (len < 0) { | |
93 | int saved_errno = errno; | |
94 | close(fd); | |
95 | errno = saved_errno; | |
96 | return -1; | |
97 | } | |
b0a33c1e | 98 | |
77b0073a CB |
99 | ret = bind(fd, (struct sockaddr *)&addr, |
100 | offsetof(struct sockaddr_un, sun_path) + len + 1); | |
101 | if (ret < 0) { | |
c62fb5e0 | 102 | int saved_errno = errno; |
b0a33c1e | 103 | close(fd); |
c62fb5e0 | 104 | errno = saved_errno; |
b0a33c1e | 105 | return -1; |
106 | } | |
ddb17f1f | 107 | |
77b0073a CB |
108 | if (type == SOCK_STREAM) { |
109 | ret = listen(fd, 100); | |
110 | if (ret < 0) { | |
c62fb5e0 | 111 | int saved_errno = errno; |
77b0073a | 112 | close(fd); |
c62fb5e0 | 113 | errno = saved_errno; |
77b0073a CB |
114 | return -1; |
115 | } | |
b0a33c1e | 116 | } |
117 | ||
118 | return fd; | |
119 | } | |
120 | ||
9044b79e | 121 | void lxc_abstract_unix_close(int fd) |
b0a33c1e | 122 | { |
b0a33c1e | 123 | close(fd); |
b0a33c1e | 124 | } |
125 | ||
aae93dd3 | 126 | int lxc_abstract_unix_connect(const char *path) |
b0a33c1e | 127 | { |
77b0073a | 128 | int fd, ret; |
c62fb5e0 | 129 | ssize_t len; |
b0a33c1e | 130 | struct sockaddr_un addr; |
131 | ||
ad9429e5 | 132 | fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
b0a33c1e | 133 | if (fd < 0) |
134 | return -1; | |
135 | ||
c62fb5e0 | 136 | len = lxc_abstract_unix_set_sockaddr(&addr, path); |
137 | if (len < 0) { | |
138 | int saved_errno = errno; | |
aae93dd3 | 139 | close(fd); |
c62fb5e0 | 140 | errno = saved_errno; |
aae93dd3 ÇO |
141 | return -1; |
142 | } | |
9de31d5a | 143 | |
77b0073a CB |
144 | ret = connect(fd, (struct sockaddr *)&addr, |
145 | offsetof(struct sockaddr_un, sun_path) + len + 1); | |
146 | if (ret < 0) { | |
9044b79e | 147 | int saved_errno = errno; |
b0a33c1e | 148 | close(fd); |
9044b79e | 149 | errno = saved_errno; |
b0a33c1e | 150 | return -1; |
151 | } | |
152 | ||
153 | return fd; | |
154 | } | |
155 | ||
e1726045 WB |
156 | int lxc_abstract_unix_send_fds_iov(int fd, int *sendfds, int num_sendfds, |
157 | struct iovec *iov, size_t iovlen) | |
b0a33c1e | 158 | { |
c3e3c21a CB |
159 | __do_free char *cmsgbuf = NULL; |
160 | int ret; | |
ae467c54 | 161 | struct msghdr msg; |
ae467c54 | 162 | struct cmsghdr *cmsg = NULL; |
ae467c54 CB |
163 | size_t cmsgbufsize = CMSG_SPACE(num_sendfds * sizeof(int)); |
164 | ||
165 | memset(&msg, 0, sizeof(msg)); | |
ae467c54 CB |
166 | |
167 | cmsgbuf = malloc(cmsgbufsize); | |
9044b79e | 168 | if (!cmsgbuf) { |
169 | errno = ENOMEM; | |
ae467c54 | 170 | return -1; |
9044b79e | 171 | } |
b0a33c1e | 172 | |
604f0955 | 173 | msg.msg_control = cmsgbuf; |
ae467c54 | 174 | msg.msg_controllen = cmsgbufsize; |
b0a33c1e | 175 | |
604f0955 | 176 | cmsg = CMSG_FIRSTHDR(&msg); |
604f0955 ÇO |
177 | cmsg->cmsg_level = SOL_SOCKET; |
178 | cmsg->cmsg_type = SCM_RIGHTS; | |
ae467c54 | 179 | cmsg->cmsg_len = CMSG_LEN(num_sendfds * sizeof(int)); |
b0a33c1e | 180 | |
ae467c54 CB |
181 | msg.msg_controllen = cmsg->cmsg_len; |
182 | ||
183 | memcpy(CMSG_DATA(cmsg), sendfds, num_sendfds * sizeof(int)); | |
b0a33c1e | 184 | |
e1726045 WB |
185 | msg.msg_iov = iov; |
186 | msg.msg_iovlen = iovlen; | |
b0a33c1e | 187 | |
c3e3c21a CB |
188 | again: |
189 | ret = sendmsg(fd, &msg, MSG_NOSIGNAL); | |
190 | if (ret < 0) | |
191 | if (errno == EINTR) | |
192 | goto again; | |
193 | ||
194 | return ret; | |
b0a33c1e | 195 | } |
196 | ||
e1726045 WB |
197 | int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds, |
198 | void *data, size_t size) | |
199 | { | |
200 | char buf[1] = {0}; | |
201 | struct iovec iov = { | |
202 | .iov_base = data ? data : buf, | |
203 | .iov_len = data ? size : sizeof(buf), | |
204 | }; | |
205 | return lxc_abstract_unix_send_fds_iov(fd, sendfds, num_sendfds, &iov, | |
206 | 1); | |
207 | } | |
208 | ||
5ed06d3a CB |
209 | int lxc_unix_send_fds(int fd, int *sendfds, int num_sendfds, void *data, |
210 | size_t size) | |
211 | { | |
212 | return lxc_abstract_unix_send_fds(fd, sendfds, num_sendfds, data, size); | |
213 | } | |
214 | ||
dc85e31e CB |
215 | static int lxc_abstract_unix_recv_fds_iov(int fd, int *recvfds, int num_recvfds, |
216 | struct iovec *iov, size_t iovlen) | |
b0a33c1e | 217 | { |
c3e3c21a | 218 | __do_free char *cmsgbuf = NULL; |
ae467c54 CB |
219 | int ret; |
220 | struct msghdr msg; | |
ae467c54 | 221 | struct cmsghdr *cmsg = NULL; |
caf3beb0 | 222 | char buf[1] = {0}; |
cdb2a47f CB |
223 | size_t cmsgbufsize = CMSG_SPACE(sizeof(struct ucred)) + |
224 | CMSG_SPACE(num_recvfds * sizeof(int)); | |
ae467c54 CB |
225 | |
226 | memset(&msg, 0, sizeof(msg)); | |
ae467c54 CB |
227 | |
228 | cmsgbuf = malloc(cmsgbufsize); | |
9044b79e | 229 | if (!cmsgbuf) { |
230 | errno = ENOMEM; | |
ae467c54 | 231 | return -1; |
9044b79e | 232 | } |
b0a33c1e | 233 | |
604f0955 | 234 | msg.msg_control = cmsgbuf; |
ae467c54 | 235 | msg.msg_controllen = cmsgbufsize; |
b0a33c1e | 236 | |
dc85e31e CB |
237 | msg.msg_iov = iov; |
238 | msg.msg_iovlen = iovlen; | |
b0a33c1e | 239 | |
c3e3c21a | 240 | again: |
b0a33c1e | 241 | ret = recvmsg(fd, &msg, 0); |
c3e3c21a CB |
242 | if (ret < 0) { |
243 | if (errno == EINTR) | |
244 | goto again; | |
245 | ||
246 | goto out; | |
247 | } | |
248 | if (ret == 0) | |
b0a33c1e | 249 | goto out; |
250 | ||
cdb2a47f CB |
251 | /* |
252 | * If SO_PASSCRED is set we will always get a ucred message. | |
253 | */ | |
254 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
255 | if (cmsg->cmsg_type != SCM_RIGHTS) | |
256 | continue; | |
257 | ||
258 | memset(recvfds, -1, num_recvfds * sizeof(int)); | |
259 | if (cmsg && | |
260 | cmsg->cmsg_len == CMSG_LEN(num_recvfds * sizeof(int)) && | |
261 | cmsg->cmsg_level == SOL_SOCKET) | |
262 | memcpy(recvfds, CMSG_DATA(cmsg), num_recvfds * sizeof(int)); | |
263 | break; | |
264 | } | |
ae467c54 | 265 | |
b0a33c1e | 266 | out: |
604f0955 | 267 | return ret; |
b0a33c1e | 268 | } |
269 | ||
dc85e31e CB |
270 | int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds, |
271 | void *data, size_t size) | |
272 | { | |
273 | char buf[1] = {0}; | |
274 | struct iovec iov = { | |
275 | .iov_base = data ? data : buf, | |
276 | .iov_len = data ? size : sizeof(buf), | |
277 | }; | |
278 | return lxc_abstract_unix_recv_fds_iov(fd, recvfds, num_recvfds, &iov, 1); | |
279 | } | |
280 | ||
aae93dd3 | 281 | int lxc_abstract_unix_send_credential(int fd, void *data, size_t size) |
b0a33c1e | 282 | { |
77b0073a | 283 | struct msghdr msg = {0}; |
604f0955 ÇO |
284 | struct iovec iov; |
285 | struct cmsghdr *cmsg; | |
b0a33c1e | 286 | struct ucred cred = { |
0059379f | 287 | .pid = lxc_raw_getpid(), .uid = getuid(), .gid = getgid(), |
b0a33c1e | 288 | }; |
caf3beb0 CB |
289 | char cmsgbuf[CMSG_SPACE(sizeof(cred))] = {0}; |
290 | char buf[1] = {0}; | |
b0a33c1e | 291 | |
604f0955 ÇO |
292 | msg.msg_control = cmsgbuf; |
293 | msg.msg_controllen = sizeof(cmsgbuf); | |
b0a33c1e | 294 | |
604f0955 ÇO |
295 | cmsg = CMSG_FIRSTHDR(&msg); |
296 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); | |
297 | cmsg->cmsg_level = SOL_SOCKET; | |
298 | cmsg->cmsg_type = SCM_CREDENTIALS; | |
0e391e57 | 299 | memcpy(CMSG_DATA(cmsg), &cred, sizeof(cred)); |
b0a33c1e | 300 | |
604f0955 ÇO |
301 | msg.msg_name = NULL; |
302 | msg.msg_namelen = 0; | |
b0a33c1e | 303 | |
604f0955 ÇO |
304 | iov.iov_base = data ? data : buf; |
305 | iov.iov_len = data ? size : sizeof(buf); | |
306 | msg.msg_iov = &iov; | |
307 | msg.msg_iovlen = 1; | |
b0a33c1e | 308 | |
6168ff15 | 309 | return sendmsg(fd, &msg, MSG_NOSIGNAL); |
b0a33c1e | 310 | } |
311 | ||
aae93dd3 | 312 | int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size) |
b0a33c1e | 313 | { |
77b0073a | 314 | struct msghdr msg = {0}; |
604f0955 ÇO |
315 | struct iovec iov; |
316 | struct cmsghdr *cmsg; | |
b0a33c1e | 317 | struct ucred cred; |
b0a33c1e | 318 | int ret; |
caf3beb0 CB |
319 | char cmsgbuf[CMSG_SPACE(sizeof(cred))] = {0}; |
320 | char buf[1] = {0}; | |
b0a33c1e | 321 | |
604f0955 ÇO |
322 | msg.msg_name = NULL; |
323 | msg.msg_namelen = 0; | |
324 | msg.msg_control = cmsgbuf; | |
325 | msg.msg_controllen = sizeof(cmsgbuf); | |
b0a33c1e | 326 | |
604f0955 ÇO |
327 | iov.iov_base = data ? data : buf; |
328 | iov.iov_len = data ? size : sizeof(buf); | |
329 | msg.msg_iov = &iov; | |
330 | msg.msg_iovlen = 1; | |
b0a33c1e | 331 | |
332 | ret = recvmsg(fd, &msg, 0); | |
333 | if (ret <= 0) | |
334 | goto out; | |
335 | ||
604f0955 | 336 | cmsg = CMSG_FIRSTHDR(&msg); |
b0a33c1e | 337 | |
604f0955 | 338 | if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) && |
77b0073a CB |
339 | cmsg->cmsg_level == SOL_SOCKET && |
340 | cmsg->cmsg_type == SCM_CREDENTIALS) { | |
0e391e57 | 341 | memcpy(&cred, CMSG_DATA(cmsg), sizeof(cred)); |
77b0073a CB |
342 | if (cred.uid && |
343 | (cred.uid != getuid() || cred.gid != getgid())) { | |
9044b79e | 344 | INFO("Message denied for '%d/%d'", cred.uid, cred.gid); |
345 | errno = EACCES; | |
346 | return -1; | |
2dcb28a9 | 347 | } |
604f0955 | 348 | } |
9044b79e | 349 | |
b0a33c1e | 350 | out: |
604f0955 | 351 | return ret; |
b0a33c1e | 352 | } |
86ce1da1 CB |
353 | |
354 | int lxc_unix_sockaddr(struct sockaddr_un *ret, const char *path) | |
355 | { | |
356 | size_t len; | |
357 | ||
358 | len = strlen(path); | |
359 | if (len == 0) | |
360 | return minus_one_set_errno(EINVAL); | |
361 | if (path[0] != '/' && path[0] != '@') | |
362 | return minus_one_set_errno(EINVAL); | |
363 | if (path[1] == '\0') | |
364 | return minus_one_set_errno(EINVAL); | |
365 | ||
366 | if (len + 1 > sizeof(ret->sun_path)) | |
367 | return minus_one_set_errno(EINVAL); | |
368 | ||
369 | *ret = (struct sockaddr_un){ | |
370 | .sun_family = AF_UNIX, | |
371 | }; | |
372 | ||
373 | if (path[0] == '@') { | |
374 | memcpy(ret->sun_path + 1, path + 1, len); | |
375 | return (int)(offsetof(struct sockaddr_un, sun_path) + len); | |
376 | } | |
377 | ||
378 | memcpy(ret->sun_path, path, len + 1); | |
379 | return (int)(offsetof(struct sockaddr_un, sun_path) + len + 1); | |
380 | } | |
381 | ||
970ef13d | 382 | int lxc_unix_connect_type(struct sockaddr_un *addr, int type) |
86ce1da1 CB |
383 | { |
384 | __do_close_prot_errno int fd = -EBADF; | |
385 | int ret; | |
386 | ssize_t len; | |
387 | ||
970ef13d | 388 | fd = socket(AF_UNIX, type | SOCK_CLOEXEC, 0); |
2ac0f627 CB |
389 | if (fd < 0) { |
390 | SYSERROR("Failed to open new AF_UNIX socket"); | |
86ce1da1 | 391 | return -1; |
2ac0f627 | 392 | } |
86ce1da1 CB |
393 | |
394 | if (addr->sun_path[0] == '\0') | |
395 | len = strlen(&addr->sun_path[1]); | |
396 | else | |
397 | len = strlen(&addr->sun_path[0]); | |
2ac0f627 CB |
398 | |
399 | ret = connect(fd, (struct sockaddr *)addr, | |
400 | offsetof(struct sockaddr_un, sun_path) + len); | |
401 | if (ret < 0) { | |
402 | SYSERROR("Failed to bind new AF_UNIX socket"); | |
86ce1da1 | 403 | return -1; |
2ac0f627 | 404 | } |
86ce1da1 CB |
405 | |
406 | return move_fd(fd); | |
407 | } | |
408 | ||
970ef13d WB |
409 | int lxc_unix_connect(struct sockaddr_un *addr, int type) |
410 | { | |
411 | return lxc_unix_connect_type(addr, SOCK_STREAM); | |
412 | } | |
413 | ||
86ce1da1 CB |
414 | int lxc_socket_set_timeout(int fd, int rcv_timeout, int snd_timeout) |
415 | { | |
416 | struct timeval out = {0}; | |
417 | int ret; | |
418 | ||
419 | out.tv_sec = snd_timeout; | |
420 | ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&out, | |
421 | sizeof(out)); | |
422 | if (ret < 0) | |
423 | return -1; | |
424 | ||
425 | out.tv_sec = rcv_timeout; | |
426 | ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const void *)&out, | |
427 | sizeof(out)); | |
428 | if (ret < 0) | |
429 | return -1; | |
430 | ||
431 | return 0; | |
432 | } |