diff mbox series

[1/2] preallocate: do not allow to change BDS permission improperly

Message ID 20241009140051.771660-2-den@openvz.org (mailing list archive)
State New, archived
Headers show
Series block/preallocate: fix image truncation logic | expand

Commit Message

Zhijian Li (Fujitsu)" via Oct. 9, 2024, 1:58 p.m. UTC
RW permissions could not be lifted from the preallocation filter if
truncate operation has not been finished. In the other case this would
mean WRITE operation (image truncate) called after the return from
inactivate call. This is definitely a contract violation.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
CC: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
CC: Kevin Wolf <kwolf@redhat.com>
---
 block/preallocate.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Andrey Drobyshev Oct. 9, 2024, 2:54 p.m. UTC | #1
On 10/9/24 4:58 PM, Denis V. Lunev wrote:
> RW permissions could not be lifted from the preallocation filter if
> truncate operation has not been finished. In the other case this would
> mean WRITE operation (image truncate) called after the return from
> inactivate call. This is definitely a contract violation.
> 
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> CC: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
> CC: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> CC: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/preallocate.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/block/preallocate.c b/block/preallocate.c
> index bfb638d8b1..1cf854966c 100644
> --- a/block/preallocate.c
> +++ b/block/preallocate.c
> @@ -581,6 +581,17 @@ static void preallocate_child_perm(BlockDriverState *bs, BdrvChild *c,
>      }
>  }
>  
> +static int preallocate_check_perm(BlockDriverState *bs, uint64_t perm,
> +                                  uint64_t shared, Error **errp)
> +{
> +    BDRVPreallocateState *s = bs->opaque;
> +    if (!can_write_resize(perm) && s->data_end != -EINVAL) {
> +        error_setg_errno(errp, EPERM, "Write access is required for truncate");
> +        return -EPERM;
> +    }
> +    return 0;
> +}
> +
>  static BlockDriver bdrv_preallocate_filter = {
>      .format_name = "preallocate",
>      .instance_size = sizeof(BDRVPreallocateState),
> @@ -602,6 +613,7 @@ static BlockDriver bdrv_preallocate_filter = {
>  
>      .bdrv_set_perm = preallocate_set_perm,
>      .bdrv_child_perm = preallocate_child_perm,
> +    .bdrv_check_perm = preallocate_check_perm,
>  
>      .is_filter = true,
>  };

Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
diff mbox series

Patch

diff --git a/block/preallocate.c b/block/preallocate.c
index bfb638d8b1..1cf854966c 100644
--- a/block/preallocate.c
+++ b/block/preallocate.c
@@ -581,6 +581,17 @@  static void preallocate_child_perm(BlockDriverState *bs, BdrvChild *c,
     }
 }
 
+static int preallocate_check_perm(BlockDriverState *bs, uint64_t perm,
+                                  uint64_t shared, Error **errp)
+{
+    BDRVPreallocateState *s = bs->opaque;
+    if (!can_write_resize(perm) && s->data_end != -EINVAL) {
+        error_setg_errno(errp, EPERM, "Write access is required for truncate");
+        return -EPERM;
+    }
+    return 0;
+}
+
 static BlockDriver bdrv_preallocate_filter = {
     .format_name = "preallocate",
     .instance_size = sizeof(BDRVPreallocateState),
@@ -602,6 +613,7 @@  static BlockDriver bdrv_preallocate_filter = {
 
     .bdrv_set_perm = preallocate_set_perm,
     .bdrv_child_perm = preallocate_child_perm,
+    .bdrv_check_perm = preallocate_check_perm,
 
     .is_filter = true,
 };