Blame SOURCES/kvm-file-posix-specify-expected-filetypes.patch

1bdc94
From 75d7ceaa7c842deeae5cd7c09bbfd982c3dd0f2e Mon Sep 17 00:00:00 2001
1bdc94
From: Kevin Wolf <kwolf@redhat.com>
1bdc94
Date: Fri, 13 Jul 2018 14:50:01 +0200
1bdc94
Subject: [PATCH 43/89] file-posix: specify expected filetypes
1bdc94
1bdc94
RH-Author: Kevin Wolf <kwolf@redhat.com>
1bdc94
Message-id: <20180713145002.20953-2-kwolf@redhat.com>
1bdc94
Patchwork-id: 81350
1bdc94
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 1/2] file-posix: specify expected filetypes
1bdc94
Bugzilla: 1525829
1bdc94
RH-Acked-by: John Snow <jsnow@redhat.com>
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Fam Zheng <famz@redhat.com>
1bdc94
1bdc94
From: John Snow <jsnow@redhat.com>
1bdc94
1bdc94
Adjust each caller of raw_open_common to specify if they are expecting
1bdc94
host and character devices or not. Tighten expectations of file types upon
1bdc94
open in the common code and refuse types that are not expected.
1bdc94
1bdc94
This has two effects:
1bdc94
1bdc94
(1) Character and block devices are now considered deprecated for the
1bdc94
    'file' driver, which expects only S_IFREG, and
1bdc94
(2) no file-posix driver (file, host_cdrom, or host_device) can open
1bdc94
    directories now.
1bdc94
1bdc94
I don't think there's a legitimate reason to open directories as if
1bdc94
they were files. This prevents QEMU from opening and attempting to probe
1bdc94
a directory inode, which can break in exciting ways. One of those ways
1bdc94
is lseek on ext4/xfs, which will return 0x7fffffffffffffff as the file
1bdc94
size instead of EISDIR. This can coax QEMU into responding with a
1bdc94
confusing "file too big" instead of "Hey, that's not a file".
1bdc94
1bdc94
See: https://bugs.launchpad.net/qemu/+bug/1739304/
1bdc94
Signed-off-by: John Snow <jsnow@redhat.com>
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
(cherry picked from commit 230ff73904e72dde2d7718c2da407786a1c72e57)
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 block/file-posix.c | 39 +++++++++++++++++++++++++++++++--------
1bdc94
 qemu-doc.texi      |  6 ++++++
1bdc94
 2 files changed, 37 insertions(+), 8 deletions(-)
1bdc94
1bdc94
diff --git a/block/file-posix.c b/block/file-posix.c
1bdc94
index 24c2367..06ec67d 100644
1bdc94
--- a/block/file-posix.c
1bdc94
+++ b/block/file-posix.c
1bdc94
@@ -431,7 +431,8 @@ static QemuOptsList raw_runtime_opts = {
1bdc94
 };
1bdc94
 
1bdc94
 static int raw_open_common(BlockDriverState *bs, QDict *options,
1bdc94
-                           int bdrv_flags, int open_flags, Error **errp)
1bdc94
+                           int bdrv_flags, int open_flags,
1bdc94
+                           bool device, Error **errp)
1bdc94
 {
1bdc94
     BDRVRawState *s = bs->opaque;
1bdc94
     QemuOpts *opts;
1bdc94
@@ -569,10 +570,32 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
1bdc94
         error_setg_errno(errp, errno, "Could not stat file");
1bdc94
         goto fail;
1bdc94
     }
