block/qcow*: Don't take address of fields in packed structs
cover block/qcow*: Don't take address of fields in packed structs
Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places in the affected files where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
Patches produced mechanically using spatch; in one case I also did a little hand-editing to wrap overlong lines that checkpatch would otherwise complain about.
(clang also complains about other files in block: vdi.c, vpc.c, vhdx.h, vhdx.c, vhdx-endian.c, vhdx-log.c -- I may produce patches for those later if nobody else gets there first.)
thanks
-- PMM
Peter Maydell (3):
block/qcow2: Don't take address of fields in packed structs block/qcow: Don't take address of fields in packed structs block/qcow2-bitmap: Don't take address of fields in packed structs
block/qcow.c | 18 ++++++------- block/qcow2-bitmap.c | 24 ++++++++--------- block/qcow2.c | 64 +++++++++++++++++++++++--------------------- 3 files changed, 55 insertions(+), 51 deletions(-)
Signature · 1 line
2.19.0
PATCH 1/3 block/qcow2: Don't take address of fields in packed structs
Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
This patch was produced with the following spatch script (and hand-editing to fold a few resulting overlength lines):
@@
expression E;
@@
-be16_to_cpus(&E);
+E = be16_to_cpu(E);
@@
expression E;
@@
-be32_to_cpus(&E);
+E = be32_to_cpu(E);
@@
expression E;
@@
-be64_to_cpus(&E);
+E = be64_to_cpu(E);
@@
expression E;
@@
-cpu_to_be16s(&E);
+E = cpu_to_be16(E);
@@
expression E;
@@
-cpu_to_be32s(&E);
+E = cpu_to_be32(E);
@@
expression E;
@@
-cpu_to_be64s(&E);
+E = cpu_to_be64(E);
--- block/qcow2.c | 64 +++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 30 deletions(-)
block/qcow2.c +34 -30
@@ -210,8 +210,8 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, "pread fail from offset %" PRIu64, offset);return 1 ; } -be32_to_cpus (&ext.magic); -be32_to_cpus (&ext.len); + ext.magic =be32_to_cpu (ext.magic); + ext.len =be32_to_cpu (ext.len); offset +=sizeof (ext);#ifdef DEBUG_EXT printf ("ext.magic = 0x%x\n" , ext.magic);
@@ -279,8 +279,8 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, "Unable to read CRYPTO header extension");return ret; } -be64_to_cpus (&s->crypto_header.offset); -be64_to_cpus (&s->crypto_header.length); + s->crypto_header.offset =be64_to_cpu (s->crypto_header.offset); + s->crypto_header.length =be64_to_cpu (s->crypto_header.length);if ((s->crypto_header.offset % s->cluster_size) !=0 ) { error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
@@ -342,9 +342,11 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,return -EINVAL ; } -be32_to_cpus (&bitmaps_ext.nb_bitmaps); -be64_to_cpus (&bitmaps_ext.bitmap_directory_size); -be64_to_cpus (&bitmaps_ext.bitmap_directory_offset); + bitmaps_ext.nb_bitmaps =be32_to_cpu (bitmaps_ext.nb_bitmaps); + bitmaps_ext.bitmap_directory_size = +be64_to_cpu (bitmaps_ext.bitmap_directory_size); + bitmaps_ext.bitmap_directory_offset = +be64_to_cpu (bitmaps_ext.bitmap_directory_offset);if (bitmaps_ext.nb_bitmaps >QCOW2_MAX_BITMAPS ) { error_setg(errp,
@@ -1160,19 +1162,20 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,error_setg_errno (errp, -ret,"Could not read qcow2 header" ); goto fail; } -be32_to_cpus (&header.magic); -be32_to_cpus (&header.version); -be64_to_cpus (&header.backing_file_offset); -be32_to_cpus (&header.backing_file_size); -be64_to_cpus (&header.size); -be32_to_cpus (&header.cluster_bits); -be32_to_cpus (&header.crypt_method); -be64_to_cpus (&header.l1_table_offset); -be32_to_cpus (&header.l1_size); -be64_to_cpus (&header.refcount_table_offset); -be32_to_cpus (&header.refcount_table_clusters); -be64_to_cpus (&header.snapshots_offset); -be32_to_cpus (&header.nb_snapshots); + header.magic =be32_to_cpu (header.magic); + header.version =be32_to_cpu (header.version); + header.backing_file_offset =be64_to_cpu (header.backing_file_offset); + header.backing_file_size =be32_to_cpu (header.backing_file_size); + header.size =be64_to_cpu (header.size); + header.cluster_bits =be32_to_cpu (header.cluster_bits); + header.crypt_method =be32_to_cpu (header.crypt_method); + header.l1_table_offset =be64_to_cpu (header.l1_table_offset); + header.l1_size =be32_to_cpu (header.l1_size); + header.refcount_table_offset =be64_to_cpu (header.refcount_table_offset); + header.refcount_table_clusters = +be32_to_cpu (header.refcount_table_clusters); + header.snapshots_offset =be64_to_cpu (header.snapshots_offset); + header.nb_snapshots =be32_to_cpu (header.nb_snapshots);if (header.magic !=QCOW_MAGIC ) {error_setg (errp,"Image is not in qcow2 format" );
@@ -1208,11 +1211,12 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, header.refcount_order =4 ; header.header_length =72 ; }else { -be64_to_cpus (&header.incompatible_features); -be64_to_cpus (&header.compatible_features); -be64_to_cpus (&header.autoclear_features); -be32_to_cpus (&header.refcount_order); -be32_to_cpus (&header.header_length); + header.incompatible_features = +be64_to_cpu (header.incompatible_features); + header.compatible_features =be64_to_cpu (header.compatible_features); + header.autoclear_features =be64_to_cpu (header.autoclear_features); + header.refcount_order =be32_to_cpu (header.refcount_order); + header.header_length =be32_to_cpu (header.header_length);if (header.header_length <104 ) {error_setg (errp,"qcow2 header too short" );
@@ -1401,7 +1405,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, goto fail; }for (i =0 ;i < s->l1_size; i++) { -be64_to_cpus (&s->l1_table[i]); + s->l1_table[i] =be64_to_cpu (s->l1_table[i]); } }
@@ -2346,13 +2350,13 @@ int qcow2_update_header(BlockDriverState *bs)/* Full disk encryption header pointer extension */ if (s->crypto_header.offset !=0 ) { -cpu_to_be64s (&s->crypto_header.offset); -cpu_to_be64s (&s->crypto_header.length); + s->crypto_header.offset =cpu_to_be64 (s->crypto_header.offset); + s->crypto_header.length =cpu_to_be64 (s->crypto_header.length); ret =header_ext_add (buf,QCOW2_EXT_MAGIC_CRYPTO_HEADER , &s->crypto_header,sizeof (s->crypto_header), buflen); -be64_to_cpus (&s->crypto_header.offset); -be64_to_cpus (&s->crypto_header.length); + s->crypto_header.offset =be64_to_cpu (s->crypto_header.offset); + s->crypto_header.length =be64_to_cpu (s->crypto_header.length);if (ret <0 ) { goto fail; }
Signature · 1 line
2.19.0
PATCH 2/3 block/qcow: Don't take address of fields in packed structs
In reply to Peter MaydellTaking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
This patch was produced with the following spatch script:
@@
expression E;
@@
-be16_to_cpus(&E);
+E = be16_to_cpu(E);
@@
expression E;
@@
-be32_to_cpus(&E);
+E = be32_to_cpu(E);
@@
expression E;
@@
-be64_to_cpus(&E);
+E = be64_to_cpu(E);
@@
expression E;
@@
-cpu_to_be16s(&E);
+E = cpu_to_be16(E);
@@
expression E;
@@
-cpu_to_be32s(&E);
+E = cpu_to_be32(E);
@@
expression E;
@@
-cpu_to_be64s(&E);
+E = cpu_to_be64(E);
--- block/qcow.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
block/qcow.c +9 -9
@@ -140,14 +140,14 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,if (ret <0 ) { goto fail; } -be32_to_cpus (&header.magic); -be32_to_cpus (&header.version); -be64_to_cpus (&header.backing_file_offset); -be32_to_cpus (&header.backing_file_size); -be32_to_cpus (&header.mtime); -be64_to_cpus (&header.size); -be32_to_cpus (&header.crypt_method); -be64_to_cpus (&header.l1_table_offset); + header.magic =be32_to_cpu (header.magic); + header.version =be32_to_cpu (header.version); + header.backing_file_offset =be64_to_cpu (header.backing_file_offset); + header.backing_file_size =be32_to_cpu (header.backing_file_size); + header.mtime =be32_to_cpu (header.mtime); + header.size =be64_to_cpu (header.size); + header.crypt_method =be32_to_cpu (header.crypt_method); + header.l1_table_offset =be64_to_cpu (header.l1_table_offset);if (header.magic !=QCOW_MAGIC ) {error_setg (errp,"Image not in qcow format" );
@@ -270,7 +270,7 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, }for (i =0 ;i < s->l1_size; i++) { -be64_to_cpus (&s->l1_table[i]); + s->l1_table[i] =be64_to_cpu (s->l1_table[i]); }/* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
Signature · 1 line
2.19.0
PATCH 3/3 block/qcow2-bitmap: Don't take address of fields in packed structs
In reply to Peter MaydellTaking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
This patch was produced with the following spatch script:
@@
expression E;
@@
-be16_to_cpus(&E);
+E = be16_to_cpu(E);
@@
expression E;
@@
-be32_to_cpus(&E);
+E = be32_to_cpu(E);
@@
expression E;
@@
-be64_to_cpus(&E);
+E = be64_to_cpu(E);
@@
expression E;
@@
-cpu_to_be16s(&E);
+E = cpu_to_be16(E);
@@
expression E;
@@
-cpu_to_be32s(&E);
+E = cpu_to_be32(E);
@@
expression E;
@@
-cpu_to_be64s(&E);
+E = cpu_to_be64(E);
--- block/qcow2-bitmap.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
block/qcow2-bitmap.c +12 -12
@@ -118,7 +118,7 @@ static inline void bitmap_table_to_be(uint64_t *bitmap_table, size_t size)size_t i;for (i =0 ; i < size; ++i) { -cpu_to_be64s (&bitmap_table[i]); + bitmap_table[i] =cpu_to_be64 (bitmap_table[i]); } }
@@ -231,7 +231,7 @@ static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb, }for (i =0 ; i < tb->size; ++i) { -be64_to_cpus (&table[i]); + table[i] =be64_to_cpu (table[i]); ret =check_table_entry (table[i], s->cluster_size);if (ret <0 ) { goto fail;
@@ -394,20 +394,20 @@ fail:static inline void bitmap_dir_entry_to_cpu (Qcow2BitmapDirEntry *entry) { -be64_to_cpus (&entry->bitmap_table_offset); -be32_to_cpus (&entry->bitmap_table_size); -be32_to_cpus (&entry->flags); -be16_to_cpus (&entry->name_size); -be32_to_cpus (&entry->extra_data_size); + entry->bitmap_table_offset =be64_to_cpu (entry->bitmap_table_offset); + entry->bitmap_table_size =be32_to_cpu (entry->bitmap_table_size); + entry->flags =be32_to_cpu (entry->flags); + entry->name_size =be16_to_cpu (entry->name_size); + entry->extra_data_size =be32_to_cpu (entry->extra_data_size); }static inline void bitmap_dir_entry_to_be (Qcow2BitmapDirEntry *entry) { -cpu_to_be64s (&entry->bitmap_table_offset); -cpu_to_be32s (&entry->bitmap_table_size); -cpu_to_be32s (&entry->flags); -cpu_to_be16s (&entry->name_size); -cpu_to_be32s (&entry->extra_data_size); + entry->bitmap_table_offset =cpu_to_be64 (entry->bitmap_table_offset); + entry->bitmap_table_size =cpu_to_be32 (entry->bitmap_table_size); + entry->flags =cpu_to_be32 (entry->flags); + entry->name_size =cpu_to_be16 (entry->name_size); + entry->extra_data_size =cpu_to_be32 (entry->extra_data_size); }static inline int calc_dir_entry_size (size_t name_size,size_t extra_data_size)
Signature · 1 line
2.19.0
Quoted 4 lines · Peter Maydell
Peter Maydell (3):
block/qcow2: Don't take address of fields in packed structs block/qcow: Don't take address of fields in packed structsblock/qcow2-bitmap: Don't take address of fields in packed structs
Although I do think you should put the script you used into scripts/coccinelle/, so that it may be used for the rest of the files in the tree. We can delete the script after those pointer-based functions are removed.
r~
Quoted 26 lines · Peter Maydell
Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places in the affected files where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
Patches produced mechanically using spatch; in one case I also did a little hand-editing to wrap overlong lines that checkpatch would otherwise complain about.
(clang also complains about other files in block:
vdi.c,vpc.c,vhdx.h,vhdx.c,vhdx-endian.c,vhdx-log.c-- I may produce patches for those later if nobody else gets there first.)thanks
-- PMMPeter Maydell (3):
block/qcow2: Don't take address of fields in packed structs block/qcow: Don't take address of fields in packed structsblock/qcow2-bitmap: Don't take address of fields in packed structs
Quoted 5 lines
block/qcow.c | 18 ++++++------- block/qcow2-bitmap.c | 24 ++++++++--------- block/qcow2.c | 64 +++++++++++++++++++++++--------------------- 3 files changed, 55 insertions(+), 51 deletions(-)
Kevin Wolf
AppliedAm 09.10.2018 um 19:24 hat Peter Maydell geschrieben:
Quoted 18 lines
Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places in the affected files where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
Patches produced mechanically using spatch; in one case I also did a little hand-editing to wrap overlong lines that checkpatch would otherwise complain about.
(clang also complains about other files in block:
vdi.c,vpc.c,vhdx.h,vhdx.c,vhdx-endian.c,vhdx-log.c-- I may produce patches for those later if nobody else gets there first.)
Quoted 7 lines
thanks
-- PMMPeter Maydell (3):
block/qcow2: Don't take address of fields in packed structs block/qcow: Don't take address of fields in packed structsblock/qcow2-bitmap: Don't take address of fields in packed structs
Thanks, applied to the block branch.
Kevin
Quoted 22 lines · Kevin Wolf
Am 09.10.2018 um 19:24 hat Peter Maydell geschrieben:
Quoted 21 lines
Taking the address of a field in a packed struct is a bad idea, because it might not be actually aligned enough for that pointer type (and thus cause a crash on dereference on some host architectures). Newer versions of clang warn about this. Avoid the bug by not using the "modify in place" byte swapping functions.
There are a few places in the affected files where the in-place swap function is used on something other than a packed struct field; we convert those anyway, for consistency.
Patches produced mechanically using spatch; in one case I also did a little hand-editing to wrap overlong lines that checkpatch would otherwise complain about.
(clang also complains about other files in block:
vdi.c,vpc.c,vhdx.h,vhdx.c,vhdx-endian.c,vhdx-log.c-- I may produce patches for those later if nobody else gets there first.)thanks
-- PMM
Quoted 6 lines
Quoted 4 lines
Peter Maydell (3):
block/qcow2: Don't take address of fields in packed structs block/qcow: Don't take address of fields in packed structsblock/qcow2-bitmap: Don't take address of fields in packed structsThanks, applied to the block branch.
Ping? This doesn't seem to have made it into master, unless I've missed it...
thanks
-- PMM