diff options
author | Andrew Morton <akpm@osdl.org> | 2006-02-11 20:55:52 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-02-12 00:41:11 -0500 |
commit | 643a654540579b0dcc7a206a4a7475276a41aff0 (patch) | |
tree | e31d40e4362e4dc7823b7290c0de2a9353d3d117 | |
parent | 33042a9ff4d126ba944b9dc3076665a2029e0a34 (diff) |
[PATCH] select: fix returned timeval
With David Woodhouse <dwmw2@infradead.org>
select() presently has a habit of increasing the value of the user's
`timeout' argument on return.
We were writing back a timeout larger than the original. We _deliberately_
round up, since we know we must wait at _least_ as long as the caller asks
us to.
The patch adds a couple of helper functions for magnitude comparison of
timespecs and of timevals, and uses them to prevent the various poll and
select functions from returning a timeout which is larger than the one which
was passed in.
The patch also fixes a bug in compat_sys_pselect7(): it was adding the new
timeout value to the old one and was returning that. It should just return
the new timeout value.
(We have various handy timespec/timeval-to-from-nsec conversion functions in
time.h. But this code open-codes it all).
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Andi Kleen <ak@muc.de>
Cc: Ulrich Drepper <drepper@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: george anzinger <george@mvista.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r-- | fs/compat.c | 37 | ||||
-rw-r--r-- | fs/select.c | 32 | ||||
-rw-r--r-- | include/linux/compat.h | 20 | ||||
-rw-r--r-- | include/linux/time.h | 25 |
4 files changed, 92 insertions, 22 deletions
diff --git a/fs/compat.c b/fs/compat.c index 70c5af4cc270..a2ba78bdf7f7 100644 --- a/fs/compat.c +++ b/fs/compat.c | |||
@@ -1751,11 +1751,15 @@ asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, | |||
1751 | ret = compat_core_sys_select(n, inp, outp, exp, &timeout); | 1751 | ret = compat_core_sys_select(n, inp, outp, exp, &timeout); |
1752 | 1752 | ||
1753 | if (tvp) { | 1753 | if (tvp) { |
1754 | struct compat_timeval rtv; | ||
1755 | |||
1754 | if (current->personality & STICKY_TIMEOUTS) | 1756 | if (current->personality & STICKY_TIMEOUTS) |
1755 | goto sticky; | 1757 | goto sticky; |
1756 | tv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)); | 1758 | rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)); |
1757 | tv.tv_sec = timeout; | 1759 | rtv.tv_sec = timeout; |
1758 | if (copy_to_user(tvp, &tv, sizeof(tv))) { | 1760 | if (compat_timeval_compare(&rtv, &tv) < 0) |
1761 | rtv = tv; | ||
1762 | if (copy_to_user(tvp, &rtv, sizeof(rtv))) { | ||
1759 | sticky: | 1763 | sticky: |
1760 | /* | 1764 | /* |
1761 | * If an application puts its timeval in read-only | 1765 | * If an application puts its timeval in read-only |
@@ -1822,13 +1826,17 @@ asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp, | |||
1822 | } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec)); | 1826 | } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec)); |
1823 | 1827 | ||
1824 | if (tsp && !(current->personality & STICKY_TIMEOUTS)) { | 1828 | if (tsp && !(current->personality & STICKY_TIMEOUTS)) { |
1825 | ts.tv_sec += timeout / HZ; | 1829 | struct compat_timespec rts; |
1826 | ts.tv_nsec += (timeout % HZ) * (1000000000/HZ); | 1830 | |
1827 | if (ts.tv_nsec >= 1000000000) { | 1831 | rts.tv_sec = timeout / HZ; |
1828 | ts.tv_sec++; | 1832 | rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ); |
1829 | ts.tv_nsec -= 1000000000; | 1833 | if (rts.tv_nsec >= NSEC_PER_SEC) { |
1834 | rts.tv_sec++; | ||
1835 | rts.tv_nsec -= NSEC_PER_SEC; | ||
1830 | } | 1836 | } |
1831 | (void)copy_to_user(tsp, &ts, sizeof(ts)); | 1837 | if (compat_timespec_compare(&rts, &ts) < 0) |
1838 | rts = ts; | ||
1839 | copy_to_user(tsp, &rts, sizeof(rts)); | ||
1832 | } | 1840 | } |
1833 | 1841 | ||
1834 | if (ret == -ERESTARTNOHAND) { | 1842 | if (ret == -ERESTARTNOHAND) { |
@@ -1918,12 +1926,17 @@ asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds, | |||
1918 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | 1926 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
1919 | 1927 | ||
1920 | if (tsp && timeout >= 0) { | 1928 | if (tsp && timeout >= 0) { |
1929 | struct compat_timespec rts; | ||
1930 | |||
1921 | if (current->personality & STICKY_TIMEOUTS) | 1931 | if (current->personality & STICKY_TIMEOUTS) |
1922 | goto sticky; | 1932 | goto sticky; |
1923 | /* Yes, we know it's actually an s64, but it's also positive. */ | 1933 | /* Yes, we know it's actually an s64, but it's also positive. */ |
1924 | ts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * 1000; | 1934 | rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * |
1925 | ts.tv_sec = timeout; | 1935 | 1000; |
1926 | if (copy_to_user(tsp, &ts, sizeof(ts))) { | 1936 | rts.tv_sec = timeout; |
1937 | if (compat_timespec_compare(&rts, &ts) < 0) | ||
1938 | rts = ts; | ||
1939 | if (copy_to_user(tsp, &rts, sizeof(rts))) { | ||
1927 | sticky: | 1940 | sticky: |
1928 | /* | 1941 | /* |
1929 | * If an application puts its timeval in read-only | 1942 | * If an application puts its timeval in read-only |
diff --git a/fs/select.c b/fs/select.c index bc60a3e14ef3..6ce68a9c8976 100644 --- a/fs/select.c +++ b/fs/select.c | |||
@@ -398,11 +398,15 @@ asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp, | |||
398 | ret = core_sys_select(n, inp, outp, exp, &timeout); | 398 | ret = core_sys_select(n, inp, outp, exp, &timeout); |
399 | 399 | ||
400 | if (tvp) { | 400 | if (tvp) { |
401 | struct timeval rtv; | ||
402 | |||
401 | if (current->personality & STICKY_TIMEOUTS) | 403 | if (current->personality & STICKY_TIMEOUTS) |
402 | goto sticky; | 404 | goto sticky; |
403 | tv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)); | 405 | rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)); |
404 | tv.tv_sec = timeout; | 406 | rtv.tv_sec = timeout; |
405 | if (copy_to_user(tvp, &tv, sizeof(tv))) { | 407 | if (timeval_compare(&rtv, &tv) < 0) |
408 | rtv = tv; | ||
409 | if (copy_to_user(tvp, &rtv, sizeof(rtv))) { | ||
406 | sticky: | 410 | sticky: |
407 | /* | 411 | /* |
408 | * If an application puts its timeval in read-only | 412 | * If an application puts its timeval in read-only |
@@ -460,11 +464,16 @@ asmlinkage long sys_pselect7(int n, fd_set __user *inp, fd_set __user *outp, | |||
460 | ret = core_sys_select(n, inp, outp, exp, &timeout); | 464 | ret = core_sys_select(n, inp, outp, exp, &timeout); |
461 | 465 | ||
462 | if (tsp) { | 466 | if (tsp) { |
467 | struct timespec rts; | ||
468 | |||
463 | if (current->personality & STICKY_TIMEOUTS) | 469 | if (current->personality & STICKY_TIMEOUTS) |
464 | goto sticky; | 470 | goto sticky; |
465 | ts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * 1000; | 471 | rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * |
466 | ts.tv_sec = timeout; | 472 | 1000; |
467 | if (copy_to_user(tsp, &ts, sizeof(ts))) { | 473 | rts.tv_sec = timeout; |
474 | if (timespec_compare(&rts, &ts) < 0) | ||
475 | rts = ts; | ||
476 | if (copy_to_user(tsp, &rts, sizeof(rts))) { | ||
468 | sticky: | 477 | sticky: |
469 | /* | 478 | /* |
470 | * If an application puts its timeval in read-only | 479 | * If an application puts its timeval in read-only |
@@ -758,12 +767,17 @@ asmlinkage long sys_ppoll(struct pollfd __user *ufds, unsigned int nfds, | |||
758 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | 767 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
759 | 768 | ||
760 | if (tsp && timeout >= 0) { | 769 | if (tsp && timeout >= 0) { |
770 | struct timespec rts; | ||
771 | |||
761 | if (current->personality & STICKY_TIMEOUTS) | 772 | if (current->personality & STICKY_TIMEOUTS) |
762 | goto sticky; | 773 | goto sticky; |
763 | /* Yes, we know it's actually an s64, but it's also positive. */ | 774 | /* Yes, we know it's actually an s64, but it's also positive. */ |
764 | ts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * 1000; | 775 | rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * |
765 | ts.tv_sec = timeout; | 776 | 1000; |
766 | if (copy_to_user(tsp, &ts, sizeof(ts))) { | 777 | rts.tv_sec = timeout; |
778 | if (timespec_compare(&rts, &ts) < 0) | ||
779 | rts = ts; | ||
780 | if (copy_to_user(tsp, &rts, sizeof(rts))) { | ||
767 | sticky: | 781 | sticky: |
768 | /* | 782 | /* |
769 | * If an application puts its timeval in read-only | 783 | * If an application puts its timeval in read-only |
diff --git a/include/linux/compat.h b/include/linux/compat.h index f9ca534787e2..c9ab2a26348c 100644 --- a/include/linux/compat.h +++ b/include/linux/compat.h | |||
@@ -161,5 +161,25 @@ int copy_siginfo_to_user32(struct compat_siginfo __user *to, siginfo_t *from); | |||
161 | int get_compat_sigevent(struct sigevent *event, | 161 | int get_compat_sigevent(struct sigevent *event, |
162 | const struct compat_sigevent __user *u_event); | 162 | const struct compat_sigevent __user *u_event); |
163 | 163 | ||
164 | static inline int compat_timeval_compare(struct compat_timeval *lhs, | ||
165 | struct compat_timeval *rhs) | ||
166 | { | ||
167 | if (lhs->tv_sec < rhs->tv_sec) | ||
168 | return -1; | ||
169 | if (lhs->tv_sec > rhs->tv_sec) | ||
170 | return 1; | ||
171 | return lhs->tv_usec - rhs->tv_usec; | ||
172 | } | ||
173 | |||
174 | static inline int compat_timespec_compare(struct compat_timespec *lhs, | ||
175 | struct compat_timespec *rhs) | ||
176 | { | ||
177 | if (lhs->tv_sec < rhs->tv_sec) | ||
178 | return -1; | ||
179 | if (lhs->tv_sec > rhs->tv_sec) | ||
180 | return 1; | ||
181 | return lhs->tv_nsec - rhs->tv_nsec; | ||
182 | } | ||
183 | |||
164 | #endif /* CONFIG_COMPAT */ | 184 | #endif /* CONFIG_COMPAT */ |
165 | #endif /* _LINUX_COMPAT_H */ | 185 | #endif /* _LINUX_COMPAT_H */ |
diff --git a/include/linux/time.h b/include/linux/time.h index 7b4dc36532bb..d9cdba54b789 100644 --- a/include/linux/time.h +++ b/include/linux/time.h | |||
@@ -33,11 +33,34 @@ struct timezone { | |||
33 | #define NSEC_PER_SEC 1000000000L | 33 | #define NSEC_PER_SEC 1000000000L |
34 | #define NSEC_PER_USEC 1000L | 34 | #define NSEC_PER_USEC 1000L |
35 | 35 | ||
36 | static __inline__ int timespec_equal(struct timespec *a, struct timespec *b) | 36 | static inline int timespec_equal(struct timespec *a, struct timespec *b) |
37 | { | 37 | { |
38 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); | 38 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
39 | } | 39 | } |
40 | 40 | ||
41 | /* | ||
42 | * lhs < rhs: return <0 | ||
43 | * lhs == rhs: return 0 | ||
44 | * lhs > rhs: return >0 | ||
45 | */ | ||
46 | static inline int timespec_compare(struct timespec *lhs, struct timespec *rhs) | ||
47 | { | ||
48 | if (lhs->tv_sec < rhs->tv_sec) | ||
49 | return -1; | ||
50 | if (lhs->tv_sec > rhs->tv_sec) | ||
51 | return 1; | ||
52 | return lhs->tv_nsec - rhs->tv_nsec; | ||
53 | } | ||
54 | |||
55 | static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs) | ||
56 | { | ||
57 | if (lhs->tv_sec < rhs->tv_sec) | ||
58 | return -1; | ||
59 | if (lhs->tv_sec > rhs->tv_sec) | ||
60 | return 1; | ||
61 | return lhs->tv_usec - rhs->tv_usec; | ||
62 | } | ||
63 | |||
41 | extern unsigned long mktime(const unsigned int year, const unsigned int mon, | 64 | extern unsigned long mktime(const unsigned int year, const unsigned int mon, |
42 | const unsigned int day, const unsigned int hour, | 65 | const unsigned int day, const unsigned int hour, |
43 | const unsigned int min, const unsigned int sec); | 66 | const unsigned int min, const unsigned int sec); |