blob: bf2e5f33312160172942e3e44106251a74692777 [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);
Shannon Zhao821c4472015-03-16 09:20:29 +0800265 if (msg_size < 0) {
266 return msg_size;
267 }
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530268 retval = socket_write(sockfd, iovec->iov_base, msg_size);
269 if (retval < 0) {
270 return retval;
271 }
272 return 0;
273}
274
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530275/*
276 * from man 7 capabilities, section
277 * Effect of User ID Changes on Capabilities:
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200278 * 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.
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530290 */
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200291static int setugid(int uid, int gid, int *suid, int *sgid)
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530292{
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200293 int retval;
294
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530295 /*
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200296 * We still need DAC_OVERRIDE because we don't change
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530297 * supplementary group ids, and hence may be subjected DAC rules
298 */
299 cap_value_t cap_list[] = {
300 CAP_DAC_OVERRIDE,
301 };
302
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200303 *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 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530315
316 if (uid != 0 || gid != 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200317 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
318 retval = -errno;
319 goto err_suid;
320 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530321 }
322 return 0;
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200323
324err_suid:
325 if (setresuid(-1, *suid, *suid) == -1) {
326 abort();
327 }
328err_sgid:
329 if (setresgid(-1, *sgid, *sgid) == -1) {
330 abort();
331 }
332err_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 */
340static 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 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530348}
349
350/*
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530351 * 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 */
359static 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) {
Dong Xu Wangc7e775e2013-05-09 15:53:50 +0800387 return retval;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530388 }
389 return 0;
390}
391
M. Mohan Kumard090e452011-12-14 13:58:46 +0530392/*
393 * gets generation number
394 * returns -errno on failure and sizeof(generation number) on success
395 */
396static 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);
429err_out:
430 v9fs_string_free(&path);
431#endif
432 return retval;
433}
434
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530435static 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 }
485err_out:
486 v9fs_string_free(&xattr);
487 v9fs_string_free(&path);
488 return retval;
489}
490
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530491static 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
512static 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 */
533static 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
587static 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);
Markus Armbrusterd77f7772014-02-21 17:43:09 +0100601 retval = readlink(path.data, buffer, size - 1);
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530602 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
615/*
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530616 * create other filesystem objects and send 0 on success
617 * return -errno on error
618 */
619static 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);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530629
630 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
631 if (retval < 0) {
632 return retval;
633 }
634 offset += retval;
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200635 retval = setugid(uid, gid, &cur_uid, &cur_gid);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530636 if (retval < 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200637 goto unmarshal_err_out;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530638 }
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
666err_out:
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200667 resetugid(cur_uid, cur_gid);
668unmarshal_err_out:
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530669 v9fs_string_free(&path);
670 v9fs_string_free(&oldpath);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530671 return retval;
672}
673
674/*
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530675 * create a file and send fd on success
676 * return -errno on error
677 */
678static 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 }
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200690 ret = setugid(uid, gid, &cur_uid, &cur_gid);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530691 if (ret < 0) {
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200692 goto unmarshal_err_out;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530693 }
694 ret = open(path.data, flags, mode);
695 if (ret < 0) {
696 ret = -errno;
697 }
698
Paolo Bonzini9fd2ecd2012-10-11 14:20:23 +0200699 resetugid(cur_uid, cur_gid);
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530700unmarshal_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 */
709static 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 }
723err_out:
724 v9fs_string_free(&path);
725 return ret;
726}
727
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530728/* create unix domain socket and return the descriptor */
729static 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
741 sock = socket(AF_UNIX, SOCK_STREAM, 0);
742 if (sock < 0) {
743 do_perror("socket");
744 return -1;
745 }
746
747 /* mask other part of mode bits */
748 umask(7);
749
750 proxy.sun_family = AF_UNIX;
751 strcpy(proxy.sun_path, path);
752 if (bind(sock, (struct sockaddr *)&proxy,
753 sizeof(struct sockaddr_un)) < 0) {
754 do_perror("bind");
Gonglei88ea8ed2014-11-13 20:17:06 +0800755 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530756 }
757 if (chown(proxy.sun_path, uid, gid) < 0) {
758 do_perror("chown");
Gonglei88ea8ed2014-11-13 20:17:06 +0800759 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530760 }
761 if (listen(sock, 1) < 0) {
762 do_perror("listen");
Gonglei88ea8ed2014-11-13 20:17:06 +0800763 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530764 }
765
Tim Comerb0f93002014-04-19 13:39:57 -0400766 size = sizeof(qemu);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530767 client = accept(sock, (struct sockaddr *)&qemu, &size);
768 if (client < 0) {
769 do_perror("accept");
Gonglei88ea8ed2014-11-13 20:17:06 +0800770 goto error;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530771 }
Gonglei88ea8ed2014-11-13 20:17:06 +0800772 close(sock);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530773 return client;
Gonglei88ea8ed2014-11-13 20:17:06 +0800774
775error:
776 close(sock);
777 return -1;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530778}
779
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530780static void usage(char *prog)
781{
782 fprintf(stderr, "usage: %s\n"
783 " -p|--path <path> 9p path to export\n"
784 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +0530785 " {-s|--socket <socketname> socket file used for communication\n"
786 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
787 " access to this socket\n"
788 " \tNote: -s & -f can not be used together\n"
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530789 " [-n|--nodaemon] Run as a normal program\n",
790 basename(prog));
791}
792
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530793static int process_reply(int sock, int type,
794 struct iovec *out_iovec, int retval)
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530795{
796 switch (type) {
797 case T_OPEN:
798 case T_CREATE:
799 if (send_fd(sock, retval) < 0) {
800 return -1;
801 }
802 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530803 case T_MKNOD:
804 case T_MKDIR:
805 case T_SYMLINK:
806 case T_LINK:
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530807 case T_CHMOD:
808 case T_CHOWN:
809 case T_TRUNCATE:
810 case T_UTIME:
811 case T_RENAME:
812 case T_REMOVE:
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530813 case T_LSETXATTR:
814 case T_LREMOVEXATTR:
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530815 if (send_status(sock, out_iovec, retval) < 0) {
816 return -1;
817 }
818 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530819 case T_LSTAT:
820 case T_STATFS:
821 case T_READLINK:
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530822 case T_LGETXATTR:
823 case T_LLISTXATTR:
M. Mohan Kumard090e452011-12-14 13:58:46 +0530824 case T_GETVERSION:
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530825 if (send_response(sock, out_iovec, retval) < 0) {
826 return -1;
827 }
828 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530829 default:
830 return -1;
831 break;
832 }
833 return 0;
834}
835
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530836static int process_requests(int sock)
837{
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530838 int flags;
839 int size = 0;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530840 int retval = 0;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530841 uint64_t offset;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530842 ProxyHeader header;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530843 int mode, uid, gid;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530844 V9fsString name, value;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530845 struct timespec spec[2];
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530846 V9fsString oldpath, path;
847 struct iovec in_iovec, out_iovec;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530848
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530849 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
850 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
851 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
852 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
853
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530854 while (1) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530855 /*
856 * initialize the header type, so that we send
857 * response to proper request type.
858 */
859 header.type = 0;
M. Mohan Kumar17bff522011-12-14 13:58:42 +0530860 retval = read_request(sock, &in_iovec, &header);
861 if (retval < 0) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +0530862 goto err_out;
863 }
864
865 switch (header.type) {
866 case T_OPEN:
867 retval = do_open(&in_iovec);
868 break;
869 case T_CREATE:
870 retval = do_create(&in_iovec);
871 break;
M. Mohan Kumar39f8c322011-12-14 13:58:45 +0530872 case T_MKNOD:
873 case T_MKDIR:
874 case T_SYMLINK:
875 retval = do_create_others(header.type, &in_iovec);
876 break;
877 case T_LINK:
878 v9fs_string_init(&path);
879 v9fs_string_init(&oldpath);
880 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
881 "ss", &oldpath, &path);
882 if (retval > 0) {
883 retval = link(oldpath.data, path.data);
884 if (retval < 0) {
885 retval = -errno;
886 }
887 }
888 v9fs_string_free(&oldpath);
889 v9fs_string_free(&path);
890 break;
M. Mohan Kumarb178adc2011-12-14 13:58:45 +0530891 case T_LSTAT:
892 case T_STATFS:
893 retval = do_stat(header.type, &in_iovec, &out_iovec);
894 break;
895 case T_READLINK:
896 retval = do_readlink(&in_iovec, &out_iovec);
897 break;
M. Mohan Kumarea75fc42011-12-14 13:58:45 +0530898 case T_CHMOD:
899 v9fs_string_init(&path);
900 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
901 "sd", &path, &mode);
902 if (retval > 0) {
903 retval = chmod(path.data, mode);
904 if (retval < 0) {
905 retval = -errno;
906 }
907 }
908 v9fs_string_free(&path);
909 break;
910 case T_CHOWN:
911 v9fs_string_init(&path);
912 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
913 &uid, &gid);
914 if (retval > 0) {
915 retval = lchown(path.data, uid, gid);
916 if (retval < 0) {
917 retval = -errno;
918 }
919 }
920 v9fs_string_free(&path);
921 break;
922 case T_TRUNCATE:
923 v9fs_string_init(&path);
924 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
925 &path, &offset);
926 if (retval > 0) {
927 retval = truncate(path.data, offset);
928 if (retval < 0) {
929 retval = -errno;
930 }
931 }
932 v9fs_string_free(&path);
933 break;
934 case T_UTIME:
935 v9fs_string_init(&path);
936 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
937 &spec[0].tv_sec, &spec[0].tv_nsec,
938 &spec[1].tv_sec, &spec[1].tv_nsec);
939 if (retval > 0) {
940 retval = qemu_utimens(path.data, spec);
941 if (retval < 0) {
942 retval = -errno;
943 }
944 }
945 v9fs_string_free(&path);
946 break;
947 case T_RENAME:
948 v9fs_string_init(&path);
949 v9fs_string_init(&oldpath);
950 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
951 "ss", &oldpath, &path);
952 if (retval > 0) {
953 retval = rename(oldpath.data, path.data);
954 if (retval < 0) {
955 retval = -errno;
956 }
957 }
958 v9fs_string_free(&oldpath);
959 v9fs_string_free(&path);
960 break;
961 case T_REMOVE:
962 v9fs_string_init(&path);
963 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
964 if (retval > 0) {
965 retval = remove(path.data);
966 if (retval < 0) {
967 retval = -errno;
968 }
969 }
970 v9fs_string_free(&path);
971 break;
M. Mohan Kumard52b09e2011-12-14 13:58:46 +0530972 case T_LGETXATTR:
973 case T_LLISTXATTR:
974 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
975 break;
976 case T_LSETXATTR:
977 v9fs_string_init(&path);
978 v9fs_string_init(&name);
979 v9fs_string_init(&value);
980 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
981 &name, &value, &size, &flags);
982 if (retval > 0) {
983 retval = lsetxattr(path.data,
984 name.data, value.data, size, flags);
985 if (retval < 0) {
986 retval = -errno;
987 }
988 }
989 v9fs_string_free(&path);
990 v9fs_string_free(&name);
991 v9fs_string_free(&value);
992 break;
993 case T_LREMOVEXATTR:
994 v9fs_string_init(&path);
995 v9fs_string_init(&name);
996 retval = proxy_unmarshal(&in_iovec,
997 PROXY_HDR_SZ, "ss", &path, &name);
998 if (retval > 0) {
999 retval = lremovexattr(path.data, name.data);
1000 if (retval < 0) {
1001 retval = -errno;
1002 }
1003 }
1004 v9fs_string_free(&path);
1005 v9fs_string_free(&name);
1006 break;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301007 case T_GETVERSION:
1008 retval = do_getversion(&in_iovec, &out_iovec);
1009 break;
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301010 default:
1011 goto err_out;
1012 break;
1013 }
1014
M. Mohan Kumar39f8c322011-12-14 13:58:45 +05301015 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301016 goto err_out;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301017 }
1018 }
M. Mohan Kumardaf0b9a2011-12-14 13:58:44 +05301019err_out:
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301020 g_free(in_iovec.iov_base);
M. Mohan Kumar39f8c322011-12-14 13:58:45 +05301021 g_free(out_iovec.iov_base);
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301022 return -1;
1023}
1024
1025int main(int argc, char **argv)
1026{
1027 int sock;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301028 uid_t own_u;
1029 gid_t own_g;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301030 char *rpath = NULL;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301031 char *sock_name = NULL;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301032 struct stat stbuf;
1033 int c, option_index;
M. Mohan Kumard090e452011-12-14 13:58:46 +05301034#ifdef FS_IOC_GETVERSION
1035 int retval;
1036 struct statfs st_fs;
1037#endif
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301038
1039 is_daemon = true;
1040 sock = -1;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301041 own_u = own_g = -1;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301042 while (1) {
1043 option_index = 0;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301044 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301045 &option_index);
1046 if (c == -1) {
1047 break;
1048 }
1049 switch (c) {
1050 case 'p':
Markus Armbruster606017d2013-01-22 11:08:01 +01001051 rpath = g_strdup(optarg);
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301052 break;
1053 case 'n':
1054 is_daemon = false;
1055 break;
1056 case 'f':
1057 sock = atoi(optarg);
1058 break;
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301059 case 's':
Markus Armbruster606017d2013-01-22 11:08:01 +01001060 sock_name = g_strdup(optarg);
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301061 break;
1062 case 'u':
1063 own_u = atoi(optarg);
1064 break;
1065 case 'g':
1066 own_g = atoi(optarg);
1067 break;
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301068 case '?':
1069 case 'h':
1070 default:
1071 usage(argv[0]);
1072 exit(EXIT_FAILURE);
1073 }
1074 }
1075
1076 /* Parameter validation */
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301077 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1078 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1079 usage(argv[0]);
1080 return -1;
1081 }
1082
M. Mohan Kumar5fc6dba2012-01-19 16:15:56 +05301083 if (sock_name && sock != -1) {
1084 fprintf(stderr, "both named socket and socket descriptor specified\n");
1085 usage(argv[0]);
1086 exit(EXIT_FAILURE);
1087 }
1088
1089 if (sock_name && (own_u == -1 || own_g == -1)) {
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301090 fprintf(stderr, "owner uid:gid not specified, ");
1091 fprintf(stderr,
1092 "owner uid:gid specifies who can access the socket file\n");
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301093 usage(argv[0]);
1094 exit(EXIT_FAILURE);
1095 }
1096
1097 if (lstat(rpath, &stbuf) < 0) {
1098 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1099 rpath, strerror(errno));
1100 exit(EXIT_FAILURE);
1101 }
1102
1103 if (!S_ISDIR(stbuf.st_mode)) {
1104 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1105 exit(EXIT_FAILURE);
1106 }
1107
1108 if (is_daemon) {
1109 if (daemon(0, 0) < 0) {
1110 fprintf(stderr, "daemon call failed\n");
1111 exit(EXIT_FAILURE);
1112 }
1113 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1114 }
1115
1116 do_log(LOG_INFO, "Started\n");
M. Mohan Kumar5fc6dba2012-01-19 16:15:56 +05301117 if (sock_name) {
M. Mohan Kumar84a87cc2011-12-14 13:58:47 +05301118 sock = proxy_socket(sock_name, own_u, own_g);
1119 if (sock < 0) {
1120 goto error;
1121 }
1122 }
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301123
M. Mohan Kumard090e452011-12-14 13:58:46 +05301124 get_version = false;
1125#ifdef FS_IOC_GETVERSION
1126 /* check whether underlying FS support IOC_GETVERSION */
1127 retval = statfs(rpath, &st_fs);
1128 if (!retval) {
1129 switch (st_fs.f_type) {
1130 case EXT2_SUPER_MAGIC:
1131 case BTRFS_SUPER_MAGIC:
1132 case REISERFS_SUPER_MAGIC:
1133 case XFS_SUPER_MAGIC:
1134 get_version = true;
1135 break;
1136 }
1137 }
1138#endif
1139
M. Mohan Kumar17bff522011-12-14 13:58:42 +05301140 if (chdir("/") < 0) {
1141 do_perror("chdir");
1142 goto error;
1143 }
1144 if (chroot(rpath) < 0) {
1145 do_perror("chroot");
1146 goto error;
1147 }
1148 umask(0);
1149
1150 if (init_capabilities() < 0) {
1151 goto error;
1152 }
1153
1154 process_requests(sock);
1155error:
1156 do_log(LOG_INFO, "Done\n");
1157 closelog();
1158 return 0;
1159}