mirror of
https://gitlab.com/qemu-project/qemu
synced 2024-09-06 18:05:37 +00:00
block/dmg: extract processing of resource forks
Besides the offset, also read the resource length. This length is now used in the extracted function to verify the end of the resource fork against "count" from the resource fork. Instead of relying on the value of offset to conclude whether the resource fork is available or not (info_begin==0), check the rsrc_fork_length instead. This would allow a dmg file to begin with a resource fork. This seemingly unnecessary restriction was found while trying to craft a DMG file by hand. Other changes: - Do not require resource data offset to be 0x100 (but check that it is within bounds though). - Further improve boundary checking (resource data must be within the resource fork). - Use correct value for resource data length (spotted by John Snow) - Consider the resource data offset when determining info_end. This fixes an EINVAL on the tuxpaint dmg example. The resource fork format is documented at https://developer.apple.com/legacy/library/documentation/mac/pdf/MoreMacintoshToolbox.pdf#page=151 Signed-off-by: Peter Wu <peter@lekensteyn.nl> Reviewed-by: John Snow <jsnow@redhat.com> Message-id: 1420566495-13284-4-git-send-email-peter@lekensteyn.nl Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
65a1c7c96a
commit
b0e8dc5d54
108
block/dmg.c
108
block/dmg.c
|
@ -285,13 +285,70 @@ fail:
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
|
||||
uint64_t info_begin, uint64_t info_length)
|
||||
{
|
||||
int ret;
|
||||
uint32_t count, rsrc_data_offset;
|
||||
uint64_t info_end;
|
||||
uint64_t offset;
|
||||
|
||||
/* read offset from begin of resource fork (info_begin) to resource data */
|
||||
ret = read_uint32(bs, info_begin, &rsrc_data_offset);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (rsrc_data_offset > info_length) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* read length of resource data */
|
||||
ret = read_uint32(bs, info_begin + 8, &count);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (count == 0 || rsrc_data_offset + count > info_length) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* begin of resource data (consisting of one or more resources) */
|
||||
offset = info_begin + rsrc_data_offset;
|
||||
|
||||
/* end of resource data (there is possibly a following resource map
|
||||
* which will be ignored). */
|
||||
info_end = offset + count;
|
||||
|
||||
/* read offsets (mish blocks) from one or more resources in resource data */
|
||||
while (offset < info_end) {
|
||||
/* size of following resource */
|
||||
ret = read_uint32(bs, offset, &count);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (count == 0) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
offset += 4;
|
||||
|
||||
ret = dmg_read_mish_block(bs, ds, offset, count);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
}
|
||||
/* advance offset by size of resource */
|
||||
offset += count;
|
||||
}
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
||||
Error **errp)
|
||||
{
|
||||
BDRVDMGState *s = bs->opaque;
|
||||
DmgHeaderState ds;
|
||||
uint64_t info_begin, info_end;
|
||||
uint32_t count, rsrc_data_offset;
|
||||
uint64_t rsrc_fork_offset, rsrc_fork_length;
|
||||
int64_t offset;
|
||||
int ret;
|
||||
|
||||
|
@ -311,53 +368,24 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
|||
goto fail;
|
||||
}
|
||||
|
||||
ret = read_uint64(bs, offset + 0x28, &info_begin);
|
||||
/* offset of resource fork (RsrcForkOffset) */
|
||||
ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (info_begin == 0) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = read_uint32(bs, info_begin, &rsrc_data_offset);
|
||||
ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (rsrc_data_offset != 0x100) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = read_uint32(bs, info_begin + 4, &count);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (count == 0) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
/* end of resource data, ignoring the following resource map */
|
||||
info_end = info_begin + count;
|
||||
|
||||
/* begin of resource data (consisting of one or more resources) */
|
||||
offset = info_begin + 0x100;
|
||||
|
||||
/* read offsets (mish blocks) from one or more resources in resource data */
|
||||
while (offset < info_end) {
|
||||
/* size of following resource */
|
||||
ret = read_uint32(bs, offset, &count);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
} else if (count == 0) {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
offset += 4;
|
||||
|
||||
ret = dmg_read_mish_block(bs, &ds, offset, count);
|
||||
if (rsrc_fork_length != 0) {
|
||||
ret = dmg_read_resource_fork(bs, &ds,
|
||||
rsrc_fork_offset, rsrc_fork_length);
|
||||
if (ret < 0) {
|
||||
goto fail;
|
||||
}
|
||||
/* advance offset by size of resource */
|
||||
offset += count;
|
||||
} else {
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* initialize zlib engine */
|
||||
|
|
Loading…
Reference in a new issue