hw/intc/exynos4210_combiner: fix misc range check issues
cover hw/intc/exynos4210_combiner: fix misc range check issues
This patchset's meat is patch 1, which fixes an incorrect range check that allows us to index off the end of an array. The other three are cleanups that I noticed while fixing that, where some checks that we were reporting as LOG_GUEST_ERROR should be "never happens" asserts, and an array we use for register contents has an extra unreachable element at the end.
thanks
-- PMM
Peter Maydell (4):
hw/intc/exynos4210_combiner: Fix group register range check hw/intc/exynos4210_combiner: Clean up reg_set[] bounds checks hw/intc/exynos4210_combiner: Make reg_set[] only as large as it needs
to be
hw/intc/exynos4210_combiner: write default case is unreachable, not
guest error
include/hw/intc/exynos4210_combiner.h | 6 +++++- hw/intc/exynos4210_combiner.c | 25 +++++++------------------ 2 files changed, 12 insertions(+), 19 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 hw/intc/exynos4210_combiner: Fix group register range check
The exynos4210 combiner has registers arranged in sets of 4:
offset 0x0: IIESR0 (handles interrupt groups 0..3) offset 0x4: IIECR0
offset 0x8: IISTR0
offset 0xc: IIMSR0
offset 0x10: IIESR1 (handles interrupt groups 4..7) offset 0x14: IIECR1
and so on for a total of 16 sets of registers, which puts the final IIMSR15 at offset 0xfc. After that come two registers which aren't grouped, ICIPSR0 at 0x100 and ICIPSR1 at 0x104. The entire register memory region is thus 0x108 bytes.
The logic which separates out the "offset is in one of the sets of registers" from "offset is one of the ICIPSRn registers" is incorrect, and so a write to offset 0x10448100 (which should be ICIPSR0) falls through into the code for handling accesses to a set of registers, and indexes off the end of the s->group[] array.
For an input offset of, say, 0x3C, req_quad_base_n is 0x3, because this is in the 3rd set of registers. grp_quad_base_n is then 0xC, because this set of registers handles interrupt groups 12..15, and we will index s->group[] with grp_quad_base_n through to grp_quad_base_n
+ 3. IIC_NGRP is the size of the group[] array, and so we need to check grp_quad_base_n against it, not req_quad_base_n.
Fix the bounds check, and add an assertion that the code path where we are beyond the register-set range isn't going to overrun its array, which we know to be true because of the size used for the MemoryRegion.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4709
--- hw/intc/exynos4210_combiner.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
hw/intc/exynos4210_combiner.c +4 -3
@@ -91,8 +91,9 @@ exynos4210_combiner_read(void *opaque, hwaddr offset, unsigned size) grp_quad_base_n = req_quad_base_n <<2 ; reg_n = (offset - (req_quad_base_n <<4 )) >>2 ; -if (req_quad_base_n >=IIC_NGRP ) { -/* Read of ICIPSR register */ +if (grp_quad_base_n >=IIC_NGRP ) { +/* Read of ICIPSR register. MR size ensures this must be in bounds */ +assert (reg_n <ARRAY_SIZE (s->icipsr));return s->icipsr[reg_n]; }
@@ -184,7 +185,7 @@ static void exynos4210_combiner_write(void *opaque, hwaddr offset, grp_quad_base_n = req_quad_base_n <<2 ; reg_n = (offset - (req_quad_base_n <<4 )) >>2 ; -if (req_quad_base_n >=IIC_NGRP ) { +if (grp_quad_base_n >=IIC_NGRP ) {qemu_log_mask (LOG_GUEST_ERROR ,"exynos4210.combiner: unallowed write access at offset 0x" HWADDR_FMT_plx"\n" , offset);
Signature · 1 line
2.43.0
Re: [PATCH 1/4] hw/intc/exynos4210_combiner: Fix group register range check
Quoted 31 lines · Peter Maydell
The exynos4210 combiner has registers arranged in sets of 4:
offset0x0: IIESR0 (handles interrupt groups 0..3) offset0x4: IIECR0
offset0x8: IISTR0
offset0xc: IIMSR0
offset0x10: IIESR1 (handles interrupt groups 4..7) offset0x14: IIECR1and so on for a total of 16 sets of registers, which puts the final IIMSR15 at offset
0xfc. After that come two registers which aren't grouped, ICIPSR0 at0x100and ICIPSR1 at0x104. The entire register memory region is thus0x108bytes.The logic which separates out the "offset is in one of the sets of registers" from "offset is one of the ICIPSRn registers" is incorrect, and so a write to offset
0x10448100(which should be ICIPSR0) falls through into the code for handling accesses to a set of registers, and indexes off the end of the s->group[] array.For an input offset of, say,
0x3C,req_quad_base_nis0x3, because this is in the 3rd set of registers.grp_quad_base_nis then0xC, because this set of registers handles interrupt groups 12..15, and we will index s->group[] withgrp_quad_base_nthrough togrp_quad_base_n
+ 3.IIC_NGRPis the size of the group[] array, and so we need to checkgrp_quad_base_nagainst it, notreq_quad_base_n.Fix the bounds check, and add an assertion that the code path where we are beyond the register-set range isn't going to overrun its array, which we know to be true because of the size used for the MemoryRegion.
Quoted 7 lines
Fixes 8e03cf1eeb1f9e0 ("ARM: exynos4210: IRQ subsystem support.")Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4709
Signed-off-by Peter Maydell <[email protected]>--- hw/intc/exynos4210_combiner.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
(This is now commit ab6579bf73b in the master branch).
Hi Peter!
This change has been Cc'ed qemu-stable. For 10.0.x series, I'm also picking up a previous commit,
b6d36a1af79 hw/intc/exynos4210_combiner: Avoid hw_error for guest error
instead of adjusting this change. Please let me know if I shouldn't :)
Overall, it feels like this a niche device, and it's unlikely anyone actually cares for the stable series -- actual users of this device in qemu most likely have their own local builds of qemu anyway. But sure thing I might be wrong.
Thanks!
/mjt
Re: [PATCH 1/4] hw/intc/exynos4210_combiner: Fix group register range check
Quoted 48 lines · Michael Tokarev
Quoted 31 lines · Peter Maydell
The exynos4210 combiner has registers arranged in sets of 4:
offset0x0: IIESR0 (handles interrupt groups 0..3) offset0x4: IIECR0
offset0x8: IISTR0
offset0xc: IIMSR0
offset0x10: IIESR1 (handles interrupt groups 4..7) offset0x14: IIECR1and so on for a total of 16 sets of registers, which puts the final IIMSR15 at offset
0xfc. After that come two registers which aren't grouped, ICIPSR0 at0x100and ICIPSR1 at0x104. The entire register memory region is thus0x108bytes.The logic which separates out the "offset is in one of the sets of registers" from "offset is one of the ICIPSRn registers" is incorrect, and so a write to offset
0x10448100(which should be ICIPSR0) falls through into the code for handling accesses to a set of registers, and indexes off the end of the s->group[] array.For an input offset of, say,
0x3C,req_quad_base_nis0x3, because this is in the 3rd set of registers.grp_quad_base_nis then0xC, because this set of registers handles interrupt groups 12..15, and we will index s->group[] withgrp_quad_base_nthrough togrp_quad_base_n
+ 3.IIC_NGRPis the size of the group[] array, and so we need to checkgrp_quad_base_nagainst it, notreq_quad_base_n.Fix the bounds check, and add an assertion that the code path where we are beyond the register-set range isn't going to overrun its array, which we know to be true because of the size used for the MemoryRegion.
Quoted 7 lines
Fixes 8e03cf1eeb1f9e0 ("ARM: exynos4210: IRQ subsystem support.")Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4709
Signed-off-by Peter Maydell <[email protected]>--- hw/intc/exynos4210_combiner.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)(This is now commit
ab6579bf73bin the master branch).Hi Peter!
This change has been Cc'ed qemu-stable. For 10.0.x series, I'm also picking up a previous commit,
Quoted 8 lines
b6d36a1af79hw/intc/exynos4210_combiner: Avoidhw_errorfor guest errorinstead of adjusting this change. Please let me know if I shouldn't :)
Overall, it feels like this a niche device, and it's unlikely anyone actually cares for the stable series -- actual users of this device in qemu most likely have their own local builds of qemu anyway. But sure thing I might be wrong.
Yes; I cc'd stable on this mostly because it's an array overflow, but as you say it's niche, and this device isn't covered by our security-policy as it's TCG only.
Picking up that earlier commit is fine if that's how you'd prefer to avoid the merge conflict.
thanks
-- PMM
PATCH 2/4 hw/intc/exynos4210_combiner: Clean up reg_set[] bounds checks
In reply to Peter MaydellIn the exynos420_combiner read and write functions, we check whether offset >> 2 is too large for the s->reg_set[] array we're about to access. This is written to log a guest error if it happens, but in fact the condition can't be true unless there's a QEMU bug:
* we have already dealt with and returned early for registers beyond the "sets of 4" range, by checking grp_quad_base_n; this accounts for all offsets 0x100 and above
* this means offset >> 2 is at most 0x3F
* IIC_REGSET_SIZE is 0x41, which is in fact one larger than it needs to be, but in any case the offset can't be out of range
Use assert() checking against ARRAY_SIZE for this sanity check, as failing it would be a QEMU bug, not a guest bug.
--- hw/intc/exynos4210_combiner.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
hw/intc/exynos4210_combiner.c +2 -12
@@ -119,12 +119,7 @@ exynos4210_combiner_read(void *opaque, hwaddr offset, unsigned size) s->group[grp_quad_base_n + 3].src_pending) << 24;break ;default : -if (offset >>2 >=IIC_REGSET_SIZE ) { -qemu_log_mask (LOG_GUEST_ERROR , -"exynos4210.combiner: overflow of reg_set by 0x" - HWADDR_FMT_plx"offset\n" , offset); -return 0 ; - } +assert ((offset >>2 ) <ARRAY_SIZE (s->reg_set)); val = s->reg_set[offset >>2 ]; }return val;
@@ -199,12 +194,7 @@ static void exynos4210_combiner_write(void *opaque, hwaddr offset,return ; } -if (offset >>2 >=IIC_REGSET_SIZE ) { -qemu_log_mask (LOG_GUEST_ERROR , -"exynos4210.combiner: overflow of reg_set by 0x" - HWADDR_FMT_plx"offset\n" , offset); -return ; - } +assert ((offset >>2 ) <ARRAY_SIZE (s->reg_set)); s->reg_set[offset >>2 ] = val;switch (reg_n) {
Signature · 1 line
2.43.0
PATCH 3/4 hw/intc/exynos4210_combiner: Make reg_set[] only as large as it needs to be
In reply to Peter MaydellThe reg_set[] array holds the register values for registers in the "sets of 4 registers IIESRn, IIECRn, IISTRn, IIMSRn" part of this device, not the extra non-grouped ICIPSRn registers at the end. It therefore needs at most 0x40 entries (for registers at offset 0x0 up to offset 0xFC), but we oversized it at 0x41.
Reduce the size to what it ought to be. This is a migration break, but we don't care for the exynos boards.
--- include/hw/intc/exynos4210_combiner.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/hw/intc/exynos4210_combiner.h +5 -1
@@ -39,7 +39,11 @@ OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210CombinerState, EXYNOS4210_COMBINER)/* Number of groups and total number of interrupts for the internal combiner */ #define IIC_NGRP 64#define IIC_NIRQ (IIC_NGRP * 8) -#define IIC_REGSET_SIZE 0x41 +/* +* Number of registers in the IIESRn, IIECRn, IISTRn, IIMSRn part +* of the register map. +*/ +#define IIC_REGSET_SIZE 0x40struct Exynos4210CombinerState {SysBusDevice parent_obj;
Signature · 1 line
2.43.0
PATCH 4/4 hw/intc/exynos4210_combiner: write default case is unreachable, not guest error
In reply to Peter MaydellIn exynos4210_combiner_write(), we rule out "reg_n > 1" before we start doing anything as being an invalid guest access. The default case in the later "switch (reg_n)" is therefore unreachable, because it has explicit cases for 0 and 1. Use g_assert_not_reached() instead of a LOG_GUEST_ERROR.
--- hw/intc/exynos4210_combiner.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/intc/exynos4210_combiner.c +1 -3
@@ -242,9 +242,7 @@ static void exynos4210_combiner_write(void *opaque, hwaddr offset,exynos4210_combiner_update (s, grp_quad_base_n +3 );break ;default : -qemu_log_mask (LOG_GUEST_ERROR , -"exynos4210.combiner: unallowed write access at offset 0x" - HWADDR_FMT_plx"\n" , offset); +g_assert_not_reached ();break ; } }
Signature · 1 line
2.43.0
Re: [PATCH 4/4] hw/intc/exynos4210_combiner: write default case is unreachable, not guest error
Quoted 16 lines · Peter Maydell
In
exynos4210_combiner_write(), we rule out "reg_n> 1" before we start doing anything as being an invalid guest access. The default case in the later "switch (reg_n)" is therefore unreachable, because it has explicit cases for 0 and 1. Useg_assert_not_reached()instead of aLOG_GUEST_ERROR.Signed-off-by Peter Maydell <[email protected]>--- hw/intc/exynos4210_combiner.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/intc/exynos4210_combiner.c +0
-0
hw/intc/exynos4210_combiner.c static void exynos4210_combiner_write(void *opaque, hwaddr offset, · line 248
Quoted 10 lines
hw/intc/exynos4210_combiner.c +1
-3@@ -242,9 +242,7 @@ static void exynos4210_combiner_write(void *opaque, hwaddr offset,exynos4210_combiner_update (s, grp_quad_base_n +3 );break ;default : -qemu_log_mask (LOG_GUEST_ERROR , -"exynos4210.combiner: unallowed write access at offset 0x" - HWADDR_FMT_plx"\n" , offset); +g_assert_not_reached ();break ; } }
You could remove the break as well as unreachable.
r~
Re: [PATCH 4/4] hw/intc/exynos4210_combiner: write default case is unreachable, not guest error
Quoted 22 lines · Richard Henderson
Quoted 20 lines · Peter Maydell
In
exynos4210_combiner_write(), we rule out "reg_n> 1" before we start doing anything as being an invalid guest access. The default case in the later "switch (reg_n)" is therefore unreachable, because it has explicit cases for 0 and 1. Useg_assert_not_reached()instead of aLOG_GUEST_ERROR.Signed-off-by Peter Maydell <[email protected]>--- hw/intc/exynos4210_combiner.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/intc/exynos4210_combiner.c +0
-1@@ -242,9 +242,7 @@ static void exynos4210_combiner_write(void *opaque, hwaddr offset,exynos4210_combiner_update (s, grp_quad_base_n +3 );break ;default : - qemu_log_mask(LOG_GUEST_ERROR,
Quoted 8 lines
hw/intc/exynos4210_combiner.c static void exynos4210_combiner_write(void *opaque, hwaddr offset, · line 248
Quoted 6 lines
hw/intc/exynos4210_combiner.c +1
-2-"exynos4210.combiner: unallowed write access at offset 0x" - HWADDR_FMT_plx "\n", offset); +g_assert_not_reached ();break ; } }You could remove the break as well as unreachable.
Yes, good idea.
-- PMM
Quoted 7 lines · Peter Maydell
Peter Maydell (4):
hw/intc/exynos4210_combiner: Fix group register range check hw/intc/exynos4210_combiner: Clean up reg_set[] bounds checks hw/intc/exynos4210_combiner: Make reg_set[] only as large as it needs
to be
hw/intc/exynos4210_combiner: write default case is unreachable, not
guest error
r~