From: Li Zhijian <lizhijian@cn.fujitsu.com>
To: <kwolf@redhat.com>, <mreitz@redhat.com>, <qemu-block@nongnu.org>
Cc: qemu-devel@nongnu.org, Li Zhijian <lizhijian@cn.fujitsu.com>
Subject: [PATCH] block: Improve backing file validation
Date: Mon, 10 May 2021 12:30:45 +0800 [thread overview]
Message-ID: <20210510043045.15238-1-lizhijian@cn.fujitsu.com> (raw)
Image below user cases:
case 1:
```
$ qemu-img create -f raw source.raw 1G
$ qemu-img create -f qcow2 -F raw -b source.raw ./source.raw
qemu-img info source.raw
image: source.raw
file format: qcow2
virtual size: 193K (197120 bytes)
disk size: 196K
cluster_size: 65536
backing file: source.raw <<<<<<
backing file format: raw
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
```
case 2:
```
$ qemu-img create -f raw source.raw 1G
$ ln -sf source.raw destination.qcow2
$ qemu-img create -f qcow2 -F raw -b source.raw ./destination.qcow2
qemu-img info source.raw
image: source.raw
file format: qcow2 <<<<<<
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
backing file: source.raw
backing file format: raw
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
```
Generally, we don't expect to corrupte the source.raw anyway, while
actually it does.
Here we validate the realpath of file instead the input string.
Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
---
block.c | 46 +++++++++++++++++++++++++++++++++++++++-------
1 file changed, 39 insertions(+), 7 deletions(-)
diff --git a/block.c b/block.c
index 9ad725d205..523845b763 100644
--- a/block.c
+++ b/block.c
@@ -6431,6 +6431,44 @@ bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
return true;
}
+static bool validate_backing_file(const char *filename,
+ const char *backing_file, Error **errp)
+{
+ bool ret = false;
+ char *rf, *real_filename = g_malloc0(PATH_MAX + 1);
+ char *rb, *real_backing = g_malloc0(PATH_MAX + 1);
+
+ rf = realpath(filename, real_filename);
+ if (!rf) {
+ if (errno == ENOENT) {
+ /* filename doesn't exit, ignore it */
+ rf = (char *)filename;
+ } else {
+ error_setg(errp, "Failed to resolve %s", filename);
+ goto out;
+ }
+ }
+ rb = realpath(backing_file, real_backing);
+ if (!rb) {
+ error_setg(errp, "Failed to resolve %s", backing_file);
+ goto out;
+ }
+ if (!strcmp(rf, rb)) {
+ error_setg(errp, "Error: Trying to create an image with the "
+ "same filename as the backing file");
+ goto out;
+ }
+ if (backing_file[0] == '\0') {
+ error_setg(errp, "Expected backing file name, got empty string");
+ goto out;
+ }
+ ret = true;
+out:
+ g_free(real_filename);
+ g_free(real_backing);
+ return ret;
+}
+
void bdrv_img_create(const char *filename, const char *fmt,
const char *base_filename, const char *base_fmt,
char *options, uint64_t img_size, int flags, bool quiet,
@@ -6507,13 +6545,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
if (backing_file) {
- if (!strcmp(filename, backing_file)) {
- error_setg(errp, "Error: Trying to create an image with the "
- "same filename as the backing file");
- goto out;
- }
- if (backing_file[0] == '\0') {
- error_setg(errp, "Expected backing file name, got empty string");
+ if (!validate_backing_file(filename, backing_file, errp)) {
goto out;
}
}
--
2.30.2
next reply other threads:[~2021-05-10 4:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-10 4:30 Li Zhijian [this message]
2021-05-10 8:41 ` [PATCH] block: Improve backing file validation Daniel P. Berrangé
2021-05-11 1:58 ` lizhijian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210510043045.15238-1-lizhijian@cn.fujitsu.com \
--to=lizhijian@cn.fujitsu.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).