History

posix-cpu-timers: Make timespec to nsec conversion safe

The expiry time of a posix cpu timer is supplied through sys_timer_set() via a struct timespec. The timespec is validated for correctness. In the actual set timer implementation the timespec is converted to a scalar nanoseconds value. If the tv_sec part of the time spec is large enough the conversion to nanoseconds (sec * NSEC_PER_SEC) overflows 64bit. Mitigate that by using the timespec_to_ktime() conversion function, which checks the tv_sec part for a potential mult overflow and clamps the result to KTIME_MAX, which is about 292 years.

Thomas Gleixner committed 9 years ago

Commit 098b0e01 parent 35eb7251 file · +5 −1Browse files

Unified · Split

kernel/time/posix-cpu-timers.c+5−1
@@ -580,7 +580,11 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, · 579 unmodified lines
580580
581581 WARN_ON_ONCE(p == NULL);
582582
583− new_expires = timespec64_to_ns(&new->it_value);
583+ /*
584+ * Use the to_ktime conversion because that clamps the maximum
585+ * value to KTIME_MAX and avoid multiplication overflows.
586+ */
587+ new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
584588
585589 /*
586590 * Protect against sighand release/switch in exit/exec and p->cpu_timers

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t