Skip to content

Commit 6684ba2

Browse files
author
H. Peter Anvin
committed
compat: Add helper functions to read/write struct timeval, timespec
Add helper functions to read and write struct timeval and struct timespec from userspace. We already had helper functions for reading and writing struct compat_timespec; add a set of functions to do the same with struct timeval, and add a second suite of functions which can be sensitive to COMPAT_USE_64BIT_TIME and access either 32- or 64-bit time structures. This also exports these helper functions to modules. Rename the existing inlines for converting between struct compat_timeval and native struct timespec so we can have a saner naming convention for the exported functions. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: H. Peter Anvin <hpa@zytor.com>
1 parent 45e8778 commit 6684ba2

2 files changed

Lines changed: 76 additions & 8 deletions

File tree

‎include/linux/compat.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,26 @@ typedef struct {
8787
compat_sigset_word sig[_COMPAT_NSIG_WORDS];
8888
} compat_sigset_t;
8989

90+
/*
91+
* These functions operate strictly on struct compat_time*
92+
*/
9093
extern int get_compat_timespec(struct timespec *,
9194
const struct compat_timespec __user *);
9295
extern int put_compat_timespec(const struct timespec *,
9396
struct compat_timespec __user *);
97+
extern int get_compat_timeval(struct timeval *,
98+
const struct compat_timeval __user *);
99+
extern int put_compat_timeval(const struct timeval *,
100+
struct compat_timeval __user *);
101+
/*
102+
* These functions operate on 32- or 64-bit specs depending on
103+
* COMPAT_USE_64BIT_TIME, hence the void user pointer arguments and the
104+
* naming as compat_get/put_ rather than get/put_compat_.
105+
*/
106+
extern int compat_get_timespec(struct timespec *, const void __user *);
107+
extern int compat_put_timespec(const struct timespec *, void __user *);
108+
extern int compat_get_timeval(struct timeval *, const void __user *);
109+
extern int compat_put_timeval(const struct timeval *, void __user *);
94110

95111
struct compat_iovec {
96112
compat_uptr_t iov_base;

‎kernel/compat.c‎

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131
#include <asm/uaccess.h>
3232

3333
/*
34-
* Note that the native side is already converted to a timespec, because
35-
* that's what we want anyway.
34+
* Get/set struct timeval with struct timespec on the native side
3635
*/
37-
static int compat_get_timeval(struct timespec *o,
38-
struct compat_timeval __user *i)
36+
static int compat_get_timeval_convert(struct timespec *o,
37+
struct compat_timeval __user *i)
3938
{
4039
long usec;
4140

@@ -46,8 +45,8 @@ static int compat_get_timeval(struct timespec *o,
4645
return 0;
4746
}
4847

49-
static int compat_put_timeval(struct compat_timeval __user *o,
50-
struct timeval *i)
48+
static int compat_put_timeval_convert(struct compat_timeval __user *o,
49+
struct timeval *i)
5150
{
5251
return (put_user(i->tv_sec, &o->tv_sec) ||
5352
put_user(i->tv_usec, &o->tv_usec)) ? -EFAULT : 0;
@@ -117,7 +116,7 @@ asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv,
117116
if (tv) {
118117
struct timeval ktv;
119118
do_gettimeofday(&ktv);
120-
if (compat_put_timeval(tv, &ktv))
119+
if (compat_put_timeval_convert(tv, &ktv))
121120
return -EFAULT;
122121
}
123122
if (tz) {
@@ -135,7 +134,7 @@ asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
135134
struct timezone ktz;
136135

137136
if (tv) {
138-
if (compat_get_timeval(&kts, tv))
137+
if (compat_get_timeval_convert(&kts, tv))
139138
return -EFAULT;
140139
}
141140
if (tz) {
@@ -146,12 +145,29 @@ asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv,
146145
return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
147146
}
148147

148+
int get_compat_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
149+
{
150+
return (!access_ok(VERIFY_READ, ctv, sizeof(*ctv)) ||
151+
__get_user(tv->tv_sec, &ctv->tv_sec) ||
152+
__get_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
153+
}
154+
EXPORT_SYMBOL_GPL(get_compat_timeval);
155+
156+
int put_compat_timeval(const struct timeval *tv, struct compat_timeval __user *ctv)
157+
{
158+
return (!access_ok(VERIFY_WRITE, ctv, sizeof(*ctv)) ||
159+
__put_user(tv->tv_sec, &ctv->tv_sec) ||
160+
__put_user(tv->tv_usec, &ctv->tv_usec)) ? -EFAULT : 0;
161+
}
162+
EXPORT_SYMBOL_GPL(put_compat_timeval);
163+
149164
int get_compat_timespec(struct timespec *ts, const struct compat_timespec __user *cts)
150165
{
151166
return (!access_ok(VERIFY_READ, cts, sizeof(*cts)) ||
152167
__get_user(ts->tv_sec, &cts->tv_sec) ||
153168
__get_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
154169
}
170+
EXPORT_SYMBOL_GPL(get_compat_timespec);
155171

156172
int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user *cts)
157173
{
@@ -161,6 +177,42 @@ int put_compat_timespec(const struct timespec *ts, struct compat_timespec __user
161177
}
162178
EXPORT_SYMBOL_GPL(put_compat_timespec);
163179

180+
int compat_get_timeval(struct timeval *tv, const void __user *utv)
181+
{
182+
if (COMPAT_USE_64BIT_TIME)
183+
return copy_from_user(tv, utv, sizeof *tv) ? -EFAULT : 0;
184+
else
185+
return get_compat_timeval(tv, utv);
186+
}
187+
EXPORT_SYMBOL_GPL(compat_get_timeval);
188+
189+
int compat_put_timeval(const struct timeval *tv, void __user *utv)
190+
{
191+
if (COMPAT_USE_64BIT_TIME)
192+
return copy_to_user(utv, tv, sizeof *tv) ? -EFAULT : 0;
193+
else
194+
return put_compat_timeval(tv, utv);
195+
}
196+
EXPORT_SYMBOL_GPL(compat_put_timeval);
197+
198+
int compat_get_timespec(struct timespec *ts, const void __user *uts)
199+
{
200+
if (COMPAT_USE_64BIT_TIME)
201+
return copy_from_user(ts, uts, sizeof *ts) ? -EFAULT : 0;
202+
else
203+
return get_compat_timespec(ts, uts);
204+
}
205+
EXPORT_SYMBOL_GPL(compat_get_timespec);
206+
207+
int compat_put_timespec(const struct timespec *ts, void __user *uts)
208+
{
209+
if (COMPAT_USE_64BIT_TIME)
210+
return copy_to_user(uts, ts, sizeof *ts) ? -EFAULT : 0;
211+
else
212+
return put_compat_timespec(ts, uts);
213+
}
214+
EXPORT_SYMBOL_GPL(compat_put_timespec);
215+
164216
static long compat_nanosleep_restart(struct restart_block *restart)
165217
{
166218
struct compat_timespec __user *rmtp;

0 commit comments

Comments
 (0)