qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns
@ 2023-07-12  6:08 Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 1/3] target/mips/mxu: Replace magic array size by its definition Philippe Mathieu-Daudé
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-12  6:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: Aurelien Jarno, Aleksandar Rikalo, Jiaxun Yang, Siarhei Volkau,
	Philippe Mathieu-Daudé, Craig Janeczek

Fix overruns mxu_gpr[XRb - 1] reported by Coverity.

Philippe Mathieu-Daudé (3):
  target/mips/mxu: Replace magic array size by its definition
  target/mips/mxu: Avoid overrun in gen_mxu_S32SLT()
  target/mips/mxu: Avoid overrun in gen_mxu_q8adde()

 target/mips/tcg/mxu_translate.c | 36 +++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 11 deletions(-)

-- 
2.38.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH-for-8.1 1/3] target/mips/mxu: Replace magic array size by its definition
  2023-07-12  6:08 [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Philippe Mathieu-Daudé
@ 2023-07-12  6:08 ` Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 2/3] target/mips/mxu: Avoid overrun in gen_mxu_S32SLT() Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-12  6:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: Aurelien Jarno, Aleksandar Rikalo, Jiaxun Yang, Siarhei Volkau,
	Philippe Mathieu-Daudé, Craig Janeczek

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/mips/tcg/mxu_translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target/mips/tcg/mxu_translate.c b/target/mips/tcg/mxu_translate.c
index deb8060a17..b007948a73 100644
--- a/target/mips/tcg/mxu_translate.c
+++ b/target/mips/tcg/mxu_translate.c
@@ -609,7 +609,7 @@ enum {
 static TCGv mxu_gpr[NUMBER_OF_MXU_REGISTERS - 1];
 static TCGv mxu_CR;
 
-static const char mxuregnames[][4] = {
+static const char mxuregnames[NUMBER_OF_MXU_REGISTERS][4] = {
     "XR1",  "XR2",  "XR3",  "XR4",  "XR5",  "XR6",  "XR7",  "XR8",
     "XR9",  "XR10", "XR11", "XR12", "XR13", "XR14", "XR15", "XCR",
 };
-- 
2.38.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH-for-8.1 2/3] target/mips/mxu: Avoid overrun in gen_mxu_S32SLT()
  2023-07-12  6:08 [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 1/3] target/mips/mxu: Replace magic array size by its definition Philippe Mathieu-Daudé
@ 2023-07-12  6:08 ` Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 3/3] target/mips/mxu: Avoid overrun in gen_mxu_q8adde() Philippe Mathieu-Daudé
  2023-07-12  6:13 ` [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Richard Henderson
  3 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-12  6:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: Aurelien Jarno, Aleksandar Rikalo, Jiaxun Yang, Siarhei Volkau,
	Philippe Mathieu-Daudé, Craig Janeczek

Coverity reports a potential overrun (CID 1517769):

  Overrunning array "mxu_gpr" of 15 8-byte elements at
  element index 4294967295 (byte offset 34359738367)
  using index "XRb - 1U" (which evaluates to 4294967295).

Use gen_load_mxu_gpr() to safely load MXU registers.

Fixes: ff7936f009 ("target/mips/mxu: Add S32SLT ... insns")
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/mips/tcg/mxu_translate.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/target/mips/tcg/mxu_translate.c b/target/mips/tcg/mxu_translate.c
index b007948a73..520747a597 100644
--- a/target/mips/tcg/mxu_translate.c
+++ b/target/mips/tcg/mxu_translate.c
@@ -2434,8 +2434,12 @@ static void gen_mxu_S32SLT(DisasContext *ctx)
         tcg_gen_movi_tl(mxu_gpr[XRa - 1], 0);
     } else {
         /* the most general case */
-        tcg_gen_setcond_tl(TCG_COND_LT, mxu_gpr[XRa - 1],
-                           mxu_gpr[XRb - 1], mxu_gpr[XRc - 1]);
+        TCGv t0 = tcg_temp_new();
+        TCGv t1 = tcg_temp_new();
+
+        gen_load_mxu_gpr(t0, XRb);
+        gen_load_mxu_gpr(t1, XRc);
+        tcg_gen_setcond_tl(TCG_COND_LT, mxu_gpr[XRa - 1], t0, t1);
     }
 }
 
-- 
2.38.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH-for-8.1 3/3] target/mips/mxu: Avoid overrun in gen_mxu_q8adde()
  2023-07-12  6:08 [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 1/3] target/mips/mxu: Replace magic array size by its definition Philippe Mathieu-Daudé
  2023-07-12  6:08 ` [PATCH-for-8.1 2/3] target/mips/mxu: Avoid overrun in gen_mxu_S32SLT() Philippe Mathieu-Daudé
