]>
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 | ||
84 | fd = socket(PF_UNIX, type, 0); | |
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 | ||
132 | fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
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 | ||
ae467c54 CB |
156 | int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds, |
157 | void *data, size_t size) | |
b0a33c1e | 158 | { |
83c11f1d | 159 | __do_free char *cmsgbuf; |
ae467c54 | 160 | struct msghdr msg; |
604f0955 | 161 | struct iovec iov; |
ae467c54 | 162 | struct cmsghdr *cmsg = NULL; |
caf3beb0 | 163 | char buf[1] = {0}; |
ae467c54 CB |
164 | size_t cmsgbufsize = CMSG_SPACE(num_sendfds * sizeof(int)); |
165 | ||
166 | memset(&msg, 0, sizeof(msg)); | |
167 | memset(&iov, 0, sizeof(iov)); | |
168 | ||
169 | cmsgbuf = malloc(cmsgbufsize); | |
9044b79e | 170 | if (!cmsgbuf) { |
171 | errno = ENOMEM; | |
ae467c54 | 172 | return -1; |
9044b79e | 173 | } |
b0a33c1e | 174 | |
604f0955 | 175 | msg.msg_control = cmsgbuf; |
ae467c54 | 176 | msg.msg_controllen = cmsgbufsize; |
b0a33c1e | 177 | |
604f0955 | 178 | cmsg = CMSG_FIRSTHDR(&msg); |
604f0955 ÇO |
179 | cmsg->cmsg_level = SOL_SOCKET; |
180 | cmsg->cmsg_type = SCM_RIGHTS; | |
ae467c54 | 181 | cmsg->cmsg_len = CMSG_LEN(num_sendfds * sizeof(int)); |
b0a33c1e | 182 | |
ae467c54 CB |
183 | msg.msg_controllen = cmsg->cmsg_len; |
184 | ||
185 | memcpy(CMSG_DATA(cmsg), sendfds, num_sendfds * sizeof(int)); | |
b0a33c1e | 186 | |
604f0955 ÇO |
187 | iov.iov_base = data ? data : buf; |
188 | iov.iov_len = data ? size : sizeof(buf); | |
189 | msg.msg_iov = &iov; | |
190 | msg.msg_iovlen = 1; | |
b0a33c1e | 191 | |
83c11f1d | 192 | return sendmsg(fd, &msg, MSG_NOSIGNAL); |
b0a33c1e | 193 | } |
194 | ||
ae467c54 CB |
195 | int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds, |
196 | void *data, size_t size) | |
b0a33c1e | 197 | { |
83c11f1d | 198 | __do_free char *cmsgbuf; |
ae467c54 CB |
199 | int ret; |
200 | struct msghdr msg; | |
604f0955 | 201 | struct iovec iov; |
ae467c54 | 202 | struct cmsghdr *cmsg = NULL; |
caf3beb0 | 203 | char buf[1] = {0}; |
cdb2a47f CB |
204 | size_t cmsgbufsize = CMSG_SPACE(sizeof(struct ucred)) + |
205 | CMSG_SPACE(num_recvfds * sizeof(int)); | |
ae467c54 CB |
206 | |
207 | memset(&msg, 0, sizeof(msg)); | |
208 | memset(&iov, 0, sizeof(iov)); | |
209 | ||
210 | cmsgbuf = malloc(cmsgbufsize); | |
9044b79e | 211 | if (!cmsgbuf) { |
212 | errno = ENOMEM; | |
ae467c54 | 213 | return -1; |
9044b79e | 214 | } |
b0a33c1e | 215 | |
604f0955 | 216 | msg.msg_control = cmsgbuf; |
ae467c54 | 217 | msg.msg_controllen = cmsgbufsize; |
b0a33c1e | 218 | |
604f0955 ÇO |
219 | iov.iov_base = data ? data : buf; |
220 | iov.iov_len = data ? size : sizeof(buf); | |
221 | msg.msg_iov = &iov; | |
222 | msg.msg_iovlen = 1; | |
b0a33c1e | 223 | |
224 | ret = recvmsg(fd, &msg, 0); | |
225 | if (ret <= 0) | |
226 | goto out; | |
227 | ||
cdb2a47f CB |
228 | /* |
229 | * If SO_PASSCRED is set we will always get a ucred message. | |
230 | */ | |
231 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
232 | if (cmsg->cmsg_type != SCM_RIGHTS) | |
233 | continue; | |
234 | ||
235 | memset(recvfds, -1, num_recvfds * sizeof(int)); | |
236 | if (cmsg && | |
237 | cmsg->cmsg_len == CMSG_LEN(num_recvfds * sizeof(int)) && | |
238 | cmsg->cmsg_level == SOL_SOCKET) | |
239 | memcpy(recvfds, CMSG_DATA(cmsg), num_recvfds * sizeof(int)); | |
240 | break; | |
241 | } | |
ae467c54 | 242 | |
b0a33c1e | 243 | out: |
604f0955 | 244 | return ret; |
b0a33c1e | 245 | } |
246 | ||
aae93dd3 | 247 | int lxc_abstract_unix_send_credential(int fd, void *data, size_t size) |
b0a33c1e | 248 | { |
77b0073a | 249 | struct msghdr msg = {0}; |
604f0955 ÇO |
250 | struct iovec iov; |
251 | struct cmsghdr *cmsg; | |
b0a33c1e | 252 | struct ucred cred = { |
0059379f | 253 | .pid = lxc_raw_getpid(), .uid = getuid(), .gid = getgid(), |
b0a33c1e | 254 | }; |
caf3beb0 CB |
255 | char cmsgbuf[CMSG_SPACE(sizeof(cred))] = {0}; |
256 | char buf[1] = {0}; | |
b0a33c1e | 257 | |
604f0955 ÇO |
258 | msg.msg_control = cmsgbuf; |
259 | msg.msg_controllen = sizeof(cmsgbuf); | |
b0a33c1e | 260 | |
604f0955 ÇO |
261 | cmsg = CMSG_FIRSTHDR(&msg); |
262 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); | |
263 | cmsg->cmsg_level = SOL_SOCKET; | |
264 | cmsg->cmsg_type = SCM_CREDENTIALS; | |
0e391e57 | 265 | memcpy(CMSG_DATA(cmsg), &cred, sizeof(cred)); |
b0a33c1e | 266 | |
604f0955 ÇO |
267 | msg.msg_name = NULL; |
268 | msg.msg_namelen = 0; | |
b0a33c1e | 269 | |
604f0955 ÇO |
270 | iov.iov_base = data ? data : buf; |
271 | iov.iov_len = data ? size : sizeof(buf); | |
272 | msg.msg_iov = &iov; | |
273 | msg.msg_iovlen = 1; | |
b0a33c1e | 274 | |
6168ff15 | 275 | return sendmsg(fd, &msg, MSG_NOSIGNAL); |
b0a33c1e | 276 | } |
277 | ||
aae93dd3 | 278 | int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size) |
b0a33c1e | 279 | { |
77b0073a | 280 | struct msghdr msg = {0}; |
604f0955 ÇO |
281 | struct iovec iov; |
282 | struct cmsghdr *cmsg; | |
b0a33c1e | 283 | struct ucred cred; |
b0a33c1e | 284 | int ret; |
caf3beb0 CB |
285 | char cmsgbuf[CMSG_SPACE(sizeof(cred))] = {0}; |
286 | char buf[1] = {0}; | |
b0a33c1e | 287 | |
604f0955 ÇO |
288 | msg.msg_name = NULL; |
289 | msg.msg_namelen = 0; | |
290 | msg.msg_control = cmsgbuf; | |
291 | msg.msg_controllen = sizeof(cmsgbuf); | |
b0a33c1e | 292 | |
604f0955 ÇO |
293 | iov.iov_base = data ? data : buf; |
294 | iov.iov_len = data ? size : sizeof(buf); | |
295 | msg.msg_iov = &iov; | |
296 | msg.msg_iovlen = 1; | |
b0a33c1e | 297 | |
298 | ret = recvmsg(fd, &msg, 0); | |
299 | if (ret <= 0) | |
300 | goto out; | |
301 | ||
604f0955 | 302 | cmsg = CMSG_FIRSTHDR(&msg); |
b0a33c1e | 303 | |
604f0955 | 304 | if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)) && |
77b0073a CB |
305 | cmsg->cmsg_level == SOL_SOCKET && |
306 | cmsg->cmsg_type == SCM_CREDENTIALS) { | |
0e391e57 | 307 | memcpy(&cred, CMSG_DATA(cmsg), sizeof(cred)); |
77b0073a CB |
308 | if (cred.uid && |
309 | (cred.uid != getuid() || cred.gid != getgid())) { | |
9044b79e | 310 | INFO("Message denied for '%d/%d'", cred.uid, cred.gid); |
311 | errno = EACCES; | |
312 | return -1; | |
2dcb28a9 | 313 | } |
604f0955 | 314 | } |
9044b79e | 315 | |
b0a33c1e | 316 | out: |
604f0955 | 317 | return ret; |
b0a33c1e | 318 | } |