Merge remote-tracking branch 'stefanha/block' into staging

# By Kevin Wolf
# Via Stefan Hajnoczi
* stefanha/block:
  block: Add hint to -EFBIG error message
  qcow2: Catch some L1 table index overflows

Message-id: 1368543269-29784-1-git-send-email-stefanha@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Anthony Liguori 2013-05-14 10:24:37 -05:00
commit 110db9b48c
4 changed files with 35 additions and 14 deletions

View File

@ -4857,8 +4857,12 @@ void bdrv_img_create(const char *filename, const char *fmt,
error_setg(errp,"Formatting or formatting option not supported for " error_setg(errp,"Formatting or formatting option not supported for "
"file format '%s'", fmt); "file format '%s'", fmt);
} else if (ret == -EFBIG) { } else if (ret == -EFBIG) {
error_setg(errp, "The image size is too large for file format '%s'", const char *cluster_size_hint = "";
fmt); if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
cluster_size_hint = " (try using a larger cluster size)";
}
error_setg(errp, "The image size is too large for file format '%s'%s",
fmt, cluster_size_hint);
} else { } else {
error_setg(errp, "%s: error while creating %s: %s", filename, fmt, error_setg(errp, "%s: error while creating %s: %s", filename, fmt,
strerror(-ret)); strerror(-ret));

View File

@ -29,12 +29,13 @@
#include "block/qcow2.h" #include "block/qcow2.h"
#include "trace.h" #include "trace.h"
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size) int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
bool exact_size)
{ {
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
int new_l1_size, new_l1_size2, ret, i; int new_l1_size2, ret, i;
uint64_t *new_l1_table; uint64_t *new_l1_table;
int64_t new_l1_table_offset; int64_t new_l1_table_offset, new_l1_size;
uint8_t data[12]; uint8_t data[12];
if (min_size <= s->l1_size) if (min_size <= s->l1_size)
@ -53,8 +54,13 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size)
} }
} }
if (new_l1_size > INT_MAX) {
return -EFBIG;
}
#ifdef DEBUG_ALLOC2 #ifdef DEBUG_ALLOC2
fprintf(stderr, "grow l1_table from %d to %d\n", s->l1_size, new_l1_size); fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
s->l1_size, new_l1_size);
#endif #endif
new_l1_size2 = sizeof(uint64_t) * new_l1_size; new_l1_size2 = sizeof(uint64_t) * new_l1_size;
@ -391,8 +397,8 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
int *num, uint64_t *cluster_offset) int *num, uint64_t *cluster_offset)
{ {
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
unsigned int l1_index, l2_index; unsigned int l2_index;
uint64_t l2_offset, *l2_table; uint64_t l1_index, l2_offset, *l2_table;
int l1_bits, c; int l1_bits, c;
unsigned int index_in_cluster, nb_clusters; unsigned int index_in_cluster, nb_clusters;
uint64_t nb_available, nb_needed; uint64_t nb_available, nb_needed;
@ -507,8 +513,8 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
int *new_l2_index) int *new_l2_index)
{ {
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
unsigned int l1_index, l2_index; unsigned int l2_index;
uint64_t l2_offset; uint64_t l1_index, l2_offset;
uint64_t *l2_table = NULL; uint64_t *l2_table = NULL;
int ret; int ret;
@ -522,6 +528,7 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
} }
} }
assert(l1_index < s->l1_size);
l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
/* seek the l2 table of the given l2 offset */ /* seek the l2 table of the given l2 offset */

View File

@ -307,6 +307,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
QemuOpts *opts; QemuOpts *opts;
Error *local_err = NULL; Error *local_err = NULL;
uint64_t ext_end; uint64_t ext_end;
uint64_t l1_vm_state_index;
ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
if (ret < 0) { if (ret < 0) {
@ -424,7 +425,14 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
/* read the level 1 table */ /* read the level 1 table */
s->l1_size = header.l1_size; s->l1_size = header.l1_size;
s->l1_vm_state_index = size_to_l1(s, header.size);
l1_vm_state_index = size_to_l1(s, header.size);
if (l1_vm_state_index > INT_MAX) {
ret = -EFBIG;
goto fail;
}
s->l1_vm_state_index = l1_vm_state_index;
/* the L1 table must contain at least enough entries to put /* the L1 table must contain at least enough entries to put
header.size bytes */ header.size bytes */
if (s->l1_size < s->l1_vm_state_index) { if (s->l1_size < s->l1_vm_state_index) {
@ -1480,7 +1488,8 @@ static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
static int qcow2_truncate(BlockDriverState *bs, int64_t offset) static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
{ {
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
int ret, new_l1_size; int64_t new_l1_size;
int ret;
if (offset & 511) { if (offset & 511) {
error_report("The new size must be a multiple of 512"); error_report("The new size must be a multiple of 512");

View File

@ -284,7 +284,7 @@ static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
return (size + (s->cluster_size - 1)) >> s->cluster_bits; return (size + (s->cluster_size - 1)) >> s->cluster_bits;
} }
static inline int size_to_l1(BDRVQcowState *s, int64_t size) static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
{ {
int shift = s->cluster_bits + s->l2_bits; int shift = s->cluster_bits + s->l2_bits;
return (size + (1ULL << shift) - 1) >> shift; return (size + (1ULL << shift) - 1) >> shift;
@ -360,7 +360,8 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
BdrvCheckMode fix); BdrvCheckMode fix);
/* qcow2-cluster.c functions */ /* qcow2-cluster.c functions */
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
bool exact_size);
void qcow2_l2_cache_reset(BlockDriverState *bs); void qcow2_l2_cache_reset(BlockDriverState *bs);
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,