2019-07-08 15:09:50

by zhangxiaoxu (A)

[permalink] [raw]
Subject: [v2] time: Validate the usec before covert to nsec in do_adjtimex

When covert the usec to nsec, it will multiple 1000, it maybe
overflow and lead an undefined behavior.

For example, users may input an negative tv_usec values when
call adjtimex syscall, then multiple 1000 maybe overflow it
to a positive and legal number.

So, we should validate the usec before coverted it to nsec.

Signed-off-by: ZhangXiaoxu <[email protected]>
---
kernel/time/timekeeping.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 44b726b..778796d 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2321,6 +2321,9 @@ int do_adjtimex(struct __kernel_timex *txc)

if (txc->modes & ADJ_SETOFFSET) {
struct timespec64 delta;
+
+ if (txc->time.tv_usec < 0 || txc->time.tv_usec >= USEC_PER_SEC)
+ return -EINVAL;
delta.tv_sec = txc->time.tv_sec;
delta.tv_nsec = txc->time.tv_usec;
if (!(txc->modes & ADJ_NANO))
--
2.7.4


2019-07-08 15:24:28

by zhangxiaoxu (A)

[permalink] [raw]
Subject: Re: [v2] time: Validate the usec before covert to nsec in do_adjtimex



?? 2019/7/8 21:04, Thomas Gleixner ะด??:
> On Mon, 8 Jul 2019, ZhangXiaoxu wrote:
>
>> When covert the usec to nsec, it will multiple 1000, it maybe
>> overflow and lead an undefined behavior.
>>
>> For example, users may input an negative tv_usec values when
>> call adjtimex syscall, then multiple 1000 maybe overflow it
>> to a positive and legal number.
>>
>> So, we should validate the usec before coverted it to nsec.
>
> Looking deeper before applying it. That change is wrong for two reasons:
>
> 1) The value is already validated in timekeeping_validate_timex()
>
> 2) The tv_usec value can legitimately be >= USEC_PER_SEC if the ADJ_NANO
> mode bit is set. See timekeeping_validate_timex() and the code you
> actually modified:
>
Yes, you are right.
This actually found in an old version, and doesn't check more detail on mainline.
Thank you very much.
>> if (txc->modes & ADJ_SETOFFSET) {
>> struct timespec64 delta;
>> +
>> + if (txc->time.tv_usec < 0 || txc->time.tv_usec >= USEC_PER_SEC)
>> + return -EINVAL;
>> delta.tv_sec = txc->time.tv_sec;
>> delta.tv_nsec = txc->time.tv_usec;
>> if (!(txc->modes & ADJ_NANO))
> delta.tv_nsec *= 1000;
>
> The multiplication is conditional ....
>
> Thanks,
>
> tglx
>
>
>
> .
>

2019-07-08 17:52:00

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [v2] time: Validate the usec before covert to nsec in do_adjtimex

On Mon, 8 Jul 2019, ZhangXiaoxu wrote:

> When covert the usec to nsec, it will multiple 1000, it maybe
> overflow and lead an undefined behavior.
>
> For example, users may input an negative tv_usec values when
> call adjtimex syscall, then multiple 1000 maybe overflow it
> to a positive and legal number.
>
> So, we should validate the usec before coverted it to nsec.

Looking deeper before applying it. That change is wrong for two reasons:

1) The value is already validated in timekeeping_validate_timex()

2) The tv_usec value can legitimately be >= USEC_PER_SEC if the ADJ_NANO
mode bit is set. See timekeeping_validate_timex() and the code you
actually modified:

> if (txc->modes & ADJ_SETOFFSET) {
> struct timespec64 delta;
> +
> + if (txc->time.tv_usec < 0 || txc->time.tv_usec >= USEC_PER_SEC)
> + return -EINVAL;
> delta.tv_sec = txc->time.tv_sec;
> delta.tv_nsec = txc->time.tv_usec;
> if (!(txc->modes & ADJ_NANO))
delta.tv_nsec *= 1000;

The multiplication is conditional ....

Thanks,

tglx