]>
git.proxmox.com Git - mirror_qemu.git/blob - fsdev/virtfs-proxy-helper.c
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 <sys/socket.h>
19 #include <sys/resource.h>
24 #include <sys/capability.h>
25 #include <sys/fsuid.h>
30 #include <attr/xattr.h>
31 #include <sys/ioctl.h>
33 #ifdef CONFIG_LINUX_MAGIC_H
34 #include <linux/magic.h>
36 #include "qemu-common.h"
37 #include "virtio-9p-marshal.h"
38 #include "hw/9pfs/virtio-9p-proxy.h"
39 #include "fsdev/virtio-9p-marshal.h"
41 #define PROGNAME "virtfs-proxy-helper"
43 #ifndef XFS_SUPER_MAGIC
44 #define XFS_SUPER_MAGIC 0x58465342
46 #ifndef EXT2_SUPER_MAGIC
47 #define EXT2_SUPER_MAGIC 0xEF53
49 #ifndef REISERFS_SUPER_MAGIC
50 #define REISERFS_SUPER_MAGIC 0x52654973
52 #ifndef BTRFS_SUPER_MAGIC
53 #define BTRFS_SUPER_MAGIC 0x9123683E
56 static struct option helper_opts
[] = {
57 {"fd", required_argument
, NULL
, 'f'},
58 {"path", required_argument
, NULL
, 'p'},
59 {"nodaemon", no_argument
, NULL
, 'n'},
62 static bool is_daemon
;
63 static bool get_version
; /* IOC getversion IOCTL supported */
65 static void do_log(int loglevel
, const char *format
, ...)
71 vsyslog(LOG_CRIT
, format
, ap
);
73 vfprintf(stderr
, format
, ap
);
78 static void do_perror(const char *string
)
81 syslog(LOG_CRIT
, "%s:%s", string
, strerror(errno
));
83 fprintf(stderr
, "%s:%s\n", string
, strerror(errno
));
87 static int do_cap_set(cap_value_t
*cap_value
, int size
, int reset
)
92 * Start with an empty set and set permitted and effective
96 do_perror("cap_init");
99 if (cap_set_flag(caps
, CAP_PERMITTED
, size
, cap_value
, CAP_SET
) < 0) {
100 do_perror("cap_set_flag");
104 caps
= cap_get_proc();
106 do_perror("cap_get_proc");
110 if (cap_set_flag(caps
, CAP_EFFECTIVE
, size
, cap_value
, CAP_SET
) < 0) {
111 do_perror("cap_set_flag");
114 if (cap_set_proc(caps
) < 0) {
115 do_perror("cap_set_proc");
126 static int init_capabilities(void)
128 /* helper needs following capbabilities only */
129 cap_value_t cap_list
[] = {
138 return do_cap_set(cap_list
, ARRAY_SIZE(cap_list
), 1);
141 static int socket_read(int sockfd
, void *buff
, ssize_t size
)
143 ssize_t retval
, total
= 0;
146 retval
= read(sockfd
, buff
, size
);
151 if (errno
== EINTR
) {
163 static int socket_write(int sockfd
, void *buff
, ssize_t size
)
165 ssize_t retval
, total
= 0;
168 retval
= write(sockfd
, buff
, size
);
170 if (errno
== EINTR
) {
182 static int read_request(int sockfd
, struct iovec
*iovec
, ProxyHeader
*header
)
187 * read the request header.
190 retval
= socket_read(sockfd
, iovec
->iov_base
, PROXY_HDR_SZ
);
194 iovec
->iov_len
= PROXY_HDR_SZ
;
195 retval
= proxy_unmarshal(iovec
, 0, "dd", &header
->type
, &header
->size
);
200 * We can't process message.size > PROXY_MAX_IO_SZ.
201 * Treat it as fatal error
203 if (header
->size
> PROXY_MAX_IO_SZ
) {
206 retval
= socket_read(sockfd
, iovec
->iov_base
+ PROXY_HDR_SZ
, header
->size
);
210 iovec
->iov_len
+= header
->size
;
214 static int send_fd(int sockfd
, int fd
)
219 struct cmsghdr
*cmsg
;
220 union MsgControl msg_control
;
222 iov
.iov_base
= &data
;
223 iov
.iov_len
= sizeof(data
);
225 memset(&msg
, 0, sizeof(msg
));
228 /* No ancillary data on error */
230 /* fd is really negative errno if the request failed */
233 data
= V9FS_FD_VALID
;
234 msg
.msg_control
= &msg_control
;
235 msg
.msg_controllen
= sizeof(msg_control
);
237 cmsg
= &msg_control
.cmsg
;
238 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
239 cmsg
->cmsg_level
= SOL_SOCKET
;
240 cmsg
->cmsg_type
= SCM_RIGHTS
;
241 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
245 retval
= sendmsg(sockfd
, &msg
, 0);
246 } while (retval
< 0 && errno
== EINTR
);
256 static int send_status(int sockfd
, struct iovec
*iovec
, int status
)
259 int retval
, msg_size
;;
262 header
.type
= T_ERROR
;
264 header
.type
= T_SUCCESS
;
266 header
.size
= sizeof(status
);
268 * marshal the return status. We don't check error.
269 * because we are sure we have enough space for the status
271 msg_size
= proxy_marshal(iovec
, 0, "ddd", header
.type
,
272 header
.size
, status
);
273 retval
= socket_write(sockfd
, iovec
->iov_base
, msg_size
);
281 * from man 7 capabilities, section
282 * Effect of User ID Changes on Capabilities:
283 * 4. If the file system user ID is changed from 0 to nonzero (see setfsuid(2))
284 * then the following capabilities are cleared from the effective set:
285 * CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH, CAP_FOWNER, CAP_FSETID,
286 * CAP_LINUX_IMMUTABLE (since Linux 2.2.30), CAP_MAC_OVERRIDE, and CAP_MKNOD
287 * (since Linux 2.2.30). If the file system UID is changed from nonzero to 0,
288 * then any of these capabilities that are enabled in the permitted set
289 * are enabled in the effective set.
291 static int setfsugid(int uid
, int gid
)
294 * We still need DAC_OVERRIDE because we don't change
295 * supplementary group ids, and hence may be subjected DAC rules
297 cap_value_t cap_list
[] = {
304 if (uid
!= 0 || gid
!= 0) {
305 return do_cap_set(cap_list
, ARRAY_SIZE(cap_list
), 0);
311 * send response in two parts
313 * 2) Response or error status
314 * This function should be called with marshaled response
315 * send_response constructs header part and error part only.
316 * send response sends {ProxyHeader,Response} if the request was success
317 * otherwise sends {ProxyHeader,error status}
319 static int send_response(int sock
, struct iovec
*iovec
, int size
)
325 * If response size exceeds available iovec->iov_len,
328 if (size
> PROXY_MAX_IO_SZ
) {
334 * In case of error we would not have got the error encoded
335 * already so encode the error here.
337 header
.type
= T_ERROR
;
338 header
.size
= sizeof(size
);
339 proxy_marshal(iovec
, PROXY_HDR_SZ
, "d", size
);
341 header
.type
= T_SUCCESS
;
344 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
345 retval
= socket_write(sock
, iovec
->iov_base
, header
.size
+ PROXY_HDR_SZ
);
353 * gets generation number
354 * returns -errno on failure and sizeof(generation number) on success
356 static int do_getversion(struct iovec
*iovec
, struct iovec
*out_iovec
)
359 int retval
= -ENOTTY
;
360 #ifdef FS_IOC_GETVERSION
366 /* no need to issue ioctl */
369 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
372 #ifdef FS_IOC_GETVERSION
373 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
378 fd
= open(path
.data
, O_RDONLY
);
383 if (ioctl(fd
, FS_IOC_GETVERSION
, &version
) < 0) {
386 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "q", version
);
390 v9fs_string_free(&path
);
395 static int do_getxattr(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
397 int size
= 0, offset
, retval
;
398 V9fsString path
, name
, xattr
;
400 v9fs_string_init(&xattr
);
401 v9fs_string_init(&path
);
402 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "ds", &size
, &path
);
406 offset
= PROXY_HDR_SZ
+ retval
;
409 xattr
.data
= g_malloc(size
);
414 v9fs_string_init(&name
);
415 retval
= proxy_unmarshal(iovec
, offset
, "s", &name
);
417 retval
= lgetxattr(path
.data
, name
.data
, xattr
.data
, size
);
424 v9fs_string_free(&name
);
427 retval
= llistxattr(path
.data
, xattr
.data
, size
);
440 proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "d", retval
);
441 retval
= sizeof(retval
);
443 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &xattr
);
446 v9fs_string_free(&xattr
);
447 v9fs_string_free(&path
);
451 static void stat_to_prstat(ProxyStat
*pr_stat
, struct stat
*stat
)
453 memset(pr_stat
, 0, sizeof(*pr_stat
));
454 pr_stat
->st_dev
= stat
->st_dev
;
455 pr_stat
->st_ino
= stat
->st_ino
;
456 pr_stat
->st_nlink
= stat
->st_nlink
;
457 pr_stat
->st_mode
= stat
->st_mode
;
458 pr_stat
->st_uid
= stat
->st_uid
;
459 pr_stat
->st_gid
= stat
->st_gid
;
460 pr_stat
->st_rdev
= stat
->st_rdev
;
461 pr_stat
->st_size
= stat
->st_size
;
462 pr_stat
->st_blksize
= stat
->st_blksize
;
463 pr_stat
->st_blocks
= stat
->st_blocks
;
464 pr_stat
->st_atim_sec
= stat
->st_atim
.tv_sec
;
465 pr_stat
->st_atim_nsec
= stat
->st_atim
.tv_nsec
;
466 pr_stat
->st_mtim_sec
= stat
->st_mtim
.tv_sec
;
467 pr_stat
->st_mtim_nsec
= stat
->st_mtim
.tv_nsec
;
468 pr_stat
->st_ctim_sec
= stat
->st_ctim
.tv_sec
;
469 pr_stat
->st_ctim_nsec
= stat
->st_ctim
.tv_nsec
;
472 static void statfs_to_prstatfs(ProxyStatFS
*pr_stfs
, struct statfs
*stfs
)
474 memset(pr_stfs
, 0, sizeof(*pr_stfs
));
475 pr_stfs
->f_type
= stfs
->f_type
;
476 pr_stfs
->f_bsize
= stfs
->f_bsize
;
477 pr_stfs
->f_blocks
= stfs
->f_blocks
;
478 pr_stfs
->f_bfree
= stfs
->f_bfree
;
479 pr_stfs
->f_bavail
= stfs
->f_bavail
;
480 pr_stfs
->f_files
= stfs
->f_files
;
481 pr_stfs
->f_ffree
= stfs
->f_ffree
;
482 pr_stfs
->f_fsid
[0] = stfs
->f_fsid
.__val
[0];
483 pr_stfs
->f_fsid
[1] = stfs
->f_fsid
.__val
[1];
484 pr_stfs
->f_namelen
= stfs
->f_namelen
;
485 pr_stfs
->f_frsize
= stfs
->f_frsize
;
489 * Gets stat/statfs information and packs in out_iovec structure
490 * on success returns number of bytes packed in out_iovec struture
491 * otherwise returns -errno
493 static int do_stat(int type
, struct iovec
*iovec
, struct iovec
*out_iovec
)
500 struct statfs stfs_buf
;
502 v9fs_string_init(&path
);
503 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "s", &path
);
510 retval
= lstat(path
.data
, &st_buf
);
514 stat_to_prstat(&pr_stat
, &st_buf
);
515 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
516 "qqqdddqqqqqqqqqq", pr_stat
.st_dev
,
517 pr_stat
.st_ino
, pr_stat
.st_nlink
,
518 pr_stat
.st_mode
, pr_stat
.st_uid
,
519 pr_stat
.st_gid
, pr_stat
.st_rdev
,
520 pr_stat
.st_size
, pr_stat
.st_blksize
,
522 pr_stat
.st_atim_sec
, pr_stat
.st_atim_nsec
,
523 pr_stat
.st_mtim_sec
, pr_stat
.st_mtim_nsec
,
524 pr_stat
.st_ctim_sec
, pr_stat
.st_ctim_nsec
);
528 retval
= statfs(path
.data
, &stfs_buf
);
532 statfs_to_prstatfs(&pr_stfs
, &stfs_buf
);
533 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
,
534 "qqqqqqqqqqq", pr_stfs
.f_type
,
535 pr_stfs
.f_bsize
, pr_stfs
.f_blocks
,
536 pr_stfs
.f_bfree
, pr_stfs
.f_bavail
,
537 pr_stfs
.f_files
, pr_stfs
.f_ffree
,
538 pr_stfs
.f_fsid
[0], pr_stfs
.f_fsid
[1],
539 pr_stfs
.f_namelen
, pr_stfs
.f_frsize
);
543 v9fs_string_free(&path
);
547 static int do_readlink(struct iovec
*iovec
, struct iovec
*out_iovec
)
551 V9fsString target
, path
;
553 v9fs_string_init(&path
);
554 retval
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &size
);
556 v9fs_string_free(&path
);
559 buffer
= g_malloc(size
);
560 v9fs_string_init(&target
);
561 retval
= readlink(path
.data
, buffer
, size
);
563 buffer
[retval
] = '\0';
564 v9fs_string_sprintf(&target
, "%s", buffer
);
565 retval
= proxy_marshal(out_iovec
, PROXY_HDR_SZ
, "s", &target
);
570 v9fs_string_free(&target
);
571 v9fs_string_free(&path
);
576 * create other filesystem objects and send 0 on success
577 * return -errno on error
579 static int do_create_others(int type
, struct iovec
*iovec
)
583 int offset
= PROXY_HDR_SZ
;
584 V9fsString oldpath
, path
;
585 int mode
, uid
, gid
, cur_uid
, cur_gid
;
587 v9fs_string_init(&path
);
588 v9fs_string_init(&oldpath
);
592 retval
= proxy_unmarshal(iovec
, offset
, "dd", &uid
, &gid
);
597 retval
= setfsugid(uid
, gid
);
604 retval
= proxy_unmarshal(iovec
, offset
, "sdq", &path
, &mode
, &rdev
);
608 retval
= mknod(path
.data
, mode
, rdev
);
611 retval
= proxy_unmarshal(iovec
, offset
, "sd", &path
, &mode
);
615 retval
= mkdir(path
.data
, mode
);
618 retval
= proxy_unmarshal(iovec
, offset
, "ss", &oldpath
, &path
);
622 retval
= symlink(oldpath
.data
, path
.data
);
630 v9fs_string_free(&path
);
631 v9fs_string_free(&oldpath
);
632 setfsugid(cur_uid
, cur_gid
);
637 * create a file and send fd on success
638 * return -errno on error
640 static int do_create(struct iovec
*iovec
)
644 int flags
, mode
, uid
, gid
, cur_uid
, cur_gid
;
646 v9fs_string_init(&path
);
647 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sdddd",
648 &path
, &flags
, &mode
, &uid
, &gid
);
650 goto unmarshal_err_out
;
654 ret
= setfsugid(uid
, gid
);
657 * On failure reset back to the
663 ret
= open(path
.data
, flags
, mode
);
669 setfsugid(cur_uid
, cur_gid
);
671 v9fs_string_free(&path
);
676 * open a file and send fd on success
677 * return -errno on error
679 static int do_open(struct iovec
*iovec
)
684 v9fs_string_init(&path
);
685 ret
= proxy_unmarshal(iovec
, PROXY_HDR_SZ
, "sd", &path
, &flags
);
689 ret
= open(path
.data
, flags
);
694 v9fs_string_free(&path
);
698 static void usage(char *prog
)
700 fprintf(stderr
, "usage: %s\n"
701 " -p|--path <path> 9p path to export\n"
702 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
703 " [-n|--nodaemon] Run as a normal program\n",
707 static int process_reply(int sock
, int type
,
708 struct iovec
*out_iovec
, int retval
)
713 if (send_fd(sock
, retval
) < 0) {
729 if (send_status(sock
, out_iovec
, retval
) < 0) {
739 if (send_response(sock
, out_iovec
, retval
) < 0) {
750 static int process_requests(int sock
)
758 V9fsString name
, value
;
759 struct timespec spec
[2];
760 V9fsString oldpath
, path
;
761 struct iovec in_iovec
, out_iovec
;
763 in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
764 in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
765 out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
766 out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
770 * initialize the header type, so that we send
771 * response to proper request type.
774 retval
= read_request(sock
, &in_iovec
, &header
);
779 switch (header
.type
) {
781 retval
= do_open(&in_iovec
);
784 retval
= do_create(&in_iovec
);
789 retval
= do_create_others(header
.type
, &in_iovec
);
792 v9fs_string_init(&path
);
793 v9fs_string_init(&oldpath
);
794 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
795 "ss", &oldpath
, &path
);
797 retval
= link(oldpath
.data
, path
.data
);
802 v9fs_string_free(&oldpath
);
803 v9fs_string_free(&path
);
807 retval
= do_stat(header
.type
, &in_iovec
, &out_iovec
);
810 retval
= do_readlink(&in_iovec
, &out_iovec
);
813 v9fs_string_init(&path
);
814 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
817 retval
= chmod(path
.data
, mode
);
822 v9fs_string_free(&path
);
825 v9fs_string_init(&path
);
826 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sdd", &path
,
829 retval
= lchown(path
.data
, uid
, gid
);
834 v9fs_string_free(&path
);
837 v9fs_string_init(&path
);
838 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sq",
841 retval
= truncate(path
.data
, offset
);
846 v9fs_string_free(&path
);
849 v9fs_string_init(&path
);
850 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sqqqq", &path
,
851 &spec
[0].tv_sec
, &spec
[0].tv_nsec
,
852 &spec
[1].tv_sec
, &spec
[1].tv_nsec
);
854 retval
= qemu_utimens(path
.data
, spec
);
859 v9fs_string_free(&path
);
862 v9fs_string_init(&path
);
863 v9fs_string_init(&oldpath
);
864 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
,
865 "ss", &oldpath
, &path
);
867 retval
= rename(oldpath
.data
, path
.data
);
872 v9fs_string_free(&oldpath
);
873 v9fs_string_free(&path
);
876 v9fs_string_init(&path
);
877 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "s", &path
);
879 retval
= remove(path
.data
);
884 v9fs_string_free(&path
);
888 retval
= do_getxattr(header
.type
, &in_iovec
, &out_iovec
);
891 v9fs_string_init(&path
);
892 v9fs_string_init(&name
);
893 v9fs_string_init(&value
);
894 retval
= proxy_unmarshal(&in_iovec
, PROXY_HDR_SZ
, "sssdd", &path
,
895 &name
, &value
, &size
, &flags
);
897 retval
= lsetxattr(path
.data
,
898 name
.data
, value
.data
, size
, flags
);
903 v9fs_string_free(&path
);
904 v9fs_string_free(&name
);
905 v9fs_string_free(&value
);
908 v9fs_string_init(&path
);
909 v9fs_string_init(&name
);
910 retval
= proxy_unmarshal(&in_iovec
,
911 PROXY_HDR_SZ
, "ss", &path
, &name
);
913 retval
= lremovexattr(path
.data
, name
.data
);
918 v9fs_string_free(&path
);
919 v9fs_string_free(&name
);
922 retval
= do_getversion(&in_iovec
, &out_iovec
);
929 if (process_reply(sock
, header
.type
, &out_iovec
, retval
) < 0) {
934 g_free(in_iovec
.iov_base
);
935 g_free(out_iovec
.iov_base
);
939 int main(int argc
, char **argv
)
945 #ifdef FS_IOC_GETVERSION
954 c
= getopt_long(argc
, argv
, "p:nh?f:", helper_opts
,
961 rpath
= strdup(optarg
);
977 /* Parameter validation */
978 if (sock
== -1 || rpath
== NULL
) {
979 fprintf(stderr
, "socket descriptor or path not specified\n");
984 if (lstat(rpath
, &stbuf
) < 0) {
985 fprintf(stderr
, "invalid path \"%s\" specified, %s\n",
986 rpath
, strerror(errno
));
990 if (!S_ISDIR(stbuf
.st_mode
)) {
991 fprintf(stderr
, "specified path \"%s\" is not directory\n", rpath
);
996 if (daemon(0, 0) < 0) {
997 fprintf(stderr
, "daemon call failed\n");
1000 openlog(PROGNAME
, LOG_PID
, LOG_DAEMON
);
1003 do_log(LOG_INFO
, "Started\n");
1005 get_version
= false;
1006 #ifdef FS_IOC_GETVERSION
1007 /* check whether underlying FS support IOC_GETVERSION */
1008 retval
= statfs(rpath
, &st_fs
);
1010 switch (st_fs
.f_type
) {
1011 case EXT2_SUPER_MAGIC
:
1012 case BTRFS_SUPER_MAGIC
:
1013 case REISERFS_SUPER_MAGIC
:
1014 case XFS_SUPER_MAGIC
:
1021 if (chdir("/") < 0) {
1025 if (chroot(rpath
) < 0) {
1026 do_perror("chroot");
1031 if (init_capabilities() < 0) {
1035 process_requests(sock
);
1037 do_log(LOG_INFO
, "Done\n");