[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 27/42] block/dmg: fix sector data offset calculation
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 27/42] block/dmg: fix sector data offset calculation |
Date: |
Fri, 6 Feb 2015 17:40:34 +0100 |
From: Peter Wu <address@hidden>
This patch addresses two issues:
- The data fork offset was not taken into account, resulting in failure
to read an InstallESD.dmg file (5164763151 bytes) which had a
non-zero DataForkOffset field.
- The offset of the previous block ("partition") was unconditionally
added to the current block because older files would start the input
offset of a new block at zero. Newer files (including vlc-2.1.5.dmg,
tuxpaint-0.9.15-macosx.dmg and OS X Yosemite [MAS].dmg) failed in
reads because these files have chunk offsets, relative to the begin
of a data fork.
Now the data offset of the mish is taken into account. While we could
check that the data_offset is within the data fork, let's not do that
here as it would only result in parse failures on invalid files (rather
than gracefully handling such bad files). dmg_read will error out if
the offset is incorrect.
Signed-off-by: Peter Wu <address@hidden>
Reviewed-by: John Snow <address@hidden>
Message-id: address@hidden
Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/dmg.c | 26 ++++++++++++++++++++------
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git a/block/dmg.c b/block/dmg.c
index a33c131..34dc632 100644
--- a/block/dmg.c
+++ b/block/dmg.c
@@ -186,7 +186,7 @@ static int64_t dmg_find_koly_offset(BlockDriverState
*file_bs, Error **errp)
typedef struct DmgHeaderState {
/* used internally by dmg_read_mish_block to remember offsets of blocks
* across calls */
- uint64_t last_in_offset;
+ uint64_t data_fork_offset;
uint64_t last_out_offset;
/* exported for dmg_open */
uint32_t max_compressed_size;
@@ -201,6 +201,8 @@ static int dmg_read_mish_block(BDRVDMGState *s,
DmgHeaderState *ds,
size_t new_size;
uint32_t chunk_count;
int64_t offset = 0;
+ uint64_t data_offset;
+ uint64_t in_offset = ds->data_fork_offset;
type = buff_read_uint32(buffer, offset);
/* skip data that is not a valid MISH block (invalid magic or too small) */
@@ -209,8 +211,12 @@ static int dmg_read_mish_block(BDRVDMGState *s,
DmgHeaderState *ds,
return 0;
}
- offset += 4;
- offset += 200;
+ /* location in data fork for (compressed) blob (in bytes) */
+ data_offset = buff_read_uint64(buffer, offset + 0x18);
+ in_offset += data_offset;
+
+ /* move to begin of chunk entries */
+ offset += 204;
chunk_count = (count - 204) / 40;
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
@@ -226,7 +232,6 @@ static int dmg_read_mish_block(BDRVDMGState *s,
DmgHeaderState *ds,
if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
s->types[i] != 2) {
if (s->types[i] == 0xffffffff && i > 0) {
- ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
ds->last_out_offset = s->sectors[i - 1] +
s->sectorcounts[i - 1];
}
@@ -253,7 +258,7 @@ static int dmg_read_mish_block(BDRVDMGState *s,
DmgHeaderState *ds,
}
s->offsets[i] = buff_read_uint64(buffer, offset);
- s->offsets[i] += ds->last_in_offset;
+ s->offsets[i] += in_offset;
offset += 8;
s->lengths[i] = buff_read_uint64(buffer, offset);
@@ -412,7 +417,7 @@ static int dmg_open(BlockDriverState *bs, QDict *options,
int flags,
s->n_chunks = 0;
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
/* used by dmg_read_mish_block to keep track of the current I/O position */
- ds.last_in_offset = 0;
+ ds.data_fork_offset = 0;
ds.last_out_offset = 0;
ds.max_compressed_size = 1;
ds.max_sectors_per_chunk = 1;
@@ -424,6 +429,15 @@ static int dmg_open(BlockDriverState *bs, QDict *options,
int flags,
goto fail;
}
+ /* offset of data fork (DataForkOffset) */
+ ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
+ if (ret < 0) {
+ goto fail;
+ } else if (ds.data_fork_offset > offset) {
+ ret = -EINVAL;
+ goto fail;
+ }
+
/* offset of resource fork (RsrcForkOffset) */
ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
if (ret < 0) {
--
1.8.3.1
- [Qemu-devel] [PULL 20/42] block/dmg: properly detect the UDIF trailer, (continued)
- [Qemu-devel] [PULL 20/42] block/dmg: properly detect the UDIF trailer, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 16/42] virtio-blk: add a knob to disable request merging, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 22/42] block/dmg: extract processing of resource forks, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 25/42] block/dmg: process XML plists, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 19/42] block: add event when disk usage exceeds threshold, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 26/42] block/dmg: set virtual size to a non-zero value, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 21/42] block/dmg: extract mish block decoding functionality, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 29/42] block/dmg: factor out block type check, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 24/42] block/dmg: validate chunk size to avoid overflow, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 23/42] block/dmg: process a buffer instead of reading ints, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 27/42] block/dmg: fix sector data offset calculation,
Kevin Wolf <=
- [Qemu-devel] [PULL 28/42] block/dmg: use SectorNumber from BLKX header, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 31/42] block/dmg: improve zeroes handling, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 32/42] qed: check for header size overflow, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 34/42] block: fix off-by-one error in qcow and qcow2, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 37/42] iotests: Fix 104 for NBD, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 30/42] block/dmg: support bzip2 block entry types, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 33/42] qemu-iotests: add 116 invalid QED input file tests, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 38/42] nbd: Improve error messages, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 40/42] nbd: fix max_discard/max_transfer_length, Kevin Wolf, 2015/02/06
- [Qemu-devel] [PULL 35/42] iotests: Fix 083, Kevin Wolf, 2015/02/06