blob: c1da2d78e78b711260856c72909184b4d6809324 [file] [log] [blame]
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301/*
2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
4 *
5 * Authors:
6 * M. Mohan Kumar <mohan@in.ibm.com>
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 */
Stefan Weile7e4a6c2012-01-11 19:34:30 +010011
M. Mohan Kumar17bff522011-12-14 13:58:42 +053012#include <sys/resource.h>
M. Mohan Kumar17bff522011-12-14 13:58:42 +053013#include <getopt.h>
M. Mohan Kumar17bff522011-12-14 13:58:42 +053014#include <syslog.h>
15#include <sys/capability.h>
16#include <sys/fsuid.h>
M. Mohan Kumarb178adc2011-12-14 13:58:45 +053017#include <sys/vfs.h>
M. Mohan Kumard090e452011-12-14 13:58:46 +053018#include <sys/ioctl.h>
19#include <linux/fs.h>
20#ifdef CONFIG_LINUX_MAGIC_H
21#include <linux/magic.h>
22#endif
M. Mohan Kumar17bff522011-12-14 13:58:42 +053023#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/sockets.h"
25#include "qemu/xattr.h"
M. Mohan Kumar17bff522011-12-14 13:58:42 +053026#include "virtio-9p-marshal.h"
27#include "hw/9pfs/virtio-9p-proxy.h"
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +053028#include "fsdev/virtio-9p-marshal.h"
M. Mohan Kumar17bff522011-12-14 13:58:42 +053029
30#define PROGNAME "virtfs-proxy-helper"
31
M. Mohan Kumard090e452011-12-14 13:58:46 +053032#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
M. Mohan Kumar17bff522011-12-14 13:58:42 +053045static struct option helper_opts[] = {
46 {"fd", required_argument, NULL, 'f'},
47 {"path", required_argument, NULL, 'p'},
48 {"nodaemon", no_argument, NULL, 'n'},
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +053049 {"socket", required_argument, NULL, 's'},
50 {"uid", required_argument, NULL, 'u'},
51 {"gid", required_argument, NULL, 'g'},
M. Mohan Kumar17bff522011-12-14 13:58:42 +053052};
53
54static bool is_daemon;
M. Mohan Kumard090e452011-12-14 13:58:46 +053055static bool get_version; /* IOC getversion IOCTL supported */
M. Mohan Kumar17bff522011-12-14 13:58:42 +053056
Stefan Weilc5c7d3f2012-01-11 19:47:37 +010057static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
M. Mohan Kumar17bff522011-12-14 13:58:42 +053058{
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
70static 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
79static 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
113error:
114 cap_free(caps);
115 return -1;
116}
117
118static int init_capabilities(void)
119{
120 /* helper needs following capbabilities only */
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
133static 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
155static 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
174static 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
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530206static 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
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530248static int send_status(int sockfd, struct iovec *iovec, int status)
249{
250 ProxyHeader header;
Dong Xu Wangc7e775e2013-05-09 15:53:50 +0800251 int retval, msg_size;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530252
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);
265 retval = socket_write(sockfd, iovec->iov_base, msg_size);
266 if (retval < 0) {
267 return retval;
268 }
269 return 0;
270}
271
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530272/*
273 * from man 7 capabilities, section
274 * Effect of User ID Changes on Capabilities:
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200275 * If the effective user ID is changed from nonzero to 0, then the permitted
276 * set is copied to the effective set. If the effective user ID is changed
277 * from 0 to nonzero, then all capabilities are are cleared from the effective
278 * set.
279 *
280 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
281 * expose the program to unwanted signals, but this is not true anymore: for an
282 * unprivileged (without CAP_KILL) program to send a signal, the real or
283 * effective user ID of the sending process must equal the real or saved user
284 * ID of the target process. Even when dropping privileges, it is enough to
285 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
286 * be exposed to signals. So just use setresuid/setresgid.
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530287 */
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200288static int setugid(int uid, int gid, int *suid, int *sgid)
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530289{
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200290 int retval;
291
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530292 /*
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200293 * We still need DAC_OVERRIDE because we don't change
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530294 * supplementary group ids, and hence may be subjected DAC rules
295 */
296 cap_value_t cap_list[] = {
297 CAP_DAC_OVERRIDE,
298 };
299
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200300 *suid = geteuid();
301 *sgid = getegid();
302
303 if (setresgid(-1, gid, *sgid) == -1) {
304 retval = -errno;
305 goto err_out;
306 }
307
308 if (setresuid(-1, uid, *suid) == -1) {
309 retval = -errno;
310 goto err_sgid;
311 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530312
313 if (uid != 0 || gid != 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200314 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
315 retval = -errno;
316 goto err_suid;
317 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530318 }
319 return 0;
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200320
321err_suid:
322 if (setresuid(-1, *suid, *suid) == -1) {
323 abort();
324 }
325err_sgid:
326 if (setresgid(-1, *sgid, *sgid) == -1) {
327 abort();
328 }
329err_out:
330 return retval;
331}
332
333/*
334 * This is used to reset the ugid back with the saved values
335 * There is nothing much we can do checking error values here.
336 */
337static void resetugid(int suid, int sgid)
338{
339 if (setresgid(-1, sgid, sgid) == -1) {
340 abort();
341 }
342 if (setresuid(-1, suid, suid) == -1) {
343 abort();
344 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530345}
346
347/*
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530348 * send response in two parts
349 * 1) ProxyHeader
350 * 2) Response or error status
351 * This function should be called with marshaled response
352 * send_response constructs header part and error part only.
353 * send response sends {ProxyHeader,Response} if the request was success
354 * otherwise sends {ProxyHeader,error status}
355 */
356static int send_response(int sock, struct iovec *iovec, int size)
357{
358 int retval;
359 ProxyHeader header;
360
361 /*
362 * If response size exceeds available iovec->iov_len,
363 * we return ENOBUFS
364 */
365 if (size > PROXY_MAX_IO_SZ) {
366 size = -ENOBUFS;
367 }
368
369 if (size < 0) {
370 /*
371 * In case of error we would not have got the error encoded
372 * already so encode the error here.
373 */
374 header.type = T_ERROR;
375 header.size = sizeof(size);
376 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
377 } else {
378 header.type = T_SUCCESS;
379 header.size = size;
380 }
381 proxy_marshal(iovec, 0, "dd", header.type, header.size);
382 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
383 if (retval < 0) {
Dong Xu Wangc7e775e2013-05-09 15:53:50 +0800384 return retval;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530385 }
386 return 0;
387}
388
M. Mohan Kumard090e452011-12-14 13:58:46 +0530389/*
390 * gets generation number
391 * returns -errno on failure and sizeof(generation number) on success
392 */
393static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
394{
395 uint64_t version;
396 int retval = -ENOTTY;
397#ifdef FS_IOC_GETVERSION
398 int fd;
399 V9fsString path;
400#endif
401
402
403 /* no need to issue ioctl */
404 if (!get_version) {
405 version = 0;
406 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
407 return retval;
408 }
409#ifdef FS_IOC_GETVERSION
410 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
411 if (retval < 0) {
412 return retval;
413 }
414
415 fd = open(path.data, O_RDONLY);
416 if (fd < 0) {
417 retval = -errno;
418 goto err_out;
419 }
420 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
421 retval = -errno;
422 } else {
423 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
424 }
425 close(fd);
426err_out:
427 v9fs_string_free(&path);
428#endif
429 return retval;
430}
431
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530432static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
433{
434 int size = 0, offset, retval;
435 V9fsString path, name, xattr;
436
437 v9fs_string_init(&xattr);
438 v9fs_string_init(&path);
439 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
440 if (retval < 0) {
441 return retval;
442 }
443 offset = PROXY_HDR_SZ + retval;
444
445 if (size) {
446 xattr.data = g_malloc(size);
447 xattr.size = size;
448 }
449 switch (type) {
450 case T_LGETXATTR:
451 v9fs_string_init(&name);
452 retval = proxy_unmarshal(iovec, offset, "s", &name);
453 if (retval > 0) {
454 retval = lgetxattr(path.data, name.data, xattr.data, size);
455 if (retval < 0) {
456 retval = -errno;
457 } else {
458 xattr.size = retval;
459 }
460 }
461 v9fs_string_free(&name);
462 break;
463 case T_LLISTXATTR:
464 retval = llistxattr(path.data, xattr.data, size);
465 if (retval < 0) {
466 retval = -errno;
467 } else {
468 xattr.size = retval;
469 }
470 break;
471 }
472 if (retval < 0) {
473 goto err_out;
474 }
475
476 if (!size) {
477 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
478 retval = sizeof(retval);
479 } else {
480 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
481 }
482err_out:
483 v9fs_string_free(&xattr);
484 v9fs_string_free(&path);
485 return retval;
486}
487
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530488static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
489{
490 memset(pr_stat, 0, sizeof(*pr_stat));
491 pr_stat->st_dev = stat->st_dev;
492 pr_stat->st_ino = stat->st_ino;
493 pr_stat->st_nlink = stat->st_nlink;
494 pr_stat->st_mode = stat->st_mode;
495 pr_stat->st_uid = stat->st_uid;
496 pr_stat->st_gid = stat->st_gid;
497 pr_stat->st_rdev = stat->st_rdev;
498 pr_stat->st_size = stat->st_size;
499 pr_stat->st_blksize = stat->st_blksize;
500 pr_stat->st_blocks = stat->st_blocks;
501 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
502 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
503 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
504 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
505 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
506 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
507}
508
509static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
510{
511 memset(pr_stfs, 0, sizeof(*pr_stfs));
512 pr_stfs->f_type = stfs->f_type;
513 pr_stfs->f_bsize = stfs->f_bsize;
514 pr_stfs->f_blocks = stfs->f_blocks;
515 pr_stfs->f_bfree = stfs->f_bfree;
516 pr_stfs->f_bavail = stfs->f_bavail;
517 pr_stfs->f_files = stfs->f_files;
518 pr_stfs->f_ffree = stfs->f_ffree;
519 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
520 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
521 pr_stfs->f_namelen = stfs->f_namelen;
522 pr_stfs->f_frsize = stfs->f_frsize;
523}
524
525/*
526 * Gets stat/statfs information and packs in out_iovec structure
527 * on success returns number of bytes packed in out_iovec struture
528 * otherwise returns -errno
529 */
530static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
531{
532 int retval;
533 V9fsString path;
534 ProxyStat pr_stat;
535 ProxyStatFS pr_stfs;
536 struct stat st_buf;
537 struct statfs stfs_buf;
538
539 v9fs_string_init(&path);
540 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
541 if (retval < 0) {
542 return retval;
543 }
544
545 switch (type) {
546 case T_LSTAT:
547 retval = lstat(path.data, &st_buf);
548 if (retval < 0) {
549 retval = -errno;
550 } else {
551 stat_to_prstat(&pr_stat, &st_buf);
552 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
553 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
554 pr_stat.st_ino, pr_stat.st_nlink,
555 pr_stat.st_mode, pr_stat.st_uid,
556 pr_stat.st_gid, pr_stat.st_rdev,
557 pr_stat.st_size, pr_stat.st_blksize,
558 pr_stat.st_blocks,
559 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
560 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
561 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
562 }
563 break;
564 case T_STATFS:
565 retval = statfs(path.data, &stfs_buf);
566 if (retval < 0) {
567 retval = -errno;
568 } else {
569 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
570 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
571 "qqqqqqqqqqq", pr_stfs.f_type,
572 pr_stfs.f_bsize, pr_stfs.f_blocks,
573 pr_stfs.f_bfree, pr_stfs.f_bavail,
574 pr_stfs.f_files, pr_stfs.f_ffree,
575 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
576 pr_stfs.f_namelen, pr_stfs.f_frsize);
577 }
578 break;
579 }
580 v9fs_string_free(&path);
581 return retval;
582}
583
584static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
585{
586 char *buffer;
587 int size, retval;
588 V9fsString target, path;
589
590 v9fs_string_init(&path);
591 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
592 if (retval < 0) {
593 v9fs_string_free(&path);
594 return retval;
595 }
596 buffer = g_malloc(size);
597 v9fs_string_init(&target);
Markus Armbrusterd77f7772014-02-21 17:43:09 +0100598 retval = readlink(path.data, buffer, size - 1);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530599 if (retval > 0) {
600 buffer[retval] = '\0';
601 v9fs_string_sprintf(&target, "%s", buffer);
602 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
603 } else {
604 retval = -errno;
605 }
606 g_free(buffer);
607 v9fs_string_free(&target);
608 v9fs_string_free(&path);
609 return retval;
610}
611
612/*
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530613 * create other filesystem objects and send 0 on success
614 * return -errno on error
615 */
616static int do_create_others(int type, struct iovec *iovec)
617{
618 dev_t rdev;
619 int retval = 0;
620 int offset = PROXY_HDR_SZ;
621 V9fsString oldpath, path;
622 int mode, uid, gid, cur_uid, cur_gid;
623
624 v9fs_string_init(&path);
625 v9fs_string_init(&oldpath);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530626
627 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
628 if (retval < 0) {
629 return retval;
630 }
631 offset += retval;
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200632 retval = setugid(uid, gid, &cur_uid, &cur_gid);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530633 if (retval < 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200634 goto unmarshal_err_out;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530635 }
636 switch (type) {
637 case T_MKNOD:
638 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
639 if (retval < 0) {
640 goto err_out;
641 }
642 retval = mknod(path.data, mode, rdev);
643 break;
644 case T_MKDIR:
645 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
646 if (retval < 0) {
647 goto err_out;
648 }
649 retval = mkdir(path.data, mode);
650 break;
651 case T_SYMLINK:
652 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
653 if (retval < 0) {
654 goto err_out;
655 }
656 retval = symlink(oldpath.data, path.data);
657 break;
658 }
659 if (retval < 0) {
660 retval = -errno;
661 }
662
663err_out:
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200664 resetugid(cur_uid, cur_gid);
665unmarshal_err_out:
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530666 v9fs_string_free(&path);
667 v9fs_string_free(&oldpath);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530668 return retval;
669}
670
671/*
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530672 * create a file and send fd on success
673 * return -errno on error
674 */
675static int do_create(struct iovec *iovec)
676{
677 int ret;
678 V9fsString path;
679 int flags, mode, uid, gid, cur_uid, cur_gid;
680
681 v9fs_string_init(&path);
682 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
683 &path, &flags, &mode, &uid, &gid);
684 if (ret < 0) {
685 goto unmarshal_err_out;
686 }
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200687 ret = setugid(uid, gid, &cur_uid, &cur_gid);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530688 if (ret < 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200689 goto unmarshal_err_out;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530690 }
691 ret = open(path.data, flags, mode);
692 if (ret < 0) {
693 ret = -errno;
694 }
695
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200696 resetugid(cur_uid, cur_gid);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530697unmarshal_err_out:
698 v9fs_string_free(&path);
699 return ret;
700}
701
702/*
703 * open a file and send fd on success
704 * return -errno on error
705 */
706static int do_open(struct iovec *iovec)
707{
708 int flags, ret;
709 V9fsString path;
710
711 v9fs_string_init(&path);
712 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
713 if (ret < 0) {
714 goto err_out;
715 }
716 ret = open(path.data, flags);
717 if (ret < 0) {
718 ret = -errno;
719 }
720err_out:
721 v9fs_string_free(&path);
722 return ret;
723}
724
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530725/* create unix domain socket and return the descriptor */
726static int proxy_socket(const char *path, uid_t uid, gid_t gid)
727{
728 int sock, client;
729 struct sockaddr_un proxy, qemu;
730 socklen_t size;
731
732 /* requested socket already exists, refuse to start */
733 if (!access(path, F_OK)) {
734 do_log(LOG_CRIT, "socket already exists\n");
735 return -1;
736 }
737
738 sock = socket(AF_UNIX, SOCK_STREAM, 0);
739 if (sock < 0) {
740 do_perror("socket");
741 return -1;
742 }
743
744 /* mask other part of mode bits */
745 umask(7);
746
747 proxy.sun_family = AF_UNIX;
748 strcpy(proxy.sun_path, path);
749 if (bind(sock, (struct sockaddr *)&proxy,
750 sizeof(struct sockaddr_un)) < 0) {
751 do_perror("bind");
Gonglei88ea8ed2014-11-13 20:17:06 +0800752 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530753 }
754 if (chown(proxy.sun_path, uid, gid) < 0) {
755 do_perror("chown");
Gonglei88ea8ed2014-11-13 20:17:06 +0800756 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530757 }
758 if (listen(sock, 1) < 0) {
759 do_perror("listen");
Gonglei88ea8ed2014-11-13 20:17:06 +0800760 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530761 }
762
Tim Comerb0f93002014-04-19 13:39:57 -0400763 size = sizeof(qemu);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530764 client = accept(sock, (struct sockaddr *)&qemu, &size);
765 if (client < 0) {
766 do_perror("accept");
Gonglei88ea8ed2014-11-13 20:17:06 +0800767 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530768 }
Gonglei88ea8ed2014-11-13 20:17:06 +0800769 close(sock);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530770 return client;
Gonglei88ea8ed2014-11-13 20:17:06 +0800771
772error:
773 close(sock);
774 return -1;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530775}
776
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530777static void usage(char *prog)
778{
779 fprintf(stderr, "usage: %s\n"
780 " -p|--path <path> 9p path to export\n"
781 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530782 " {-s|--socket <socketname> socket file used for communication\n"
783 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
784 " access to this socket\n"
785 " \tNote: -s & -f can not be used together\n"
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530786 " [-n|--nodaemon] Run as a normal program\n",
787 basename(prog));
788}
789
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530790static int process_reply(int sock, int type,
791 struct iovec *out_iovec, int retval)
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530792{
793 switch (type) {
794 case T_OPEN:
795 case T_CREATE:
796 if (send_fd(sock, retval) < 0) {
797 return -1;
798 }
799 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530800 case T_MKNOD:
801 case T_MKDIR:
802 case T_SYMLINK:
803 case T_LINK:
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530804 case T_CHMOD:
805 case T_CHOWN:
806 case T_TRUNCATE:
807 case T_UTIME:
808 case T_RENAME:
809 case T_REMOVE:
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530810 case T_LSETXATTR:
811 case T_LREMOVEXATTR:
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530812 if (send_status(sock, out_iovec, retval) < 0) {
813 return -1;
814 }
815 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530816 case T_LSTAT:
817 case T_STATFS:
818 case T_READLINK:
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530819 case T_LGETXATTR:
820 case T_LLISTXATTR:
M. Mohan Kumard090e452011-12-14 13:58:46 +0530821 case T_GETVERSION:
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530822 if (send_response(sock, out_iovec, retval) < 0) {
823 return -1;
824 }
825 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530826 default:
827 return -1;
828 break;
829 }
830 return 0;
831}
832
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530833static int process_requests(int sock)
834{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530835 int flags;
836 int size = 0;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530837 int retval = 0;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530838 uint64_t offset;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530839 ProxyHeader header;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530840 int mode, uid, gid;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530841 V9fsString name, value;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530842 struct timespec spec[2];
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530843 V9fsString oldpath, path;
844 struct iovec in_iovec, out_iovec;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530845
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530846 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
847 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
848 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
849 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
850
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530851 while (1) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530852 /*
853 * initialize the header type, so that we send
854 * response to proper request type.
855 */
856 header.type = 0;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530857 retval = read_request(sock, &in_iovec, &header);
858 if (retval < 0) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530859 goto err_out;
860 }
861
862 switch (header.type) {
863 case T_OPEN:
864 retval = do_open(&in_iovec);
865 break;
866 case T_CREATE:
867 retval = do_create(&in_iovec);
868 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530869 case T_MKNOD:
870 case T_MKDIR:
871 case T_SYMLINK:
872 retval = do_create_others(header.type, &in_iovec);
873 break;
874 case T_LINK:
875 v9fs_string_init(&path);
876 v9fs_string_init(&oldpath);
877 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
878 "ss", &oldpath, &path);
879 if (retval > 0) {
880 retval = link(oldpath.data, path.data);
881 if (retval < 0) {
882 retval = -errno;
883 }
884 }
885 v9fs_string_free(&oldpath);
886 v9fs_string_free(&path);
887 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530888 case T_LSTAT:
889 case T_STATFS:
890 retval = do_stat(header.type, &in_iovec, &out_iovec);
891 break;
892 case T_READLINK:
893 retval = do_readlink(&in_iovec, &out_iovec);
894 break;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530895 case T_CHMOD:
896 v9fs_string_init(&path);
897 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
898 "sd", &path, &mode);
899 if (retval > 0) {
900 retval = chmod(path.data, mode);
901 if (retval < 0) {
902 retval = -errno;
903 }
904 }
905 v9fs_string_free(&path);
906 break;
907 case T_CHOWN:
908 v9fs_string_init(&path);
909 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
910 &uid, &gid);
911 if (retval > 0) {
912 retval = lchown(path.data, uid, gid);
913 if (retval < 0) {
914 retval = -errno;
915 }
916 }
917 v9fs_string_free(&path);
918 break;
919 case T_TRUNCATE:
920 v9fs_string_init(&path);
921 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
922 &path, &offset);
923 if (retval > 0) {
924 retval = truncate(path.data, offset);
925 if (retval < 0) {
926 retval = -errno;
927 }
928 }
929 v9fs_string_free(&path);
930 break;
931 case T_UTIME:
932 v9fs_string_init(&path);
933 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
934 &spec[0].tv_sec, &spec[0].tv_nsec,
935 &spec[1].tv_sec, &spec[1].tv_nsec);
936 if (retval > 0) {
937 retval = qemu_utimens(path.data, spec);
938 if (retval < 0) {
939 retval = -errno;
940 }
941 }
942 v9fs_string_free(&path);
943 break;
944 case T_RENAME:
945 v9fs_string_init(&path);
946 v9fs_string_init(&oldpath);
947 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
948 "ss", &oldpath, &path);
949 if (retval > 0) {
950 retval = rename(oldpath.data, path.data);
951 if (retval < 0) {
952 retval = -errno;
953 }
954 }
955 v9fs_string_free(&oldpath);
956 v9fs_string_free(&path);
957 break;
958 case T_REMOVE:
959 v9fs_string_init(&path);
960 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
961 if (retval > 0) {
962 retval = remove(path.data);
963 if (retval < 0) {
964 retval = -errno;
965 }
966 }
967 v9fs_string_free(&path);
968 break;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530969 case T_LGETXATTR:
970 case T_LLISTXATTR:
971 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
972 break;
973 case T_LSETXATTR:
974 v9fs_string_init(&path);
975 v9fs_string_init(&name);
976 v9fs_string_init(&value);
977 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
978 &name, &value, &size, &flags);
979 if (retval > 0) {
980 retval = lsetxattr(path.data,
981 name.data, value.data, size, flags);
982 if (retval < 0) {
983 retval = -errno;
984 }
985 }
986 v9fs_string_free(&path);
987 v9fs_string_free(&name);
988 v9fs_string_free(&value);
989 break;
990 case T_LREMOVEXATTR:
991 v9fs_string_init(&path);
992 v9fs_string_init(&name);
993 retval = proxy_unmarshal(&in_iovec,
994 PROXY_HDR_SZ, "ss", &path, &name);
995 if (retval > 0) {
996 retval = lremovexattr(path.data, name.data);
997 if (retval < 0) {
998 retval = -errno;
999 }
1000 }
1001 v9fs_string_free(&path);
1002 v9fs_string_free(&name);
1003 break;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301004 case T_GETVERSION:
1005 retval = do_getversion(&in_iovec, &out_iovec);
1006 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301007 default:
1008 goto err_out;
1009 break;
1010 }
1011
M. Mohan Kumar39f8c322011-12-14 13:58:45 +05301012 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301013 goto err_out;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301014 }
1015 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301016err_out:
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301017 g_free(in_iovec.iov_base);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +05301018 g_free(out_iovec.iov_base);
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301019 return -1;
1020}
1021
1022int main(int argc, char **argv)
1023{
1024 int sock;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301025 uid_t own_u;
1026 gid_t own_g;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301027 char *rpath = NULL;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301028 char *sock_name = NULL;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301029 struct stat stbuf;
1030 int c, option_index;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301031#ifdef FS_IOC_GETVERSION
1032 int retval;
1033 struct statfs st_fs;
1034#endif
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301035
1036 is_daemon = true;
1037 sock = -1;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301038 own_u = own_g = -1;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301039 while (1) {
1040 option_index = 0;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301041 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301042 &option_index);
1043 if (c == -1) {
1044 break;
1045 }
1046 switch (c) {
1047 case 'p':
Markus Armbruster606017d2013-01-22 11:08:01 +01001048 rpath = g_strdup(optarg);
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301049 break;
1050 case 'n':
1051 is_daemon = false;
1052 break;
1053 case 'f':
1054 sock = atoi(optarg);
1055 break;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301056 case 's':
Markus Armbruster606017d2013-01-22 11:08:01 +01001057 sock_name = g_strdup(optarg);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301058 break;
1059 case 'u':
1060 own_u = atoi(optarg);
1061 break;
1062 case 'g':
1063 own_g = atoi(optarg);
1064 break;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301065 case '?':
1066 case 'h':
1067 default:
1068 usage(argv[0]);
1069 exit(EXIT_FAILURE);
1070 }
1071 }
1072
1073 /* Parameter validation */
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301074 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1075 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1076 usage(argv[0]);
1077 return -1;
1078 }
1079
M. Mohan Kumar5fc6dba2012-01-19 16:15:56 +05301080 if (sock_name && sock != -1) {
1081 fprintf(stderr, "both named socket and socket descriptor specified\n");
1082 usage(argv[0]);
1083 exit(EXIT_FAILURE);
1084 }
1085
1086 if (sock_name && (own_u == -1 || own_g == -1)) {
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301087 fprintf(stderr, "owner uid:gid not specified, ");
1088 fprintf(stderr,
1089 "owner uid:gid specifies who can access the socket file\n");
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301090 usage(argv[0]);
1091 exit(EXIT_FAILURE);
1092 }
1093
1094 if (lstat(rpath, &stbuf) < 0) {
1095 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1096 rpath, strerror(errno));
1097 exit(EXIT_FAILURE);
1098 }
1099
1100 if (!S_ISDIR(stbuf.st_mode)) {
1101 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1102 exit(EXIT_FAILURE);
1103 }
1104
1105 if (is_daemon) {
1106 if (daemon(0, 0) < 0) {
1107 fprintf(stderr, "daemon call failed\n");
1108 exit(EXIT_FAILURE);
1109 }
1110 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1111 }
1112
1113 do_log(LOG_INFO, "Started\n");
M. Mohan Kumar5fc6dba2012-01-19 16:15:56 +05301114 if (sock_name) {
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301115 sock = proxy_socket(sock_name, own_u, own_g);
1116 if (sock < 0) {
1117 goto error;
1118 }
1119 }
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301120
M. Mohan Kumard090e452011-12-14 13:58:46 +05301121 get_version = false;
1122#ifdef FS_IOC_GETVERSION
1123 /* check whether underlying FS support IOC_GETVERSION */
1124 retval = statfs(rpath, &st_fs);
1125 if (!retval) {
1126 switch (st_fs.f_type) {
1127 case EXT2_SUPER_MAGIC:
1128 case BTRFS_SUPER_MAGIC:
1129 case REISERFS_SUPER_MAGIC:
1130 case XFS_SUPER_MAGIC:
1131 get_version = true;
1132 break;
1133 }
1134 }
1135#endif
1136
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301137 if (chdir("/") < 0) {
1138 do_perror("chdir");
1139 goto error;
1140 }
1141 if (chroot(rpath) < 0) {
1142 do_perror("chroot");
1143 goto error;
1144 }
1145 umask(0);
1146
1147 if (init_capabilities() < 0) {
1148 goto error;
1149 }
1150
1151 process_requests(sock);
1152error:
1153 do_log(LOG_INFO, "Done\n");
1154 closelog();
1155 return 0;
1156}