crypto: Make block callbacks return 0 on success
They currently return the value of their headerlen/buflen parameter on success. Returning 0 instead makes it clear that short reads/writes are not possible. Signed-off-by: Alberto Faria <afaria@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20220609152744.3891847-5-afaria@redhat.com> Reviewed-by: Hanna Reitz <hreitz@redhat.com> Signed-off-by: Hanna Reitz <hreitz@redhat.com>
This commit is contained in:
parent
353a5d84b2
commit
757dda54b4
@ -55,12 +55,12 @@ static int block_crypto_probe_generic(QCryptoBlockFormat format,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t block_crypto_read_func(QCryptoBlock *block,
|
static int block_crypto_read_func(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
uint8_t *buf,
|
uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = opaque;
|
BlockDriverState *bs = opaque;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
@ -70,15 +70,15 @@ static ssize_t block_crypto_read_func(QCryptoBlock *block,
|
|||||||
error_setg_errno(errp, -ret, "Could not read encryption header");
|
error_setg_errno(errp, -ret, "Could not read encryption header");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t block_crypto_write_func(QCryptoBlock *block,
|
static int block_crypto_write_func(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
const uint8_t *buf,
|
const uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = opaque;
|
BlockDriverState *bs = opaque;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
@ -88,7 +88,7 @@ static ssize_t block_crypto_write_func(QCryptoBlock *block,
|
|||||||
error_setg_errno(errp, -ret, "Could not write encryption header");
|
error_setg_errno(errp, -ret, "Could not write encryption header");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -99,12 +99,12 @@ struct BlockCryptoCreateData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static ssize_t block_crypto_create_write_func(QCryptoBlock *block,
|
static int block_crypto_create_write_func(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
const uint8_t *buf,
|
const uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
struct BlockCryptoCreateData *data = opaque;
|
struct BlockCryptoCreateData *data = opaque;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
@ -114,13 +114,13 @@ static ssize_t block_crypto_create_write_func(QCryptoBlock *block,
|
|||||||
error_setg_errno(errp, -ret, "Could not write encryption header");
|
error_setg_errno(errp, -ret, "Could not write encryption header");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t block_crypto_create_init_func(QCryptoBlock *block,
|
static int block_crypto_create_init_func(QCryptoBlock *block,
|
||||||
size_t headerlen,
|
size_t headerlen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
struct BlockCryptoCreateData *data = opaque;
|
struct BlockCryptoCreateData *data = opaque;
|
||||||
Error *local_error = NULL;
|
Error *local_error = NULL;
|
||||||
@ -139,7 +139,7 @@ static ssize_t block_crypto_create_init_func(QCryptoBlock *block,
|
|||||||
data->prealloc, 0, &local_error);
|
data->prealloc, 0, &local_error);
|
||||||
|
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
@ -94,9 +94,9 @@ static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
|
static int qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
|
||||||
uint8_t *buf, size_t buflen,
|
uint8_t *buf, size_t buflen,
|
||||||
void *opaque, Error **errp)
|
void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = opaque;
|
BlockDriverState *bs = opaque;
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcow2State *s = bs->opaque;
|
||||||
@ -113,12 +113,12 @@ static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
|
|||||||
error_setg_errno(errp, -ret, "Could not read encryption header");
|
error_setg_errno(errp, -ret, "Could not read encryption header");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
|
static int qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
|
||||||
void *opaque, Error **errp)
|
void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = opaque;
|
BlockDriverState *bs = opaque;
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcow2State *s = bs->opaque;
|
||||||
@ -151,13 +151,13 @@ static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
|
static int qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
|
||||||
const uint8_t *buf, size_t buflen,
|
const uint8_t *buf, size_t buflen,
|
||||||
void *opaque, Error **errp)
|
void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs = opaque;
|
BlockDriverState *bs = opaque;
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcow2State *s = bs->opaque;
|
||||||
@ -174,7 +174,7 @@ static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
|
|||||||
error_setg_errno(errp, -ret, "Could not read encryption header");
|
error_setg_errno(errp, -ret, "Could not read encryption header");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QDict*
|
static QDict*
|
||||||
|
@ -495,7 +495,7 @@ qcrypto_block_luks_load_header(QCryptoBlock *block,
|
|||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
ssize_t rv;
|
int rv;
|
||||||
size_t i;
|
size_t i;
|
||||||
QCryptoBlockLUKS *luks = block->opaque;
|
QCryptoBlockLUKS *luks = block->opaque;
|
||||||
|
|
||||||
@ -856,7 +856,7 @@ qcrypto_block_luks_store_key(QCryptoBlock *block,
|
|||||||
QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
|
QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
|
||||||
splitkey, splitkeylen,
|
splitkey, splitkeylen,
|
||||||
opaque,
|
opaque,
|
||||||
errp) != splitkeylen) {
|
errp) < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -903,7 +903,7 @@ qcrypto_block_luks_load_key(QCryptoBlock *block,
|
|||||||
g_autofree uint8_t *splitkey = NULL;
|
g_autofree uint8_t *splitkey = NULL;
|
||||||
size_t splitkeylen;
|
size_t splitkeylen;
|
||||||
g_autofree uint8_t *possiblekey = NULL;
|
g_autofree uint8_t *possiblekey = NULL;
|
||||||
ssize_t rv;
|
int rv;
|
||||||
g_autoptr(QCryptoCipher) cipher = NULL;
|
g_autoptr(QCryptoCipher) cipher = NULL;
|
||||||
uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
|
uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
|
||||||
g_autoptr(QCryptoIVGen) ivgen = NULL;
|
g_autoptr(QCryptoIVGen) ivgen = NULL;
|
||||||
@ -1193,7 +1193,7 @@ qcrypto_block_luks_erase_key(QCryptoBlock *block,
|
|||||||
garbagesplitkey,
|
garbagesplitkey,
|
||||||
splitkeylen,
|
splitkeylen,
|
||||||
opaque,
|
opaque,
|
||||||
&local_err) != splitkeylen) {
|
&local_err) < 0) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
|
static int qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
|
||||||
size_t headerlen, void *opaque, Error **errp)
|
size_t headerlen, void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
size_t *headerlenp = opaque;
|
size_t *headerlenp = opaque;
|
||||||
@ -126,12 +126,12 @@ static ssize_t qcrypto_block_headerlen_hdr_init_func(QCryptoBlock *block,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t qcrypto_block_headerlen_hdr_write_func(QCryptoBlock *block,
|
static int qcrypto_block_headerlen_hdr_write_func(QCryptoBlock *block,
|
||||||
size_t offset, const uint8_t *buf, size_t buflen,
|
size_t offset, const uint8_t *buf, size_t buflen,
|
||||||
void *opaque, Error **errp)
|
void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
/* Discard the bytes, we're not actually writing to an image */
|
/* Discard the bytes, we're not actually writing to an image */
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,24 +29,24 @@ typedef struct QCryptoBlock QCryptoBlock;
|
|||||||
/* See also QCryptoBlockFormat, QCryptoBlockCreateOptions
|
/* See also QCryptoBlockFormat, QCryptoBlockCreateOptions
|
||||||
* and QCryptoBlockOpenOptions in qapi/crypto.json */
|
* and QCryptoBlockOpenOptions in qapi/crypto.json */
|
||||||
|
|
||||||
typedef ssize_t (*QCryptoBlockReadFunc)(QCryptoBlock *block,
|
typedef int (*QCryptoBlockReadFunc)(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
uint8_t *buf,
|
uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp);
|
Error **errp);
|
||||||
|
|
||||||
typedef ssize_t (*QCryptoBlockInitFunc)(QCryptoBlock *block,
|
typedef int (*QCryptoBlockInitFunc)(QCryptoBlock *block,
|
||||||
size_t headerlen,
|
size_t headerlen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp);
|
Error **errp);
|
||||||
|
|
||||||
typedef ssize_t (*QCryptoBlockWriteFunc)(QCryptoBlock *block,
|
typedef int (*QCryptoBlockWriteFunc)(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
const uint8_t *buf,
|
const uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp);
|
Error **errp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* qcrypto_block_has_format:
|
* qcrypto_block_has_format:
|
||||||
|
@ -188,12 +188,12 @@ static struct QCryptoBlockTestData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static ssize_t test_block_read_func(QCryptoBlock *block,
|
static int test_block_read_func(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
uint8_t *buf,
|
uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
Buffer *header = opaque;
|
Buffer *header = opaque;
|
||||||
|
|
||||||
@ -201,14 +201,14 @@ static ssize_t test_block_read_func(QCryptoBlock *block,
|
|||||||
|
|
||||||
memcpy(buf, header->buffer + offset, buflen);
|
memcpy(buf, header->buffer + offset, buflen);
|
||||||
|
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t test_block_init_func(QCryptoBlock *block,
|
static int test_block_init_func(QCryptoBlock *block,
|
||||||
size_t headerlen,
|
size_t headerlen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
Buffer *header = opaque;
|
Buffer *header = opaque;
|
||||||
|
|
||||||
@ -216,16 +216,16 @@ static ssize_t test_block_init_func(QCryptoBlock *block,
|
|||||||
|
|
||||||
buffer_reserve(header, headerlen);
|
buffer_reserve(header, headerlen);
|
||||||
|
|
||||||
return headerlen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static ssize_t test_block_write_func(QCryptoBlock *block,
|
static int test_block_write_func(QCryptoBlock *block,
|
||||||
size_t offset,
|
size_t offset,
|
||||||
const uint8_t *buf,
|
const uint8_t *buf,
|
||||||
size_t buflen,
|
size_t buflen,
|
||||||
void *opaque,
|
void *opaque,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
Buffer *header = opaque;
|
Buffer *header = opaque;
|
||||||
|
|
||||||
@ -234,7 +234,7 @@ static ssize_t test_block_write_func(QCryptoBlock *block,
|
|||||||
memcpy(header->buffer + offset, buf, buflen);
|
memcpy(header->buffer + offset, buf, buflen);
|
||||||
header->offset = offset + buflen;
|
header->offset = offset + buflen;
|
||||||
|
|
||||||
return buflen;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user