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 <sys/resource.h>
16 #include <sys/capability.h>
17 #include <sys/fsuid.h>
19 #include <sys/ioctl.h>
21 #ifdef CONFIG_LINUX_MAGIC_H
22 #include <linux/magic.h>
24 #include "qemu-common.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 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 */
59 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
65 vsyslog(LOG_CRIT, format, ap);
67 vfprintf(stderr, format, ap);
72 static void do_perror(const char *string)
75 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
77 fprintf(stderr, "%s:%s\n", string, strerror(errno));
81 static int do_cap_set(cap_value_t *cap_value, int size, int reset)
86 * Start with an empty set and set permitted and effective
90 do_perror("cap_init");
93 if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
94 do_perror("cap_set_flag");
98 caps = cap_get_proc();
100 do_perror("cap_get_proc");
104 if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
105 do_perror("cap_set_flag");
108 if (cap_set_proc(caps) < 0) {
109 do_perror("cap_set_proc");
120 static int init_capabilities(void)
122 /* helper needs following capabilities only */
123 cap_value_t cap_list[] = {
132 return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
135 static int socket_read(int sockfd, void *buff, ssize_t size)
137 ssize_t retval, total = 0;
140 retval = read(sockfd, buff, size);
145 if (errno == EINTR) {
157 static int socket_write(int sockfd, void *buff, ssize_t size)
159 ssize_t retval, total = 0;
162 retval = write(sockfd, buff, size);
164 if (errno == EINTR) {
176 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
181 * read the request header.
184 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
188 iovec->iov_len = PROXY_HDR_SZ;
189 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
194 * We can't process message.size > PROXY_MAX_IO_SZ.
195 * Treat it as fatal error
197 if (header->size > PROXY_MAX_IO_SZ) {
200 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
204 iovec->iov_len += header->size;
208 static int send_fd(int sockfd, int fd)
213 struct cmsghdr *cmsg;
214 union MsgControl msg_control;
216 iov.iov_base = &data;
217 iov.iov_len = sizeof(data);
219 memset(&msg, 0, sizeof(msg));
222 /* No ancillary data on error */
224 /* fd is really negative errno if the request failed */
227 data = V9FS_FD_VALID;
228 msg.msg_control = &msg_control;
229 msg.msg_controllen = sizeof(msg_control);
231 cmsg = &msg_control.cmsg;
232 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
233 cmsg->cmsg_level = SOL_SOCKET;
234 cmsg->cmsg_type = SCM_RIGHTS;
235 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
239 retval = sendmsg(sockfd, &msg, 0);
240 } while (retval < 0 && errno == EINTR);
250 static int send_status(int sockfd, struct iovec *iovec, int status)
253 int retval, msg_size;
256 header.type = T_ERROR;
258 header.type = T_SUCCESS;
260 header.size = sizeof(status);
262 * marshal the return status. We don't check error.
263 * because we are sure we have enough space for the status
265 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
266 header.size, status);
270 retval = socket_write(sockfd, iovec->iov_base, msg_size);
278 * from man 7 capabilities, section
279 * Effect of User ID Changes on Capabilities:
280 * If the effective user ID is changed from nonzero to 0, then the permitted
281 * set is copied to the effective set. If the effective user ID is changed
282 * from 0 to nonzero, then all capabilities are are cleared from the effective
285 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
286 * expose the program to unwanted signals, but this is not true anymore: for an
287 * unprivileged (without CAP_KILL) program to send a signal, the real or
288 * effective user ID of the sending process must equal the real or saved user
289 * ID of the target process. Even when dropping privileges, it is enough to
290 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
291 * be exposed to signals. So just use setresuid/setresgid.
293 static int setugid(int uid, int gid, int *suid, int *sgid)
298 * We still need DAC_OVERRIDE because we don't change
299 * supplementary group ids, and hence may be subjected DAC rules
301 cap_value_t cap_list[] = {
308 if (setresgid(-1, gid, *sgid) == -1) {
313 if (setresuid(-1, uid, *suid) == -1) {
318 if (uid != 0 || gid != 0) {
319 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
327 if (setresuid(-1, *suid, *suid) == -1) {
331 if (setresgid(-1, *sgid, *sgid) == -1) {
339 * This is used to reset the ugid back with the saved values
340 * There is nothing much we can do checking error values here.
342 static void resetugid(int suid, int sgid)
344 if (setresgid(-1, sgid, sgid) == -1) {
347 if (setresuid(-1, suid, suid) == -1) {
353 * send response in two parts
355 * 2) Response or error status
356 * This function should be called with marshaled response
357 * send_response constructs header part and error part only.
358 * send response sends {ProxyHeader,Response} if the request was success
359 * otherwise sends {ProxyHeader,error status}
361 static int send_response(int sock, struct iovec *iovec, int size)
367 * If response size exceeds available iovec->iov_len,
370 if (size > PROXY_MAX_IO_SZ) {
376 * In case of error we would not have got the error encoded
377 * already so encode the error here.
379 header.type = T_ERROR;
380 header.size = sizeof(size);
381 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
383 header.type = T_SUCCESS;
386 proxy_marshal(iovec, 0, "dd", header.type, header.size);
387 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
395 * gets generation number
396 * returns -errno on failure and sizeof(generation number) on success
398 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
401 int retval = -ENOTTY;
402 #ifdef FS_IOC_GETVERSION
408 /* no need to issue ioctl */
411 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
414 #ifdef FS_IOC_GETVERSION
415 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
420 fd = open(path.data, O_RDONLY);
425 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
428 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
432 v9fs_string_free(&path);
437 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
439 int size = 0, offset, retval;
440 V9fsString path, name, xattr;
442 v9fs_string_init(&xattr);
443 v9fs_string_init(&path);
444 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
448 offset = PROXY_HDR_SZ + retval;
451 xattr.data = g_malloc(size);
456 v9fs_string_init(&name);
457 retval = proxy_unmarshal(iovec, offset, "s", &name);
459 retval = lgetxattr(path.data, name.data, xattr.data, size);
466 v9fs_string_free(&name);
469 retval = llistxattr(path.data, xattr.data, size);
482 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
483 retval = sizeof(retval);
485 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
488 v9fs_string_free(&xattr);
489 v9fs_string_free(&path);
493 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
495 memset(pr_stat, 0, sizeof(*pr_stat));
496 pr_stat->st_dev = stat->st_dev;
497 pr_stat->st_ino = stat->st_ino;
498 pr_stat->st_nlink = stat->st_nlink;
499 pr_stat->st_mode = stat->st_mode;
500 pr_stat->st_uid = stat->st_uid;
501 pr_stat->st_gid = stat->st_gid;
502 pr_stat->st_rdev = stat->st_rdev;
503 pr_stat->st_size = stat->st_size;
504 pr_stat->st_blksize = stat->st_blksize;
505 pr_stat->st_blocks = stat->st_blocks;
506 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
507 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
508 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
509 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
510 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
511 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
514 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
516 memset(pr_stfs, 0, sizeof(*pr_stfs));
517 pr_stfs->f_type = stfs->f_type;
518 pr_stfs->f_bsize = stfs->f_bsize;
519 pr_stfs->f_blocks = stfs->f_blocks;
520 pr_stfs->f_bfree = stfs->f_bfree;
521 pr_stfs->f_bavail = stfs->f_bavail;
522 pr_stfs->f_files = stfs->f_files;
523 pr_stfs->f_ffree = stfs->f_ffree;
524 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
525 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
526 pr_stfs->f_namelen = stfs->f_namelen;
527 pr_stfs->f_frsize = stfs->f_frsize;
531 * Gets stat/statfs information and packs in out_iovec structure
532 * on success returns number of bytes packed in out_iovec struture
533 * otherwise returns -errno
535 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
542 struct statfs stfs_buf;
544 v9fs_string_init(&path);
545 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
552 retval = lstat(path.data, &st_buf);
556 stat_to_prstat(&pr_stat, &st_buf);
557 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
558 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
559 pr_stat.st_ino, pr_stat.st_nlink,
560 pr_stat.st_mode, pr_stat.st_uid,
561 pr_stat.st_gid, pr_stat.st_rdev,
562 pr_stat.st_size, pr_stat.st_blksize,
564 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
565 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
566 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
570 retval = statfs(path.data, &stfs_buf);
574 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
575 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
576 "qqqqqqqqqqq", pr_stfs.f_type,
577 pr_stfs.f_bsize, pr_stfs.f_blocks,
578 pr_stfs.f_bfree, pr_stfs.f_bavail,
579 pr_stfs.f_files, pr_stfs.f_ffree,
580 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
581 pr_stfs.f_namelen, pr_stfs.f_frsize);
585 v9fs_string_free(&path);
589 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
593 V9fsString target, path;
595 v9fs_string_init(&path);
596 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
598 v9fs_string_free(&path);
601 buffer = g_malloc(size);
602 v9fs_string_init(&target);
603 retval = readlink(path.data, buffer, size - 1);
605 buffer[retval] = '\0';
606 v9fs_string_sprintf(&target, "%s", buffer);
607 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
612 v9fs_string_free(&target);
613 v9fs_string_free(&path);
618 * create other filesystem objects and send 0 on success
619 * return -errno on error
621 static int do_create_others(int type, struct iovec *iovec)
625 int offset = PROXY_HDR_SZ;
626 V9fsString oldpath, path;
627 int mode, uid, gid, cur_uid, cur_gid;
629 v9fs_string_init(&path);
630 v9fs_string_init(&oldpath);
632 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
637 retval = setugid(uid, gid, &cur_uid, &cur_gid);
639 goto unmarshal_err_out;
643 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
647 retval = mknod(path.data, mode, rdev);
650 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
654 retval = mkdir(path.data, mode);
657 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
661 retval = symlink(oldpath.data, path.data);
669 resetugid(cur_uid, cur_gid);
671 v9fs_string_free(&path);
672 v9fs_string_free(&oldpath);
677 * create a file and send fd on success
678 * return -errno on error
680 static int do_create(struct iovec *iovec)
684 int flags, mode, uid, gid, cur_uid, cur_gid;
686 v9fs_string_init(&path);
687 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
688 &path, &flags, &mode, &uid, &gid);
690 goto unmarshal_err_out;
692 ret = setugid(uid, gid, &cur_uid, &cur_gid);
694 goto unmarshal_err_out;
696 ret = open(path.data, flags, mode);
701 resetugid(cur_uid, cur_gid);
703 v9fs_string_free(&path);
708 * open a file and send fd on success
709 * return -errno on error
711 static int do_open(struct iovec *iovec)
716 v9fs_string_init(&path);
717 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
721 ret = open(path.data, flags);
726 v9fs_string_free(&path);
730 /* create unix domain socket and return the descriptor */
731 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
734 struct sockaddr_un proxy, qemu;
737 /* requested socket already exists, refuse to start */
738 if (!access(path, F_OK)) {
739 do_log(LOG_CRIT, "socket already exists\n");
743 if (strlen(path) >= sizeof(proxy.sun_path)) {
744 do_log(LOG_CRIT, "UNIX domain socket path exceeds %zu characters\n",
745 sizeof(proxy.sun_path));
749 sock = socket(AF_UNIX, SOCK_STREAM, 0);
755 /* mask other part of mode bits */
758 proxy.sun_family = AF_UNIX;
759 strcpy(proxy.sun_path, path);
760 if (bind(sock, (struct sockaddr *)&proxy,
761 sizeof(struct sockaddr_un)) < 0) {
765 if (chown(proxy.sun_path, uid, gid) < 0) {
769 if (listen(sock, 1) < 0) {
775 client = accept(sock, (struct sockaddr *)&qemu, &size);
788 static void usage(char *prog)
790 fprintf(stderr, "usage: %s\n"
791 " -p|--path <path> 9p path to export\n"
792 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
793 " {-s|--socket <socketname> socket file used for communication\n"
794 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
795 " access to this socket\n"
796 " \tNote: -s & -f can not be used together\n"
797 " [-n|--nodaemon] Run as a normal program\n",
801 static int process_reply(int sock, int type,
802 struct iovec *out_iovec, int retval)
807 if (send_fd(sock, retval) < 0) {
823 if (send_status(sock, out_iovec, retval) < 0) {
833 if (send_response(sock, out_iovec, retval) < 0) {
844 static int process_requests(int sock)
852 V9fsString name, value;
853 struct timespec spec[2];
854 V9fsString oldpath, path;
855 struct iovec in_iovec, out_iovec;
857 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
858 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
859 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
860 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
864 * initialize the header type, so that we send
865 * response to proper request type.
868 retval = read_request(sock, &in_iovec, &header);
873 switch (header.type) {
875 retval = do_open(&in_iovec);
878 retval = do_create(&in_iovec);
883 retval = do_create_others(header.type, &in_iovec);
886 v9fs_string_init(&path);
887 v9fs_string_init(&oldpath);
888 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
889 "ss", &oldpath, &path);
891 retval = link(oldpath.data, path.data);
896 v9fs_string_free(&oldpath);
897 v9fs_string_free(&path);
901 retval = do_stat(header.type, &in_iovec, &out_iovec);
904 retval = do_readlink(&in_iovec, &out_iovec);
907 v9fs_string_init(&path);
908 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
911 retval = chmod(path.data, mode);
916 v9fs_string_free(&path);
919 v9fs_string_init(&path);
920 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
923 retval = lchown(path.data, uid, gid);
928 v9fs_string_free(&path);
931 v9fs_string_init(&path);
932 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
935 retval = truncate(path.data, offset);
940 v9fs_string_free(&path);
943 v9fs_string_init(&path);
944 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
945 &spec[0].tv_sec, &spec[0].tv_nsec,
946 &spec[1].tv_sec, &spec[1].tv_nsec);
948 retval = qemu_utimens(path.data, spec);
953 v9fs_string_free(&path);
956 v9fs_string_init(&path);
957 v9fs_string_init(&oldpath);
958 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
959 "ss", &oldpath, &path);
961 retval = rename(oldpath.data, path.data);
966 v9fs_string_free(&oldpath);
967 v9fs_string_free(&path);
970 v9fs_string_init(&path);
971 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
973 retval = remove(path.data);
978 v9fs_string_free(&path);
982 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
985 v9fs_string_init(&path);
986 v9fs_string_init(&name);
987 v9fs_string_init(&value);
988 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
989 &name, &value, &size, &flags);
991 retval = lsetxattr(path.data,
992 name.data, value.data, size, flags);
997 v9fs_string_free(&path);
998 v9fs_string_free(&name);
999 v9fs_string_free(&value);
1001 case T_LREMOVEXATTR:
1002 v9fs_string_init(&path);
1003 v9fs_string_init(&name);
1004 retval = proxy_unmarshal(&in_iovec,
1005 PROXY_HDR_SZ, "ss", &path, &name);
1007 retval = lremovexattr(path.data, name.data);
1012 v9fs_string_free(&path);
1013 v9fs_string_free(&name);
1016 retval = do_getversion(&in_iovec, &out_iovec);
1023 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1028 g_free(in_iovec.iov_base);
1029 g_free(out_iovec.iov_base);
1033 int main(int argc, char **argv)
1039 char *sock_name = NULL;
1041 int c, option_index;
1042 #ifdef FS_IOC_GETVERSION
1044 struct statfs st_fs;
1052 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1059 rpath = g_strdup(optarg);
1065 sock = atoi(optarg);
1068 sock_name = g_strdup(optarg);
1071 own_u = atoi(optarg);
1074 own_g = atoi(optarg);
1084 /* Parameter validation */
1085 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1086 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1091 if (sock_name && sock != -1) {
1092 fprintf(stderr, "both named socket and socket descriptor specified\n");
1097 if (sock_name && (own_u == -1 || own_g == -1)) {
1098 fprintf(stderr, "owner uid:gid not specified, ");
1100 "owner uid:gid specifies who can access the socket file\n");
1105 if (lstat(rpath, &stbuf) < 0) {
1106 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1107 rpath, strerror(errno));
1111 if (!S_ISDIR(stbuf.st_mode)) {
1112 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1117 if (daemon(0, 0) < 0) {
1118 fprintf(stderr, "daemon call failed\n");
1121 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1124 do_log(LOG_INFO, "Started\n");
1126 sock = proxy_socket(sock_name, own_u, own_g);
1132 if (chdir("/") < 0) {
1136 if (chroot(rpath) < 0) {
1137 do_perror("chroot");
1141 get_version = false;
1142 #ifdef FS_IOC_GETVERSION
1143 /* check whether underlying FS support IOC_GETVERSION */
1144 retval = statfs("/", &st_fs);
1146 switch (st_fs.f_type) {
1147 case EXT2_SUPER_MAGIC:
1148 case BTRFS_SUPER_MAGIC:
1149 case REISERFS_SUPER_MAGIC:
1150 case XFS_SUPER_MAGIC:
1158 if (init_capabilities() < 0) {
1162 process_requests(sock);
1164 do_log(LOG_INFO, "Done\n");