]>
Commit | Line | Data |
---|---|---|
17bff52b MK |
1 | /* |
2 | * Helper for QEMU Proxy FS Driver | |
3 | * Copyright IBM, Corp. 2011 | |
4 | * | |
5 | * Authors: | |
6 | * M. Mohan Kumar <[email protected]> | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
9 | * the COPYING file in the top-level directory. | |
10 | */ | |
e7e4a6cc | 11 | |
17bff52b | 12 | #include <sys/resource.h> |
17bff52b | 13 | #include <getopt.h> |
17bff52b MK |
14 | #include <syslog.h> |
15 | #include <sys/capability.h> | |
16 | #include <sys/fsuid.h> | |
b178adc3 | 17 | #include <sys/vfs.h> |
d090e452 MK |
18 | #include <sys/ioctl.h> |
19 | #include <linux/fs.h> | |
20 | #ifdef CONFIG_LINUX_MAGIC_H | |
21 | #include <linux/magic.h> | |
22 | #endif | |
17bff52b | 23 | #include "qemu-common.h" |
1de7afc9 PB |
24 | #include "qemu/sockets.h" |
25 | #include "qemu/xattr.h" | |
17bff52b MK |
26 | #include "virtio-9p-marshal.h" |
27 | #include "hw/9pfs/virtio-9p-proxy.h" | |
daf0b9ac | 28 | #include "fsdev/virtio-9p-marshal.h" |
17bff52b MK |
29 | |
30 | #define PROGNAME "virtfs-proxy-helper" | |
31 | ||
d090e452 MK |
32 | #ifndef XFS_SUPER_MAGIC |
33 | #define XFS_SUPER_MAGIC 0x58465342 | |
34 | #endif | |
35 | #ifndef EXT2_SUPER_MAGIC | |
36 | #define EXT2_SUPER_MAGIC 0xEF53 | |
37 | #endif | |
38 | #ifndef REISERFS_SUPER_MAGIC | |
39 | #define REISERFS_SUPER_MAGIC 0x52654973 | |
40 | #endif | |
41 | #ifndef BTRFS_SUPER_MAGIC | |
42 | #define BTRFS_SUPER_MAGIC 0x9123683E | |
43 | #endif | |
44 | ||
17bff52b MK |
45 | static struct option helper_opts[] = { |
46 | {"fd", required_argument, NULL, 'f'}, | |
47 | {"path", required_argument, NULL, 'p'}, | |
48 | {"nodaemon", no_argument, NULL, 'n'}, | |
84a87cc4 MK |
49 | {"socket", required_argument, NULL, 's'}, |
50 | {"uid", required_argument, NULL, 'u'}, | |
51 | {"gid", required_argument, NULL, 'g'}, | |
17bff52b MK |
52 | }; |
53 | ||
54 | static bool is_daemon; | |
d090e452 | 55 | static bool get_version; /* IOC getversion IOCTL supported */ |
17bff52b | 56 | |
c5c7d3f0 | 57 | static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...) |
17bff52b MK |
58 | { |
59 | va_list ap; | |
60 | ||
61 | va_start(ap, format); | |
62 | if (is_daemon) { | |
63 | vsyslog(LOG_CRIT, format, ap); | |
64 | } else { | |
65 | vfprintf(stderr, format, ap); | |
66 | } | |
67 | va_end(ap); | |
68 | } | |
69 | ||
70 | static void do_perror(const char *string) | |
71 | { | |
72 | if (is_daemon) { | |
73 | syslog(LOG_CRIT, "%s:%s", string, strerror(errno)); | |
74 | } else { | |
75 | fprintf(stderr, "%s:%s\n", string, strerror(errno)); | |
76 | } | |
77 | } | |
78 | ||
79 | static int do_cap_set(cap_value_t *cap_value, int size, int reset) | |
80 | { | |
81 | cap_t caps; | |
82 | if (reset) { | |
83 | /* | |
84 | * Start with an empty set and set permitted and effective | |
85 | */ | |
86 | caps = cap_init(); | |
87 | if (caps == NULL) { | |
88 | do_perror("cap_init"); | |
89 | return -1; | |
90 | } | |
91 | if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) { | |
92 | do_perror("cap_set_flag"); | |
93 | goto error; | |
94 | } | |
95 | } else { | |
96 | caps = cap_get_proc(); | |
97 | if (!caps) { | |
98 | do_perror("cap_get_proc"); | |
99 | return -1; | |
100 | } | |
101 | } | |
102 | if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) { | |
103 | do_perror("cap_set_flag"); | |
104 | goto error; | |
105 | } | |
106 | if (cap_set_proc(caps) < 0) { | |
107 | do_perror("cap_set_proc"); | |
108 | goto error; | |
109 | } | |
110 | cap_free(caps); | |
111 | return 0; | |
112 | ||
113 | error: | |
114 | cap_free(caps); | |
115 | return -1; | |
116 | } | |
117 | ||
118 | static int init_capabilities(void) | |
119 | { | |
de7ad4ce | 120 | /* helper needs following capabilities only */ |
17bff52b MK |
121 | cap_value_t cap_list[] = { |
122 | CAP_CHOWN, | |
123 | CAP_DAC_OVERRIDE, | |
124 | CAP_FOWNER, | |
125 | CAP_FSETID, | |
126 | CAP_SETGID, | |
127 | CAP_MKNOD, | |
128 | CAP_SETUID, | |
129 | }; | |
130 | return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1); | |
131 | } | |
132 | ||
133 | static int socket_read(int sockfd, void *buff, ssize_t size) | |
134 | { | |
135 | ssize_t retval, total = 0; | |
136 | ||
137 | while (size) { | |
138 | retval = read(sockfd, buff, size); | |
139 | if (retval == 0) { | |
140 | return -EIO; | |
141 | } | |
142 | if (retval < 0) { | |
143 | if (errno == EINTR) { | |
144 | continue; | |
145 | } | |
146 | return -errno; | |
147 | } | |
148 | size -= retval; | |
149 | buff += retval; | |
150 | total += retval; | |
151 | } | |
152 | return total; | |
153 | } | |
154 | ||
155 | static int socket_write(int sockfd, void *buff, ssize_t size) | |
156 | { | |
157 | ssize_t retval, total = 0; | |
158 | ||
159 | while (size) { | |
160 | retval = write(sockfd, buff, size); | |
161 | if (retval < 0) { | |
162 | if (errno == EINTR) { | |
163 | continue; | |
164 | } | |
165 | return -errno; | |
166 | } | |
167 | size -= retval; | |
168 | buff += retval; | |
169 | total += retval; | |
170 | } | |
171 | return total; | |
172 | } | |
173 | ||
174 | static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header) | |
175 | { | |
176 | int retval; | |
177 | ||
178 | /* | |
179 | * read the request header. | |
180 | */ | |
181 | iovec->iov_len = 0; | |
182 | retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ); | |
183 | if (retval < 0) { | |
184 | return retval; | |
185 | } | |
186 | iovec->iov_len = PROXY_HDR_SZ; | |
187 | retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size); | |
188 | if (retval < 0) { | |
189 | return retval; | |
190 | } | |
191 | /* | |
192 | * We can't process message.size > PROXY_MAX_IO_SZ. | |
193 | * Treat it as fatal error | |
194 | */ | |
195 | if (header->size > PROXY_MAX_IO_SZ) { | |
196 | return -ENOBUFS; | |
197 | } | |
198 | retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size); | |
199 | if (retval < 0) { | |
200 | return retval; | |
201 | } | |
202 | iovec->iov_len += header->size; | |
203 | return 0; | |
204 | } | |
205 | ||
daf0b9ac MK |
206 | static int send_fd(int sockfd, int fd) |
207 | { | |
208 | struct msghdr msg; | |
209 | struct iovec iov; | |
210 | int retval, data; | |
211 | struct cmsghdr *cmsg; | |
212 | union MsgControl msg_control; | |
213 | ||
214 | iov.iov_base = &data; | |
215 | iov.iov_len = sizeof(data); | |
216 | ||
217 | memset(&msg, 0, sizeof(msg)); | |
218 | msg.msg_iov = &iov; | |
219 | msg.msg_iovlen = 1; | |
220 | /* No ancillary data on error */ | |
221 | if (fd < 0) { | |
222 | /* fd is really negative errno if the request failed */ | |
223 | data = fd; | |
224 | } else { | |
225 | data = V9FS_FD_VALID; | |
226 | msg.msg_control = &msg_control; | |
227 | msg.msg_controllen = sizeof(msg_control); | |
228 | ||
229 | cmsg = &msg_control.cmsg; | |
230 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); | |
231 | cmsg->cmsg_level = SOL_SOCKET; | |
232 | cmsg->cmsg_type = SCM_RIGHTS; | |
233 | memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); | |
234 | } | |
235 | ||
236 | do { | |
237 | retval = sendmsg(sockfd, &msg, 0); | |
238 | } while (retval < 0 && errno == EINTR); | |
239 | if (fd >= 0) { | |
240 | close(fd); | |
241 | } | |
242 | if (retval < 0) { | |
243 | return retval; | |
244 | } | |
245 | return 0; | |
246 | } | |
247 | ||
39f8c32c MK |
248 | static int send_status(int sockfd, struct iovec *iovec, int status) |
249 | { | |
250 | ProxyHeader header; | |
c7e775e4 | 251 | int retval, msg_size; |
39f8c32c MK |
252 | |
253 | if (status < 0) { | |
254 | header.type = T_ERROR; | |
255 | } else { | |
256 | header.type = T_SUCCESS; | |
257 | } | |
258 | header.size = sizeof(status); | |
259 | /* | |
260 | * marshal the return status. We don't check error. | |
261 | * because we are sure we have enough space for the status | |
262 | */ | |
263 | msg_size = proxy_marshal(iovec, 0, "ddd", header.type, | |
264 | header.size, status); | |
821c4476 SZ |
265 | if (msg_size < 0) { |
266 | return msg_size; | |
267 | } | |
39f8c32c MK |
268 | retval = socket_write(sockfd, iovec->iov_base, msg_size); |
269 | if (retval < 0) { | |
270 | return retval; | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
daf0b9ac MK |
275 | /* |
276 | * from man 7 capabilities, section | |
277 | * Effect of User ID Changes on Capabilities: | |
9fd2ecdc PB |
278 | * If the effective user ID is changed from nonzero to 0, then the permitted |
279 | * set is copied to the effective set. If the effective user ID is changed | |
280 | * from 0 to nonzero, then all capabilities are are cleared from the effective | |
281 | * set. | |
282 | * | |
283 | * The setfsuid/setfsgid man pages warn that changing the effective user ID may | |
284 | * expose the program to unwanted signals, but this is not true anymore: for an | |
285 | * unprivileged (without CAP_KILL) program to send a signal, the real or | |
286 | * effective user ID of the sending process must equal the real or saved user | |
287 | * ID of the target process. Even when dropping privileges, it is enough to | |
288 | * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't | |
289 | * be exposed to signals. So just use setresuid/setresgid. | |
daf0b9ac | 290 | */ |
9fd2ecdc | 291 | static int setugid(int uid, int gid, int *suid, int *sgid) |
daf0b9ac | 292 | { |
9fd2ecdc PB |
293 | int retval; |
294 | ||
daf0b9ac | 295 | /* |
9fd2ecdc | 296 | * We still need DAC_OVERRIDE because we don't change |
daf0b9ac MK |
297 | * supplementary group ids, and hence may be subjected DAC rules |
298 | */ | |
299 | cap_value_t cap_list[] = { | |
300 | CAP_DAC_OVERRIDE, | |
301 | }; | |
302 | ||
9fd2ecdc PB |
303 | *suid = geteuid(); |
304 | *sgid = getegid(); | |
305 | ||
306 | if (setresgid(-1, gid, *sgid) == -1) { | |
307 | retval = -errno; | |
308 | goto err_out; | |
309 | } | |
310 | ||
311 | if (setresuid(-1, uid, *suid) == -1) { | |
312 | retval = -errno; | |
313 | goto err_sgid; | |
314 | } | |
daf0b9ac MK |
315 | |
316 | if (uid != 0 || gid != 0) { | |
9fd2ecdc PB |
317 | if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) { |
318 | retval = -errno; | |
319 | goto err_suid; | |
320 | } | |
daf0b9ac MK |
321 | } |
322 | return 0; | |
9fd2ecdc PB |
323 | |
324 | err_suid: | |
325 | if (setresuid(-1, *suid, *suid) == -1) { | |
326 | abort(); | |
327 | } | |
328 | err_sgid: | |
329 | if (setresgid(-1, *sgid, *sgid) == -1) { | |
330 | abort(); | |
331 | } | |
332 | err_out: | |
333 | return retval; | |
334 | } | |
335 | ||
336 | /* | |
337 | * This is used to reset the ugid back with the saved values | |
338 | * There is nothing much we can do checking error values here. | |
339 | */ | |
340 | static void resetugid(int suid, int sgid) | |
341 | { | |
342 | if (setresgid(-1, sgid, sgid) == -1) { | |
343 | abort(); | |
344 | } | |
345 | if (setresuid(-1, suid, suid) == -1) { | |
346 | abort(); | |
347 | } | |
daf0b9ac MK |
348 | } |
349 | ||
b178adc3 MK |
350 | /* |
351 | * send response in two parts | |
352 | * 1) ProxyHeader | |
353 | * 2) Response or error status | |
354 | * This function should be called with marshaled response | |
355 | * send_response constructs header part and error part only. | |
356 | * send response sends {ProxyHeader,Response} if the request was success | |
357 | * otherwise sends {ProxyHeader,error status} | |
358 | */ | |
359 | static int send_response(int sock, struct iovec *iovec, int size) | |
360 | { | |
361 | int retval; | |
362 | ProxyHeader header; | |
363 | ||
364 | /* | |
365 | * If response size exceeds available iovec->iov_len, | |
366 | * we return ENOBUFS | |
367 | */ | |
368 | if (size > PROXY_MAX_IO_SZ) { | |
369 | size = -ENOBUFS; | |
370 | } | |
371 | ||
372 | if (size < 0) { | |
373 | /* | |
374 | * In case of error we would not have got the error encoded | |
375 | * already so encode the error here. | |
376 | */ | |
377 | header.type = T_ERROR; | |
378 | header.size = sizeof(size); | |
379 | proxy_marshal(iovec, PROXY_HDR_SZ, "d", size); | |
380 | } else { | |
381 | header.type = T_SUCCESS; | |
382 | header.size = size; | |
383 | } | |
384 | proxy_marshal(iovec, 0, "dd", header.type, header.size); | |
385 | retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ); | |
386 | if (retval < 0) { | |
c7e775e4 | 387 | return retval; |
b178adc3 MK |
388 | } |
389 | return 0; | |
390 | } | |
391 | ||
d090e452 MK |
392 | /* |
393 | * gets generation number | |
394 | * returns -errno on failure and sizeof(generation number) on success | |
395 | */ | |
396 | static int do_getversion(struct iovec *iovec, struct iovec *out_iovec) | |
397 | { | |
398 | uint64_t version; | |
399 | int retval = -ENOTTY; | |
400 | #ifdef FS_IOC_GETVERSION | |
401 | int fd; | |
402 | V9fsString path; | |
403 | #endif | |
404 | ||
405 | ||
406 | /* no need to issue ioctl */ | |
407 | if (!get_version) { | |
408 | version = 0; | |
409 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version); | |
410 | return retval; | |
411 | } | |
412 | #ifdef FS_IOC_GETVERSION | |
413 | retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path); | |
414 | if (retval < 0) { | |
415 | return retval; | |
416 | } | |
417 | ||
418 | fd = open(path.data, O_RDONLY); | |
419 | if (fd < 0) { | |
420 | retval = -errno; | |
421 | goto err_out; | |
422 | } | |
423 | if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) { | |
424 | retval = -errno; | |
425 | } else { | |
426 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version); | |
427 | } | |
428 | close(fd); | |
429 | err_out: | |
430 | v9fs_string_free(&path); | |
431 | #endif | |
432 | return retval; | |
433 | } | |
434 | ||
d52b09e4 MK |
435 | static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec) |
436 | { | |
437 | int size = 0, offset, retval; | |
438 | V9fsString path, name, xattr; | |
439 | ||
440 | v9fs_string_init(&xattr); | |
441 | v9fs_string_init(&path); | |
442 | retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path); | |
443 | if (retval < 0) { | |
444 | return retval; | |
445 | } | |
446 | offset = PROXY_HDR_SZ + retval; | |
447 | ||
448 | if (size) { | |
449 | xattr.data = g_malloc(size); | |
450 | xattr.size = size; | |
451 | } | |
452 | switch (type) { | |
453 | case T_LGETXATTR: | |
454 | v9fs_string_init(&name); | |
455 | retval = proxy_unmarshal(iovec, offset, "s", &name); | |
456 | if (retval > 0) { | |
457 | retval = lgetxattr(path.data, name.data, xattr.data, size); | |
458 | if (retval < 0) { | |
459 | retval = -errno; | |
460 | } else { | |
461 | xattr.size = retval; | |
462 | } | |
463 | } | |
464 | v9fs_string_free(&name); | |
465 | break; | |
466 | case T_LLISTXATTR: | |
467 | retval = llistxattr(path.data, xattr.data, size); | |
468 | if (retval < 0) { | |
469 | retval = -errno; | |
470 | } else { | |
471 | xattr.size = retval; | |
472 | } | |
473 | break; | |
474 | } | |
475 | if (retval < 0) { | |
476 | goto err_out; | |
477 | } | |
478 | ||
479 | if (!size) { | |
480 | proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval); | |
481 | retval = sizeof(retval); | |
482 | } else { | |
483 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr); | |
484 | } | |
485 | err_out: | |
486 | v9fs_string_free(&xattr); | |
487 | v9fs_string_free(&path); | |
488 | return retval; | |
489 | } | |
490 | ||
b178adc3 MK |
491 | static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat) |
492 | { | |
493 | memset(pr_stat, 0, sizeof(*pr_stat)); | |
494 | pr_stat->st_dev = stat->st_dev; | |
495 | pr_stat->st_ino = stat->st_ino; | |
496 | pr_stat->st_nlink = stat->st_nlink; | |
497 | pr_stat->st_mode = stat->st_mode; | |
498 | pr_stat->st_uid = stat->st_uid; | |
499 | pr_stat->st_gid = stat->st_gid; | |
500 | pr_stat->st_rdev = stat->st_rdev; | |
501 | pr_stat->st_size = stat->st_size; | |
502 | pr_stat->st_blksize = stat->st_blksize; | |
503 | pr_stat->st_blocks = stat->st_blocks; | |
504 | pr_stat->st_atim_sec = stat->st_atim.tv_sec; | |
505 | pr_stat->st_atim_nsec = stat->st_atim.tv_nsec; | |
506 | pr_stat->st_mtim_sec = stat->st_mtim.tv_sec; | |
507 | pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec; | |
508 | pr_stat->st_ctim_sec = stat->st_ctim.tv_sec; | |
509 | pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec; | |
510 | } | |
511 | ||
512 | static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs) | |
513 | { | |
514 | memset(pr_stfs, 0, sizeof(*pr_stfs)); | |
515 | pr_stfs->f_type = stfs->f_type; | |
516 | pr_stfs->f_bsize = stfs->f_bsize; | |
517 | pr_stfs->f_blocks = stfs->f_blocks; | |
518 | pr_stfs->f_bfree = stfs->f_bfree; | |
519 | pr_stfs->f_bavail = stfs->f_bavail; | |
520 | pr_stfs->f_files = stfs->f_files; | |
521 | pr_stfs->f_ffree = stfs->f_ffree; | |
522 | pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0]; | |
523 | pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1]; | |
524 | pr_stfs->f_namelen = stfs->f_namelen; | |
525 | pr_stfs->f_frsize = stfs->f_frsize; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Gets stat/statfs information and packs in out_iovec structure | |
530 | * on success returns number of bytes packed in out_iovec struture | |
531 | * otherwise returns -errno | |
532 | */ | |
533 | static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec) | |
534 | { | |
535 | int retval; | |
536 | V9fsString path; | |
537 | ProxyStat pr_stat; | |
538 | ProxyStatFS pr_stfs; | |
539 | struct stat st_buf; | |
540 | struct statfs stfs_buf; | |
541 | ||
542 | v9fs_string_init(&path); | |
543 | retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path); | |
544 | if (retval < 0) { | |
545 | return retval; | |
546 | } | |
547 | ||
548 | switch (type) { | |
549 | case T_LSTAT: | |
550 | retval = lstat(path.data, &st_buf); | |
551 | if (retval < 0) { | |
552 | retval = -errno; | |
553 | } else { | |
554 | stat_to_prstat(&pr_stat, &st_buf); | |
555 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, | |
556 | "qqqdddqqqqqqqqqq", pr_stat.st_dev, | |
557 | pr_stat.st_ino, pr_stat.st_nlink, | |
558 | pr_stat.st_mode, pr_stat.st_uid, | |
559 | pr_stat.st_gid, pr_stat.st_rdev, | |
560 | pr_stat.st_size, pr_stat.st_blksize, | |
561 | pr_stat.st_blocks, | |
562 | pr_stat.st_atim_sec, pr_stat.st_atim_nsec, | |
563 | pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec, | |
564 | pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec); | |
565 | } | |
566 | break; | |
567 | case T_STATFS: | |
568 | retval = statfs(path.data, &stfs_buf); | |
569 | if (retval < 0) { | |
570 | retval = -errno; | |
571 | } else { | |
572 | statfs_to_prstatfs(&pr_stfs, &stfs_buf); | |
573 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, | |
574 | "qqqqqqqqqqq", pr_stfs.f_type, | |
575 | pr_stfs.f_bsize, pr_stfs.f_blocks, | |
576 | pr_stfs.f_bfree, pr_stfs.f_bavail, | |
577 | pr_stfs.f_files, pr_stfs.f_ffree, | |
578 | pr_stfs.f_fsid[0], pr_stfs.f_fsid[1], | |
579 | pr_stfs.f_namelen, pr_stfs.f_frsize); | |
580 | } | |
581 | break; | |
582 | } | |
583 | v9fs_string_free(&path); | |
584 | return retval; | |
585 | } | |
586 | ||
587 | static int do_readlink(struct iovec *iovec, struct iovec *out_iovec) | |
588 | { | |
589 | char *buffer; | |
590 | int size, retval; | |
591 | V9fsString target, path; | |
592 | ||
593 | v9fs_string_init(&path); | |
594 | retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size); | |
595 | if (retval < 0) { | |
596 | v9fs_string_free(&path); | |
597 | return retval; | |
598 | } | |
599 | buffer = g_malloc(size); | |
600 | v9fs_string_init(&target); | |
d77f7779 | 601 | retval = readlink(path.data, buffer, size - 1); |
b178adc3 MK |
602 | if (retval > 0) { |
603 | buffer[retval] = '\0'; | |
604 | v9fs_string_sprintf(&target, "%s", buffer); | |
605 | retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target); | |
606 | } else { | |
607 | retval = -errno; | |
608 | } | |
609 | g_free(buffer); | |
610 | v9fs_string_free(&target); | |
611 | v9fs_string_free(&path); | |
612 | return retval; | |
613 | } | |
614 | ||
39f8c32c MK |
615 | /* |
616 | * create other filesystem objects and send 0 on success | |
617 | * return -errno on error | |
618 | */ | |
619 | static int do_create_others(int type, struct iovec *iovec) | |
620 | { | |
621 | dev_t rdev; | |
622 | int retval = 0; | |
623 | int offset = PROXY_HDR_SZ; | |
624 | V9fsString oldpath, path; | |
625 | int mode, uid, gid, cur_uid, cur_gid; | |
626 | ||
627 | v9fs_string_init(&path); | |
628 | v9fs_string_init(&oldpath); | |
39f8c32c MK |
629 | |
630 | retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid); | |
631 | if (retval < 0) { | |
632 | return retval; | |
633 | } | |
634 | offset += retval; | |
9fd2ecdc | 635 | retval = setugid(uid, gid, &cur_uid, &cur_gid); |
39f8c32c | 636 | if (retval < 0) { |
9fd2ecdc | 637 | goto unmarshal_err_out; |
39f8c32c MK |
638 | } |
639 | switch (type) { | |
640 | case T_MKNOD: | |
641 | retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev); | |
642 | if (retval < 0) { | |
643 | goto err_out; | |
644 | } | |
645 | retval = mknod(path.data, mode, rdev); | |
646 | break; | |
647 | case T_MKDIR: | |
648 | retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode); | |
649 | if (retval < 0) { | |
650 | goto err_out; | |
651 | } | |
652 | retval = mkdir(path.data, mode); | |
653 | break; | |
654 | case T_SYMLINK: | |
655 | retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path); | |
656 | if (retval < 0) { | |
657 | goto err_out; | |
658 | } | |
659 | retval = symlink(oldpath.data, path.data); | |
660 | break; | |
661 | } | |
662 | if (retval < 0) { | |
663 | retval = -errno; | |
664 | } | |
665 | ||
666 | err_out: | |
9fd2ecdc PB |
667 | resetugid(cur_uid, cur_gid); |
668 | unmarshal_err_out: | |
39f8c32c MK |
669 | v9fs_string_free(&path); |
670 | v9fs_string_free(&oldpath); | |
39f8c32c MK |
671 | return retval; |
672 | } | |
673 | ||
daf0b9ac MK |
674 | /* |
675 | * create a file and send fd on success | |
676 | * return -errno on error | |
677 | */ | |
678 | static int do_create(struct iovec *iovec) | |
679 | { | |
680 | int ret; | |
681 | V9fsString path; | |
682 | int flags, mode, uid, gid, cur_uid, cur_gid; | |
683 | ||
684 | v9fs_string_init(&path); | |
685 | ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd", | |
686 | &path, &flags, &mode, &uid, &gid); | |
687 | if (ret < 0) { | |
688 | goto unmarshal_err_out; | |
689 | } | |
9fd2ecdc | 690 | ret = setugid(uid, gid, &cur_uid, &cur_gid); |
daf0b9ac | 691 | if (ret < 0) { |
9fd2ecdc | 692 | goto unmarshal_err_out; |
daf0b9ac MK |
693 | } |
694 | ret = open(path.data, flags, mode); | |
695 | if (ret < 0) { | |
696 | ret = -errno; | |
697 | } | |
698 | ||
9fd2ecdc | 699 | resetugid(cur_uid, cur_gid); |
daf0b9ac MK |
700 | unmarshal_err_out: |
701 | v9fs_string_free(&path); | |
702 | return ret; | |
703 | } | |
704 | ||
705 | /* | |
706 | * open a file and send fd on success | |
707 | * return -errno on error | |
708 | */ | |
709 | static int do_open(struct iovec *iovec) | |
710 | { | |
711 | int flags, ret; | |
712 | V9fsString path; | |
713 | ||
714 | v9fs_string_init(&path); | |
715 | ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags); | |
716 | if (ret < 0) { | |
717 | goto err_out; | |
718 | } | |
719 | ret = open(path.data, flags); | |
720 | if (ret < 0) { | |
721 | ret = -errno; | |
722 | } | |
723 | err_out: | |
724 | v9fs_string_free(&path); | |
725 | return ret; | |
726 | } | |
727 | ||
84a87cc4 MK |
728 | /* create unix domain socket and return the descriptor */ |
729 | static int proxy_socket(const char *path, uid_t uid, gid_t gid) | |
730 | { | |
731 | int sock, client; | |
732 | struct sockaddr_un proxy, qemu; | |
733 | socklen_t size; | |
734 | ||
735 | /* requested socket already exists, refuse to start */ | |
736 | if (!access(path, F_OK)) { | |
737 | do_log(LOG_CRIT, "socket already exists\n"); | |
738 | return -1; | |
739 | } | |
740 | ||
25ee9a7f | 741 | g_assert(strlen(path) < sizeof(proxy.sun_path)); |
84a87cc4 MK |
742 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
743 | if (sock < 0) { | |
744 | do_perror("socket"); | |
745 | return -1; | |
746 | } | |
747 | ||
748 | /* mask other part of mode bits */ | |
749 | umask(7); | |
750 | ||
751 | proxy.sun_family = AF_UNIX; | |
752 | strcpy(proxy.sun_path, path); | |
753 | if (bind(sock, (struct sockaddr *)&proxy, | |
754 | sizeof(struct sockaddr_un)) < 0) { | |
755 | do_perror("bind"); | |
88ea8ed7 | 756 | goto error; |
84a87cc4 MK |
757 | } |
758 | if (chown(proxy.sun_path, uid, gid) < 0) { | |
759 | do_perror("chown"); | |
88ea8ed7 | 760 | goto error; |
84a87cc4 MK |
761 | } |
762 | if (listen(sock, 1) < 0) { | |
763 | do_perror("listen"); | |
88ea8ed7 | 764 | goto error; |
84a87cc4 MK |
765 | } |
766 | ||
b0f9300c | 767 | size = sizeof(qemu); |
84a87cc4 MK |
768 | client = accept(sock, (struct sockaddr *)&qemu, &size); |
769 | if (client < 0) { | |
770 | do_perror("accept"); | |
88ea8ed7 | 771 | goto error; |
84a87cc4 | 772 | } |
88ea8ed7 | 773 | close(sock); |
84a87cc4 | 774 | return client; |
88ea8ed7 GA |
775 | |
776 | error: | |
777 | close(sock); | |
778 | return -1; | |
84a87cc4 MK |
779 | } |
780 | ||
17bff52b MK |
781 | static void usage(char *prog) |
782 | { | |
783 | fprintf(stderr, "usage: %s\n" | |
784 | " -p|--path <path> 9p path to export\n" | |
785 | " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n" | |
84a87cc4 MK |
786 | " {-s|--socket <socketname> socket file used for communication\n" |
787 | " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give " | |
788 | " access to this socket\n" | |
789 | " \tNote: -s & -f can not be used together\n" | |
17bff52b MK |
790 | " [-n|--nodaemon] Run as a normal program\n", |
791 | basename(prog)); | |
792 | } | |
793 | ||
39f8c32c MK |
794 | static int process_reply(int sock, int type, |
795 | struct iovec *out_iovec, int retval) | |
daf0b9ac MK |
796 | { |
797 | switch (type) { | |
798 | case T_OPEN: | |
799 | case T_CREATE: | |
800 | if (send_fd(sock, retval) < 0) { | |
801 | return -1; | |
802 | } | |
803 | break; | |
39f8c32c MK |
804 | case T_MKNOD: |
805 | case T_MKDIR: | |
806 | case T_SYMLINK: | |
807 | case T_LINK: | |
ea75fc4e MK |
808 | case T_CHMOD: |
809 | case T_CHOWN: | |
810 | case T_TRUNCATE: | |
811 | case T_UTIME: | |
812 | case T_RENAME: | |
813 | case T_REMOVE: | |
d52b09e4 MK |
814 | case T_LSETXATTR: |
815 | case T_LREMOVEXATTR: | |
39f8c32c MK |
816 | if (send_status(sock, out_iovec, retval) < 0) { |
817 | return -1; | |
818 | } | |
819 | break; | |
b178adc3 MK |
820 | case T_LSTAT: |
821 | case T_STATFS: | |
822 | case T_READLINK: | |
d52b09e4 MK |
823 | case T_LGETXATTR: |
824 | case T_LLISTXATTR: | |
d090e452 | 825 | case T_GETVERSION: |
b178adc3 MK |
826 | if (send_response(sock, out_iovec, retval) < 0) { |
827 | return -1; | |
828 | } | |
829 | break; | |
daf0b9ac MK |
830 | default: |
831 | return -1; | |
832 | break; | |
833 | } | |
834 | return 0; | |
835 | } | |
836 | ||
17bff52b MK |
837 | static int process_requests(int sock) |
838 | { | |
d52b09e4 MK |
839 | int flags; |
840 | int size = 0; | |
daf0b9ac | 841 | int retval = 0; |
ea75fc4e | 842 | uint64_t offset; |
17bff52b | 843 | ProxyHeader header; |
ea75fc4e | 844 | int mode, uid, gid; |
d52b09e4 | 845 | V9fsString name, value; |
ea75fc4e | 846 | struct timespec spec[2]; |
39f8c32c MK |
847 | V9fsString oldpath, path; |
848 | struct iovec in_iovec, out_iovec; | |
849 | ||
850 | in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); | |
851 | in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; | |
852 | out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ); | |
853 | out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ; | |
17bff52b | 854 | |
17bff52b | 855 | while (1) { |
daf0b9ac MK |
856 | /* |
857 | * initialize the header type, so that we send | |
858 | * response to proper request type. | |
859 | */ | |
860 | header.type = 0; | |
17bff52b MK |
861 | retval = read_request(sock, &in_iovec, &header); |
862 | if (retval < 0) { | |
daf0b9ac MK |
863 | goto err_out; |
864 | } | |
865 | ||
866 | switch (header.type) { | |
867 | case T_OPEN: | |
868 | retval = do_open(&in_iovec); | |
869 | break; | |
870 | case T_CREATE: | |
871 | retval = do_create(&in_iovec); | |
872 | break; | |
39f8c32c MK |
873 | case T_MKNOD: |
874 | case T_MKDIR: | |
875 | case T_SYMLINK: | |
876 | retval = do_create_others(header.type, &in_iovec); | |
877 | break; | |
878 | case T_LINK: | |
879 | v9fs_string_init(&path); | |
880 | v9fs_string_init(&oldpath); | |
881 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, | |
882 | "ss", &oldpath, &path); | |
883 | if (retval > 0) { | |
884 | retval = link(oldpath.data, path.data); | |
885 | if (retval < 0) { | |
886 | retval = -errno; | |
887 | } | |
888 | } | |
889 | v9fs_string_free(&oldpath); | |
890 | v9fs_string_free(&path); | |
891 | break; | |
b178adc3 MK |
892 | case T_LSTAT: |
893 | case T_STATFS: | |
894 | retval = do_stat(header.type, &in_iovec, &out_iovec); | |
895 | break; | |
896 | case T_READLINK: | |
897 | retval = do_readlink(&in_iovec, &out_iovec); | |
898 | break; | |
ea75fc4e MK |
899 | case T_CHMOD: |
900 | v9fs_string_init(&path); | |
901 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, | |
902 | "sd", &path, &mode); | |
903 | if (retval > 0) { | |
904 | retval = chmod(path.data, mode); | |
905 | if (retval < 0) { | |
906 | retval = -errno; | |
907 | } | |
908 | } | |
909 | v9fs_string_free(&path); | |
910 | break; | |
911 | case T_CHOWN: | |
912 | v9fs_string_init(&path); | |
913 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path, | |
914 | &uid, &gid); | |
915 | if (retval > 0) { | |
916 | retval = lchown(path.data, uid, gid); | |
917 | if (retval < 0) { | |
918 | retval = -errno; | |
919 | } | |
920 | } | |
921 | v9fs_string_free(&path); | |
922 | break; | |
923 | case T_TRUNCATE: | |
924 | v9fs_string_init(&path); | |
925 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq", | |
926 | &path, &offset); | |
927 | if (retval > 0) { | |
928 | retval = truncate(path.data, offset); | |
929 | if (retval < 0) { | |
930 | retval = -errno; | |
931 | } | |
932 | } | |
933 | v9fs_string_free(&path); | |
934 | break; | |
935 | case T_UTIME: | |
936 | v9fs_string_init(&path); | |
937 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path, | |
938 | &spec[0].tv_sec, &spec[0].tv_nsec, | |
939 | &spec[1].tv_sec, &spec[1].tv_nsec); | |
940 | if (retval > 0) { | |
941 | retval = qemu_utimens(path.data, spec); | |
942 | if (retval < 0) { | |
943 | retval = -errno; | |
944 | } | |
945 | } | |
946 | v9fs_string_free(&path); | |
947 | break; | |
948 | case T_RENAME: | |
949 | v9fs_string_init(&path); | |
950 | v9fs_string_init(&oldpath); | |
951 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, | |
952 | "ss", &oldpath, &path); | |
953 | if (retval > 0) { | |
954 | retval = rename(oldpath.data, path.data); | |
955 | if (retval < 0) { | |
956 | retval = -errno; | |
957 | } | |
958 | } | |
959 | v9fs_string_free(&oldpath); | |
960 | v9fs_string_free(&path); | |
961 | break; | |
962 | case T_REMOVE: | |
963 | v9fs_string_init(&path); | |
964 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path); | |
965 | if (retval > 0) { | |
966 | retval = remove(path.data); | |
967 | if (retval < 0) { | |
968 | retval = -errno; | |
969 | } | |
970 | } | |
971 | v9fs_string_free(&path); | |
972 | break; | |
d52b09e4 MK |
973 | case T_LGETXATTR: |
974 | case T_LLISTXATTR: | |
975 | retval = do_getxattr(header.type, &in_iovec, &out_iovec); | |
976 | break; | |
977 | case T_LSETXATTR: | |
978 | v9fs_string_init(&path); | |
979 | v9fs_string_init(&name); | |
980 | v9fs_string_init(&value); | |
981 | retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path, | |
982 | &name, &value, &size, &flags); | |
983 | if (retval > 0) { | |
984 | retval = lsetxattr(path.data, | |
985 | name.data, value.data, size, flags); | |
986 | if (retval < 0) { | |
987 | retval = -errno; | |
988 | } | |
989 | } | |
990 | v9fs_string_free(&path); | |
991 | v9fs_string_free(&name); | |
992 | v9fs_string_free(&value); | |
993 | break; | |
994 | case T_LREMOVEXATTR: | |
995 | v9fs_string_init(&path); | |
996 | v9fs_string_init(&name); | |
997 | retval = proxy_unmarshal(&in_iovec, | |
998 | PROXY_HDR_SZ, "ss", &path, &name); | |
999 | if (retval > 0) { | |
1000 | retval = lremovexattr(path.data, name.data); | |
1001 | if (retval < 0) { | |
1002 | retval = -errno; | |
1003 | } | |
1004 | } | |
1005 | v9fs_string_free(&path); | |
1006 | v9fs_string_free(&name); | |
1007 | break; | |
d090e452 MK |
1008 | case T_GETVERSION: |
1009 | retval = do_getversion(&in_iovec, &out_iovec); | |
1010 | break; | |
daf0b9ac MK |
1011 | default: |
1012 | goto err_out; | |
1013 | break; | |
1014 | } | |
1015 | ||
39f8c32c | 1016 | if (process_reply(sock, header.type, &out_iovec, retval) < 0) { |
daf0b9ac | 1017 | goto err_out; |
17bff52b MK |
1018 | } |
1019 | } | |
daf0b9ac | 1020 | err_out: |
17bff52b | 1021 | g_free(in_iovec.iov_base); |
39f8c32c | 1022 | g_free(out_iovec.iov_base); |
17bff52b MK |
1023 | return -1; |
1024 | } | |
1025 | ||
1026 | int main(int argc, char **argv) | |
1027 | { | |
1028 | int sock; | |
84a87cc4 MK |
1029 | uid_t own_u; |
1030 | gid_t own_g; | |
17bff52b | 1031 | char *rpath = NULL; |
84a87cc4 | 1032 | char *sock_name = NULL; |
17bff52b MK |
1033 | struct stat stbuf; |
1034 | int c, option_index; | |
d090e452 MK |
1035 | #ifdef FS_IOC_GETVERSION |
1036 | int retval; | |
1037 | struct statfs st_fs; | |
1038 | #endif | |
17bff52b MK |
1039 | |
1040 | is_daemon = true; | |
1041 | sock = -1; | |
84a87cc4 | 1042 | own_u = own_g = -1; |
17bff52b MK |
1043 | while (1) { |
1044 | option_index = 0; | |
84a87cc4 | 1045 | c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts, |
17bff52b MK |
1046 | &option_index); |
1047 | if (c == -1) { | |
1048 | break; | |
1049 | } | |
1050 | switch (c) { | |
1051 | case 'p': | |
606017de | 1052 | rpath = g_strdup(optarg); |
17bff52b MK |
1053 | break; |
1054 | case 'n': | |
1055 | is_daemon = false; | |
1056 | break; | |
1057 | case 'f': | |
1058 | sock = atoi(optarg); | |
1059 | break; | |
84a87cc4 | 1060 | case 's': |
606017de | 1061 | sock_name = g_strdup(optarg); |
84a87cc4 MK |
1062 | break; |
1063 | case 'u': | |
1064 | own_u = atoi(optarg); | |
1065 | break; | |
1066 | case 'g': | |
1067 | own_g = atoi(optarg); | |
1068 | break; | |
17bff52b MK |
1069 | case '?': |
1070 | case 'h': | |
1071 | default: | |
1072 | usage(argv[0]); | |
1073 | exit(EXIT_FAILURE); | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | /* Parameter validation */ | |
84a87cc4 MK |
1078 | if ((sock_name == NULL && sock == -1) || rpath == NULL) { |
1079 | fprintf(stderr, "socket, socket descriptor or path not specified\n"); | |
1080 | usage(argv[0]); | |
1081 | return -1; | |
1082 | } | |
1083 | ||
5fc6dbae MK |
1084 | if (sock_name && sock != -1) { |
1085 | fprintf(stderr, "both named socket and socket descriptor specified\n"); | |
1086 | usage(argv[0]); | |
1087 | exit(EXIT_FAILURE); | |
1088 | } | |
1089 | ||
1090 | if (sock_name && (own_u == -1 || own_g == -1)) { | |
84a87cc4 MK |
1091 | fprintf(stderr, "owner uid:gid not specified, "); |
1092 | fprintf(stderr, | |
1093 | "owner uid:gid specifies who can access the socket file\n"); | |
17bff52b MK |
1094 | usage(argv[0]); |
1095 | exit(EXIT_FAILURE); | |
1096 | } | |
1097 | ||
1098 | if (lstat(rpath, &stbuf) < 0) { | |
1099 | fprintf(stderr, "invalid path \"%s\" specified, %s\n", | |
1100 | rpath, strerror(errno)); | |
1101 | exit(EXIT_FAILURE); | |
1102 | } | |
1103 | ||
1104 | if (!S_ISDIR(stbuf.st_mode)) { | |
1105 | fprintf(stderr, "specified path \"%s\" is not directory\n", rpath); | |
1106 | exit(EXIT_FAILURE); | |
1107 | } | |
1108 | ||
1109 | if (is_daemon) { | |
1110 | if (daemon(0, 0) < 0) { | |
1111 | fprintf(stderr, "daemon call failed\n"); | |
1112 | exit(EXIT_FAILURE); | |
1113 | } | |
1114 | openlog(PROGNAME, LOG_PID, LOG_DAEMON); | |
1115 | } | |
1116 | ||
1117 | do_log(LOG_INFO, "Started\n"); | |
5fc6dbae | 1118 | if (sock_name) { |
84a87cc4 MK |
1119 | sock = proxy_socket(sock_name, own_u, own_g); |
1120 | if (sock < 0) { | |
1121 | goto error; | |
1122 | } | |
1123 | } | |
17bff52b | 1124 | |
d090e452 MK |
1125 | get_version = false; |
1126 | #ifdef FS_IOC_GETVERSION | |
1127 | /* check whether underlying FS support IOC_GETVERSION */ | |
1128 | retval = statfs(rpath, &st_fs); | |
1129 | if (!retval) { | |
1130 | switch (st_fs.f_type) { | |
1131 | case EXT2_SUPER_MAGIC: | |
1132 | case BTRFS_SUPER_MAGIC: | |
1133 | case REISERFS_SUPER_MAGIC: | |
1134 | case XFS_SUPER_MAGIC: | |
1135 | get_version = true; | |
1136 | break; | |
1137 | } | |
1138 | } | |
1139 | #endif | |
1140 | ||
17bff52b MK |
1141 | if (chdir("/") < 0) { |
1142 | do_perror("chdir"); | |
1143 | goto error; | |
1144 | } | |
1145 | if (chroot(rpath) < 0) { | |
1146 | do_perror("chroot"); | |
1147 | goto error; | |
1148 | } | |
1149 | umask(0); | |
1150 | ||
1151 | if (init_capabilities() < 0) { | |
1152 | goto error; | |
1153 | } | |
1154 | ||
1155 | process_requests(sock); | |
1156 | error: | |
1157 | do_log(LOG_INFO, "Done\n"); | |
1158 | closelog(); | |
1159 | return 0; | |
1160 | } |