Skip to contentSkip to conversation

nbd/client: Reject inaccessible tail of inconsistent server

PATCH Vladimir Sementsov-Ogievskiy 2 participants 4 messages Apr 1, 2019 3 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Vladimir Sementsov-Ogievskiy

author Reviewed-by
#2 lore raw
Quoted 34 lines · Eric Blake

The NBD spec suggests that a server should never advertise a size inconsistent with its minimum block alignment, as that tail is effectively inaccessible to a compliant client obeying those block constraints. Since we have a habit of rounding up rather than truncating, to avoid losing the last few bytes of user input, and we cannot access the tail when the server advertises bogus block sizing, abort the connection to alert the server to fix their bug. And rejecting such servers matches what we already did for a min_block that was not a power of 2 or which was larger than max_block.

Does not impact either qemu (which always sends properly aligned sizes) or nbdkit (which does not send minimum block requirements yet); so this is mostly aimed at new NBD server implementations, and ensures that the rest of our code can assume the size is aligned.

Message-Id: <[email protected]>
---

This is the rewrite of the original 7/6 that I posted, retitled and hoisted earlier in the series (since Vladimir was right that 4/6 depends on it). I've added patches 1-3 to my NBD queue for a pull request on Monday as they now have R-b; I'd love to also have enough reviews for 3.5, 4, 5, 6, and also another thread on fixing 'qemu-img info' output, to include those as well.

  nbd/client.c | 8 ++++++++
  1 file changed, 8 insertions(+)
nbd/client.c +0 -0
index de7da48246b..9f5eb79930c 100644

nbd/client.c static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt, · line 431

Quoted 6 lines
nbd/client.c +3 -0
@@ -426,6 +426,14 @@ static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt,
                  nbd_send_opt_abort(ioc);
                  return -1;
              }
+            if (info->min_block &&
+                !QEMU_IS_ALIGNED(info->size, info->min_block)) {
+                error_setg(errp, "server size %" PRIu64 "is not multiple of "

s/server/export/ I think

nbd/client.c static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt, · line 439

Quoted 8 lines
nbd/client.c +5 -0
+                           "minimum block size %" PRIu32, info->size,
+                           info->min_block);
+                nbd_send_opt_abort(ioc);
+                return -1;
+            }
              trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
              break;
 
Signature · 2 lines
Best regards,
Vladimir

A message that is not in the archive (sent off-list or not yet received): [email protected]

author Reviewed-by
#4 lore raw

Re: [Qemu-devel] [PATCH v3 6.5/6] nbd/client: Trace server noncompliance on structured reads

Quoted 15 lines · Eric Blake

Just as we recently added a trace for a server sending block status that doesn't match the server's advertised minimum block alignment, let's do the same for read chunks. But since qemu 3.1 is such a server (because it advertised 512-byte alignment, but when serving a file that ends in data but is not sector-aligned, NBD_CMD_READ would detect a mid-sector change between data and hole at EOF and the resulting read chunks are unaligned), we don't want to change our behavior of otherwise tolerating unaligned reads.

Note that even though we fixed the server for 4.0 to advertise an actual block alignment (which gets rid of the unaligned reads at EOF for posix files), we can still trigger it via other means:

$ qemu-nbd --image-opts driver=blkdebug,align=512,image.driver=file,image.filename=/path/to/non-aligned-file
Quoted 7 lines

Arguably, that is a bug in the blkdebug block status function, for leaking a block status that is not aligned. It may also be possible to observe issues with a backing layer with smaller alignment than the active layer, although so far I have been unable to write a reliable iotest for that scenario.

Signature · 2 lines
Best regards,
Vladimir
#5 lore raw

Re: [Qemu-devel] [PATCH v3 6.5/6] nbd/client: Trace server noncompliance on structured reads

In reply to a message not in the archive
Quoted 13 lines · Eric Blake