@ 2023-07-12  6:08 ` Philippe Mathieu-Daudé
  2023-07-12  6:13 ` [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Richard Henderson
  3 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-07-12  6:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: Aurelien Jarno, Aleksandar Rikalo, Jiaxun Yang, Siarhei Volkau,
	Philippe Mathieu-Daudé, Craig Janeczek

Coverity reports a potential overruns (CID 1517770):

  Overrunning array "mxu_gpr" of 15 8-byte elements at
  element index 4294967295 (byte offset 34359738367)
  using index "XRb - 1U" (which evaluates to 4294967295).

Add a gen_extract_mxu_gpr() helper similar to
gen_load_mxu_gpr() to safely extract MXU registers.

Fixes: eb79951ab6 ("target/mips/mxu: Add Q8ADDE ... insns")
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/mips/tcg/mxu_translate.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/target/mips/tcg/mxu_translate.c b/target/mips/tcg/mxu_translate.c
index 520747a597..e662acd5df 100644
--- a/target/mips/tcg/mxu_translate.c
+++ b/target/mips/tcg/mxu_translate.c
@@ -644,6 +644,16 @@ static inline void gen_store_mxu_gpr(TCGv t, unsigned int reg)
     }
 }
 
+static inline void gen_extract_mxu_gpr(TCGv t, unsigned int reg,
+                                       unsigned int ofs, unsigned int len)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
+    } else if (reg <= 15) {
+        tcg_gen_extract_tl(t, mxu_gpr[reg - 1], ofs, len);
+    }
+}
+
 /* MXU control register moves. */
 static inline void gen_load_mxu_cr(TCGv t)
 {
@@ -3004,10 +3014,10 @@ static void gen_mxu_q8adde(DisasContext *ctx, bool accumulate)
         TCGv t5 = tcg_temp_new();
 
         if (XRa != 0) {
-            tcg_gen_extract_tl(t0, mxu_gpr[XRb - 1], 16, 8);
-            tcg_gen_extract_tl(t1, mxu_gpr[XRc - 1], 16, 8);
-            tcg_gen_extract_tl(t2, mxu_gpr[XRb - 1], 24, 8);
-            tcg_gen_extract_tl(t3, mxu_gpr[XRc - 1], 24, 8);
+            gen_extract_mxu_gpr(t0, XRb, 16, 8);
+            gen_extract_mxu_gpr(t1, XRc, 16, 8);
+            gen_extract_mxu_gpr(t2, XRb, 24, 8);
+            gen_extract_mxu_gpr(t3, XRc, 24, 8);
             if (aptn2 & 2) {
                 tcg_gen_sub_tl(t0, t0, t1);
                 tcg_gen_sub_tl(t2, t2, t3);
@@ -3027,10 +3037,10 @@ static void gen_mxu_q8adde(DisasContext *ctx, bool accumulate)
             tcg_gen_or_tl(t4, t2, t0);
         }
         if (XRd != 0) {
-            tcg_gen_extract_tl(t0, mxu_gpr[XRb - 1], 0, 8);
-            tcg_gen_extract_tl(t1, mxu_gpr[XRc - 1], 0, 8);
-            tcg_gen_extract_tl(t2, mxu_gpr[XRb - 1], 8, 8);
-            tcg_gen_extract_tl(t3, mxu_gpr[XRc - 1], 8, 8);
+            gen_extract_mxu_gpr(t0, XRb, 0, 8);
+            gen_extract_mxu_gpr(t1, XRc, 0, 8);
+            gen_extract_mxu_gpr(t2, XRb, 8, 8);
+            gen_extract_mxu_gpr(t3, XRc, 8, 8);
             if (aptn2 & 1) {
                 tcg_gen_sub_tl(t0, t0, t1);
                 tcg_gen_sub_tl(t2, t2, t3);
-- 
2.38.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns
  2023-07-12  6:08 [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2023-07-12  6:08 ` [PATCH-for-8.1 3/3] target/mips/mxu: Avoid overrun in gen_mxu_q8adde() Philippe Mathieu-Daudé
@ 2023-07-12  6:13 ` Richard Henderson
  3 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2023-07-12  6:13 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Aurelien Jarno, Aleksandar Rikalo, Jiaxun Yang, Siarhei Volkau,
	Craig Janeczek

On 7/12/23 07:08, Philippe Mathieu-Daudé wrote:
> Fix overruns mxu_gpr[XRb - 1] reported by Coverity.
> 
> Philippe Mathieu-Daudé (3):
>    target/mips/mxu: Replace magic array size by its definition
>    target/mips/mxu: Avoid overrun in gen_mxu_S32SLT()
>    target/mips/mxu: Avoid overrun in gen_mxu_q8adde()

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-12  6:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-12  6:08 [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Philippe Mathieu-Daudé
2023-07-12  6:08 ` [PATCH-for-8.1 1/3] target/mips/mxu: Replace magic array size by its definition Philippe Mathieu-Daudé
2023-07-12  6:08 ` [PATCH-for-8.1 2/3] target/mips/mxu: Avoid overrun in gen_mxu_S32SLT() Philippe Mathieu-Daudé
2023-07-12  6:08 ` [PATCH-for-8.1 3/3] target/mips/mxu: Avoid overrun in gen_mxu_q8adde() Philippe Mathieu-Daudé
2023-07-12  6:13 ` [PATCH-for-8.1 0/3] target/mips/mxu: Fix overruns Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).