2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
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 */
58 static char *prog_name;
60 static void GCC_FMT_ATTR(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 do_cap_set(cap_value_t *cap_value, int size, int reset)
87 * Start with an empty set and set permitted and effective
91 do_perror("cap_init");
94 if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
95 do_perror("cap_set_flag");
99 caps = cap_get_proc();
101 do_perror("cap_get_proc");
105 if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
106 do_perror("cap_set_flag");
109 if (cap_set_proc(caps) < 0) {
110 do_perror("cap_set_proc");
121 static int init_capabilities(void)
123 /* helper needs following capabilities only */
124 cap_value_t cap_list[] = {
133 return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
136 static int socket_read(int sockfd, void *buff, ssize_t size)
138 ssize_t retval, total = 0;
141 retval = read(sockfd, buff, size);
146 if (errno == EINTR) {
158 static int socket_write(int sockfd, void *buff, ssize_t size)
160 ssize_t retval, total = 0;
163 retval = write(sockfd, buff, size);
165 if (errno == EINTR) {
177 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
182 * read the request header.
185 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
189 iovec->iov_len = PROXY_HDR_SZ;
190 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
195 * We can't process message.size > PROXY_MAX_IO_SZ.
196 * Treat it as fatal error
198 if (header->size > PROXY_MAX_IO_SZ) {
201 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
205 iovec->iov_len += header->size;
209 static int send_fd(int sockfd, int fd)
214 struct cmsghdr *cmsg;
215 union MsgControl msg_control;
217 iov.iov_base = &data;
218 iov.iov_len = sizeof(data);
220 memset(&msg, 0, sizeof(msg));
223 /* No ancillary data on error */
225 /* fd is really negative errno if the request failed */
228 data = V9FS_FD_VALID;
229 msg.msg_control = &msg_control;
230 msg.msg_controllen = sizeof(msg_control);
232 cmsg = &msg_control.cmsg;
233 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
234 cmsg->cmsg_level = SOL_SOCKET;
235 cmsg->cmsg_type = SCM_RIGHTS;
236 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
240 retval = sendmsg(sockfd, &msg, 0);
241 } while (retval < 0 && errno == EINTR);
251 static int send_status(int sockfd, struct iovec *iovec, int status)
254 int retval, msg_size;
257 header.type = T_ERROR;
259 header.type = T_SUCCESS;
261 header.size = sizeof(status);
263 * marshal the return status. We don't check error.
264 * because we are sure we have enough space for the status
266 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
267 header.size, status);
271 retval = socket_write(sockfd, iovec->iov_base, msg_size);
279 * from man 7 capabilities, section
280 * Effect of User ID Changes on Capabilities:
281 * If the effective user ID is changed from nonzero to 0, then the permitted
282 * set is copied to the effective set. If the effective user ID is changed
283 * from 0 to nonzero, then all capabilities are are cleared from the effective
286 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
287 * expose the program to unwanted signals, but this is not true anymore: for an
288 * unprivileged (without CAP_KILL) program to send a signal, the real or
289 * effective user ID of the sending process must equal the real or saved user
290 * ID of the target process. Even when dropping privileges, it is enough to
291 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
292 * be exposed to signals. So just use setresuid/setresgid.
294 static int setugid(int uid, int gid, int *suid, int *sgid)
299 * We still need DAC_OVERRIDE because we don't change
300 * supplementary group ids, and hence may be subjected DAC rules
302 cap_value_t cap_list[] = {
309 if (setresgid(-1, gid, *sgid) == -1) {
314 if (setresuid(-1, uid, *suid) == -1) {
319 if (uid != 0 || gid != 0) {
320 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
328 if (setresuid(-1, *suid, *suid) == -1) {
332 if (setresgid(-1, *sgid, *sgid) == -1) {
340 * This is used to reset the ugid back with the saved values
341 * There is nothing much we can do checking error values here.
343 static void resetugid(int suid, int sgid)
345 if (setresgid(-1, sgid, sgid) == -1) {
348 if (setresuid(-1, suid, suid) == -1) {
354 * send response in two parts
356 * 2) Response or error status
357 * This function should be called with marshaled response
358 * send_response constructs header part and error part only.
359 * send response sends {ProxyHeader,Response} if the request was success
360 * otherwise sends {ProxyHeader,error status}
362 static int send_response(int sock, struct iovec *iovec, int size)
368 * If response size exceeds available iovec->iov_len,
371 if (size > PROXY_MAX_IO_SZ) {
377 * In case of error we would not have got the error encoded
378 * already so encode the error here.
380 header.type = T_ERROR;
381 header.size = sizeof(size);
382 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
384 header.type = T_SUCCESS;
387 proxy_marshal(iovec, 0, "dd", header.type, header.size);
388 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
396 * gets generation number
397 * returns -errno on failure and sizeof(generation number) on success
399 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
402 int retval = -ENOTTY;
403 #ifdef FS_IOC_GETVERSION
409 /* no need to issue ioctl */
412 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
415 #ifdef FS_IOC_GETVERSION
416 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
421 fd = open(path.data, O_RDONLY);
426 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
429 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
433 v9fs_string_free(&path);
438 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
440 int size = 0, offset, retval;
441 V9fsString path, name, xattr;
443 v9fs_string_init(&xattr);
444 v9fs_string_init(&path);
445 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
449 offset = PROXY_HDR_SZ + retval;
452 xattr.data = g_malloc(size);
457 v9fs_string_init(&name);
458 retval = proxy_unmarshal(iovec, offset, "s", &name);
460 retval = lgetxattr(path.data, name.data, xattr.data, size);
467 v9fs_string_free(&name);
470 retval = llistxattr(path.data, xattr.data, size);
483 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
484 retval = sizeof(retval);
486 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
489 v9fs_string_free(&xattr);
490 v9fs_string_free(&path);
494 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
496 memset(pr_stat, 0, sizeof(*pr_stat));
497 pr_stat->st_dev = stat->st_dev;
498 pr_stat->st_ino = stat->st_ino;
499 pr_stat->st_nlink = stat->st_nlink;
500 pr_stat->st_mode = stat->st_mode;
501 pr_stat->st_uid = stat->st_uid;
502 pr_stat->st_gid = stat->st_gid;
503 pr_stat->st_rdev = stat->st_rdev;
504 pr_stat->st_size = stat->st_size;
505 pr_stat->st_blksize = stat->st_blksize;
506 pr_stat->st_blocks = stat->st_blocks;
507 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
508 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
509 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
510 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
511 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
512 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
515 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
517 memset(pr_stfs, 0, sizeof(*pr_stfs));
518 pr_stfs->f_type = stfs->f_type;
519 pr_stfs->f_bsize = stfs->f_bsize;
520 pr_stfs->f_blocks = stfs->f_blocks;
521 pr_stfs->f_bfree = stfs->f_bfree;
522 pr_stfs->f_bavail = stfs->f_bavail;
523 pr_stfs->f_files = stfs->f_files;
524 pr_stfs->f_ffree = stfs->f_ffree;
525 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
526 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
527 pr_stfs->f_namelen = stfs->f_namelen;
528 pr_stfs->f_frsize = stfs->f_frsize;
532 * Gets stat/statfs information and packs in out_iovec structure
533 * on success returns number of bytes packed in out_iovec struture
534 * otherwise returns -errno
536 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
543 struct statfs stfs_buf;
545 v9fs_string_init(&path);
546 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
553 retval = lstat(path.data, &st_buf);
557 stat_to_prstat(&pr_stat, &st_buf);
558 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
559 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
560 pr_stat.st_ino, pr_stat.st_nlink,
561 pr_stat.st_mode, pr_stat.st_uid,
562 pr_stat.st_gid, pr_stat.st_rdev,
563 pr_stat.st_size, pr_stat.st_blksize,
565 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
566 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
567 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
571 retval = statfs(path.data, &stfs_buf);
575 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
576 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
577 "qqqqqqqqqqq", pr_stfs.f_type,
578 pr_stfs.f_bsize, pr_stfs.f_blocks,
579 pr_stfs.f_bfree, pr_stfs.f_bavail,
580 pr_stfs.f_files, pr_stfs.f_ffree,
581 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
582 pr_stfs.f_namelen, pr_stfs.f_frsize);
586 v9fs_string_free(&path);
590 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
594 V9fsString target, path;
596 v9fs_string_init(&path);
597 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
599 v9fs_string_free(&path);
602 buffer = g_malloc(size);
603 v9fs_string_init(&target);
604 retval = readlink(path.data, buffer, size - 1);
606 buffer[retval] = '\0';
607 v9fs_string_sprintf(&target, "%s", buffer);
608 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
613 v9fs_string_free(&target);
614 v9fs_string_free(&path);
619 * create other filesystem objects and send 0 on success
620 * return -errno on error
622 static int do_create_others(int type, struct iovec *iovec)
626 int offset = PROXY_HDR_SZ;
627 V9fsString oldpath, path;
628 int mode, uid, gid, cur_uid, cur_gid;
630 v9fs_string_init(&path);
631 v9fs_string_init(&oldpath);
633 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
638 retval = setugid(uid, gid, &cur_uid, &cur_gid);
640 goto unmarshal_err_out;
644 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
648 retval = mknod(path.data, mode, rdev);
651 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
655 retval = mkdir(path.data, mode);
658 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
662 retval = symlink(oldpath.data, path.data);
670 resetugid(cur_uid, cur_gid);
672 v9fs_string_free(&path);
673 v9fs_string_free(&oldpath);
678 * create a file and send fd on success
679 * return -errno on error
681 static int do_create(struct iovec *iovec)
685 int flags, mode, uid, gid, cur_uid, cur_gid;
687 v9fs_string_init(&path);
688 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
689 &path, &flags, &mode, &uid, &gid);
691 goto unmarshal_err_out;
693 ret = setugid(uid, gid, &cur_uid, &cur_gid);
695 goto unmarshal_err_out;
697 ret = open(path.data, flags, mode);
702 resetugid(cur_uid, cur_gid);
704 v9fs_string_free(&path);
709 * open a file and send fd on success
710 * return -errno on error
712 static int do_open(struct iovec *iovec)
717 v9fs_string_init(&path);
718 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
722 ret = open(path.data, flags);
727 v9fs_string_free(&path);
731 /* create unix domain socket and return the descriptor */
732 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
735 struct sockaddr_un proxy, qemu;
738 /* requested socket already exists, refuse to start */
739 if (!access(path, F_OK)) {
740 do_log(LOG_CRIT, "socket already exists\n");
744 if (strlen(path) >= sizeof(proxy.sun_path)) {
745 do_log(LOG_CRIT, "UNIX domain socket path exceeds %zu characters\n",
746 sizeof(proxy.sun_path));
750 sock = socket(AF_UNIX, SOCK_STREAM, 0);
756 /* mask other part of mode bits */
759 proxy.sun_family = AF_UNIX;
760 strcpy(proxy.sun_path, path);
761 if (bind(sock, (struct sockaddr *)&proxy,
762 sizeof(struct sockaddr_un)) < 0) {
766 if (chown(proxy.sun_path, uid, gid) < 0) {
770 if (listen(sock, 1) < 0) {
776 client = accept(sock, (struct sockaddr *)&qemu, &size);
789 static void usage(void)
791 fprintf(stderr, "usage: %s\n"
792 " -p|--path <path> 9p path to export\n"
793 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
794 " {-s|--socket <socketname> socket file used for communication\n"
795 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
796 " access to this socket\n"
797 " \tNote: -s & -f can not be used together\n"
798 " [-n|--nodaemon] Run as a normal program\n",
802 static int process_reply(int sock, int type,
803 struct iovec *out_iovec, int retval)
808 if (send_fd(sock, retval) < 0) {
824 if (send_status(sock, out_iovec, retval) < 0) {
834 if (send_response(sock, out_iovec, retval) < 0) {
845 static int process_requests(int sock)
853 V9fsString name, value;
854 struct timespec spec[2];
855 V9fsString oldpath, path;
856 struct iovec in_iovec, out_iovec;
858 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
859 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
860 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
861 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
865 * initialize the header type, so that we send
866 * response to proper request type.
869 retval = read_request(sock, &in_iovec, &header);
874 switch (header.type) {
876 retval = do_open(&in_iovec);
879 retval = do_create(&in_iovec);
884 retval = do_create_others(header.type, &in_iovec);
887 v9fs_string_init(&path);
888 v9fs_string_init(&oldpath);
889 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
890 "ss", &oldpath, &path);
892 retval = link(oldpath.data, path.data);
897 v9fs_string_free(&oldpath);
898 v9fs_string_free(&path);
902 retval = do_stat(header.type, &in_iovec, &out_iovec);
905 retval = do_readlink(&in_iovec, &out_iovec);
908 v9fs_string_init(&path);
909 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
912 retval = chmod(path.data, mode);
917 v9fs_string_free(&path);
920 v9fs_string_init(&path);
921 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
924 retval = lchown(path.data, uid, gid);
929 v9fs_string_free(&path);
932 v9fs_string_init(&path);
933 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
936 retval = truncate(path.data, offset);
941 v9fs_string_free(&path);
944 v9fs_string_init(&path);
945 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
946 &spec[0].tv_sec, &spec[0].tv_nsec,
947 &spec[1].tv_sec, &spec[1].tv_nsec);
949 retval = utimensat(AT_FDCWD, path.data, spec,
950 AT_SYMLINK_NOFOLLOW);
955 v9fs_string_free(&path);
958 v9fs_string_init(&path);
959 v9fs_string_init(&oldpath);
960 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
961 "ss", &oldpath, &path);
963 retval = rename(oldpath.data, path.data);
968 v9fs_string_free(&oldpath);
969 v9fs_string_free(&path);
972 v9fs_string_init(&path);
973 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
975 retval = remove(path.data);
980 v9fs_string_free(&path);
984 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
987 v9fs_string_init(&path);
988 v9fs_string_init(&name);
989 v9fs_string_init(&value);
990 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
991 &name, &value, &size, &flags);
993 retval = lsetxattr(path.data,
994 name.data, value.data, size, flags);
999 v9fs_string_free(&path);
1000 v9fs_string_free(&name);
1001 v9fs_string_free(&value);
1003 case T_LREMOVEXATTR:
1004 v9fs_string_init(&path);
1005 v9fs_string_init(&name);
1006 retval = proxy_unmarshal(&in_iovec,
1007 PROXY_HDR_SZ, "ss", &path, &name);
1009 retval = lremovexattr(path.data, name.data);
1014 v9fs_string_free(&path);
1015 v9fs_string_free(&name);
1018 retval = do_getversion(&in_iovec, &out_iovec);
1025 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1030 g_free(in_iovec.iov_base);
1031 g_free(out_iovec.iov_base);
1035 int main(int argc, char **argv)
1041 char *sock_name = NULL;
1043 int c, option_index;
1044 #ifdef FS_IOC_GETVERSION
1046 struct statfs st_fs;
1049 prog_name = g_path_get_basename(argv[0]);
1056 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1063 rpath = g_strdup(optarg);
1069 sock = atoi(optarg);
1072 sock_name = g_strdup(optarg);
1075 own_u = atoi(optarg);
1078 own_g = atoi(optarg);
1088 /* Parameter validation */
1089 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1090 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1095 if (sock_name && sock != -1) {
1096 fprintf(stderr, "both named socket and socket descriptor specified\n");
1101 if (sock_name && (own_u == -1 || own_g == -1)) {
1102 fprintf(stderr, "owner uid:gid not specified, ");
1104 "owner uid:gid specifies who can access the socket file\n");
1109 if (lstat(rpath, &stbuf) < 0) {
1110 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1111 rpath, strerror(errno));
1115 if (!S_ISDIR(stbuf.st_mode)) {
1116 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1121 if (daemon(0, 0) < 0) {
1122 fprintf(stderr, "daemon call failed\n");
1125 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1128 do_log(LOG_INFO, "Started\n");
1130 sock = proxy_socket(sock_name, own_u, own_g);
1136 if (chroot(rpath) < 0) {
1137 do_perror("chroot");
1140 if (chdir("/") < 0) {
1145 get_version = false;
1146 #ifdef FS_IOC_GETVERSION
1147 /* check whether underlying FS support IOC_GETVERSION */
1148 retval = statfs("/", &st_fs);
1150 switch (st_fs.f_type) {
1151 case EXT2_SUPER_MAGIC:
1152 case BTRFS_SUPER_MAGIC:
1153 case REISERFS_SUPER_MAGIC:
1154 case XFS_SUPER_MAGIC:
1162 if (init_capabilities() < 0) {
1166 process_requests(sock);
1170 do_log(LOG_INFO, "Done\n");