hw/net_rocker_of_dpa: two bugfixes
cover hw/net_rocker_of_dpa: two bugfixes
These patches fix two bugs: a NULL pointer dereference if the guest does something wrong (reported a few years back as issue #1851), and a sanitizer complaint about unaligned accesses that I noticed while reproducing the first bug.
thanks
-- PMM
Peter Maydell (2):
hw/net/rocker_of_dpa: Check group ID pointers are not NULL hw/net/rocker_of_dpa: Avoid unaligned accesses in _of_dpa_flow_match()
hw/net/rocker/rocker_of_dpa.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 hw/net/rocker_of_dpa: Check group ID pointers are not NULL
In of_dpa_cmd_add_l2_flood(), we use rocker_tlv_parse_nested() to fill in a tlvs[] array. If the guest command is valid then the entries should be pointers to TLV data items with group IDs. However, if the guest gives us bogus data then rocker_tlv_parse_nested() indicates this by leaving the tlvs[] entries NULL. In the other places that use this function, we check for this before using the value, but here we forgot, and the result is that QEMU can crash:
#0 __memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:331
#1 0x00005555574f7137 in __asan_memcpy ()
#2 0x0000555558106792 in ldl_he_p (ptr=0x8) at /home/pm215/qemu/include/qemu/bswap.h:278
#3 0x0000555558106755 in ldl_le_p (ptr=0x8) at /home/pm215/qemu/include/qemu/bswap.h:311
#4 0x00005555580f85ed in rocker_tlv_get_le32 (tlv=0x0) at ../../hw/net/rocker/rocker_tlv.h:114
#5 0x000055555810a8ad in of_dpa_cmd_add_l2_flood (of_dpa=0x506000082e38, group=0x503000b4e440, group_tlvs=0x7fff68702c20)
at ../../hw/net/rocker/rocker_of_dpa.c:2032
#6 0x0000555558108a74 in of_dpa_cmd_group_do (of_dpa=0x506000082e38, group_id=1073741824, group=0x503000b4e440, group_tlvs=0x7fff68702c20)
at ../../hw/net/rocker/rocker_of_dpa.c:2115
#7 0x0000555558108730 in of_dpa_cmd_group_add (of_dpa=0x506000082e38, group_id=1073741824, group_tlvs=0x7fff68702c20)
at ../../hw/net/rocker/rocker_of_dpa.c:2135
#8 0x00005555580f66ec in of_dpa_group_cmd
(of_dpa=0x506000082e38, info=0x514000072e40, buf=0x5070002356c0 "\001", cmd=7, group_tlvs=0x7fff68702c20)
at ../../hw/net/rocker/rocker_of_dpa.c:2194Check for NULL values and return an error.
Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1851
--- hw/net/rocker/rocker_of_dpa.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/net/rocker/rocker_of_dpa.c +4 -0
@@ -2029,6 +2029,10 @@ static int of_dpa_cmd_add_l2_flood(OfDpa *of_dpa, OfDpaGroup *group, group_tlvs[ROCKER_TLV_OF_DPA_GROUP_IDS]);for (i =0 ; i < group->l2_flood.group_count; i++) { +if (!tlvs[i +1 ]) { + err = -ROCKER_EINVAL ; + goto err_out; + } group->l2_flood.group_ids[i] =rocker_tlv_get_le32 (tlvs[i +1 ]); }
Signature · 1 line
2.43.0
PATCH 2/2 hw/net/rocker_of_dpa: Avoid unaligned accesses in _of_dpa_flow_match()
In reply to Peter Maydell_of_dpa_flow_match() tries to do masked comparisons of OfDpaFlowkey structs by casting pointers to them to uint64_t* and then doing the memory accesses as 64-bit. This is undefined behaviour because the pointers might not be 64-bit aligned, and the UB sanitizer spots this:
../../hw/net/rocker/rocker_of_dpa.c:321:20: runtime error: load of misaligned address 0x512000164044 for type 'uint64_t' (aka 'unsigned long'), which requires 8 byte alignment 0x512000164044: note: pointer points here
02 00 00 00 00 00 ff ff 00 00 00 00 ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
We do know that OfDpaFlowKey structs must be at least aligned enough for uint32_t accesses, because that's the type of the first field. Switch to using uint32_t accesses in the loop.
Because the "width" field is always set via the FLOW_KEY_WIDTH macro and not exposed to the guest, we can adjust the macro to store the number of uint32_t to be checked rather than needing to change the loop boundary in the match function.
--- hw/net/rocker/rocker_of_dpa.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
hw/net/rocker/rocker_of_dpa.c +6 -6
@@ -99,13 +99,13 @@ typedef struct of_dpa_flow_key { } nd; } ipv6; }; -int width;/* how many uint64_t's in key? */ +int width;/* how many uint32_t's in key? */ } OfDpaFlowKey; -/* Width of key which includes field 'f' in u64s, rounded up */ +/* Width of key which includes field 'f' in u32s, rounded up */ #define FLOW_KEY_WIDTH (f) \ DIV_ROUND_UP(offsetof(OfDpaFlowKey, f) + sizeof_field(OfDpaFlowKey, f), \ - sizeof(uint64_t)) + sizeof(uint32_t)) typedef struct of_dpa_flow_action { uint32_t goto_tbl;
@@ -304,9 +304,9 @@ static void _of_dpa_flow_match(void *key, void *value, void *user_data) {OfDpaFlow *flow = value;OfDpaFlowMatch *match = user_data; -uint64_t *k = (uint64_t *)&flow->key; -uint64_t *m = (uint64_t *)&flow->mask; -uint64_t *v = (uint64_t *)&match->value; +uint32_t *k = (uint32_t *)&flow->key; +uint32_t *m = (uint32_t *)&flow->mask; +uint32_t *v = (uint32_t *)&match->value;int i;if (flow->key.tbl_id == match->value.tbl_id) {
Signature · 1 line
2.43.0
Ping for code review, please?
thanks
-- PMM
Quoted 15 lines · Peter Maydell
These patches fix two bugs: a NULL pointer dereference if the guest does something wrong (reported a few years back as issue #1851), and a sanitizer complaint about unaligned accesses that I noticed while reproducing the first bug.
thanks
-- PMMPeter Maydell (2):
hw/net/rocker_of_dpa: Check group ID pointers are not NULL hw/net/rocker_of_dpa: Avoid unaligned accesses in_of_dpa_flow_match()hw/net/rocker/rocker_of_dpa.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)