xl: Return an error if an empty file is passed to cd-insert
PATCH 1/1 xl: Return an error if an empty file is passed to cd-insert
Two changes:
* Stat the file before calling libxl_cdrom_insert()
* Return an error if anything fails (including libxl_cdrom_insert)
This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
v2:
- Avoid leaking buf
--- tools/libxl/xl_cmdimpl.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
tools/libxl/xl_cmdimpl.c +24 -5
@@ -2505,25 +2505,45 @@ int main_memset(int argc, char **argv)return 0 ; } -static void cd_insert (uint32_t domid,const char *virtdev,char *phys) +static int cd_insert (uint32_t domid,const char *virtdev,char *phys) {libxl_device_disk disk;/* we don't free disk's contents */ char *buf =NULL ;XLU_Config *config =0 ; +struct stat b; +int rc =0 ;if (asprintf (&buf,"vdev=%s,access=r,devtype=cdrom,target=%s" , virtdev, phys ? phys :"" ) <0 ) {fprintf (stderr,"out of memory\n" ); -return ; + rc =1 ; + goto out; }parse_disk_config (&config, buf, &disk); -libxl_cdrom_insert (ctx, domid, &disk,NULL ); +/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ +if (disk.format !=LIBXL_DISK_FORMAT_EMPTY + &&stat (disk.pdev_path, &b)) { +fprintf (stderr,"Cannot stat file: %s\n" , + disk.pdev_path); + rc =1 ; + goto out; + } + +if (libxl_cdrom_insert (ctx, domid, &disk,NULL )) + rc=1 ; +out:libxl_device_disk_dispose (&disk);free (buf); + +return rc; }int main_cd_eject (int argc,char **argv)
@@ -2539,8 +2559,7 @@ int main_cd_eject(int argc, char **argv) domid =find_domain (argv[optind]); virtdev = argv[optind +1 ]; -cd_insert (domid, virtdev,NULL ); -return 0 ; +return cd_insert (domid, virtdev,NULL ); }int main_cd_insert (int argc,char **argv)
Signature · 1 line
1.7.9.5
Quoted 9 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Quoted 4 lines
v2:
- Avoid leaking bufSigned-off-by George Dunlap <[email protected]>
Acked + applied, thanks.
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2530
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +5
-0+/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */
Is this on your 4.4 list?
Ian.
Quoted 17 lines · Ian Campbell
Quoted 9 lines · George Dunlap
Two changes:
* Stat the file before callinglibxl_cdrom_insert()
* Return an error if anything fails (includinglibxl_cdrom_insert)This is in part to work around the fact that the RAW disk type is used for things that aren't actually files; so we can't call stat in libxl_device.c:libxl__device_disk_set_backend() because it may be going over a remote protocol.
Quoted 4 lines
v2:
- Avoid leaking bufSigned-off-by George Dunlap <[email protected]>Acked + applied, thanks.
Quoted 7 lines
tools/libxl/xl_cmdimpl.c int main_memset(int argc, char **argv) · line 2530
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +5
-0+/* ATM the existence of the backing file is not checked for qdisk +* in libxl_cdrom_insert() because RAW is used for remote +* protocols as well as plain files. This will ideally be changed +* for 4.4, but this work-around fixes the problem of "cd-insert" +* returning success for non-existent files. */ Is this on your 4.4 list?
Yes:
* libxl: Don't use RAW format for "URL"-based qdisks (e.g., rbd:rbd/foo.img)
We talked about this at the hackathon as well.
-George