All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block: Improve backing file validation
@ 2021-05-10  4:30 Li Zhijian
  2021-05-10  8:41 ` Daniel P. Berrangé
  0 siblings, 1 reply; 3+ messages in thread
From: Li Zhijian @ 2021-05-10  4:30 UTC (permalink / raw)
  To: kwolf, mreitz, qemu-block; +Cc: qemu-devel, Li Zhijian

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





^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Improve backing file validation
  2021-05-10  4:30 [PATCH] block: Improve backing file validation Li Zhijian
@ 2021-05-10  8:41 ` Daniel P. Berrangé
  2021-05-11  1:58   ` lizhijian
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel P. Berrangé @ 2021-05-10  8:41 UTC (permalink / raw)
  To: Li Zhijian; +Cc: kwolf, qemu-devel, qemu-block, mreitz

On Mon, May 10, 2021 at 12:30:45PM +0800, Li Zhijian wrote:
> 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.

That still won't handle the case where you use hard links

  $ ln source.raw destination.qcow2

To properly validate the scenarios I think it is neccessary
to ignore the filename sentirely.

Instead attempt to open both files, and if successful, fstat()
them both, and then compare the st_dev + st_ino  fields.


> 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);

Don't do this - PATH_MAX is not required to be defined by POSIX, and
when it is defined, POSIX states that it may be so large that it is
inappropriate to use for allocation.

> +
> +    rf = realpath(filename, real_filename);

GLibC, macOS, FreeBSD, NetBSD and OpenBSD all implement the
POSIX.1-2008 extension that allows the output buffer to be
NULL, in which case realpath returns a newly allocated
string of the correct size.

Windows doesn't provide realpath at all, so I'm surprised
this doesn't break the windows builds.


> +    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
> 
> 
> 
> 

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Improve backing file validation
  2021-05-10  8:41 ` Daniel P. Berrangé
@ 2021-05-11  1:58   ` lizhijian
  0 siblings, 0 replies; 3+ messages in thread
From: lizhijian @ 2021-05-11  1:58 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: kwolf, qemu-devel, qemu-block, mreitz


On 2021/5/10 16:41, Daniel P. Berrangé wrote:
> On Mon, May 10, 2021 at 12:30:45PM +0800, Li Zhijian wrote:
>> 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.
> That still won't handle the case where you use hard links
>
>    $ ln source.raw destination.qcow2
>
> To properly validate the scenarios I think it is neccessary
> to ignore the filename sentirely.
>
> Instead attempt to open both files, and if successful, fstat()
> them both, and then compare the st_dev + st_ino  fields.


Sounds great, i will update it.

Thanks

Zhijian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-11  1:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-10  4:30 [PATCH] block: Improve backing file validation Li Zhijian
2021-05-10  8:41 ` Daniel P. Berrangé
2021-05-11  1:58   ` lizhijian

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.