Re: [PATCH] ntp: safeguard against time_constant overflow case

From: Thomas Gleixner
Date: Tue May 14 2024 - 04:53:09 EST


On Tue, May 07 2024 at 22:03, Justin Stitt wrote:
> On Mon, May 06, 2024 at 11:02:17PM -0700, John Stultz wrote:
>> > @@ -734,10 +737,10 @@ static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
>> >
>> > if (txc->modes & ADJ_TIMECONST) {
>> > time_constant = txc->constant;
>> > - if (!(time_status & STA_NANO))
>> > - time_constant += 4;
>> > - time_constant = min(time_constant, (long)MAXTC);
>> > - time_constant = max(time_constant, 0l);
>> > + if (!(time_status & STA_NANO) &&
>> > + unlikely(LONG_MAX - time_constant_inc >= time_constant))
>> > + time_constant += time_constant_inc;
>> > + time_constant = clamp_t(long, time_constant, 0, MAXTC);
>> > }
>>
>> Overall, this looks fine. Though the time_status conditional is now a
>> little unwieldy.
>>
>> I wonder if some sort of a helper like:
>> time_constant = safe_add(time_constant, TIME_CONSTANT_INC, LONG_MAX);
>>
>> Might make this a little easier to read?
>
> How about something like this:
>
> if (txc->modes & ADJ_TIMECONST) {
> if (!(time_status & STA_NANO))
> time_constant = clamp_t(long, txc->constant,
> -TIME_CONSTANT_INC,
> MAXTC - TIME_CONSTANT_INC) +
> TIME_CONSTANT_INC;
> else
> time_constant = clamp_t(long, txc->constant, 0, MAXTC);
> }
>
> We can remove the initial assignment and use some fancy clamps.

That's unreadable TBH.