1bdc94
-    if (S_ISREG(st.st_mode)) {
1bdc94
-        s->discard_zeroes = true;
1bdc94
-        s->has_fallocate = true;
1bdc94
+
1bdc94
+    if (!device) {
1bdc94
+        if (S_ISBLK(st.st_mode)) {
1bdc94
+            warn_report("Opening a block device as a file using the '%s' "
1bdc94
+                        "driver is deprecated", bs->drv->format_name);
1bdc94
+        } else if (S_ISCHR(st.st_mode)) {
1bdc94
+            warn_report("Opening a character device as a file using the '%s' "
1bdc94
+                        "driver is deprecated", bs->drv->format_name);
1bdc94
+        } else if (!S_ISREG(st.st_mode)) {
1bdc94
+            error_setg(errp, "A regular file was expected by the '%s' driver, "
1bdc94
+                       "but something else was given", bs->drv->format_name);
1bdc94
+            ret = -EINVAL;
1bdc94
+            goto fail;
1bdc94
+        } else {
1bdc94
+            s->discard_zeroes = true;
1bdc94
+            s->has_fallocate = true;
1bdc94
+        }
1bdc94
+    } else {
1bdc94
+        if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1bdc94
+            error_setg(errp, "'%s' driver expects either "
1bdc94
+                       "a character or block device", bs->drv->format_name);
1bdc94
+            ret = -EINVAL;
1bdc94
+            goto fail;
1bdc94
+        }
1bdc94
     }
1bdc94
+
1bdc94
     if (S_ISBLK(st.st_mode)) {
1bdc94
 #ifdef BLKDISCARDZEROES
1bdc94
         unsigned int arg;
1bdc94
@@ -625,7 +648,7 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
1bdc94
     BDRVRawState *s = bs->opaque;
1bdc94
 
1bdc94
     s->type = FTYPE_FILE;
1bdc94
-    return raw_open_common(bs, options, flags, 0, errp);
1bdc94
+    return raw_open_common(bs, options, flags, 0, false, errp);
1bdc94
 }
1bdc94
 
1bdc94
 typedef enum {
1bdc94
@@ -2794,7 +2817,7 @@ hdev_open_Mac_error:
1bdc94
 
1bdc94
     s->type = FTYPE_FILE;
1bdc94
 
1bdc94
-    ret = raw_open_common(bs, options, flags, 0, &local_err);
1bdc94
+    ret = raw_open_common(bs, options, flags, 0, true, &local_err);
1bdc94
     if (ret < 0) {
1bdc94
         error_propagate(errp, local_err);
1bdc94
 #if defined(__APPLE__) && defined(__MACH__)
1bdc94
@@ -3023,7 +3046,7 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1bdc94
     s->type = FTYPE_CD;
1bdc94
 
1bdc94
     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1bdc94
-    return raw_open_common(bs, options, flags, O_NONBLOCK, errp);
1bdc94
+    return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp);
1bdc94
 }
1bdc94
 
1bdc94
 static int cdrom_probe_device(const char *filename)
1bdc94
@@ -3136,7 +3159,7 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1bdc94
 
1bdc94
     s->type = FTYPE_CD;
1bdc94
 
1bdc94
-    ret = raw_open_common(bs, options, flags, 0, &local_err);
1bdc94
+    ret = raw_open_common(bs, options, flags, 0, true, &local_err);
1bdc94
     if (ret) {
1bdc94
         error_propagate(errp, local_err);
1bdc94
         return ret;
1bdc94
diff --git a/qemu-doc.texi b/qemu-doc.texi
1bdc94
index de5097a..985e0f2 100644
1bdc94
--- a/qemu-doc.texi
1bdc94
+++ b/qemu-doc.texi
1bdc94
@@ -2938,6 +2938,12 @@ The @code{-startdate} option has been replaced by @code{-rtc base=@var{date}}.
1bdc94
 The ``convert -s snapshot_id_or_name'' argument is obsoleted
1bdc94
 by the ``convert -l snapshot_param'' argument instead.
1bdc94
 
1bdc94
+@subsection -drive file=json:@{...@{'driver':'file'@}@} (since 3.0)
1bdc94
+
1bdc94
+The 'file' driver for drives is no longer appropriate for character or host
1bdc94
+devices and will only accept regular files (S_IFREG). The correct driver
1bdc94
+for these file types is 'host_cdrom' or 'host_device' as appropriate.
1bdc94
+
1bdc94
 @section QEMU Machine Protocol (QMP) commands
1bdc94
 
1bdc94
 @subsection block-dirty-bitmap-add "autoload" parameter (since 2.12.0)
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94