file descriptor passing using fd sets
v8 cover file descriptor passing using fd sets
libvirt's sVirt security driver provides SELinux MAC isolation for Qemu guest processes and their corresponding image files. In other words, sVirt uses SELinux to prevent a QEMU process from opening files that do not belong to it.
sVirt provides this support by labeling guests and resources with security labels that are stored in file system extended attributes. Some file systems, such as NFS, do not support the extended attribute security namespace, and therefore cannot support sVirt isolation.
A solution to this problem is to provide fd passing support, where libvirt opens files and passes file descriptors to QEMU. This, along with SELinux policy to prevent QEMU from opening files, can provide image file isolation for NFS files stored on the same NFS mount.
This patch series adds the add-fd, remove-fd, and query-fdsets QMP monitor commands, which allow file descriptors to be passed via SCM_RIGHTS, and assigned to specified fd sets. This allows fd sets to be created per file with fds having, for example, different access rights. When QEMU needs to reopen a file with different access rights, it can search for a matching fd in the fd set. Fd sets also allow for easy tracking of fds per file, helping to prevent fd leaks.
Support is also added to the block layer to allow QEMU to dup an fd from an fdset when the filename is of the /dev/fdset/nnn format, where nnn is the fd set ID.
No new SELinux policy is required to prevent open of NFS files (files with type nfs_t). The virt_use_nfs boolean type simply needs to be set to false, and open will be prevented (and dup will be allowed). For example:
# setsebool virt_use_nfs 0
# getsebool virt_use_nfs
virt_use_nfs --> offCorey Bryant (7):
qemu-char: Add MSG_CMSG_CLOEXEC flag to recvmsg
qapi: Introduce add-fd, remove-fd, query-fdsets
monitor: Clean up fd sets on monitor disconnect
block: Prevent detection of /dev/fdset/ as floppy
block: Convert open calls to qemu_open
block: Convert close calls to qemu_close
block: Enable qemu_open/close to work with fd sets
block/raw-posix.c | 46 +++++---- block/raw-win32.c | 6 +- block/vdi.c | 5 +- block/vmdk.c | 25 ++--- block/vpc.c | 4 +- block/vvfat.c | 16 +-- cutils.c | 5 + monitor.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.h | 5 + osdep.c | 117 +++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++ qemu-char.c | 12 ++- qemu-common.h | 2 + qemu-tool.c | 20 ++++ qmp-commands.hx | 117 +++++++++++++++++++++ savevm.c | 4 +- 16 files changed, 721 insertions(+), 55 deletions(-)
Signature · 1 line
1.7.10.4
PATCH v8 3/7 monitor: Clean up fd sets on monitor disconnect
Fd sets are shared by all monitor connections. Fd sets are considered to be in use while at least one monitor is connected. When the last monitor disconnects, all fds that are members of an fd set with refcount of zero are closed. This prevents any fd leakage associated with a client disconnect prior to using a passed fd.
---
v5:
-This patch is new in v5.
-This support addresses concerns from v4 regarding fd leakage
if the client disconnects unexpectedly. ([email protected], [email protected], [email protected])
v6:
-No changes
v7:
-Removed monitor_fdsets_set_in_use() function since we now use
mon_refcount to determine if fdsets are in use.
-Added monitor_fdsets_cleanup() function, and increment/decrement
of mon_refcount when monitor connects/disconnects.
v8:
-Use camel case for structures. ([email protected])
monitor.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
monitor.c +19 -0
@@ -2433,6 +2433,16 @@ static void monitor_fdset_cleanup(MonFdset *mon_fdset) } } +static void monitor_fdsets_cleanup (void ) +{ +MonFdset *mon_fdset; +MonFdset *mon_fdset_next; + + QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { +monitor_fdset_cleanup (mon_fdset); + } +} +AddfdInfo *qmp_add_fd (bool has_fdset_id,int64_t fdset_id,bool has_opaque,const char *opaque,Error **errp) {
@@ -4781,9 +4791,12 @@ static void monitor_control_event(void *opaque, int event) data =get_qmp_greeting ();monitor_json_emitter (mon, data);qobject_decref (data); + mon_refcount++;break ;case CHR_EVENT_CLOSED :json_message_parser_destroy (&mon->mc->parser); + mon_refcount--; +monitor_fdsets_cleanup ();break ; } }
@@ -4824,6 +4837,12 @@ static void monitor_event(void *opaque, int event)readline_show_prompt (mon->rs); } mon->reset_seen =1 ; + mon_refcount++; +break ; + +case CHR_EVENT_CLOSED : + mon_refcount--; +monitor_fdsets_cleanup ();break ; } }
Signature · 1 line
1.7.10.4
PATCH v8 7/7 block: Enable qemu_open/close to work with fd sets
In reply to Corey BryantWhen qemu_open is passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned from qemu_open.
Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open dup() references that have not yet been closed. It is incremented on qemu_open and decremented on qemu_close. It is not until the refcount is zero that file desriptors in an fd set can be closed. If an fd set has dup() references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.
---
v2:
-Get rid of file_open and move dup code to qemu_open
([email protected])
-Use strtol wrapper instead of atoi ([email protected])
v3:
-Add note about fd leakage ([email protected])
v4
-Moved patch to be later in series ([email protected]) -Update qemu_open to check access mode flags and set flags that
can be set ([email protected], [email protected])
v5:
-This patch was overhauled quite a bit in this version, with
the addition of fd set and refcount support.
-Use qemu_set_cloexec() on dup'd fd ([email protected]) -Modify flags set by fcntl on dup'd fd ([email protected]) -Reduce syscalls when setting flags for dup'd fd ([email protected]) -Fix O_RDWR, O_RDONLY, O_WRONLY checks ([email protected])
v6:
-Pass only the fd to qemu_close() and keep track of dup fds per fd
set. ([email protected], [email protected])
-Handle refcount incr/decr in new dup_fd_add/remove fd functions. -Use qemu_set_cloexec() appropriately in qemu_dup() ([email protected]) -Simplify setting of setfl_flags in qemu_dup() ([email protected]) -Add preprocessor checks for F_DUPFD_CLOEXEC ([email protected]) -Simplify flag checking in monitor_fdset_get_fd() ([email protected])
v7:
-Minor updates to reference global mon_fdsets, and to remove
default_mon usage in osdep.c. ([email protected])
v8:
-Use camel case for structures. ([email protected])
cutils.c | 5 +++ monitor.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ monitor.h | 5 +++ osdep.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-common.h | 1 + qemu-tool.c | 20 +++++++++++ 6 files changed, 230 insertions(+)
cutils.c +5 -0
@@ -382,3 +382,8 @@ int qemu_parse_fd(const char *param) }return fd; } + +int qemu_parse_fdset (const char *param) +{ +return qemu_parse_fd (param); +}
monitor.c +87 -0
99 lines not on this page · raw message
monitor.h +5 -0
@@ -86,4 +86,9 @@ int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret);int qmp_qom_get (Monitor *mon,const QDict *qdict,QObject **ret); +int monitor_fdset_get_fd (int64_t fdset_id,int flags); +int monitor_fdset_dup_fd_add (int64_t fdset_id,int dup_fd); +int monitor_fdset_dup_fd_remove (int dup_fd); +int monitor_fdset_dup_fd_find (int dup_fd); +#endif /* !MONITOR_H */
osdep.c +112 -0
136 lines not on this page · raw message
qemu-common.h +1 -0
@@ -166,6 +166,7 @@ int qemu_fls(int i);int qemu_fdatasync (int fd);int fcntl_setfl (int fd,int flag);int qemu_parse_fd (const char *param); +int qemu_parse_fdset (const char *param);/* * strtosz() suffixes used to specify the default treatment of an
qemu-tool.c +20 -0
@@ -57,6 +57,26 @@ void monitor_protocol_event(MonitorEvent event, QObject *data) { } +int monitor_fdset_get_fd (int64_t fdset_id,int flags) +{ +return -1 ; +} + +int monitor_fdset_dup_fd_add (int64_t fdset_id,int dup_fd) +{ +return -1 ; +} + +int monitor_fdset_dup_fd_remove (int dup_fd) +{ +return -1 ; +} + +int monitor_fdset_dup_fd_find (int dup_fd) +{ +return -1 ; +} +int64_t cpu_get_clock (void ) {return qemu_get_clock_ns (rt_clock);
Signature · 1 line
1.7.10.4
PATCH v8 1/7 qemu-char: Add MSG_CMSG_CLOEXEC flag to recvmsg
In reply to Corey BryantSet the close-on-exec flag for the file descriptor received via SCM_RIGHTS.
---
v4
-This patch is new in v4 ([email protected])
v5
-Fallback to FD_CLOEXEC if MSG_CMSG_CLOEXEC is not available
([email protected], [email protected])
v6
-Set cloexec on correct fd ([email protected])
v7-v8
-No changes
qemu-char.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
qemu-char.c +10 -2
@@ -2238,6 +2238,9 @@ static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)if (fd <0 )continue ; +#ifndef MSG_CMSG_CLOEXEC +qemu_set_cloexec (fd); +#endif if (s->msgfd !=-1 )close (s->msgfd); s->msgfd = fd;
@@ -2253,6 +2256,7 @@ static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)struct cmsghdr cmsg;char control[CMSG_SPACE (sizeof (int ))]; } msg_control; +int flags =0 ;ssize_t ret; iov[0 ].iov_base = buf;
@@ -2263,9 +2267,13 @@ static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) msg.msg_control = &msg_control; msg.msg_controllen =sizeof (msg_control); - ret =recvmsg (s->fd, &msg,0 ); -if (ret >0 && s->is_unix) +#ifdef MSG_CMSG_CLOEXEC + flags |=MSG_CMSG_CLOEXEC ; +#endif + ret =recvmsg (s->fd, &msg, flags); +if (ret >0 && s->is_unix) {unix_process_msgfd (chr, &msg); + }return ret; }
Signature · 1 line
1.7.10.4
PATCH v8 2/7 qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Corey BryantThis patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets.
A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (O_RDWR, O_WRONLY, O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) "open" and "reopen" the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests.
The new QMP commands are:
add-fd: Add a file descriptor to an fd set
remove-fd: Remove a file descriptor from an fd set
query-fdsets: Return information describing all fd sets
Note: These commands are not compatible with the existing getfd and closefd QMP commands.
---
v5:
-This patch is new in v5 and replaces the pass-fd QMP command
from v4.
-By grouping fds in fd sets, we ease managability with an fd
set per file, addressing concerns raised in v4 about handling "reopens" and preventing fd leakage. ([email protected], [email protected], [email protected])
v6
-Make @fd optional for remove-fd ([email protected]) -Make @fdset-id optional for add-fd ([email protected])
v7:
-Share fd sets among all monitor connections ([email protected]) -Added mon_refcount to keep track of monitor connection count.
v8:
-Add opaque string to add-fd/query-fdsets.
([email protected])
-Use camel case for structures. ([email protected]) -Don't return in-use and refcount from query-fdsets.
([email protected])
-Don't return removed fd's from query-fdsets.
([email protected])
-Use fdset-id rather than fdset_id. ([email protected]) -Fix fd leak in qmp_add_fd(). ([email protected]) -Update QMP errors. ([email protected], [email protected])
monitor.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++++++++++++ qmp-commands.hx | 117 +++++++++++++++++++++++++++++++++ 3 files changed, 403 insertions(+)
monitor.c +188 -0
@@ -140,6 +140,24 @@ struct mon_fd_t {QLIST_ENTRY (mon_fd_t) next; }; +/* file descriptor associated with a file descriptor set */ +typedef struct MonFdsetFd MonFdsetFd ; +struct MonFdsetFd { +int fd; +bool removed; +char *opaque; +QLIST_ENTRY (MonFdsetFd) next; +}; + +/* file descriptor set containing fds passed via SCM_RIGHTS */ +typedef struct MonFdset MonFdset ; +struct MonFdset { +int64_t id; +int refcount; + QLIST_HEAD(, MonFdsetFd) fds; +QLIST_ENTRY (MonFdset) next; +}; + typedef struct MonitorControl { QObject *id; JSONMessageParser parser;
@@ -211,6 +229,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; }#define QMP_ACCEPT_UNKNOWNS 1 static QLIST_HEAD(mon_list, Monitor) mon_list; +static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; +static int mon_refcount;static mon_cmd_t mon_cmds[];static mon_cmd_t info_cmds[];
@@ -2389,6 +2409,174 @@ int monitor_get_fd(Monitor *mon, const char *fdname)return -1 ; } +static void monitor_fdset_cleanup (MonFdset *mon_fdset) +{ +MonFdsetFd *mon_fdset_fd; +MonFdsetFd *mon_fdset_fd_next; + +if (mon_fdset->refcount !=0 ) { +return ; + } + + QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) { +if (mon_refcount ==0 || mon_fdset_fd->removed) { +close (mon_fdset_fd->fd); +g_free (mon_fdset_fd->opaque); +QLIST_REMOVE (mon_fdset_fd, next); +g_free (mon_fdset_fd); + } + } + +if (QLIST_EMPTY (&mon_fdset->fds)) { +QLIST_REMOVE (mon_fdset, next); +g_free (mon_fdset); + } +} + +AddfdInfo *qmp_add_fd (bool has_fdset_id,int64_t fdset_id,bool has_opaque, +const char *opaque,Error **errp) +{ +int fd; +Monitor *mon = cur_mon; +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +AddfdInfo *fdinfo; + + fd =qemu_chr_fe_get_msgfd (mon->chr); +if (fd ==-1 ) { +error_set (errp,QERR_FD_NOT_SUPPLIED ); + goto error; + } + +if (has_fdset_id) { + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id == fdset_id) { +break ; + } + } +if (mon_fdset ==NULL ) { +error_set (errp,QERR_INVALID_PARAMETER_VALUE ,"fdset-id" , +"an existing fdset-id or no fdset-id" ); + goto error; + } + }else { +int64_t fdset_id_prev =-1 ; +MonFdset *mon_fdset_cur =QLIST_FIRST (&mon_fdsets); + +/* Use first available fdset ID */ + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { + mon_fdset_cur = mon_fdset; +if (fdset_id_prev == mon_fdset_cur->id -1 ) { + fdset_id_prev = mon_fdset_cur->id; +continue ; + } +break ; + } + + mon_fdset =g_malloc0 (sizeof (*mon_fdset)); + mon_fdset->id = fdset_id_prev +1 ; + mon_fdset->refcount =0 ; + +/* The fdset list is ordered by fdset ID */ +if (mon_fdset->id ==0 ) { +QLIST_INSERT_HEAD (&mon_fdsets, mon_fdset, next); + }else if (mon_fdset->id < mon_fdset_cur->id) { +QLIST_INSERT_BEFORE (mon_fdset_cur, mon_fdset, next); + }else { +QLIST_INSERT_AFTER (mon_fdset_cur, mon_fdset, next); + } + } + + mon_fdset_fd =g_malloc0 (sizeof (*mon_fdset_fd)); + mon_fdset_fd->fd = fd; + mon_fdset_fd->removed = false; +if (has_opaque) { + mon_fdset_fd->opaque =g_strdup (opaque); + } +QLIST_INSERT_HEAD (&mon_fdset->fds, mon_fdset_fd, next); + + fdinfo =g_malloc0 (sizeof (*fdinfo)); + fdinfo->fdset_id = mon_fdset->id; + fdinfo->fd = mon_fdset_fd->fd; + +return fdinfo; + +error: +if (fd !=-1 ) { +close (fd); + } +return NULL ; +} + +void qmp_remove_fd (int64_t fdset_id,bool has_fd,int64_t fd,Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20 ]; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (has_fd && mon_fdset_fd->fd != fd) { +continue ; + } + mon_fdset_fd->removed = true; +if (has_fd) { +break ; + } + } +monitor_fdset_cleanup (mon_fdset); +return ; + } +snprintf (fd_str,sizeof (fd_str),"%" PRId64, fd); +error_set (errp,QERR_FD_NOT_FOUND , fd_str); +} + +FdsetInfoList *qmp_query_fdsets (Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +FdsetInfoList *fdset_list =NULL ; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +FdsetInfoList *fdset_info =g_malloc0 (sizeof (*fdset_info)); +FdsetFdInfoList *fdsetfd_list =NULL ; + + fdset_info->value =g_malloc0 (sizeof (*fdset_info->value)); + fdset_info->value->fdset_id = mon_fdset->id; + + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +FdsetFdInfoList *fdsetfd_info; + +if (mon_fdset_fd->removed) { +continue ; + } + + fdsetfd_info =g_malloc0 (sizeof (*fdsetfd_info)); + fdsetfd_info->value =g_malloc0 (sizeof (*fdsetfd_info->value)); + fdsetfd_info->value->fd = mon_fdset_fd->fd; +if (mon_fdset_fd->opaque) { + fdsetfd_info->value->has_opaque = true; + fdsetfd_info->value->opaque =g_strdup (mon_fdset_fd->opaque); + }else { + fdsetfd_info->value->has_opaque = false; + } + + fdsetfd_info->next = fdsetfd_list; + fdsetfd_list = fdsetfd_info; + } + + fdset_info->value->fds = fdsetfd_list; + + fdset_info->next = fdset_list; + fdset_list = fdset_info; + } + +return fdset_list; +} +/* mon_cmds and info_cmds would be sorted at runtime */ static mon_cmd_t mon_cmds[] = { #include "hmp-commands.h"
qapi-schema.json +98 -0
101 lines not on this page · raw message
qmp-commands.hx +117 -0
123 lines not on this page · raw message
Signature · 1 line
1.7.10.4
PATCH v8 4/7 block: Prevent detection of /dev/fdset/ as floppy
In reply to Corey Bryant---
v8
-This patch is new in v8. It was reported on a prior fd passing
approach and I realized it's needed in this series. ([email protected])
block/raw-posix.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
block/raw-posix.c +3 -1
@@ -1052,8 +1052,10 @@ static int floppy_probe_device(const char *filename)struct floppy_struct fdparam;struct stat st; -if (strstart (filename,"/dev/fd" ,NULL )) +if (strstart (filename,"/dev/fd" ,NULL ) && + !strstart (filename,"/dev/fdset/" ,NULL )) { prio =50 ; + } fd =open (filename,O_RDONLY |O_NONBLOCK );if (fd <0 ) {
Signature · 1 line
1.7.10.4
PATCH v8 6/7 block: Convert close calls to qemu_close
In reply to Corey BryantThis patch converts all block layer close calls, that correspond to qemu_open calls, to qemu_close.
---
v5:
-This patch is new in v5. ([email protected], [email protected])
v6-v8:
-No changes
block/raw-posix.c | 24 ++++++++++++------------ block/raw-win32.c | 2 +- block/vmdk.c | 4 ++-- block/vpc.c | 2 +- block/vvfat.c | 12 ++++++------ osdep.c | 5 +++++ qemu-common.h | 1 + savevm.c | 4 ++-- 8 files changed, 30 insertions(+), 24 deletions(-)
block/raw-posix.c +12 -12
@@ -271,7 +271,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, out_free_buf:qemu_vfree (s->aligned_buf); out_close: -close (fd); +qemu_close (fd);return -errno; }
@@ -376,7 +376,7 @@ static void raw_close(BlockDriverState *bs) {BDRVRawState *s = bs->opaque;if (s->fd >=0 ) { -close (s->fd); +qemu_close (s->fd); s->fd =-1 ;if (s->aligned_buf !=NULL )qemu_vfree (s->aligned_buf);
@@ -580,7 +580,7 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)if (ftruncate (fd, total_size *BDRV_SECTOR_SIZE ) !=0 ) { result = -errno; } -if (close (fd) !=0 ) { +if (qemu_close (fd) !=0 ) { result = -errno; } }
@@ -850,7 +850,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)if (fd <0 ) { bsdPath[strlen (bsdPath)-1 ] ='1' ; }else { -close (fd); +qemu_close (fd); } filename = bsdPath; }
@@ -889,7 +889,7 @@ static int fd_open(BlockDriverState *bs) last_media_present = (s->fd >=0 );if (s->fd >=0 && (get_clock () - s->fd_open_time) >=FD_OPEN_TIMEOUT ) { -close (s->fd); +qemu_close (s->fd); s->fd =-1 ;#ifdef DEBUG_FLOPPY printf ("Floppy closed\n" );
@@ -988,7 +988,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options)else if (lseek (fd,0 ,SEEK_END ) < total_size *BDRV_SECTOR_SIZE ) ret = -ENOSPC ; -close (fd); +qemu_close (fd);return ret; }
@@ -1038,7 +1038,7 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags)return ret;/* close fd so that we can reopen it as needed */ -close (s->fd); +qemu_close (s->fd); s->fd =-1 ; s->fd_media_changed =1 ;
@@ -1072,7 +1072,7 @@ static int floppy_probe_device(const char *filename) prio =100 ; outc: -close (fd); +qemu_close (fd); out:return prio; }
@@ -1107,14 +1107,14 @@ static void floppy_eject(BlockDriverState *bs, bool eject_flag)int fd;if (s->fd >=0 ) { -close (s->fd); +qemu_close (s->fd); s->fd =-1 ; } fd =qemu_open (bs->filename, s->open_flags |O_NONBLOCK );if (fd >=0 ) {if (ioctl (fd,FDEJECT ,0 ) <0 )perror ("FDEJECT" ); -close (fd); +qemu_close (fd); } }
@@ -1175,7 +1175,7 @@ static int cdrom_probe_device(const char *filename) prio =100 ; outc: -close (fd); +qemu_close (fd); out:return prio; }
@@ -1283,7 +1283,7 @@ static int cdrom_reopen(BlockDriverState *bs)* FreeBSD seems to not notice sometimes... */ if (s->fd >=0 ) -close (s->fd); +qemu_close (s->fd); fd =qemu_open (bs->filename, s->open_flags,0644 );if (fd <0 ) { s->fd =-1 ;
block/raw-win32.c +1 -1
@@ -261,7 +261,7 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)return -EIO ;set_sparse (fd);ftruncate (fd, total_size *512 ); -close (fd); +qemu_close (fd);return 0 ; }
block/vmdk.c +2 -2
@@ -1258,7 +1258,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize, ret =0 ; exit: -close (fd); +qemu_close (fd);return ret; }
@@ -1506,7 +1506,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) } ret =0 ; exit: -close (fd); +qemu_close (fd);return ret; }
block/vpc.c +1 -1
@@ -744,7 +744,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options) } fail: -close (fd); +qemu_close (fd);return ret; }
block/vvfat.c +6 -6
@@ -1105,7 +1105,7 @@ static inline void vvfat_close_current_file(BDRVVVFATState *s)if (s->current_mapping) { s->current_mapping =NULL ;if (s->current_fd) { -close (s->current_fd); +qemu_close (s->current_fd); s->current_fd =0 ; } }
@@ -2230,7 +2230,7 @@ static int commit_one_file(BDRVVVFATState* s, }if (offset >0 ) {if (lseek (fd, offset,SEEK_SET ) != offset) { -close (fd); +qemu_close (fd);g_free (cluster);return -3 ; }
@@ -2251,13 +2251,13 @@ static int commit_one_file(BDRVVVFATState* s, (uint8_t*)cluster, (rest_size + 0x1ff) / 0x200);if (ret <0 ) { -close (fd); +qemu_close (fd);g_free (cluster);return ret; }if (write (fd, cluster, rest_size) <0 ) { -close (fd); +qemu_close (fd);g_free (cluster);return -2 ; }
@@ -2268,11 +2268,11 @@ static int commit_one_file(BDRVVVFATState* s,if (ftruncate (fd, size)) {perror ("ftruncate()" ); -close (fd); +qemu_close (fd);g_free (cluster);return -4 ; } -close (fd); +qemu_close (fd);g_free (cluster);return commit_mappings (s, first_cluster, dir_index);
osdep.c +5 -0
@@ -107,6 +107,11 @@ int qemu_open(const char *name, int flags, ...)return ret; } +int qemu_close (int fd) +{ +return close (fd); +} +/* * A variant of write(2) which handles partial write. *
qemu-common.h +1 -0
@@ -207,6 +207,7 @@ const char *path(const char *pathname);void *qemu_oom_check (void *ptr);int qemu_open (const char *name,int flags, ...); +int qemu_close (int fd); ssize_t qemu_write_full(int fd, const void *buf, size_t count) QEMU_WARN_UNUSED_RESULT;ssize_t qemu_send_full (int fd,const void *buf,size_t count,int flags)
savevm.c +2 -2
@@ -513,7 +513,7 @@ static void qemu_fill_buffer(QEMUFile *f)* * Returns f->close() return value, or 0 if close function is not set. */ -static int qemu_close (QEMUFile *f) +static int _qemu_fclose (QEMUFile *f) {int ret =0 ;if (f->close) {
@@ -535,7 +535,7 @@ int qemu_fclose(QEMUFile *f) {int ret;qemu_fflush (f); - ret =qemu_close (f); + ret =_qemu_fclose (f);/* If any error was spotted before closing, we should report it * instead of the close() return value. */
Signature · 1 line
1.7.10.4
PATCH v8 5/7 block: Convert open calls to qemu_open
In reply to Corey BryantThis patch converts all block layer open calls to qemu_open.
Note that this adds the O_CLOEXEC flag to the changed open paths when the O_CLOEXEC macro is defined.
---
v2:
-Convert calls to qemu_open instead of file_open ([email protected]) -Mention introduction of O_CLOEXEC ([email protected])
v3-v8:
-No changes
block/raw-posix.c | 18 +++++++++--------- block/raw-win32.c | 4 ++-- block/vdi.c | 5 +++-- block/vmdk.c | 21 +++++++++------------ block/vpc.c | 2 +- block/vvfat.c | 4 ++-- 6 files changed, 26 insertions(+), 28 deletions(-)
block/raw-posix.c +9 -9
@@ -572,8 +572,8 @@ static int raw_create(const char *filename, QEMUOptionParameter *options) options++; } - fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY , -0644 ); + fd =qemu_open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY , +0644 );if (fd <0 ) { result = -errno; }else {
@@ -846,7 +846,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)if ( bsdPath[0 ] !='\0' ) {strcat (bsdPath,"s0" );/* some CDs don't have a partition 0 */ - fd =open (bsdPath,O_RDONLY |O_BINARY |O_LARGEFILE ); + fd =qemu_open (bsdPath,O_RDONLY |O_BINARY |O_LARGEFILE );if (fd <0 ) { bsdPath[strlen (bsdPath)-1 ] ='1' ; }else {
@@ -903,7 +903,7 @@ static int fd_open(BlockDriverState *bs)#endif return -EIO ; } - s->fd =open (bs->filename, s->open_flags & ~O_NONBLOCK ); + s->fd =qemu_open (bs->filename, s->open_flags & ~O_NONBLOCK );if (s->fd <0 ) { s->fd_error_time =get_clock (); s->fd_got_error =1 ;
@@ -977,7 +977,7 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options) options++; } - fd =open (filename,O_WRONLY |O_BINARY ); + fd =qemu_open (filename,O_WRONLY |O_BINARY );if (fd <0 )return -errno;
@@ -1057,7 +1057,7 @@ static int floppy_probe_device(const char *filename) prio =50 ; } - fd =open (filename,O_RDONLY |O_NONBLOCK ); + fd =qemu_open (filename,O_RDONLY |O_NONBLOCK );if (fd <0 ) { goto out; }
@@ -1110,7 +1110,7 @@ static void floppy_eject(BlockDriverState *bs, bool eject_flag)close (s->fd); s->fd =-1 ; } - fd =open (bs->filename, s->open_flags |O_NONBLOCK ); + fd =qemu_open (bs->filename, s->open_flags |O_NONBLOCK );if (fd >=0 ) {if (ioctl (fd,FDEJECT ,0 ) <0 )perror ("FDEJECT" );
@@ -1160,7 +1160,7 @@ static int cdrom_probe_device(const char *filename)int prio =0 ;struct stat st; - fd =open (filename,O_RDONLY |O_NONBLOCK ); + fd =qemu_open (filename,O_RDONLY |O_NONBLOCK );if (fd <0 ) { goto out; }
@@ -1284,7 +1284,7 @@ static int cdrom_reopen(BlockDriverState *bs)*/ if (s->fd >=0 )close (s->fd); - fd =open (bs->filename, s->open_flags,0644 ); + fd =qemu_open (bs->filename, s->open_flags,0644 );if (fd <0 ) { s->fd =-1 ;return -EIO ;
block/raw-win32.c +2 -2
@@ -255,8 +255,8 @@ static int raw_create(const char *filename, QEMUOptionParameter *options) options++; } - fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY , -0644 ); + fd =qemu_open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY , +0644 );if (fd <0 )return -EIO ;set_sparse (fd);
block/vdi.c +3 -2
@@ -653,8 +653,9 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options) options++; } - fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , -0644 ); + fd =qemu_open (filename, +O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , +0644 );if (fd <0 ) {return -errno; }
block/vmdk.c +9 -12
@@ -1161,10 +1161,9 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,VMDK4Header header;uint32_t tmp, magic, grains, gd_size, gt_size, gt_count; - fd =open ( - filename, -O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , -0644 ); + fd =qemu_open (filename, +O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , +0644 );if (fd <0 ) {return -errno; }
@@ -1484,15 +1483,13 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options) (flags &BLOCK_FLAG_COMPAT6 ?6 :4 ), total_size / (int64_t)(63 * 16 * 512));if (split || flat) { - fd =open ( - filename, -O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , -0644 ); + fd =qemu_open (filename, +O_WRONLY |O_CREAT |O_TRUNC |O_BINARY |O_LARGEFILE , +0644 ); }else { - fd =open ( - filename, -O_WRONLY |O_BINARY |O_LARGEFILE , -0644 ); + fd =qemu_open (filename, +O_WRONLY |O_BINARY |O_LARGEFILE , +0644 ); }if (fd <0 ) {return -errno;
block/vpc.c +1 -1
@@ -678,7 +678,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options) }/* Create the file */ - fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,0644 ); + fd =qemu_open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,0644 );if (fd <0 ) {return -EIO ; }
block/vvfat.c +2 -2
@@ -1162,7 +1162,7 @@ static int open_file(BDRVVVFATState* s,mapping_t* mapping)if (!s->current_mapping ||strcmp (s->current_mapping->path,mapping->path)) {/* open file */ -int fd =open (mapping->path,O_RDONLY |O_BINARY |O_LARGEFILE ); +int fd =qemu_open (mapping->path,O_RDONLY |O_BINARY |O_LARGEFILE );if (fd<0 )return -1 ;vvfat_close_current_file (s);
@@ -2221,7 +2221,7 @@ static int commit_one_file(BDRVVVFATState* s,for (i = s->cluster_size; i < offset; i += s->cluster_size) c =modified_fat_get (s, c); - fd =open (mapping->path,O_RDWR |O_CREAT |O_BINARY ,0666 ); + fd =qemu_open (mapping->path,O_RDWR |O_CREAT |O_BINARY ,0666 );if (fd <0 ) {fprintf (stderr,"Could not open %s... (%s, %d)\n" , mapping->path,strerror (errno), errno);
Signature · 1 line
1.7.10.4
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Corey BryantQuoted 37 lines · Corey Bryant
This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets.
A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (
O_RDWR,O_WRONLY,O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) "open" and "reopen" the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests.The new QMP commands are:
add-fd: Add a file descriptor to an fd set
remove-fd: Remove a file descriptor from an fd set
query-fdsets: Return information describing all fd setsNote: These commands are not compatible with the existing getfd and closefd QMP commands.
Signed-off-by Corey Bryant <[email protected]>monitor.c +14
-0+void qmp_remove_fd (int64_t fdset_id,bool has_fd,int64_t fd,Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20 ]; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (has_fd && mon_fdset_fd->fd != fd) { +continue ; + }
monitor.c int monitor_get_fd(Monitor *mon, const char *fdname) · line 2534
Quoted 10 lines
monitor.c +10
-0+ mon_fdset_fd->removed = true; +if (has_fd) { +break ; + } + } +monitor_fdset_cleanup (mon_fdset); +return ; + } +snprintf (fd_str,sizeof (fd_str),"%" PRId64, fd); +error_set (errp,QERR_FD_NOT_FOUND , fd_str);
This error catches the case of fdset_id not found, but you never raise an error when has_fd is true but fd is not found within fdset_id. You also don't raise an error for back-to-back remove-fd(fdset-id=1,fd=3), because you aren't checking whether mon_fdset_fd->removed was already true.
I'm not sure which semantic is better, but I _am_ worried that we have both semantics in teh same function (are we arguing that this command succeeds when the fd is gone, even if it already was gone? Or are we arguing that this command diagnoses failure on an attempt to remove something that doesn't exist). I guess I'm 60-40 towards always issuing an error on an attempt to remove something that can't be found or is already removed.
Quoted 11 lines
monitor.c +11
-0+} + +FdsetInfoList *qmp_query_fdsets (Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +FdsetInfoList *fdset_list =NULL ; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +FdsetInfoList *fdset_info =g_malloc0 (sizeof (*fdset_info)); +FdsetFdInfoList *fdsetfd_list =NULL ;
monitor.c int monitor_get_fd(Monitor *mon, const char *fdname) · line 2555
Quoted 10 lines
monitor.c +10
-0+ + fdset_info->value =g_malloc0 (sizeof (*fdset_info->value)); + fdset_info->value->fdset_id = mon_fdset->id; + + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +FdsetFdInfoList *fdsetfd_info; + +if (mon_fdset_fd->removed) { +continue ; + }
This means that an fdset with all fds removed will show up as empty in the output; I guess that's okay, as libvirt could use that to infer that the dup()d fds are still in use. The alternative is to keep track of whether we have output any information about an fd within a set before including the set itself in the overall output.
Quoted 4 lines
+++
b/qapi-schema.jsonqapi-schema.json +2
-0+## +# @add-fd:
qapi-schema.json line 2227
Quoted 10 lines
qapi-schema.json +10
-0+# +# Add a file descriptor, that was passed via SCM rights, to an fd set. +# +# @fdset-id: #optional The ID of the fd set to add the file descriptor to. +# +# @opaque: #optional A free-form string that can be used to describe the fd. +# +# Returns: @AddfdInfo on success +# If file descriptor was not received, FdNotSupplied +# If @fdset_id does not exist, InvalidParameterValue
Missed one: s/_/-/
qapi-schema.json line 2248
Quoted 11 lines
qapi-schema.json +11
-0+## +# @remove-fd: +# +# Remove a file descriptor from an fd set. +# +# @fdset-id: The ID of the fd set that the file descriptor belongs to. +# +# @fd: #optional The file descriptor that is to be removed. +# +# Returns: Nothing on success +# If @fdset_id or @fd is not found, FdNotFound
and another s/_/-/
Quoted 4 lines
qmp-commands.hx +4
-0+SQMP +query-fdsets +------------- +
qmp-commands.hx Example: · line 1021
Quoted 10 lines
qmp-commands.hx +10
-0+Return information describing all fd sets. + +Arguments: None + +Example: + +-> { "execute": "query-fdsets" } +<- { "return": [ + { + "fdset-id": 1
missing a comma
qmp-commands.hx Example: · line 1024
Quoted 3 lines
qmp-commands.hx +3
-0+ "fds": [ + { + "fd": 21,
JSON does not permit trailing commas.
qmp-commands.hx Example: · line 1027
Quoted 3 lines
qmp-commands.hx +3
-0+ }, + { + "fd": 23,
and again
qmp-commands.hx Example: · line 1032
Quoted 5 lines
qmp-commands.hx +5
-0+ } + ], + }, + { + "fdset-id": 2
missing a comma
qmp-commands.hx Example: · line 1035
Quoted 3 lines
qmp-commands.hx +3
-0+ "fds": [ + { + "fd": 22,
trailing comma
Also, it might be nice to include something like:
"opaque":"rdonly:/path/to/file"
in one of the examples to give a hint to the reader how to use opaque.
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Corey BryantQuoted 11 lines · Corey Bryant
When
qemu_openis passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned fromqemu_open.Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open
dup()references that have not yet been closed. It is incremented onqemu_openand decremented onqemu_close. It is not until the refcount is zero that file desriptors in an fd set
s/desriptors/descriptors/
Quoted 10 lines
can be closed. If an fd set has
dup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.monitor.c +4
-0+int monitor_fdset_get_fd (int64_t fdset_id,int flags) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd;
monitor.c FdsetInfoList *qmp_query_fdsets(Error **errp) · line 2604
Quoted 10 lines
monitor.c +10
-0+int mon_fd_flags; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (mon_fdset_fd->removed) { +continue ; + }
Is this right? According to the commit message, the whole point of leaving an fd in the set is to allow the fd to be reused as a dup() target for as long as the fdset is alive, even if the monitor no longer cares about the existence of the fd. But this will always skip over an fd marked for removal. Maybe this function needs a flag to say whether this is an initial open driven by an explicit user string (in which case, honor the removed flag - if the user removed the O_RDWR fd and then tries a drive_add with the same fdset, the drive_add should fail because from the user's perspective, there is no O_RDWR fd in the set); vs. an internal usage due to a reopen (use an fd even if removed is true, because we may be toggling between O_RDWR and O_RDONLY multiple times long after the monitor has already removed the fdset, based on actions that were not drive by an explicit /dev/fdset name.)
monitor.c FdsetInfoList *qmp_query_fdsets(Error **errp) · line 2613
Quoted 9 lines
monitor.c +9
-0+ + mon_fd_flags =fcntl (mon_fdset_fd->fd,F_GETFL ); +if (mon_fd_flags ==-1 ) { +return -1 ; + } + +if ((flags &O_ACCMODE ) == (mon_fd_flags &O_ACCMODE )) { +return mon_fdset_fd->fd; + }
I still wonder if a request for O_RDONLY should be satisfied by an existing O_RDWR fd, especially in light of the fact that libvirt would rather pass in only one RDWR fd but qemu block opening currently opens twice during probing. But if it turns out to be a problem in practice, and if libvirt can't really manage to pass two fds into the set, we can hack that in later. Meanwhile, I'm okay with this first round patch requiring an exact match.
Quoted 21 lines
osdep.c +17
-0@@ -87,6 +151,39 @@ int qemu_open(const char *name, int flags, ...)int ret;int mode =0 ; +#ifndef _WIN32 +const char *fdset_id_str; + +/* Attempt dup of fd from fd set */ +if (strstart (name,"/dev/fdset/" , &fdset_id_str)) { +int64_t fdset_id; +int fd, dupfd; + + fdset_id =qemu_parse_fdset (fdset_id_str); +if (fdset_id ==-1 ) { + errno =EINVAL ; +return -1 ; + } + + fd =monitor_fdset_get_fd (fdset_id, flags); +if (fd ==-1 ) { +return -1 ;
osdep.c int qemu_open(const char *name, int flags, ...) · line 180
Quoted 10 lines
osdep.c +10
-0+ } + + dupfd =qemu_dup (fd, flags); +if (fd ==-1 ) { +return -1 ; + } + + ret =monitor_fdset_dup_fd_add (fdset_id, dupfd); +if (ret ==-1 ) { +return -1 ;
Leaks dupfd (admittedly only on a corner-case failure, but still worth addressing).
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Corey BryantQuoted 14 lines · Corey Bryant
monitor.c +14
-0+void qmp_remove_fd (int64_t fdset_id,bool has_fd,int64_t fd,Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20 ]; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (has_fd && mon_fdset_fd->fd != fd) { +continue ; + }
monitor.c int monitor_get_fd(Monitor *mon, const char *fdname) · line 2534
Quoted 10 lines
monitor.c +10
-0+ mon_fdset_fd->removed = true; +if (has_fd) { +break ; + } + } +monitor_fdset_cleanup (mon_fdset); +return ; + } +snprintf (fd_str,sizeof (fd_str),"%" PRId64, fd); +error_set (errp,QERR_FD_NOT_FOUND , fd_str);
fd is optional and may be uninitialized. I think the human-readable string should be:
if has_fd:
fd_str = '%s:%s' % (fdset_id, fd)
else:
fd_str = '%s' % fdset_idOtherwise, looks good.
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Eric BlakeQuoted 54 lines · Eric Blake
Quoted 37 lines · Corey Bryant
This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets.
A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (
O_RDWR,O_WRONLY,O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) "open" and "reopen" the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests.The new QMP commands are:
add-fd: Add a file descriptor to an fd set
remove-fd: Remove a file descriptor from an fd set
query-fdsets: Return information describing all fd setsNote: These commands are not compatible with the existing getfd and closefd QMP commands.
Signed-off-by Corey Bryant <[email protected]>monitor.c +14
-0+void qmp_remove_fd (int64_t fdset_id,bool has_fd,int64_t fd,Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20 ]; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (has_fd && mon_fdset_fd->fd != fd) { +continue ; + }monitor.c int monitor_get_fd(Monitor *mon, const char *fdname) · line 2534
Quoted 10 lines
monitor.c +10
-0+ mon_fdset_fd->removed = true; +if (has_fd) { +break ; + } + } +monitor_fdset_cleanup (mon_fdset); +return ; + } +snprintf (fd_str,sizeof (fd_str),"%" PRId64, fd); +error_set (errp,QERR_FD_NOT_FOUND , fd_str);This error catches the case of
fdset_idnot found, but you never raise an error whenhas_fdis true but fd is not found withinfdset_id. You also don't raise an error for back-to-back remove-fd(fdset-id=1,fd=3), because you aren't checking whethermon_fdset_fd->removedwas already true.
Quoted 8 lines
I'm not sure which semantic is better, but I _am_ worried that we have both semantics in teh same function (are we arguing that this command succeeds when the fd is gone, even if it already was gone? Or are we arguing that this command diagnoses failure on an attempt to remove something that doesn't exist). I guess I'm 60-40 towards always issuing an error on an attempt to remove something that can't be found or is already removed.
Thanks for catching this. The code used to fall through to the QERR_FD_NOT_FOUND error before I moved the return outside of the mon_fdset_fd loop. Anyway I intended to set QERR_FD_NOT_FOUND when either the fdset or fd is not found. I'll fix that.
I hadn't thought of back-to-back remove-fd's requiring an error. I could go either way on that. But since I'll be touching the code again I'll issue an error for that too.
Quoted 19 lines
Quoted 19 lines
monitor.c +19
-0+} + +FdsetInfoList *qmp_query_fdsets (Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +FdsetInfoList *fdset_list =NULL ; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +FdsetInfoList *fdset_info =g_malloc0 (sizeof (*fdset_info)); +FdsetFdInfoList *fdsetfd_list =NULL ; + + fdset_info->value =g_malloc0 (sizeof (*fdset_info->value)); + fdset_info->value->fdset_id = mon_fdset->id; + + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +FdsetFdInfoList *fdsetfd_info; + +if (mon_fdset_fd->removed) {
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ continue; + }This means that an fdset with all fds removed will show up as empty in the output; I guess that's okay, as libvirt could use that to infer that the dup()d fds are still in use. The alternative is to keep track of whether we have output any information about an fd within a set before including the set itself in the overall output.
You're right, an empty set will be shown in this case. I chose the route of less code. :) I'm going to leave this as-is unless there's an objection.
Quoted 17 lines
Quoted 4 lines
+++
b/qapi-schema.jsonqapi-schema.json +2
-0+## +# @add-fd:qapi-schema.json line 2227
Quoted 10 lines
qapi-schema.json +10
-0+# +# Add a file descriptor, that was passed via SCM rights, to an fd set. +# +# @fdset-id: #optional The ID of the fd set to add the file descriptor to. +# +# @opaque: #optional A free-form string that can be used to describe the fd. +# +# Returns: @AddfdInfo on success +# If file descriptor was not received, FdNotSupplied +# If @fdset_id does not exist, InvalidParameterValueMissed one: s/_/-/
Gah! Sorry.. and thanks for catching.
Quoted 5 lines
Quoted 5 lines
qapi-schema.json +5
-0+## +# @remove-fd: +# +# Remove a file descriptor from an fd set. +#
Quoted 9 lines
qapi-schema.json line 2248
Quoted 6 lines
qapi-schema.json +6
-0+# @fdset-id: The ID of the fd set that the file descriptor belongs to. +# +# @fd: #optional The file descriptor that is to be removed. +# +# Returns: Nothing on success +# If @fdset_id or @fd is not found, FdNotFoundand another s/_/-/
Ok
Quoted 8 lines
Quoted 8 lines
qmp-commands.hx +8
-0+SQMP +query-fdsets +------------- + +Return information describing all fd sets. + +Arguments: None +
Quoted 9 lines
qmp-commands.hx Example: · line 1021
Quoted 6 lines
qmp-commands.hx +6
-0+Example: + +-> { "execute": "query-fdsets" } +<- { "return": [ + { + "fdset-id": 1missing a comma
Ok. Perhaps I should just use a real example rather than editing the old one!
Quoted 6 lines
qmp-commands.hx Example: · line 1024
Quoted 3 lines
qmp-commands.hx +3
-0+ "fds": [ + { + "fd": 21,JSON does not permit trailing commas.
Ok
Quoted 6 lines
qmp-commands.hx Example: · line 1027
Quoted 3 lines
qmp-commands.hx +3
-0+ }, + { + "fd": 23,and again
Ok
Quoted 8 lines
qmp-commands.hx Example: · line 1032
Quoted 5 lines
qmp-commands.hx +5
-0+ } + ], + }, + { + "fdset-id": 2missing a comma
Ok
Quoted 6 lines
qmp-commands.hx Example: · line 1035
Quoted 3 lines
qmp-commands.hx +3
-0+ "fds": [ + { + "fd": 22,trailing comma
Ok
Quoted 6 lines
Also, it might be nice to include something like:
"opaque":"rdonly:/path/to/file"
in one of the examples to give a hint to the reader how to use opaque.
No problem I'll do that.
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Eric BlakeQuoted 7 lines · Eric Blake
Quoted 6 lines · Corey Bryant
When
qemu_openis passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned fromqemu_open.
Quoted 8 lines
Quoted 5 lines
Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open
dup()references that have not yet been closed. It is incremented onqemu_openand decremented onqemu_close. It is not until the refcount is zero that file desriptors in an fd sets/desriptors/descriptors/
Thanks
Quoted 26 lines
Quoted 10 lines
can be closed. If an fd set has
dup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.monitor.c +4
-0+int monitor_fdset_get_fd (int64_t fdset_id,int flags) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd;monitor.c FdsetInfoList *qmp_query_fdsets(Error **errp) · line 2604
Quoted 10 lines
monitor.c +10
-0+int mon_fd_flags; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (mon_fdset_fd->removed) { +continue ; + }Is this right? According to the commit message, the whole point of leaving an fd in the set is to allow the fd to be reused as a
dup()target for as long as the fdset is alive, even if the monitor no longer cares about the existence of the fd. But this will always skip over an fd marked for removal. Maybe this function needs a flag to say whether
Quoted 9 lines
this is an initial open driven by an explicit user string (in which case, honor the removed flag - if the user removed the
O_RDWRfd and then tries adrive_addwith the same fdset, thedrive_addshould fail because from the user's perspective, there is noO_RDWRfd in the set); vs. an internal usage due to a reopen (use an fd even if removed is true, because we may be toggling betweenO_RDWRandO_RDONLYmultiple times long after the monitor has already removed the fdset, based on actions that were not drive by an explicit/dev/fdsetname.)
Hmm.. something needs to change here, either the commit message or the code.
For security purposes, I'm thinking that an fd should no longer be available for opening after libvirt issues a remove-fd command, with no exceptions. That allows libvirt to have complete control over usage of an fd. Note that other fds in the fd set could still be used on a reopen, assuming remove-fd hasn't been called for them.
Does that work for you? In that case, the code will remain as-is.
Apologies if I've sent conflicting messages on this in the past.
Quoted 10 lines
monitor.c FdsetInfoList *qmp_query_fdsets(Error **errp) · line 2613
Quoted 9 lines
monitor.c +9
-0+ + mon_fd_flags =fcntl (mon_fdset_fd->fd,F_GETFL ); +if (mon_fd_flags ==-1 ) { +return -1 ; + } + +if ((flags &O_ACCMODE ) == (mon_fd_flags &O_ACCMODE )) { +return mon_fdset_fd->fd; + }
Quoted 8 lines
I still wonder if a request for
O_RDONLYshould be satisfied by an existingO_RDWRfd, especially in light of the fact that libvirt would rather pass in only one RDWR fd but qemu block opening currently opens twice during probing. But if it turns out to be a problem in practice, and if libvirt can't really manage to pass two fds into the set, we can hack that in later. Meanwhile, I'm okay with this first round patch requiring an exact match.
I see your point but I think allowing subset access mode matches could allow for a client to get lazy and cause security implications (client only adds RW fd's and QEMU only needs R access in some cases). So I'll keep this as is.
Quoted 26 lines
Quoted 26 lines
osdep.c +22
-0@@ -87,6 +151,39 @@ int qemu_open(const char *name, int flags, ...)int ret;int mode =0 ; +#ifndef _WIN32 +const char *fdset_id_str; + +/* Attempt dup of fd from fd set */ +if (strstart (name,"/dev/fdset/" , &fdset_id_str)) { +int64_t fdset_id; +int fd, dupfd; + + fdset_id =qemu_parse_fdset (fdset_id_str); +if (fdset_id ==-1 ) { + errno =EINVAL ; +return -1 ; + } + + fd =monitor_fdset_get_fd (fdset_id, flags); +if (fd ==-1 ) { +return -1 ; + } + + dupfd =qemu_dup (fd, flags); +if (fd ==-1 ) { +return -1 ;
Quoted 9 lines
osdep.c int qemu_open(const char *name, int flags, ...) · line 180
Quoted 5 lines
osdep.c +5
-0+ } + + ret =monitor_fdset_dup_fd_add (fdset_id, dupfd); +if (ret ==-1 ) { +return -1 ;Leaks dupfd (admittedly only on a corner-case failure, but still worth addressing).
Thanks, I'll fix this.
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Stefan HajnocziQuoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Corey Bryant
monitor.c +14
-0+void qmp_remove_fd (int64_t fdset_id,bool has_fd,int64_t fd,Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20 ]; + + QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { +if (mon_fdset->id != fdset_id) { +continue ; + } + QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { +if (has_fd && mon_fdset_fd->fd != fd) { +continue ; + }monitor.c int monitor_get_fd(Monitor *mon, const char *fdname) · line 2534
Quoted 10 lines
monitor.c +10
-0+ mon_fdset_fd->removed = true; +if (has_fd) { +break ; + } + } +monitor_fdset_cleanup (mon_fdset); +return ; + } +snprintf (fd_str,sizeof (fd_str),"%" PRId64, fd); +error_set (errp,QERR_FD_NOT_FOUND , fd_str);fd is optional and may be uninitialized. I think the human-readable string should be:
Quoted 7 lines
if
has_fd:fd_str = '%s:%s' % (fdset_id, fd) else: fd_str = '%s' % fdset_idOtherwise, looks good.
Good point, thanks. I'll fix this.
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Corey BryantQuoted 14 lines · Corey Bryant
Quoted 7 lines
Quoted 5 lines
can be closed. If an fd set has
dup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.Quoted 5 lines
Is this right? According to the commit message, the whole point of leaving an fd in the set is to allow the fd to be reused as a
dup()target for as long as the fdset is alive, even if the monitor no longer cares about the existence of the fd. But this will always skip over an fd marked for removal.
Quoted 3 lines
For security purposes, I'm thinking that an fd should no longer be available for opening after libvirt issues a remove-fd command, with no exceptions.
If that's the case, then you don't need the removed flag. Simply close the original fd and forget about it at the point that the monitor removes the fd. The fdset will still remain as long as there is a refcount, so that libvirt can then re-add to the set. And that also argues that query-fdsets MUST list empty fdsets, where all existing fds have been removed, but where the set can still be added to again just prior to the next reopen operation.
That is, consider this life cycle:
libvirt> add-fd opaque="RDWR"
qemu< fdset=1, fd=3; the set is in use because of the monitor but with
refcount 0
libvirt> drive_add /dev/fdset/1
qemu< success; internally, fd 4 was created as a dup of 3, and fdset 1
now has refcount 1 and tracks that fd 4 is tied to the set
libvirt> remove-fd fdset=1 fd=3, since the drive_add is complete and
libvirt no longer needs to track what fd qemu is using, but does remember internally that qemu is using fdset 1
qemu< success; internally, fdset 1 is still refcount 1 later on, libvirt prepares to do a snapshot, and knows that after the
snapshot, qemu will want to reopen the file RDONLY, as part of making it the backing image
libvirt> add-fd fdset=1 opaque="RDONLY"
qemu< fdset=1, fd=3 (yes, fd 3 is available for use again, since the
earlier remove closed it out)
libvirt> blockdev-snapshot-sync
qemu< success; internally, the block device knows that /dev/fdset/1 is
now becoming a backing file, so it tries a reopen to convert its current RDWR fd 4 into something RDONLY; the probe succeeds by returning fd 5 as a dup of the readonly fd 3
Quoted 2 lines
Does that work for you? In that case, the code will remain as-is.
I think that lifecycle will work (either libvirt leaves an fd in the set for as long as it thinks qemu might need to do independent open operations, or it removes the fd as soon as it knows that qemu is done with open, but the fdset remains reserved, tracking all dup'd fds that were created from the set.
For that matter, your refcount doesn't even have to be a separate field, but can simply be the length of the list of dup'd fds that are tied to the set until a call to qemu_close. That is, a set must remain alive as long as there is either an add-fd that has not yet been removed, or there is at least one dup'd fd that has not been qemu_close()d.
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
Quoted 16 lines · Eric Blake
Quoted 14 lines · Corey Bryant
Quoted 12 lines
Quoted 5 lines
can be closed. If an fd set has
dup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.Is this right? According to the commit message, the whole point of leaving an fd in the set is to allow the fd to be reused as a
dup()target for as long as the fdset is alive, even if the monitor no longer cares about the existence of the fd. But this will always skip over an fd marked for removal.
Quoted 6 lines
Quoted 3 lines
For security purposes, I'm thinking that an fd should no longer be available for opening after libvirt issues a remove-fd command, with no exceptions.
If that's the case, then you don't need the removed flag. Simply close the original fd and forget about it at the point that the monitor
You're right. Kevin also brought this up to me offline during a discussion a little while ago. I'll plan on closing the fd immediately when remove-fd is issued.
Quoted 21 lines
removes the fd. The fdset will still remain as long as there is a refcount, so that libvirt can then re-add to the set. And that also argues that query-fdsets MUST list empty fdsets, where all existing fds have been removed, but where the set can still be added to again just prior to the next reopen operation.
That is, consider this life cycle:
libvirt> add-fd opaque="RDWR"
qemu< fdset=1, fd=3; the set is in use because of the monitor but with
refcount 0
libvirt>drive_add/dev/fdset/1
qemu< success; internally, fd 4 was created as a dup of 3, and fdset 1
now has refcount 1 and tracks that fd 4 is tied to the set
libvirt> remove-fd fdset=1 fd=3, since thedrive_addis complete and
libvirt no longer needs to track what fd qemu is using, but does remember internally that qemu is using fdset 1
qemu< success; internally, fdset 1 is still refcount 1 later on, libvirt prepares to do a snapshot, and knows that after the
snapshot, qemu will want to reopen the file RDONLY, as part of making it the backing image
Quoted 9 lines
libvirt> add-fd fdset=1 opaque="RDONLY"
qemu< fdset=1, fd=3 (yes, fd 3 is available for use again, since the
earlier remove closed it out)
libvirt> blockdev-snapshot-sync
qemu< success; internally, the block device knows that/dev/fdset/1is
now becoming a backing file, so it tries a reopen to convert its current RDWR fd 4 into something RDONLY; the probe succeeds by returning fd 5 as a dup of the readonly fd 3
This all makes sense to me.
Quoted 8 lines
Quoted 2 lines
Does that work for you? In that case, the code will remain as-is.
I think that lifecycle will work (either libvirt leaves an fd in the set for as long as it thinks qemu might need to do independent open operations, or it removes the fd as soon as it knows that qemu is done with open, but the fdset remains reserved, tracking all dup'd fds that were created from the set.
Yep
Quoted 7 lines
For that matter, your refcount doesn't even have to be a separate field, but can simply be the length of the list of dup'd fds that are tied to the set until a call to
qemu_close. That is, a set must remain alive as long as there is either an add-fd that has not yet been removed, or there is at least one dup'd fd that has not been qemu_close()d.
I agree. I'll plan on dropping refcount and just checking the dup_fds list.
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Corey BryantAm 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 86 lines
This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets.
A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (
O_RDWR,O_WRONLY,O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) "open" and "reopen" the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests.The new QMP commands are:
add-fd: Add a file descriptor to an fd set
remove-fd: Remove a file descriptor from an fd set
query-fdsets: Return information describing all fd setsNote: These commands are not compatible with the existing getfd and closefd QMP commands.
Signed-off-by Corey Bryant <[email protected]>---
v5:
-This patch is new in v5 and replaces the pass-fd QMP command
from v4.
-By grouping fds in fd sets, we ease managability with an fd
set per file, addressing concerns raised in v4 about handling "reopens" and preventing fd leakage. ([email protected], [email protected], [email protected])v6
-Make @fd optional for remove-fd ([email protected]) -Make @fdset-id optional for add-fd ([email protected])v7:
-Share fd sets among all monitor connections ([email protected]) -Addedmon_refcountto keep track of monitor connection count.v8:
-Add opaque string to add-fd/query-fdsets.
([email protected])
-Use camel case for structures. ([email protected]) -Don't return in-use and refcount from query-fdsets.
([email protected])
-Don't return removed fd's from query-fdsets.
([email protected])
-Use fdset-id rather thanfdset_id. ([email protected]) -Fix fd leak inqmp_add_fd(). ([email protected]) -Update QMP errors. ([email protected], [email protected])monitor.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++++++++++++ qmp-commands.hx | 117 +++++++++++++++++++++++++++++++++ 3 files changed, 403 insertions(+)monitor.c +18
-0@@ -140,6 +140,24 @@ struct mon_fd_t {QLIST_ENTRY (mon_fd_t) next; }; +/* file descriptor associated with a file descriptor set */ +typedef struct MonFdsetFd MonFdsetFd ; +struct MonFdsetFd { +int fd; +bool removed; +char *opaque; +QLIST_ENTRY (MonFdsetFd) next; +}; + +/* file descriptor set containing fds passed via SCM_RIGHTS */ +typedef struct MonFdset MonFdset ; +struct MonFdset { +int64_t id; +int refcount; + QLIST_HEAD(, MonFdsetFd) fds; +QLIST_ENTRY (MonFdset) next; +}; + typedef struct MonitorControl { QObject *id; JSONMessageParser parser;
monitor.c static inline int mon_print_count_get(const Monitor *mon) { return 0; } · line 233
Quoted 5 lines
monitor.c +2
-0@@ -211,6 +229,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; }#define QMP_ACCEPT_UNKNOWNS 1 static QLIST_HEAD(mon_list, Monitor) mon_list; +static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; +static int mon_refcount;
You introduce mon_refcount in this patch and check it against 0 in one place, but it never gets changed until patch 3 is applied. Would it make sense to do both in the same patch?
Kevin
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Corey BryantAm 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 19 lines
When
qemu_openis passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned fromqemu_open.Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open
dup()references that have not yet been closed. It is incremented onqemu_openand decremented onqemu_close. It is not until the refcount is zero that file desriptors in an fd set can be closed. If an fd set hasdup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.Signed-off-by Corey Bryant <[email protected]>
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 85
Quoted 7 lines
osdep.c +4
-0@@ -78,6 +79,69 @@ int qemu_madvise(void *addr, size_t len, int advice)#endif } +/* +* Dups an fd and sets the flags +*/ +static int qemu_dup (int fd,int flags)
qemu_dup() is probably not a good name. We'll want to use it when we need to get a wrapper around dup(). And I suspect that we will need it for making bdrv_reopen() compatible with fdset refcounting.
Quoted 8 lines
osdep.c +8
-0+{ +int ret; +int serrno; +int dup_flags; +int setfl_flags; + +if (flags &O_CLOEXEC ) { +#ifdef F_DUPFD_CLOEXEC
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 103
Quoted 10 lines
osdep.c +10
-0+ ret =fcntl (fd,F_DUPFD_CLOEXEC ,0 ); +#else + ret =dup (fd); +if (ret !=-1 ) { +qemu_set_cloexec (ret); + } +#endif + }else { + ret =dup (fd); + }
qemu_open() is supposed to add O_CLOEXEC by itself, so I think we should execute the then branch unconditionally (or we would have to change the qemu_dup() call below to add it - but the fact that O_CLOEXEC isn't even necessarily defined doesn't help with that).
Kevin
Am 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 57 lines
libvirt's sVirt security driver provides SELinux MAC isolation for Qemu guest processes and their corresponding image files. In other words, sVirt uses SELinux to prevent a QEMU process from opening files that do not belong to it.
sVirt provides this support by labeling guests and resources with security labels that are stored in file system extended attributes. Some file systems, such as NFS, do not support the extended attribute security namespace, and therefore cannot support sVirt isolation.
A solution to this problem is to provide fd passing support, where libvirt opens files and passes file descriptors to QEMU. This, along with SELinux policy to prevent QEMU from opening files, can provide image file isolation for NFS files stored on the same NFS mount.
This patch series adds the add-fd, remove-fd, and query-fdsets QMP monitor commands, which allow file descriptors to be passed via
SCM_RIGHTS, and assigned to specified fd sets. This allows fd sets to be created per file with fds having, for example, different access rights. When QEMU needs to reopen a file with different access rights, it can search for a matching fd in the fd set. Fd sets also allow for easy tracking of fds per file, helping to prevent fd leaks.Support is also added to the block layer to allow QEMU to dup an fd from an fdset when the filename is of the
/dev/fdset/nnnformat, where nnn is the fd set ID.No new SELinux policy is required to prevent open of NFS files (files with type
nfs_t). Thevirt_use_nfsboolean type simply needs to be set to false, and open will be prevented (and dup will be allowed). For example:# setsebool virt_use_nfs 0 # getsebool virt_use_nfs virt_use_nfs --> offCorey Bryant (7):
qemu-char: AddMSG_CMSG_CLOEXECflag to recvmsg
qapi: Introduce add-fd, remove-fd, query-fdsets
monitor: Clean up fd sets on monitor disconnect
block: Prevent detection of/dev/fdset/as floppy
block: Convert open calls toqemu_open
block: Convert close calls toqemu_close
block: Enable qemu_open/close to work with fd setsblock/raw-posix.c | 46 +++++---- block/raw-win32.c | 6 +- block/vdi.c | 5 +- block/vmdk.c | 25 ++--- block/vpc.c | 4 +- block/vvfat.c | 16 +-- cutils.c | 5 + monitor.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.h | 5 +
Quoted 8 lines
osdep.c | 117 +++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++ qemu-char.c | 12 ++- qemu-common.h | 2 + qemu-tool.c | 20 ++++ qmp-commands.hx | 117 +++++++++++++++++++++ savevm.c | 4 +- 16 files changed, 721 insertions(+), 55 deletions(-)
Apart from the few comments I made, I like this series. Maybe v9 will be the last one. :-)
Kevin
Re: [Qemu-devel] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
In reply to Kevin WolfQuoted 89 lines · Kevin Wolf
Am 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 88 lines
This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets.
A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (
O_RDWR,O_WRONLY,O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) "open" and "reopen" the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests.The new QMP commands are:
add-fd: Add a file descriptor to an fd set
remove-fd: Remove a file descriptor from an fd set
query-fdsets: Return information describing all fd setsNote: These commands are not compatible with the existing getfd and closefd QMP commands.
Signed-off-by Corey Bryant <[email protected]>---
v5:
-This patch is new in v5 and replaces the pass-fd QMP command
from v4.
-By grouping fds in fd sets, we ease managability with an fd
set per file, addressing concerns raised in v4 about handling "reopens" and preventing fd leakage. ([email protected], [email protected], [email protected])v6
-Make @fd optional for remove-fd ([email protected]) -Make @fdset-id optional for add-fd ([email protected])v7:
-Share fd sets among all monitor connections ([email protected]) -Addedmon_refcountto keep track of monitor connection count.v8:
-Add opaque string to add-fd/query-fdsets.
([email protected])
-Use camel case for structures. ([email protected]) -Don't return in-use and refcount from query-fdsets.
([email protected])
-Don't return removed fd's from query-fdsets.
([email protected])
-Use fdset-id rather thanfdset_id. ([email protected]) -Fix fd leak inqmp_add_fd(). ([email protected]) -Update QMP errors. ([email protected], [email protected])monitor.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++++++++++++ qmp-commands.hx | 117 +++++++++++++++++++++++++++++++++ 3 files changed, 403 insertions(+)monitor.c +18
-0@@ -140,6 +140,24 @@ struct mon_fd_t {QLIST_ENTRY (mon_fd_t) next; }; +/* file descriptor associated with a file descriptor set */ +typedef struct MonFdsetFd MonFdsetFd ; +struct MonFdsetFd { +int fd; +bool removed; +char *opaque; +QLIST_ENTRY (MonFdsetFd) next; +}; + +/* file descriptor set containing fds passed via SCM_RIGHTS */ +typedef struct MonFdset MonFdset ; +struct MonFdset { +int64_t id; +int refcount; + QLIST_HEAD(, MonFdsetFd) fds; +QLIST_ENTRY (MonFdset) next; +}; + typedef struct MonitorControl { QObject *id; JSONMessageParser parser;@@ -211,6 +229,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; }#define QMP_ACCEPT_UNKNOWNS 1
Quoted 7 lines
monitor.c static inline int mon_print_count_get(const Monitor *mon) { return 0; } · line 233
Quoted 3 lines
monitor.c +2
-0static QLIST_HEAD(mon_list, Monitor) mon_list; +static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; +static int mon_refcount;You introduce
mon_refcountin this patch and check it against 0 in one place, but it never gets changed until patch 3 is applied. Would it make sense to do both in the same patch?
Yes, I'll go ahead and move the mon_refcount code from this patch to patch 3.
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Kevin WolfQuoted 23 lines · Kevin Wolf
Am 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 22 lines
When
qemu_openis passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned fromqemu_open.Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open
dup()references that have not yet been closed. It is incremented onqemu_openand decremented onqemu_close. It is not until the refcount is zero that file desriptors in an fd set can be closed. If an fd set hasdup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.Signed-off-by Corey Bryant <[email protected]>(quoted diff) +0
-0@@ -78,6 +79,69 @@ int qemu_madvise(void *addr, size_t len, int advice) #endif }
Quoted 9 lines
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 85
Quoted 4 lines
osdep.c +4
-0+/* +* Dups an fd and sets the flags +*/ +static int qemu_dup (int fd,int flags)
qemu_dup()is probably not a good name. We'll want to use it when we need to get a wrapper arounddup(). And I suspect that we will need it for makingbdrv_reopen()compatible with fdset refcounting.
Do you you have a suggestion for a name?
Quoted 15 lines
Quoted 15 lines
osdep.c +15
-0+{ +int ret; +int serrno; +int dup_flags; +int setfl_flags; + +if (flags &O_CLOEXEC ) { +#ifdef F_DUPFD_CLOEXEC + ret =fcntl (fd,F_DUPFD_CLOEXEC ,0 ); +#else + ret =dup (fd); +if (ret !=-1 ) { +qemu_set_cloexec (ret); + } +#endif
Quoted 8 lines
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 103
Quoted 3 lines
osdep.c +3
-0+ }else { + ret =dup (fd); + }
qemu_open()is supposed to addO_CLOEXECby itself, so I think we should execute the then branch unconditionally (or we would have to change theqemu_dup()call below to add it - but the fact thatO_CLOEXECisn't even necessarily defined doesn't help with that).
Sure I can modify this to always add O_CLOEXEC. (I know you mentioned this before but I think I interpreted the comment as a question to others.)
Do you also want me to modify qemu_open to always add O_CLOEXEC?
Signature · 2 lines
Regards, Corey
Quoted 67 lines · Kevin Wolf
Am 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 57 lines
libvirt's sVirt security driver provides SELinux MAC isolation for Qemu guest processes and their corresponding image files. In other words, sVirt uses SELinux to prevent a QEMU process from opening files that do not belong to it.
sVirt provides this support by labeling guests and resources with security labels that are stored in file system extended attributes. Some file systems, such as NFS, do not support the extended attribute security namespace, and therefore cannot support sVirt isolation.
A solution to this problem is to provide fd passing support, where libvirt opens files and passes file descriptors to QEMU. This, along with SELinux policy to prevent QEMU from opening files, can provide image file isolation for NFS files stored on the same NFS mount.
This patch series adds the add-fd, remove-fd, and query-fdsets QMP monitor commands, which allow file descriptors to be passed via
SCM_RIGHTS, and assigned to specified fd sets. This allows fd sets to be created per file with fds having, for example, different access rights. When QEMU needs to reopen a file with different access rights, it can search for a matching fd in the fd set. Fd sets also allow for easy tracking of fds per file, helping to prevent fd leaks.Support is also added to the block layer to allow QEMU to dup an fd from an fdset when the filename is of the
/dev/fdset/nnnformat, where nnn is the fd set ID.No new SELinux policy is required to prevent open of NFS files (files with type
nfs_t). Thevirt_use_nfsboolean type simply needs to be set to false, and open will be prevented (and dup will be allowed). For example:# setsebool virt_use_nfs 0 # getsebool virt_use_nfs virt_use_nfs --> offCorey Bryant (7):
qemu-char: AddMSG_CMSG_CLOEXECflag to recvmsg
qapi: Introduce add-fd, remove-fd, query-fdsets
monitor: Clean up fd sets on monitor disconnect
block: Prevent detection of/dev/fdset/as floppy
block: Convert open calls toqemu_open
block: Convert close calls toqemu_close
block: Enable qemu_open/close to work with fd setsblock/raw-posix.c | 46 +++++---- block/raw-win32.c | 6 +- block/vdi.c | 5 +- block/vmdk.c | 25 ++--- block/vpc.c | 4 +- block/vvfat.c | 16 +-- cutils.c | 5 + monitor.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.h | 5 +Quoted 8 lines
osdep.c | 117 +++++++++++++++++++++ qapi-schema.json | 98 ++++++++++++++++++ qemu-char.c | 12 ++- qemu-common.h | 2 + qemu-tool.c | 20 ++++ qmp-commands.hx | 117 +++++++++++++++++++++ savevm.c | 4 +- 16 files changed, 721 insertions(+), 55 deletions(-)
Quoted 2 lines
Apart from the few comments I made, I like this series. Maybe v9 will be the last one. :-)
Thanks, I hope so too!
Signature · 2 lines
Regards, Corey
Re: [Qemu-devel] [PATCH v8 7/7] block: Enable qemu_open/close to work with fd sets
In reply to Corey BryantQuoted 62 lines · Corey Bryant
Quoted 23 lines · Kevin Wolf
Am 10.08.2012 04:10, schrieb Corey Bryant:
Quoted 22 lines
When
qemu_openis passed a filename of the "/dev/fdset/nnn" format (where nnn is the fdset ID), an fd with matching access mode flags will be searched for within the specified monitor fd set. If the fd is found, a dup of the fd will be returned fromqemu_open.Each fd set has a reference count. The purpose of the reference count is to determine if an fd set contains file descriptors that have open
dup()references that have not yet been closed. It is incremented onqemu_openand decremented onqemu_close. It is not until the refcount is zero that file desriptors in an fd set can be closed. If an fd set hasdup()references open, then we must keep the other fds in the fd set open in case a reopen of the file occurs that requires an fd with a different access mode.Signed-off-by Corey Bryant <[email protected]>(quoted diff) +0
-0@@ -78,6 +79,69 @@ int qemu_madvise(void *addr, size_t len, int advice) #endif }Quoted 9 lines
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 85
Quoted 4 lines
osdep.c +4
-0+/* +* Dups an fd and sets the flags +*/ +static int qemu_dup (int fd,int flags)
qemu_dup()is probably not a good name. We'll want to use it when we need to get a wrapper arounddup(). And I suspect that we will need it for makingbdrv_reopen()compatible with fdset refcounting.Do you you have a suggestion for a name?
Quoted 15 lines
Quoted 15 lines
osdep.c +15
-0+{ +int ret; +int serrno; +int dup_flags; +int setfl_flags; + +if (flags &O_CLOEXEC ) { +#ifdef F_DUPFD_CLOEXEC + ret =fcntl (fd,F_DUPFD_CLOEXEC ,0 ); +#else + ret =dup (fd); +if (ret !=-1 ) { +qemu_set_cloexec (ret); + } +#endif Quoted 8 lines
osdep.c int qemu_madvise(void *addr, size_t len, int advice) · line 103
Quoted 3 lines
osdep.c +3
-0+ }else { + ret =dup (fd); + }
qemu_open()is supposed to addO_CLOEXECby itself, so I think we should execute the then branch unconditionally (or we would have to change theqemu_dup()call below to add it - but the fact thatO_CLOEXECisn't even necessarily defined doesn't help with that).
Quoted 5 lines
Sure I can modify this to always add
O_CLOEXEC. (I know you mentioned this before but I think I interpreted the comment as a question to others.)Do you also want me to modify
qemu_opento always addO_CLOEXEC?
My mistake.. it's always set in qemu_open already.
Signature · 2 lines
Regards, Corey