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 <sys/resource.h>
15 #include <sys/capability.h>
16 #include <sys/fsuid.h>
18 #include <sys/ioctl.h>
20 #ifdef CONFIG_LINUX_MAGIC_H
21 #include <linux/magic.h>
23 #include "qemu-common.h"
24 #include "qemu/sockets.h"
25 #include "qemu/xattr.h"
26 #include "virtio-9p-marshal.h"
27 #include "hw/9pfs/virtio-9p-proxy.h"
28 #include "fsdev/virtio-9p-marshal.h"
30 #define PROGNAME "virtfs-proxy-helper"
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
45 static struct option helper_opts[] = {
46 {"fd", required_argument, NULL, 'f'},
47 {"path", required_argument, NULL, 'p'},
48 {"nodaemon", no_argument, NULL, 'n'},
49 {"socket", required_argument, NULL, 's'},
50 {"uid", required_argument, NULL, 'u'},
51 {"gid", required_argument, NULL, 'g'},
55 static bool is_daemon;
56 static bool get_version; /* IOC getversion IOCTL supported */
58 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
64 vsyslog(LOG_CRIT, format, ap);
66 vfprintf(stderr, format, ap);
71 static void do_perror(const char *string)
74 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
76 fprintf(stderr, "%s:%s\n", string, strerror(errno));
80 static int do_cap_set(cap_value_t *cap_value, int size, int reset)
85 * Start with an empty set and set permitted and effective
89 do_perror("cap_init");
92 if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
93 do_perror("cap_set_flag");
97 caps = cap_get_proc();
99 do_perror("cap_get_proc");
103 if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
104 do_perror("cap_set_flag");
107 if (cap_set_proc(caps) < 0) {
108 do_perror("cap_set_proc");
119 static int init_capabilities(void)
121 /* helper needs following capabilities only */
122 cap_value_t cap_list[] = {
131 return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
134 static int socket_read(int sockfd, void *buff, ssize_t size)
136 ssize_t retval, total = 0;
139 retval = read(sockfd, buff, size);
144 if (errno == EINTR) {
156 static int socket_write(int sockfd, void *buff, ssize_t size)
158 ssize_t retval, total = 0;
161 retval = write(sockfd, buff, size);
163 if (errno == EINTR) {
175 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
180 * read the request header.
183 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
187 iovec->iov_len = PROXY_HDR_SZ;
188 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
193 * We can't process message.size > PROXY_MAX_IO_SZ.
194 * Treat it as fatal error
196 if (header->size > PROXY_MAX_IO_SZ) {
199 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
203 iovec->iov_len += header->size;
207 static int send_fd(int sockfd, int fd)
212 struct cmsghdr *cmsg;
213 union MsgControl msg_control;
215 iov.iov_base = &data;
216 iov.iov_len = sizeof(data);
218 memset(&msg, 0, sizeof(msg));
221 /* No ancillary data on error */
223 /* fd is really negative errno if the request failed */
226 data = V9FS_FD_VALID;
227 msg.msg_control = &msg_control;
228 msg.msg_controllen = sizeof(msg_control);
230 cmsg = &msg_control.cmsg;
231 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
232 cmsg->cmsg_level = SOL_SOCKET;
233 cmsg->cmsg_type = SCM_RIGHTS;
234 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
238 retval = sendmsg(sockfd, &msg, 0);
239 } while (retval < 0 && errno == EINTR);
249 static int send_status(int sockfd, struct iovec *iovec, int status)
252 int retval, msg_size;
255 header.type = T_ERROR;
257 header.type = T_SUCCESS;
259 header.size = sizeof(status);
261 * marshal the return status. We don't check error.
262 * because we are sure we have enough space for the status
264 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
265 header.size, status);
269 retval = socket_write(sockfd, iovec->iov_base, msg_size);
277 * from man 7 capabilities, section
278 * Effect of User ID Changes on Capabilities:
279 * If the effective user ID is changed from nonzero to 0, then the permitted
280 * set is copied to the effective set. If the effective user ID is changed
281 * from 0 to nonzero, then all capabilities are are cleared from the effective
284 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
285 * expose the program to unwanted signals, but this is not true anymore: for an
286 * unprivileged (without CAP_KILL) program to send a signal, the real or
287 * effective user ID of the sending process must equal the real or saved user
288 * ID of the target process. Even when dropping privileges, it is enough to
289 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
290 * be exposed to signals. So just use setresuid/setresgid.
292 static int setugid(int uid, int gid, int *suid, int *sgid)
297 * We still need DAC_OVERRIDE because we don't change
298 * supplementary group ids, and hence may be subjected DAC rules
300 cap_value_t cap_list[] = {
307 if (setresgid(-1, gid, *sgid) == -1) {
312 if (setresuid(-1, uid, *suid) == -1) {
317 if (uid != 0 || gid != 0) {
318 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
326 if (setresuid(-1, *suid, *suid) == -1) {
330 if (setresgid(-1, *sgid, *sgid) == -1) {
338 * This is used to reset the ugid back with the saved values
339 * There is nothing much we can do checking error values here.
341 static void resetugid(int suid, int sgid)
343 if (setresgid(-1, sgid, sgid) == -1) {
346 if (setresuid(-1, suid, suid) == -1) {
352 * send response in two parts
354 * 2) Response or error status
355 * This function should be called with marshaled response
356 * send_response constructs header part and error part only.
357 * send response sends {ProxyHeader,Response} if the request was success
358 * otherwise sends {ProxyHeader,error status}
360 static int send_response(int sock, struct iovec *iovec, int size)
366 * If response size exceeds available iovec->iov_len,
369 if (size > PROXY_MAX_IO_SZ) {
375 * In case of error we would not have got the error encoded
376 * already so encode the error here.
378 header.type = T_ERROR;
379 header.size = sizeof(size);
380 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
382 header.type = T_SUCCESS;
385 proxy_marshal(iovec, 0, "dd", header.type, header.size);
386 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
394 * gets generation number
395 * returns -errno on failure and sizeof(generation number) on success
397 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
400 int retval = -ENOTTY;
401 #ifdef FS_IOC_GETVERSION
407 /* no need to issue ioctl */
410 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
413 #ifdef FS_IOC_GETVERSION
414 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
419 fd = open(path.data, O_RDONLY);
424 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
427 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
431 v9fs_string_free(&path);
436 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
438 int size = 0, offset, retval;
439 V9fsString path, name, xattr;
441 v9fs_string_init(&xattr);
442 v9fs_string_init(&path);
443 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
447 offset = PROXY_HDR_SZ + retval;
450 xattr.data = g_malloc(size);
455 v9fs_string_init(&name);
456 retval = proxy_unmarshal(iovec, offset, "s", &name);
458 retval = lgetxattr(path.data, name.data, xattr.data, size);
465 v9fs_string_free(&name);
468 retval = llistxattr(path.data, xattr.data, size);
481 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
482 retval = sizeof(retval);
484 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
487 v9fs_string_free(&xattr);
488 v9fs_string_free(&path);
492 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
494 memset(pr_stat, 0, sizeof(*pr_stat));
495 pr_stat->st_dev = stat->st_dev;
496 pr_stat->st_ino = stat->st_ino;
497 pr_stat->st_nlink = stat->st_nlink;
498 pr_stat->st_mode = stat->st_mode;
499 pr_stat->st_uid = stat->st_uid;
500 pr_stat->st_gid = stat->st_gid;
501 pr_stat->st_rdev = stat->st_rdev;
502 pr_stat->st_size = stat->st_size;
503 pr_stat->st_blksize = stat->st_blksize;
504 pr_stat->st_blocks = stat->st_blocks;
505 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
506 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
507 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
508 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
509 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
510 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
513 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
515 memset(pr_stfs, 0, sizeof(*pr_stfs));
516 pr_stfs->f_type = stfs->f_type;
517 pr_stfs->f_bsize = stfs->f_bsize;
518 pr_stfs->f_blocks = stfs->f_blocks;
519 pr_stfs->f_bfree = stfs->f_bfree;
520 pr_stfs->f_bavail = stfs->f_bavail;
521 pr_stfs->f_files = stfs->f_files;
522 pr_stfs->f_ffree = stfs->f_ffree;
523 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
524 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
525 pr_stfs->f_namelen = stfs->f_namelen;
526 pr_stfs->f_frsize = stfs->f_frsize;
530 * Gets stat/statfs information and packs in out_iovec structure
531 * on success returns number of bytes packed in out_iovec struture
532 * otherwise returns -errno
534 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
541 struct statfs stfs_buf;
543 v9fs_string_init(&path);
544 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
551 retval = lstat(path.data, &st_buf);
555 stat_to_prstat(&pr_stat, &st_buf);
556 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
557 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
558 pr_stat.st_ino, pr_stat.st_nlink,
559 pr_stat.st_mode, pr_stat.st_uid,
560 pr_stat.st_gid, pr_stat.st_rdev,
561 pr_stat.st_size, pr_stat.st_blksize,
563 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
564 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
565 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
569 retval = statfs(path.data, &stfs_buf);
573 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
574 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
575 "qqqqqqqqqqq", pr_stfs.f_type,
576 pr_stfs.f_bsize, pr_stfs.f_blocks,
577 pr_stfs.f_bfree, pr_stfs.f_bavail,
578 pr_stfs.f_files, pr_stfs.f_ffree,
579 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
580 pr_stfs.f_namelen, pr_stfs.f_frsize);
584 v9fs_string_free(&path);
588 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
592 V9fsString target, path;
594 v9fs_string_init(&path);
595 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
597 v9fs_string_free(&path);
600 buffer = g_malloc(size);
601 v9fs_string_init(&target);
602 retval = readlink(path.data, buffer, size - 1);
604 buffer[retval] = '\0';
605 v9fs_string_sprintf(&target, "%s", buffer);
606 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
611 v9fs_string_free(&target);
612 v9fs_string_free(&path);
617 * create other filesystem objects and send 0 on success
618 * return -errno on error
620 static int do_create_others(int type, struct iovec *iovec)
624 int offset = PROXY_HDR_SZ;
625 V9fsString oldpath, path;
626 int mode, uid, gid, cur_uid, cur_gid;
628 v9fs_string_init(&path);
629 v9fs_string_init(&oldpath);
631 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
636 retval = setugid(uid, gid, &cur_uid, &cur_gid);
638 goto unmarshal_err_out;
642 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
646 retval = mknod(path.data, mode, rdev);
649 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
653 retval = mkdir(path.data, mode);
656 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
660 retval = symlink(oldpath.data, path.data);
668 resetugid(cur_uid, cur_gid);
670 v9fs_string_free(&path);
671 v9fs_string_free(&oldpath);
676 * create a file and send fd on success
677 * return -errno on error
679 static int do_create(struct iovec *iovec)
683 int flags, mode, uid, gid, cur_uid, cur_gid;
685 v9fs_string_init(&path);
686 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
687 &path, &flags, &mode, &uid, &gid);
689 goto unmarshal_err_out;
691 ret = setugid(uid, gid, &cur_uid, &cur_gid);
693 goto unmarshal_err_out;
695 ret = open(path.data, flags, mode);
700 resetugid(cur_uid, cur_gid);
702 v9fs_string_free(&path);
707 * open a file and send fd on success
708 * return -errno on error
710 static int do_open(struct iovec *iovec)
715 v9fs_string_init(&path);
716 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
720 ret = open(path.data, flags);
725 v9fs_string_free(&path);
729 /* create unix domain socket and return the descriptor */
730 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
733 struct sockaddr_un proxy, qemu;
736 /* requested socket already exists, refuse to start */
737 if (!access(path, F_OK)) {
738 do_log(LOG_CRIT, "socket already exists\n");
742 if (strlen(path) >= sizeof(proxy.sun_path)) {
743 do_log(LOG_CRIT, "UNIX domain socket path exceeds %zu characters\n",
744 sizeof(proxy.sun_path));
748 sock = socket(AF_UNIX, SOCK_STREAM, 0);
754 /* mask other part of mode bits */
757 proxy.sun_family = AF_UNIX;
758 strcpy(proxy.sun_path, path);
759 if (bind(sock, (struct sockaddr *)&proxy,
760 sizeof(struct sockaddr_un)) < 0) {
764 if (chown(proxy.sun_path, uid, gid) < 0) {
768 if (listen(sock, 1) < 0) {
774 client = accept(sock, (struct sockaddr *)&qemu, &size);
787 static void usage(char *prog)
789 fprintf(stderr, "usage: %s\n"
790 " -p|--path <path> 9p path to export\n"
791 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
792 " {-s|--socket <socketname> socket file used for communication\n"
793 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
794 " access to this socket\n"
795 " \tNote: -s & -f can not be used together\n"
796 " [-n|--nodaemon] Run as a normal program\n",
800 static int process_reply(int sock, int type,
801 struct iovec *out_iovec, int retval)
806 if (send_fd(sock, retval) < 0) {
822 if (send_status(sock, out_iovec, retval) < 0) {
832 if (send_response(sock, out_iovec, retval) < 0) {
843 static int process_requests(int sock)
851 V9fsString name, value;
852 struct timespec spec[2];
853 V9fsString oldpath, path;
854 struct iovec in_iovec, out_iovec;
856 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
857 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
858 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
859 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
863 * initialize the header type, so that we send
864 * response to proper request type.
867 retval = read_request(sock, &in_iovec, &header);
872 switch (header.type) {
874 retval = do_open(&in_iovec);
877 retval = do_create(&in_iovec);
882 retval = do_create_others(header.type, &in_iovec);
885 v9fs_string_init(&path);
886 v9fs_string_init(&oldpath);
887 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
888 "ss", &oldpath, &path);
890 retval = link(oldpath.data, path.data);
895 v9fs_string_free(&oldpath);
896 v9fs_string_free(&path);
900 retval = do_stat(header.type, &in_iovec, &out_iovec);
903 retval = do_readlink(&in_iovec, &out_iovec);
906 v9fs_string_init(&path);
907 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
910 retval = chmod(path.data, mode);
915 v9fs_string_free(&path);
918 v9fs_string_init(&path);
919 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
922 retval = lchown(path.data, uid, gid);
927 v9fs_string_free(&path);
930 v9fs_string_init(&path);
931 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
934 retval = truncate(path.data, offset);
939 v9fs_string_free(&path);
942 v9fs_string_init(&path);
943 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
944 &spec[0].tv_sec, &spec[0].tv_nsec,
945 &spec[1].tv_sec, &spec[1].tv_nsec);
947 retval = qemu_utimens(path.data, spec);
952 v9fs_string_free(&path);
955 v9fs_string_init(&path);
956 v9fs_string_init(&oldpath);
957 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
958 "ss", &oldpath, &path);
960 retval = rename(oldpath.data, path.data);
965 v9fs_string_free(&oldpath);
966 v9fs_string_free(&path);
969 v9fs_string_init(&path);
970 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
972 retval = remove(path.data);
977 v9fs_string_free(&path);
981 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
984 v9fs_string_init(&path);
985 v9fs_string_init(&name);
986 v9fs_string_init(&value);
987 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
988 &name, &value, &size, &flags);
990 retval = lsetxattr(path.data,
991 name.data, value.data, size, flags);
996 v9fs_string_free(&path);
997 v9fs_string_free(&name);
998 v9fs_string_free(&value);
1000 case T_LREMOVEXATTR:
1001 v9fs_string_init(&path);
1002 v9fs_string_init(&name);
1003 retval = proxy_unmarshal(&in_iovec,
1004 PROXY_HDR_SZ, "ss", &path, &name);
1006 retval = lremovexattr(path.data, name.data);
1011 v9fs_string_free(&path);
1012 v9fs_string_free(&name);
1015 retval = do_getversion(&in_iovec, &out_iovec);
1022 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1027 g_free(in_iovec.iov_base);
1028 g_free(out_iovec.iov_base);
1032 int main(int argc, char **argv)
1038 char *sock_name = NULL;
1040 int c, option_index;
1041 #ifdef FS_IOC_GETVERSION
1043 struct statfs st_fs;
1051 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1058 rpath = g_strdup(optarg);
1064 sock = atoi(optarg);
1067 sock_name = g_strdup(optarg);
1070 own_u = atoi(optarg);
1073 own_g = atoi(optarg);
1083 /* Parameter validation */
1084 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1085 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1090 if (sock_name && sock != -1) {
1091 fprintf(stderr, "both named socket and socket descriptor specified\n");
1096 if (sock_name && (own_u == -1 || own_g == -1)) {
1097 fprintf(stderr, "owner uid:gid not specified, ");
1099 "owner uid:gid specifies who can access the socket file\n");
1104 if (lstat(rpath, &stbuf) < 0) {
1105 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1106 rpath, strerror(errno));
1110 if (!S_ISDIR(stbuf.st_mode)) {
1111 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1116 if (daemon(0, 0) < 0) {
1117 fprintf(stderr, "daemon call failed\n");
1120 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1123 do_log(LOG_INFO, "Started\n");
1125 sock = proxy_socket(sock_name, own_u, own_g);
1131 get_version = false;
1132 #ifdef FS_IOC_GETVERSION
1133 /* check whether underlying FS support IOC_GETVERSION */
1134 retval = statfs(rpath, &st_fs);
1136 switch (st_fs.f_type) {
1137 case EXT2_SUPER_MAGIC:
1138 case BTRFS_SUPER_MAGIC:
1139 case REISERFS_SUPER_MAGIC:
1140 case XFS_SUPER_MAGIC:
1147 if (chdir("/") < 0) {
1151 if (chroot(rpath) < 0) {
1152 do_perror("chroot");
1157 if (init_capabilities() < 0) {
1161 process_requests(sock);
1163 do_log(LOG_INFO, "Done\n");