Skip to content

Commit

Permalink
block: fix data type casting for crypto payload offset
Browse files Browse the repository at this point in the history
The crypto APIs report the offset of the data payload as an uint64_t
type, but the block driver is casting to size_t or ssize_t which will
potentially truncate.

Most of the block APIs use int64_t for offsets meanwhile, so even if
using uint64_t in the crypto block driver we are still at risk of
truncation.

Change the block crypto driver to use uint64_t, but add asserts that
the value is less than INT64_MAX.

Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-id: 20170927125340.12360-4-berrange@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
  • Loading branch information
berrange authored and XanClic committed Oct 6, 2017
1 parent 850f49d commit 3137655
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions block/crypto.c
Expand Up @@ -364,8 +364,9 @@ static int block_crypto_truncate(BlockDriverState *bs, int64_t offset,
PreallocMode prealloc, Error **errp)
{
BlockCrypto *crypto = bs->opaque;
size_t payload_offset =
uint64_t payload_offset =
qcrypto_block_get_payload_offset(crypto->block);
assert(payload_offset < (INT64_MAX - offset));

offset += payload_offset;

Expand Down Expand Up @@ -395,8 +396,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
uint8_t *cipher_data = NULL;
QEMUIOVector hd_qiov;
int ret = 0;
size_t payload_offset =
uint64_t payload_offset =
qcrypto_block_get_payload_offset(crypto->block) / 512;
assert(payload_offset < (INT64_MAX / 512));

qemu_iovec_init(&hd_qiov, qiov->niov);

Expand Down Expand Up @@ -462,8 +464,9 @@ block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num,
uint8_t *cipher_data = NULL;
QEMUIOVector hd_qiov;
int ret = 0;
size_t payload_offset =
uint64_t payload_offset =
qcrypto_block_get_payload_offset(crypto->block) / 512;
assert(payload_offset < (INT64_MAX / 512));

qemu_iovec_init(&hd_qiov, qiov->niov);

Expand Down Expand Up @@ -524,7 +527,9 @@ static int64_t block_crypto_getlength(BlockDriverState *bs)
BlockCrypto *crypto = bs->opaque;
int64_t len = bdrv_getlength(bs->file->bs);

ssize_t offset = qcrypto_block_get_payload_offset(crypto->block);
uint64_t offset = qcrypto_block_get_payload_offset(crypto->block);
assert(offset < INT64_MAX);
assert(offset < len);

len -= offset;

Expand Down

0 comments on commit 3137655

Please sign in to comment.