2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
6 * M. Mohan Kumar <mohan@in.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include <glib/gstdio.h>
14 #include <sys/resource.h>
17 #include <sys/fsuid.h>
19 #include <sys/ioctl.h>
21 #ifdef CONFIG_LINUX_MAGIC_H
22 #include <linux/magic.h>
25 #include "qemu/sockets.h"
26 #include "qemu/xattr.h"
27 #include "9p-iov-marshal.h"
28 #include "hw/9pfs/9p-proxy.h"
29 #include "fsdev/9p-iov-marshal.h"
31 #define PROGNAME "virtfs-proxy-helper"
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
46 static const struct option helper_opts[] = {
47 {"fd", required_argument, NULL, 'f'},
48 {"path", required_argument, NULL, 'p'},
49 {"nodaemon", no_argument, NULL, 'n'},
50 {"socket", required_argument, NULL, 's'},
51 {"uid", required_argument, NULL, 'u'},
52 {"gid", required_argument, NULL, 'g'},
56 static bool is_daemon;
57 static bool get_version; /* IOC getversion IOCTL supported */
58 static char *prog_name;
60 static void G_GNUC_PRINTF(2, 3) do_log(int loglevel, const char *format, ...)
66 vsyslog(LOG_CRIT, format, ap);
68 vfprintf(stderr, format, ap);
73 static void do_perror(const char *string)
76 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
78 fprintf(stderr, "%s:%s\n", string, strerror(errno));
82 static int init_capabilities(void)
84 /* helper needs following capabilities only */
96 capng_clear(CAPNG_SELECT_BOTH);
97 for (i = 0; i < ARRAY_SIZE(cap_list); i++) {
98 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
100 do_perror("capng_update");
104 if (capng_apply(CAPNG_SELECT_BOTH) < 0) {
105 do_perror("capng_apply");
109 /* Prepare effective set for setugid. */
110 for (i = 0; i < ARRAY_SIZE(cap_list); i++) {
111 if (cap_list[i] == CAP_DAC_OVERRIDE) {
115 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE,
117 do_perror("capng_update");
124 static int socket_read(int sockfd, void *buff, ssize_t size)
126 ssize_t retval, total = 0;
129 retval = read(sockfd, buff, size);
134 if (errno == EINTR) {
146 static int socket_write(int sockfd, void *buff, ssize_t size)
148 ssize_t retval, total = 0;
151 retval = write(sockfd, buff, size);
153 if (errno == EINTR) {
165 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
170 * read the request header.
173 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
177 iovec->iov_len = PROXY_HDR_SZ;
178 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
183 * We can't process message.size > PROXY_MAX_IO_SZ.
184 * Treat it as fatal error
186 if (header->size > PROXY_MAX_IO_SZ) {
189 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
193 iovec->iov_len += header->size;
197 static int send_fd(int sockfd, int fd)
202 struct cmsghdr *cmsg;
203 union MsgControl msg_control;
205 iov.iov_base = &data;
206 iov.iov_len = sizeof(data);
208 memset(&msg, 0, sizeof(msg));
211 /* No ancillary data on error */
213 /* fd is really negative errno if the request failed */
216 data = V9FS_FD_VALID;
217 msg.msg_control = &msg_control;
218 msg.msg_controllen = sizeof(msg_control);
220 cmsg = &msg_control.cmsg;
221 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
222 cmsg->cmsg_level = SOL_SOCKET;
223 cmsg->cmsg_type = SCM_RIGHTS;
224 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
228 retval = sendmsg(sockfd, &msg, 0);
229 } while (retval < 0 && errno == EINTR);
239 static int send_status(int sockfd, struct iovec *iovec, int status)
242 int retval, msg_size;
245 header.type = T_ERROR;
247 header.type = T_SUCCESS;
249 header.size = sizeof(status);
251 * marshal the return status. We don't check error.
252 * because we are sure we have enough space for the status
254 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
255 header.size, status);
259 retval = socket_write(sockfd, iovec->iov_base, msg_size);
267 * from man 7 capabilities, section
268 * Effect of User ID Changes on Capabilities:
269 * If the effective user ID is changed from nonzero to 0, then the permitted
270 * set is copied to the effective set. If the effective user ID is changed
271 * from 0 to nonzero, then all capabilities are are cleared from the effective
274 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
275 * expose the program to unwanted signals, but this is not true anymore: for an
276 * unprivileged (without CAP_KILL) program to send a signal, the real or
277 * effective user ID of the sending process must equal the real or saved user
278 * ID of the target process. Even when dropping privileges, it is enough to
279 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
280 * be exposed to signals. So just use setresuid/setresgid.
282 static int setugid(int uid, int gid, int *suid, int *sgid)
289 if (setresgid(-1, gid, *sgid) == -1) {
293 if (setresuid(-1, uid, *suid) == -1) {
298 if (uid == 0 && gid == 0) {
299 /* Linux has already copied the permitted set to the effective set. */
304 * All capabilities have been cleared from the effective set. However
305 * we still need DAC_OVERRIDE because we don't change supplementary
306 * group ids, and hence may be subject to DAC rules. init_capabilities
307 * left the set of capabilities that we want in libcap-ng's state.
309 if (capng_apply(CAPNG_SELECT_CAPS) < 0) {
311 do_perror("capng_apply");
317 if (setresuid(-1, *suid, *suid) == -1) {
321 if (setresgid(-1, *sgid, *sgid) == -1) {
328 * This is used to reset the ugid back with the saved values
329 * There is nothing much we can do checking error values here.
331 static void resetugid(int suid, int sgid)
333 if (setresgid(-1, sgid, sgid) == -1) {
336 if (setresuid(-1, suid, suid) == -1) {
342 * send response in two parts
344 * 2) Response or error status
345 * This function should be called with marshaled response
346 * send_response constructs header part and error part only.
347 * send response sends {ProxyHeader,Response} if the request was success
348 * otherwise sends {ProxyHeader,error status}
350 static int send_response(int sock, struct iovec *iovec, int size)
356 * If response size exceeds available iovec->iov_len,
359 if (size > PROXY_MAX_IO_SZ) {
365 * In case of error we would not have got the error encoded
366 * already so encode the error here.
368 header.type = T_ERROR;
369 header.size = sizeof(size);
370 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
372 header.type = T_SUCCESS;
375 proxy_marshal(iovec, 0, "dd", header.type, header.size);
376 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
384 * gets generation number
385 * returns -errno on failure and sizeof(generation number) on success
387 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
390 int retval = -ENOTTY;
391 #ifdef FS_IOC_GETVERSION
397 /* no need to issue ioctl */
400 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
403 #ifdef FS_IOC_GETVERSION
404 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
409 fd = open(path.data, O_RDONLY);
414 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
417 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
421 v9fs_string_free(&path);
426 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
428 int size = 0, offset, retval;
429 V9fsString path, name, xattr;
431 v9fs_string_init(&xattr);
432 v9fs_string_init(&path);
433 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
437 offset = PROXY_HDR_SZ + retval;
440 xattr.data = g_malloc(size);
445 v9fs_string_init(&name);
446 retval = proxy_unmarshal(iovec, offset, "s", &name);
448 retval = lgetxattr(path.data, name.data, xattr.data, size);
455 v9fs_string_free(&name);
458 retval = llistxattr(path.data, xattr.data, size);
471 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
472 retval = sizeof(retval);
474 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
477 v9fs_string_free(&xattr);
478 v9fs_string_free(&path);
482 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
484 memset(pr_stat, 0, sizeof(*pr_stat));
485 pr_stat->st_dev = stat->st_dev;
486 pr_stat->st_ino = stat->st_ino;
487 pr_stat->st_nlink = stat->st_nlink;
488 pr_stat->st_mode = stat->st_mode;
489 pr_stat->st_uid = stat->st_uid;
490 pr_stat->st_gid = stat->st_gid;
491 pr_stat->st_rdev = stat->st_rdev;
492 pr_stat->st_size = stat->st_size;
493 pr_stat->st_blksize = stat->st_blksize;
494 pr_stat->st_blocks = stat->st_blocks;
495 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
496 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
497 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
498 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
499 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
500 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
503 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
505 memset(pr_stfs, 0, sizeof(*pr_stfs));
506 pr_stfs->f_type = stfs->f_type;
507 pr_stfs->f_bsize = stfs->f_bsize;
508 pr_stfs->f_blocks = stfs->f_blocks;
509 pr_stfs->f_bfree = stfs->f_bfree;
510 pr_stfs->f_bavail = stfs->f_bavail;
511 pr_stfs->f_files = stfs->f_files;
512 pr_stfs->f_ffree = stfs->f_ffree;
513 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
514 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
515 pr_stfs->f_namelen = stfs->f_namelen;
516 pr_stfs->f_frsize = stfs->f_frsize;
520 * Gets stat/statfs information and packs in out_iovec structure
521 * on success returns number of bytes packed in out_iovec structure
522 * otherwise returns -errno
524 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
531 struct statfs stfs_buf;
533 v9fs_string_init(&path);
534 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
541 retval = lstat(path.data, &st_buf);
545 stat_to_prstat(&pr_stat, &st_buf);
546 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
547 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
548 pr_stat.st_ino, pr_stat.st_nlink,
549 pr_stat.st_mode, pr_stat.st_uid,
550 pr_stat.st_gid, pr_stat.st_rdev,
551 pr_stat.st_size, pr_stat.st_blksize,
553 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
554 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
555 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
559 retval = statfs(path.data, &stfs_buf);
563 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
564 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
565 "qqqqqqqqqqq", pr_stfs.f_type,
566 pr_stfs.f_bsize, pr_stfs.f_blocks,
567 pr_stfs.f_bfree, pr_stfs.f_bavail,
568 pr_stfs.f_files, pr_stfs.f_ffree,
569 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
570 pr_stfs.f_namelen, pr_stfs.f_frsize);
574 v9fs_string_free(&path);
578 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
582 V9fsString target, path;
584 v9fs_string_init(&path);
585 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
587 v9fs_string_free(&path);
590 buffer = g_malloc(size);
591 v9fs_string_init(&target);
592 retval = readlink(path.data, buffer, size - 1);
594 buffer[retval] = '\0';
595 v9fs_string_sprintf(&target, "%s", buffer);
596 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
601 v9fs_string_free(&target);
602 v9fs_string_free(&path);
607 * create other filesystem objects and send 0 on success
608 * return -errno on error
610 static int do_create_others(int type, struct iovec *iovec)
614 int offset = PROXY_HDR_SZ;
615 V9fsString oldpath, path;
616 int mode, uid, gid, cur_uid, cur_gid;
618 v9fs_string_init(&path);
619 v9fs_string_init(&oldpath);
621 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
626 retval = setugid(uid, gid, &cur_uid, &cur_gid);
628 goto unmarshal_err_out;
632 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
636 retval = mknod(path.data, mode, rdev);
639 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
643 retval = g_mkdir(path.data, mode);
646 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
650 retval = symlink(oldpath.data, path.data);
658 resetugid(cur_uid, cur_gid);
660 v9fs_string_free(&path);
661 v9fs_string_free(&oldpath);
666 * create a file and send fd on success
667 * return -errno on error
669 static int do_create(struct iovec *iovec)
673 int flags, mode, uid, gid, cur_uid, cur_gid;
675 v9fs_string_init(&path);
676 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
677 &path, &flags, &mode, &uid, &gid);
679 goto unmarshal_err_out;
681 ret = setugid(uid, gid, &cur_uid, &cur_gid);
683 goto unmarshal_err_out;
685 ret = open(path.data, flags, mode);
690 resetugid(cur_uid, cur_gid);
692 v9fs_string_free(&path);
697 * open a file and send fd on success
698 * return -errno on error
700 static int do_open(struct iovec *iovec)
705 v9fs_string_init(&path);
706 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
710 ret = open(path.data, flags);
715 v9fs_string_free(&path);
719 /* create unix domain socket and return the descriptor */
720 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
723 struct sockaddr_un proxy, qemu;
726 /* requested socket already exists, refuse to start */
727 if (!access(path, F_OK)) {
728 do_log(LOG_CRIT, "socket already exists\n");
732 if (strlen(path) >= sizeof(proxy.sun_path)) {
733 do_log(LOG_CRIT, "UNIX domain socket path exceeds %zu characters\n",
734 sizeof(proxy.sun_path));
738 sock = socket(AF_UNIX, SOCK_STREAM, 0);
744 /* mask other part of mode bits */
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) {
754 if (chown(proxy.sun_path, uid, gid) < 0) {
758 if (listen(sock, 1) < 0) {
764 client = accept(sock, (struct sockaddr *)&qemu, &size);
777 static void usage(void)
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"
782 " {-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"
786 " [-n|--nodaemon] Run as a normal program\n",
790 static int process_reply(int sock, int type,
791 struct iovec *out_iovec, int retval)
796 if (send_fd(sock, retval) < 0) {
812 if (send_status(sock, out_iovec, retval) < 0) {
822 if (send_response(sock, out_iovec, retval) < 0) {
833 static int process_requests(int sock)
841 V9fsString name, value;
842 struct timespec spec[2];
843 V9fsString oldpath, path;
844 struct iovec in_iovec, out_iovec;
846 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;
853 * initialize the header type, so that we send
854 * response to proper request type.
857 retval = read_request(sock, &in_iovec, &header);
862 switch (header.type) {
864 retval = do_open(&in_iovec);
867 retval = do_create(&in_iovec);
872 retval = do_create_others(header.type, &in_iovec);
875 v9fs_string_init(&path);
876 v9fs_string_init(&oldpath);
877 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
878 "ss", &oldpath, &path);
880 retval = link(oldpath.data, path.data);
885 v9fs_string_free(&oldpath);
886 v9fs_string_free(&path);
890 retval = do_stat(header.type, &in_iovec, &out_iovec);
893 retval = do_readlink(&in_iovec, &out_iovec);
896 v9fs_string_init(&path);
897 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
900 retval = chmod(path.data, mode);
905 v9fs_string_free(&path);
908 v9fs_string_init(&path);
909 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
912 retval = lchown(path.data, uid, gid);
917 v9fs_string_free(&path);
920 v9fs_string_init(&path);
921 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
924 retval = truncate(path.data, offset);
929 v9fs_string_free(&path);
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);
937 retval = utimensat(AT_FDCWD, path.data, spec,
938 AT_SYMLINK_NOFOLLOW);
943 v9fs_string_free(&path);
946 v9fs_string_init(&path);
947 v9fs_string_init(&oldpath);
948 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
949 "ss", &oldpath, &path);
951 retval = rename(oldpath.data, path.data);
956 v9fs_string_free(&oldpath);
957 v9fs_string_free(&path);
960 v9fs_string_init(&path);
961 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
963 retval = remove(path.data);
968 v9fs_string_free(&path);
972 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
975 v9fs_string_init(&path);
976 v9fs_string_init(&name);
977 v9fs_string_init(&value);
978 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
979 &name, &value, &size, &flags);
981 retval = lsetxattr(path.data,
982 name.data, value.data, size, flags);
987 v9fs_string_free(&path);
988 v9fs_string_free(&name);
989 v9fs_string_free(&value);
992 v9fs_string_init(&path);
993 v9fs_string_init(&name);
994 retval = proxy_unmarshal(&in_iovec,
995 PROXY_HDR_SZ, "ss", &path, &name);
997 retval = lremovexattr(path.data, name.data);
1002 v9fs_string_free(&path);
1003 v9fs_string_free(&name);
1006 retval = do_getversion(&in_iovec, &out_iovec);
1013 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1018 g_free(in_iovec.iov_base);
1019 g_free(out_iovec.iov_base);
1023 int main(int argc, char **argv)
1029 char *sock_name = NULL;
1031 int c, option_index;
1032 #ifdef FS_IOC_GETVERSION
1034 struct statfs st_fs;
1037 prog_name = g_path_get_basename(argv[0]);
1044 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1051 rpath = g_strdup(optarg);
1057 sock = atoi(optarg);
1060 sock_name = g_strdup(optarg);
1063 own_u = atoi(optarg);
1066 own_g = atoi(optarg);
1076 /* Parameter validation */
1077 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1078 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1083 if (sock_name && sock != -1) {
1084 fprintf(stderr, "both named socket and socket descriptor specified\n");
1089 if (sock_name && (own_u == -1 || own_g == -1)) {
1090 fprintf(stderr, "owner uid:gid not specified, ");
1092 "owner uid:gid specifies who can access the socket file\n");
1097 if (lstat(rpath, &stbuf) < 0) {
1098 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1099 rpath, strerror(errno));
1103 if (!S_ISDIR(stbuf.st_mode)) {
1104 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1109 if (daemon(0, 0) < 0) {
1110 fprintf(stderr, "daemon call failed\n");
1113 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1116 do_log(LOG_INFO, "Started\n");
1118 sock = proxy_socket(sock_name, own_u, own_g);
1124 if (chroot(rpath) < 0) {
1125 do_perror("chroot");
1128 if (chdir("/") < 0) {
1133 get_version = false;
1134 #ifdef FS_IOC_GETVERSION
1135 /* check whether underlying FS support IOC_GETVERSION */
1136 retval = statfs("/", &st_fs);
1138 switch (st_fs.f_type) {
1139 case EXT2_SUPER_MAGIC:
1140 case BTRFS_SUPER_MAGIC:
1141 case REISERFS_SUPER_MAGIC:
1142 case XFS_SUPER_MAGIC:
1150 if (init_capabilities() < 0) {
1154 process_requests(sock);
1158 do_log(LOG_INFO, "Done\n");