Just as we recently added a trace for a server sending block status that doesn't match the server's advertised minimum block alignment, let's do the same for read chunks. But since qemu 3.1 is such a server (because it advertised 512-byte alignment, but when serving a file that ends in data but is not sector-aligned, NBD_CMD_READ would detect a mid-sector change between data and hole at EOF and the resulting read chunks are unaligned), we don't want to change our behavior of otherwise tolerating unaligned reads.

Note that even though we fixed the server for 4.0 to advertise an actual block alignment (which gets rid of the unaligned reads at EOF for posix files), we can still trigger it via other means:

Quoted 7 lines
$ qemu-nbd --image-opts driver=blkdebug,align=512,image.driver=file,image.filename=/path/to/non-aligned-file

Arguably, that is a bug in the blkdebug block status function, for leaking a block status that is not aligned. It may also be possible to observe issues with a backing layer with smaller alignment than the active layer, although so far I have been unable to write a reliable iotest for that scenario.

More details on why I can't write an iotest (yet) - I wanted to use blkdebug to let me expose an active image with a required alignment greater than that of a backing image, but sadly, the current implementation of blkdebug (and filters in general) reports block status ONLY for the active layer (it does not properly follow the backing chain, without Max's patches):

https://lists.gnu.org/archive/html/qemu-devel/2019-04/msg00221.html

But I _finally_ found a way around the fact that blkdebug doesn't (yet) do what I want - the qcow2 driver has a way to force alignment of 512, by using encryption. Thus, with this patch applied, I was able to set up a scenario that proves our server is still non-compliant (and I hope to have my patch for fixing it polished enough to send, soon, now that I can actually test my patch without one-off hacks to manually force the server into a fixed-size advertisement).

$ printf %01000d 0 > base
$ qemu-img create -f qcow2 -b base -F raw \
  --object=secret,id=s0,data=12345 \
  -o encrypt.format=luks,encrypt.key-secret=s0,encrypt.iter-time=10 \
  top.qcow2 16k
$ qemu-nbd --object secret,id=s0,data=12345 \
 --image-opts driver=qcow2,file.filename=top.qcow2,encrypt.key-secret=s0
$ # in second terminal
$ qemu-io --trace=nbd_\* -f raw nbd://localhost:10809 -c 'r 0 2k'
...
[email protected]:nbd_send_request Sending request to server: {
.from = 0, .len = 2048, .handle = 93824995142096, .flags = 0x0, .type =
0 (read) }
[email protected]:nbd_receive_structured_reply_chunk Got
structured reply chunk: { flags = 0x0, type = 1 (data), handle =
93824995142096, length = 1008 }
[email protected]:nbd_structured_read_compliance server sent
non-compliant unaligned read data chunk
[email protected]:nbd_receive_structured_reply_chunk Got
structured reply chunk: { flags = 0x1, type = 2 (hole), handle =
93824995142096, length = 12 }
[email protected]:nbd_structured_read_compliance server sent
non-compliant unaligned read hole chunk
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH for-4.0 v3 0/6] NBD server alignment improvement

In reply to a message not in the archive
Quoted 11 lines · Eric Blake

I'm tired of nbdkit being able to trigger an assertion failure in qemu-img convert. Let's get this into 4.0-rc2.

Since v2:
- add more patches, including a respin of a separately-posted nbd/client: Deal with unaligned size from server
- Don't make unaligned sector from server inaccessible; instead, round our requests down and handle the tail locally
- fix another issue reported by Rich where 'qemu-img map' output was less than stellar

Quoted 7 lines

Eric Blake (6):
iotests: Add 241 to test NBD on unaligned images nbd/client: Lower min_block for block-status, unaligned size nbd/client: Report offsets in bdrv_block_status nbd/client: Support qemu-img convert from unaligned size
block: Add bdrv_get_request_alignment() nbd/server: Advertise actual minimum block size

I've queued this series on my NBD tree, and will send a pull request shortly.

I have a couple more patches that I started work on that fix the final few alignment bugs, but as they have not been on the list yet, they may be too risky for 4.0; we'll see what happens based on how fast I can polish them.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org