All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Hoppenbrouwers <david@salt-inc.org>
To: qemu-devel@nongnu.org
Cc: Bin Meng <bin.meng@windriver.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	David Hoppenbrouwers <david@salt-inc.org>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	"open list:SiFive Machines" <qemu-riscv@nongnu.org>
Subject: [PATCH] hw/intc/sifive_clint: Fix overflow in sifive_clint_write_timecmp()
Date: Mon, 16 Aug 2021 19:30:35 +0200	[thread overview]
Message-ID: <20210816173035.5165-1-david@salt-inc.org> (raw)

`next` is an `uint64_t` value, but `timer_mod` takes an `int64_t`. This
resulted in high values such as `UINT64_MAX` being converted to `-1`,
which caused an immediate timer interrupt.

By limiting `next` to `INT64_MAX` no overflow will happen while the
timer will still be effectively set to "infinitely" far in the future.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
Signed-off-by: David Hoppenbrouwers <david@salt-inc.org>
---
 hw/intc/sifive_clint.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/hw/intc/sifive_clint.c b/hw/intc/sifive_clint.c
index 0f41e5ea1c..e65e71e5ec 100644
--- a/hw/intc/sifive_clint.c
+++ b/hw/intc/sifive_clint.c
@@ -61,6 +61,8 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value,
     /* back to ns (note args switched in muldiv64) */
     next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
         muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
+    /* ensure next does not overflow, as timer_mod takes a signed value */
+    next = MAX(next, INT64_MAX);
     timer_mod(cpu->env.timer, next);
 }
 
-- 
2.20.1



WARNING: multiple messages have this Message-ID (diff)
From: David Hoppenbrouwers <david@salt-inc.org>
To: qemu-devel@nongnu.org
Cc: David Hoppenbrouwers <david@salt-inc.org>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	Bin Meng <bin.meng@windriver.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	qemu-riscv@nongnu.org (open list:SiFive Machines)
Subject: [PATCH] hw/intc/sifive_clint: Fix overflow in sifive_clint_write_timecmp()
Date: Mon, 16 Aug 2021 19:30:35 +0200	[thread overview]
Message-ID: <20210816173035.5165-1-david@salt-inc.org> (raw)

`next` is an `uint64_t` value, but `timer_mod` takes an `int64_t`. This
resulted in high values such as `UINT64_MAX` being converted to `-1`,
which caused an immediate timer interrupt.

By limiting `next` to `INT64_MAX` no overflow will happen while the
timer will still be effectively set to "infinitely" far in the future.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
Signed-off-by: David Hoppenbrouwers <david@salt-inc.org>
---
 hw/intc/sifive_clint.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/hw/intc/sifive_clint.c b/hw/intc/sifive_clint.c
index 0f41e5ea1c..e65e71e5ec 100644
--- a/hw/intc/sifive_clint.c
+++ b/hw/intc/sifive_clint.c
@@ -61,6 +61,8 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value,
     /* back to ns (note args switched in muldiv64) */
     next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
         muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
+    /* ensure next does not overflow, as timer_mod takes a signed value */
+    next = MAX(next, INT64_MAX);
     timer_mod(cpu->env.timer, next);
 }
 
-- 
2.20.1



             reply	other threads:[~2021-08-16 18:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-16 17:30 David Hoppenbrouwers [this message]
2021-08-16 17:30 ` [PATCH] hw/intc/sifive_clint: Fix overflow in sifive_clint_write_timecmp() David Hoppenbrouwers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210816173035.5165-1-david@salt-inc.org \
    --to=david@salt-inc.org \
    --cc=Alistair.Francis@wdc.com \
    --cc=bin.meng@windriver.com \
    --cc=palmer@dabbelt.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.