Skip to content

Commit 7d99b7d

Browse files
KAGA-KOKOLinus Torvalds
authored andcommitted
[PATCH] Validate and sanitze itimer timeval from userspace
According to the specification the timevals must be validated and an errorcode -EINVAL returned in case the timevals are not in canonical form. This check was never done in Linux. The pre 2.6.16 code converted invalid timevals silently. Negative timeouts were converted by the timeval_to_jiffies conversion to the maximum timeout. hrtimers and the ktime_t operations expect timevals in canonical form. Otherwise random results might happen on 32 bits machines due to the optimized ktime_add/sub operations. Negative timeouts are treated as already expired. This might break applications which work on pre 2.6.16. To prevent random behaviour and API breakage the timevals are checked and invalid timevals sanitized in a simliar way as the pre 2.6.16 code did. Invalid timevals are reported with a per boot limited number of kernel messages so applications which use this misfeature can be corrected. After a grace period of one year the sanitizing should be replaced by a correct validation check. This is also documented in Documentation/feature-removal-schedule.txt The validation and sanitizing is done inside do_setitimer so all callers (sys_setitimer, compat_sys_setitimer, osf_setitimer) are catched. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent c08b8a4 commit 7d99b7d

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎Documentation/feature-removal-schedule.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ Who: Richard Knutsson <ricknu-0@student.ltu.se> and Greg Kroah-Hartman <gregkh@s
176176

177177
---------------------------
178178

179+
What: Usage of invalid timevals in setitimer
180+
When: March 2007
181+
Why: POSIX requires to validate timevals in the setitimer call. This
182+
was never done by Linux. The invalid (e.g. negative timevals) were
183+
silently converted to more or less random timeouts and intervals.
184+
Until the removal a per boot limited number of warnings is printed
185+
and the timevals are sanitized.
186+
187+
Who: Thomas Gleixner <tglx@linutronix.de>
188+
189+
---------------------------
190+
179191
What: I2C interface of the it87 driver
180192
When: January 2007
181193
Why: The ISA interface is faster and should be always available. The I2C

‎kernel/itimer.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,79 @@ int it_real_fn(void *data)
143143
return HRTIMER_NORESTART;
144144
}
145145

146+
/*
147+
* We do not care about correctness. We just sanitize the values so
148+
* the ktime_t operations which expect normalized values do not
149+
* break. This converts negative values to long timeouts similar to
150+
* the code in kernel versions < 2.6.16
151+
*
152+
* Print a limited number of warning messages when an invalid timeval
153+
* is detected.
154+
*/
155+
static void fixup_timeval(struct timeval *tv, int interval)
156+
{
157+
static int warnlimit = 10;
158+
unsigned long tmp;
159+
160+
if (warnlimit > 0) {
161+
warnlimit--;
162+
printk(KERN_WARNING
163+
"setitimer: %s (pid = %d) provided "
164+
"invalid timeval %s: tv_sec = %ld tv_usec = %ld\n",
165+
current->comm, current->pid,
166+
interval ? "it_interval" : "it_value",
167+
tv->tv_sec, (long) tv->tv_usec);
168+
}
169+
170+
tmp = tv->tv_usec;
171+
if (tmp >= USEC_PER_SEC) {
172+
tv->tv_usec = tmp % USEC_PER_SEC;
173+
tv->tv_sec += tmp / USEC_PER_SEC;
174+
}
175+
176+
tmp = tv->tv_sec;
177+
if (tmp > LONG_MAX)
178+
tv->tv_sec = LONG_MAX;
179+
}
180+
181+
/*
182+
* Returns true if the timeval is in canonical form
183+
*/
184+
#define timeval_valid(t) \
185+
(((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
186+
187+
/*
188+
* Check for invalid timevals, sanitize them and print a limited
189+
* number of warnings.
190+
*/
191+
static void check_itimerval(struct itimerval *value) {
192+
193+
if (unlikely(!timeval_valid(&value->it_value)))
194+
fixup_timeval(&value->it_value, 0);
195+
196+
if (unlikely(!timeval_valid(&value->it_interval)))
197+
fixup_timeval(&value->it_interval, 1);
198+
}
199+
146200
int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
147201
{
148202
struct task_struct *tsk = current;
149203
struct hrtimer *timer;
150204
ktime_t expires;
151205
cputime_t cval, cinterval, nval, ninterval;
152206

207+
/*
208+
* Validate the timevals in value.
209+
*
210+
* Note: Although the spec requires that invalid values shall
211+
* return -EINVAL, we just fixup the value and print a limited
212+
* number of warnings in order not to break users of this
213+
* historical misfeature.
214+
*
215+
* Scheduled for replacement in March 2007
216+
*/
217+
check_itimerval(value);
218+
153219
switch (which) {
154220
case ITIMER_REAL:
155221
again:

0 commit comments

Comments
 (0)