PATCH: Set close-on-exec flag for QEMU disks
QEMU does not currently set the close-on-exec flag after opening its virtual disk images. This causes problems when it later runs the /etc/xen/qemu-ifup script because the file descriptors get propagated to networking commands like brctl / ifconfig. The SELinux policy quite rightly does not allow the networking scripts to access the virtual disk images, so these inherited file descriptors for AVC denials to be logged.
The attached patch modifies all the QEMU disk driver backends to make sure the close-on-exec flag is turned on
Regards,
Dan.
Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=| |=- Perl modules: http://search.cpan.org/~danberr/ -=| |=- Projects: http://freshmeat.net/~danielpb/ -=| |=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|
tools/ioemu/block-bochs.c +11 -2
@@ -88,7 +88,7 @@ static int bochs_open(BlockDriverState *static int bochs_open (BlockDriverState *bs,const char *filename) {BDRVBochsState *s = bs->opaque; -int fd, i; +int fd, i, flags;struct bochs_header bochs; fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE );
@@ -97,7 +97,16 @@ static int bochs_open(BlockDriverState *if (fd <0 )return -1 ; } - +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + bs->read_only =1 ;// no write support yet s->fd = fd;
tools/ioemu/block-cloop.c +14 -2
@@ -53,11 +53,23 @@ static int cloop_open(BlockDriverState *static int cloop_open (BlockDriverState *bs,const char *filename) {BDRVCloopState *s = bs->opaque; +int fd, flags;uint32_t offsets_size,max_compressed_block_size=1 ,i; - s->fd =open (filename,O_RDONLY |O_BINARY |O_LARGEFILE ); -if (s->fd <0 ) + fd =open (filename,O_RDONLY |O_BINARY |O_LARGEFILE ); +if (fd <0 )return -1 ; +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + + s->fd = fd; bs->read_only =1 ;/* read header */
tools/ioemu/block-cow.c +11 -1
@@ -65,7 +65,7 @@ static int cow_open(BlockDriverState *bsstatic int cow_open (BlockDriverState *bs,const char *filename) {BDRVCowState *s = bs->opaque; -int fd; +int fd, flags;struct cow_header_v2 cow_header;int64_t size;
@@ -75,6 +75,16 @@ static int cow_open(BlockDriverState *bsif (fd <0 )return -1 ; } +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + s->fd = fd;/* see if it is a cow image */ if (read (fd, &cow_header,sizeof (cow_header)) !=sizeof (cow_header)) {
tools/ioemu/block-dmg.c +14 -2
@@ -76,13 +76,25 @@ static int dmg_open(BlockDriverState *bsstatic int dmg_open (BlockDriverState *bs,const char *filename) {BDRVDMGState *s = bs->opaque; +int fd, flags;off_t info_begin,info_end,last_in_offset,last_out_offset;uint32_t count;uint32_t max_compressed_size=1 ,max_sectors_per_chunk=1 ,i; - s->fd =open (filename,O_RDONLY |O_BINARY |O_LARGEFILE ); -if (s->fd <0 ) + fd =open (filename,O_RDONLY |O_BINARY |O_LARGEFILE ); +if (fd <0 )return -1 ; +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + + s->fd = fd; bs->read_only =1 ; s->n_chunks =0 ; s->offsets = s->lengths = s->sectors = s->sectorcounts =0 ;
tools/ioemu/block-qcow.c +11 -1
@@ -92,7 +92,7 @@ static int qcow_open(BlockDriverState *bstatic int qcow_open (BlockDriverState *bs,const char *filename) {BDRVQcowState *s = bs->opaque; -int fd, len, i, shift; +int fd, len, i, shift, flags;QCowHeader header; fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE );
@@ -101,6 +101,16 @@ static int qcow_open(BlockDriverState *bif (fd <0 )return -1 ; } +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + s->fd = fd;if (read (fd, &header,sizeof (header)) !=sizeof (header)) goto fail;
tools/ioemu/block-vmdk.c +11 -1
@@ -92,7 +92,7 @@ static int vmdk_open(BlockDriverState *bstatic int vmdk_open (BlockDriverState *bs,const char *filename) {BDRVVmdkState *s = bs->opaque; -int fd, i; +int fd, i, flags;uint32_t magic;int l1_size;
@@ -103,6 +103,16 @@ static int vmdk_open(BlockDriverState *breturn -1 ; bs->read_only =1 ; } +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } +if (read (fd, &magic,sizeof (magic)) !=sizeof (magic)) goto fail; magic =be32_to_cpu (magic);
tools/ioemu/block-vpc.c +11 -1
@@ -89,7 +89,7 @@ static int vpc_open(BlockDriverState *bsstatic int vpc_open (BlockDriverState *bs,const char *filename) {BDRVVPCState *s = bs->opaque; -int fd, i; +int fd, i, flags;struct vpc_subheader header; fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE );
@@ -99,6 +99,16 @@ static int vpc_open(BlockDriverState *bsreturn -1 ; } +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } + bs->read_only =1 ;// no write support yet s->fd = fd;
tools/ioemu/block.c +22 -2
@@ -180,7 +180,7 @@ void get_tmp_filename(char *filename, insimplify the BSD case */ static BlockDriver *find_image_format (const char *filename) { -int fd, ret, score, score_max; +int fd, ret, score, score_max, flags;BlockDriver *drv1, *drv;uint8_t *buf;size_t bufsize =1024 ;
@@ -190,6 +190,16 @@ static BlockDriver *find_image_format(co buf =NULL ; ret =0 ; }else { +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } +#ifdef DIOCGSECTORSIZE {unsigned int sectorsize =512 ;
@@ -675,7 +685,7 @@ static int raw_open(BlockDriverState *bsstatic int raw_open (BlockDriverState *bs,const char *filename) {BDRVRawState *s = bs->opaque; -int fd; +int fd, flags;int64_t size;#ifdef _BSDstruct stat sb;
@@ -692,6 +702,16 @@ static int raw_open(BlockDriverState *bsreturn -1 ; bs->read_only =1 ; } +if ((flags =fcntl (fd,F_GETFD )) <0 ) { +close (fd); +return -1 ; + } + flags |=FD_CLOEXEC ; +if ((fcntl (fd,F_SETFD , flags)) <0 ) { +close (fd); +return -1 ; + } +#ifdef _BSDif (!fstat (fd, &sb) && (S_IFCHR & sb.st_mode)) {#ifdef DIOCGMEDIASIZE
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 9 lines · Daniel P. Berrange
QEMU does not currently set the close-on-exec flag after opening its virtual disk images. This causes problems when it later runs the
/etc/xen/qemu-ifupscript because the file descriptors get propagated to networking commands like brctl / ifconfig. The SELinux policy quite rightly does not allow the networking scripts to access the virtual disk images, so these inherited file descriptors for AVC denials to be logged.The attached patch modifies all the QEMU disk driver backends to make sure the close-on-exec flag is turned on
It would be nicer to implement an open_cloexec() function in e.g., vl.c to do the open() and fcntl() in one go and in one place.
There are lots of uses of open() throughout the qemu sources and the patch only fixes up a subset of them -- is this correct?
-- Keir
Quoted 10 lines · Keir Fraser
Quoted 7 lines · Daniel P. Berrange
QEMU does not currently set the close-on-exec flag after opening its virtual disk images. This causes problems when it later runs the
/etc/xen/qemu-ifupscript because the file descriptors get propagated to networking commands like brctl / ifconfig. The SELinux policy quite rightly does not allow the networking scripts to access the virtual disk images, so these inherited file descriptors for AVC denials to be logged.
Quoted 7 lines
Quoted 3 lines
The attached patch modifies all the QEMU disk driver backends to make sure the close-on-exec flag is turned on
It would be nicer to implement an
open_cloexec()function in e.g.,vl.cto do theopen()andfcntl()in one go and in one place.
There are few areas where scripts are executed. Why not just introduce an exec() wrapper that closes file descriptors appropriately.
That makes it less likely that this problem will occur in the future.
Regards,
Anthony Liguori
Quoted 12 lines
There are lots of uses of
open()throughout the qemu sources and the patch only fixes up a subset of them -- is this correct?-- Keir
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 17 lines · Anthony Liguori
Quoted 16 lines · Keir Fraser
Quoted 12 lines · Daniel P. Berrange
QEMU does not currently set the close-on-exec flag after opening its virtual
disk images. This causes problems when it later runs the/etc/xen/qemu-ifup
script because the file descriptors get propagated to networking commands like brctl / ifconfig. The SELinux policy quite rightly does not allow the networking scripts to access the virtual disk images, so these inherited file descriptors for AVC denials to be logged.The attached patch modifies all the QEMU disk driver backends to make sure the close-on-exec flag is turned on
Quoted 6 lines
Quoted 3 lines
It would be nicer to implement an
open_cloexec()function in e.g.,vl.cto do theopen()andfcntl()in one go and in one place.There are few areas where scripts are executed. Why not just introduce an
exec()wrapper that closes file descriptors appropriately.
Looking at the QEMU code in Xen there are only two places where exec is used, and one of those is for the vncviewer spawning which isn't upstream. One of them uses execlp() while the other uses execv(). So rather than doing a refactoring of code which will lead to even more divergance with upstream, the attached patch just fixes up those 2 locations to close all file handles upto sysconf(_SC_OPEN_MAX), with exception of STDIN/OUT/ERR and the TAP deevice handle.
I've tested against Xen 3.0.4 in Fedora to validate neither the qemu-ifup or vncviewer processes receive any extraneous file handles. The attached patch applies cleanly to xen-unstable.hg too
Regards,
Dan.
Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=| |=- Perl modules: http://search.cpan.org/~danberr/ -=| |=- Projects: http://freshmeat.net/~danielpb/ -=| |=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|
tools/ioemu/vl.c +8 -0
@@ -3250,6 +3250,14 @@ static int net_tap_init(VLANState *vlan, pid =fork ();if (pid >=0 ) {if (pid ==0 ) { +int open_max =sysconf (_SC_OPEN_MAX), i; +for (i =0 ; i < open_max; i++) +if (i !=STDIN_FILENO && + i !=STDOUT_FILENO && + i !=STDERR_FILENO && + i != fd) +close (i); + parg = args; *parg++ = (char *)setup_script; *parg++ = ifname;
tools/ioemu/vnc.c +7 -1
@@ -1445,7 +1445,7 @@ int vnc_display_init(DisplayState *ds, iint vnc_start_viewer (int port) { -int pid; +int pid, i, open_max;char s[16 ];sprintf (s,":%d" , port);
@@ -1456,6 +1456,12 @@ int vnc_start_viewer(int port)exit (1 );case 0 :/* child */ + open_max =sysconf (_SC_OPEN_MAX); +for (i =0 ; i < open_max; i++) +if (i !=STDIN_FILENO && + i !=STDOUT_FILENO && + i !=STDERR_FILENO ) +close (i);execlp ("vncviewer" ,"vncviewer" , s,NULL );fprintf (stderr,"vncviewer execlp failed\n" );exit (1 );
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel