[PATCH] nbd: Avoid off-by-one in long export name truncation

Eric Blake posted 1 patch 3 years, 10 months ago
Test FreeBSD passed
Test asan passed
Test docker-quick@centos7 passed
Test checkpatch passed
Test docker-mingw@fedora passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20200622210355.414941-1-eblake@redhat.com
Maintainers: Max Reitz <mreitz@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Eric Blake <eblake@redhat.com>
block/nbd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] nbd: Avoid off-by-one in long export name truncation
Posted by Eric Blake 3 years, 10 months ago
When snprintf returns the same value as the buffer size, the final
byte was truncated to ensure a NUL terminator.  Fortunately, such long
export names are unusual enough, with no real impact other than what
is displayed to the user.

Fixes: 5c86bdf12089
Reported-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/nbd.c b/block/nbd.c
index eed160c5cda1..8301fcac0589 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -2009,7 +2009,7 @@ static void nbd_refresh_filename(BlockDriverState *bs)
         len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
                        "nbd://%s:%s", host, port);
     }
-    if (len > sizeof(bs->exact_filename)) {
+    if (len >= sizeof(bs->exact_filename)) {
         /* Name is too long to represent exactly, so leave it empty. */
         bs->exact_filename[0] = '\0';
     }
-- 
2.27.0


Re: [PATCH] nbd: Avoid off-by-one in long export name truncation
Posted by Vladimir Sementsov-Ogievskiy 3 years, 10 months ago
23.06.2020 00:03, Eric Blake wrote:
> When snprintf returns the same value as the buffer size, the final
> byte was truncated to ensure a NUL terminator.  Fortunately, such long
> export names are unusual enough, with no real impact other than what
> is displayed to the user.
> 
> Fixes: 5c86bdf12089

Oh, I remember myself checking snprintf spec when reviewing this, but
still I missed this thing :(

> Reported-by: Max Reitz <mreitz@redhat.com>
> Signed-off-by: Eric Blake <eblake@redhat.com>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

> ---
>   block/nbd.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/block/nbd.c b/block/nbd.c
> index eed160c5cda1..8301fcac0589 100644
> --- a/block/nbd.c
> +++ b/block/nbd.c
> @@ -2009,7 +2009,7 @@ static void nbd_refresh_filename(BlockDriverState *bs)
>           len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
>                          "nbd://%s:%s", host, port);
>       }
> -    if (len > sizeof(bs->exact_filename)) {
> +    if (len >= sizeof(bs->exact_filename)) {
>           /* Name is too long to represent exactly, so leave it empty. */
>           bs->exact_filename[0] = '\0';
>       }
> 

Hmm, interesting, that in case of len=0 (no one branch was executed from previous
if-else-if, which seems possible) we do nothing. Shouldn't this if actually be
if (!len || len >= sizeof(bs->exact_filename)) {
   /* Name is too long or we have nothing to report */
   bs->exact_filename[0] = '\0';
}

Probably not, as bdrv_refresh_filename() does it anyway prior to calling drv->bdrv_refresh_filename().


-- 
Best regards,
Vladimir