Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1461512 > unrolled thread
| Started by | Vegard Nossum <vegard.nossum@oracle.com> |
|---|---|
| First post | 2016-08-13 01:40 +0200 |
| Last post | 2016-08-17 22:20 +0200 |
| Articles | 2 — 2 participants |
Back to article view | Back to linux.kernel
[PATCH] time: avoid undefined behaviour in ktime_add_safe() Vegard Nossum <vegard.nossum@oracle.com> - 2016-08-13 01:40 +0200
Re: [PATCH] time: avoid undefined behaviour in ktime_add_safe() John Stultz <john.stultz@linaro.org> - 2016-08-17 22:20 +0200
| From | Vegard Nossum <vegard.nossum@oracle.com> |
|---|---|
| Date | 2016-08-13 01:40 +0200 |
| Subject | [PATCH] time: avoid undefined behaviour in ktime_add_safe() |
| Message-ID | <s5udr-8uq-9@gated-at.bofh.it> |
I ran into this:
================================================================================
UBSAN: Undefined behaviour in kernel/time/hrtimer.c:310:16
signed integer overflow:
9223372036854775807 + 50000 cannot be represented in type 'long long int'
CPU: 2 PID: 4798 Comm: trinity-c2 Not tainted 4.8.0-rc1+ #91
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.9.3-0-ge2fc41e-prebuilt.qemu-project.org 04/01/2014
0000000000000000 ffff88010ce6fb88 ffffffff82344740 0000000041b58ab3
ffffffff84f97a20 ffffffff82344694 ffff88010ce6fbb0 ffff88010ce6fb60
000000000000c350 ffff88010ce6f968 dffffc0000000000 ffffffff857bc320
Call Trace:
[<ffffffff82344740>] dump_stack+0xac/0xfc
[<ffffffff82344694>] ? _atomic_dec_and_lock+0xc4/0xc4
[<ffffffff8242df78>] ubsan_epilogue+0xd/0x8a
[<ffffffff8242e6b4>] handle_overflow+0x202/0x23d
[<ffffffff8242e4b2>] ? val_to_string.constprop.6+0x11e/0x11e
[<ffffffff8236df71>] ? timerqueue_add+0x151/0x410
[<ffffffff81485c48>] ? hrtimer_start_range_ns+0x3b8/0x1380
[<ffffffff81795631>] ? memset+0x31/0x40
[<ffffffff8242e6fd>] __ubsan_handle_add_overflow+0xe/0x10
[<ffffffff81488ac9>] hrtimer_nanosleep+0x5d9/0x790
[<ffffffff814884f0>] ? hrtimer_init_sleeper+0x80/0x80
[<ffffffff813a9ffb>] ? __might_sleep+0x5b/0x260
[<ffffffff8148be10>] common_nsleep+0x20/0x30
[<ffffffff814906c7>] SyS_clock_nanosleep+0x197/0x210
[<ffffffff81490530>] ? SyS_clock_getres+0x150/0x150
[<ffffffff823c7113>] ? __this_cpu_preempt_check+0x13/0x20
[<ffffffff8162ef60>] ? __context_tracking_exit.part.3+0x30/0x1b0
[<ffffffff81490530>] ? SyS_clock_getres+0x150/0x150
[<ffffffff81007bd3>] do_syscall_64+0x1b3/0x4b0
[<ffffffff845f85aa>] entry_SYSCALL64_slow_path+0x25/0x25
================================================================================
Add a new ktime_add_unsafe() helper which doesn't check for overflow, but
doesn't throw a UBSAN warning when it does overflow either.
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
---
include/linux/ktime.h | 7 +++++++
kernel/time/hrtimer.c | 2 +-
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index 2b6a204..3ffc69e 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -64,6 +64,13 @@ static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs)
({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
/*
+ * Same as ktime_add(), but avoids undefined behaviour on overflow; however,
+ * this means that you must check the result for overflow yourself.
+ */
+#define ktime_add_unsafe(lhs, rhs) \
+ ({ (ktime_t){ .tv64 = (u64) (lhs).tv64 + (rhs).tv64 }; })
+
+/*
* Add a ktime_t variable and a scalar nanosecond value.
* res = kt + nsval:
*/
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 9ba7c82..3172d16 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -307,7 +307,7 @@ EXPORT_SYMBOL_GPL(__ktime_divns);
*/
ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
{
- ktime_t res = ktime_add(lhs, rhs);
+ ktime_t res = ktime_add_unsafe(lhs, rhs);
/*
* We use KTIME_SEC_MAX here, the maximum timeout which we can
--
1.9.1
[toc] | [next] | [standalone]
| From | John Stultz <john.stultz@linaro.org> |
|---|---|
| Date | 2016-08-17 22:20 +0200 |
| Message-ID | <s7ftE-5TG-13@gated-at.bofh.it> |
| In reply to | #1461512 |
On Fri, Aug 12, 2016 at 4:37 PM, Vegard Nossum <vegard.nossum@oracle.com> wrote: > I ran into this: > > ================================================================================ > UBSAN: Undefined behaviour in kernel/time/hrtimer.c:310:16 > signed integer overflow: > 9223372036854775807 + 50000 cannot be represented in type 'long long int' > CPU: 2 PID: 4798 Comm: trinity-c2 Not tainted 4.8.0-rc1+ #91 > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.9.3-0-ge2fc41e-prebuilt.qemu-project.org 04/01/2014 > 0000000000000000 ffff88010ce6fb88 ffffffff82344740 0000000041b58ab3 > ffffffff84f97a20 ffffffff82344694 ffff88010ce6fbb0 ffff88010ce6fb60 > 000000000000c350 ffff88010ce6f968 dffffc0000000000 ffffffff857bc320 > Call Trace: > [<ffffffff82344740>] dump_stack+0xac/0xfc > [<ffffffff82344694>] ? _atomic_dec_and_lock+0xc4/0xc4 > [<ffffffff8242df78>] ubsan_epilogue+0xd/0x8a > [<ffffffff8242e6b4>] handle_overflow+0x202/0x23d > [<ffffffff8242e4b2>] ? val_to_string.constprop.6+0x11e/0x11e > [<ffffffff8236df71>] ? timerqueue_add+0x151/0x410 > [<ffffffff81485c48>] ? hrtimer_start_range_ns+0x3b8/0x1380 > [<ffffffff81795631>] ? memset+0x31/0x40 > [<ffffffff8242e6fd>] __ubsan_handle_add_overflow+0xe/0x10 > [<ffffffff81488ac9>] hrtimer_nanosleep+0x5d9/0x790 > [<ffffffff814884f0>] ? hrtimer_init_sleeper+0x80/0x80 > [<ffffffff813a9ffb>] ? __might_sleep+0x5b/0x260 > [<ffffffff8148be10>] common_nsleep+0x20/0x30 > [<ffffffff814906c7>] SyS_clock_nanosleep+0x197/0x210 > [<ffffffff81490530>] ? SyS_clock_getres+0x150/0x150 > [<ffffffff823c7113>] ? __this_cpu_preempt_check+0x13/0x20 > [<ffffffff8162ef60>] ? __context_tracking_exit.part.3+0x30/0x1b0 > [<ffffffff81490530>] ? SyS_clock_getres+0x150/0x150 > [<ffffffff81007bd3>] do_syscall_64+0x1b3/0x4b0 > [<ffffffff845f85aa>] entry_SYSCALL64_slow_path+0x25/0x25 > ================================================================================ > > Add a new ktime_add_unsafe() helper which doesn't check for overflow, but > doesn't throw a UBSAN warning when it does overflow either. > > Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com> Queued for testing, targeting 4.9. thanks -john
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web