Skip to content

Commit 3a4d44b

Browse files
Al ViroKAGA-KOKO
authored andcommitted
ntp: Move adjtimex related compat syscalls to native counterparts
Get rid of set_fs() mess and sanitize compat_{get,put}_timex(), while we are at it. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: John Stultz <john.stultz@linaro.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20170607084241.28657-9-viro@ZenIV.linux.org.uk
1 parent fb923c4 commit 3a4d44b

5 files changed

Lines changed: 108 additions & 90 deletions

File tree

‎include/linux/compat.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ struct compat_timex {
128128
compat_int_t:32; compat_int_t:32; compat_int_t:32;
129129
};
130130

131+
struct timex;
132+
int compat_get_timex(struct timex *, const struct compat_timex __user *);
133+
int compat_put_timex(struct compat_timex __user *, const struct timex *);
134+
131135
#define _COMPAT_NSIG_WORDS (_COMPAT_NSIG / _COMPAT_NSIG_BPW)
132136

133137
typedef struct {

‎kernel/compat.c‎

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,64 @@
3030

3131
#include <linux/uaccess.h>
3232

33-
static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp)
33+
int compat_get_timex(struct timex *txc, const struct compat_timex __user *utp)
3434
{
35-
memset(txc, 0, sizeof(struct timex));
36-
37-
if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
38-
__get_user(txc->modes, &utp->modes) ||
39-
__get_user(txc->offset, &utp->offset) ||
40-
__get_user(txc->freq, &utp->freq) ||
41-
__get_user(txc->maxerror, &utp->maxerror) ||
42-
__get_user(txc->esterror, &utp->esterror) ||
43-
__get_user(txc->status, &utp->status) ||
44-
__get_user(txc->constant, &utp->constant) ||
45-
__get_user(txc->precision, &utp->precision) ||
46-
__get_user(txc->tolerance, &utp->tolerance) ||
47-
__get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
48-
__get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
49-
__get_user(txc->tick, &utp->tick) ||
50-
__get_user(txc->ppsfreq, &utp->ppsfreq) ||
51-
__get_user(txc->jitter, &utp->jitter) ||
52-
__get_user(txc->shift, &utp->shift) ||
53-
__get_user(txc->stabil, &utp->stabil) ||
54-
__get_user(txc->jitcnt, &utp->jitcnt) ||
55-
__get_user(txc->calcnt, &utp->calcnt) ||
56-
__get_user(txc->errcnt, &utp->errcnt) ||
57-
__get_user(txc->stbcnt, &utp->stbcnt))
35+
struct compat_timex tx32;
36+
37+
if (copy_from_user(&tx32, utp, sizeof(struct compat_timex)))
5838
return -EFAULT;
5939

40+
txc->modes = tx32.modes;
41+
txc->offset = tx32.offset;
42+
txc->freq = tx32.freq;
43+
txc->maxerror = tx32.maxerror;
44+
txc->esterror = tx32.esterror;
45+
txc->status = tx32.status;
46+
txc->constant = tx32.constant;
47+
txc->precision = tx32.precision;
48+
txc->tolerance = tx32.tolerance;
49+
txc->time.tv_sec = tx32.time.tv_sec;
50+
txc->time.tv_usec = tx32.time.tv_usec;
51+
txc->tick = tx32.tick;
52+
txc->ppsfreq = tx32.ppsfreq;
53+
txc->jitter = tx32.jitter;
54+
txc->shift = tx32.shift;
55+
txc->stabil = tx32.stabil;
56+
txc->jitcnt = tx32.jitcnt;
57+
txc->calcnt = tx32.calcnt;
58+
txc->errcnt = tx32.errcnt;
59+
txc->stbcnt = tx32.stbcnt;
60+
6061
return 0;
6162
}
6263

63-
static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
64-
{
65-
if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
66-
__put_user(txc->modes, &utp->modes) ||
67-
__put_user(txc->offset, &utp->offset) ||
68-
__put_user(txc->freq, &utp->freq) ||
69-
__put_user(txc->maxerror, &utp->maxerror) ||
70-
__put_user(txc->esterror, &utp->esterror) ||
71-
__put_user(txc->status, &utp->status) ||
72-
__put_user(txc->constant, &utp->constant) ||
73-
__put_user(txc->precision, &utp->precision) ||
74-
__put_user(txc->tolerance, &utp->tolerance) ||
75-
__put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
76-
__put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
77-
__put_user(txc->tick, &utp->tick) ||
78-
__put_user(txc->ppsfreq, &utp->ppsfreq) ||
79-
__put_user(txc->jitter, &utp->jitter) ||
80-
__put_user(txc->shift, &utp->shift) ||
81-
__put_user(txc->stabil, &utp->stabil) ||
82-
__put_user(txc->jitcnt, &utp->jitcnt) ||
83-
__put_user(txc->calcnt, &utp->calcnt) ||
84-
__put_user(txc->errcnt, &utp->errcnt) ||
85-
__put_user(txc->stbcnt, &utp->stbcnt) ||
86-
__put_user(txc->tai, &utp->tai))
64+
int compat_put_timex(struct compat_timex __user *utp, const struct timex *txc)
65+
{
66+
struct compat_timex tx32;
67+
68+
memset(&tx32, 0, sizeof(struct compat_timex));
69+
tx32.modes = txc->modes;
70+
tx32.offset = txc->offset;
71+
tx32.freq = txc->freq;
72+
tx32.maxerror = txc->maxerror;
73+
tx32.esterror = txc->esterror;
74+
tx32.status = txc->status;
75+
tx32.constant = txc->constant;
76+
tx32.precision = txc->precision;
77+
tx32.tolerance = txc->tolerance;
78+
tx32.time.tv_sec = txc->time.tv_sec;
79+
tx32.time.tv_usec = txc->time.tv_usec;
80+
tx32.tick = txc->tick;
81+
tx32.ppsfreq = txc->ppsfreq;
82+
tx32.jitter = txc->jitter;
83+
tx32.shift = txc->shift;
84+
tx32.stabil = txc->stabil;
85+
tx32.jitcnt = txc->jitcnt;
86+
tx32.calcnt = txc->calcnt;
87+
tx32.errcnt = txc->errcnt;
88+
tx32.stbcnt = txc->stbcnt;
89+
tx32.tai = txc->tai;
90+
if (copy_to_user(utp, &tx32, sizeof(struct compat_timex)))
8791
return -EFAULT;
8892
return 0;
8993
}
@@ -705,29 +709,6 @@ COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
705709
return err;
706710
}
707711

708-
COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
709-
struct compat_timex __user *, utp)
710-
{
711-
struct timex txc;
712-
mm_segment_t oldfs;
713-
int err, ret;
714-
715-
err = compat_get_timex(&txc, utp);
716-
if (err)
717-
return err;
718-
719-
oldfs = get_fs();
720-
set_fs(KERNEL_DS);
721-
ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
722-
set_fs(oldfs);
723-
724-
err = compat_put_timex(utp, &txc);
725-
if (err)
726-
return err;
727-
728-
return ret;
729-
}
730-
731712
COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
732713
struct compat_timespec __user *, tp)
733714
{
@@ -944,24 +925,6 @@ COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
944925

945926
#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
946927

947-
COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
948-
{
949-
struct timex txc;
950-
int err, ret;
951-
952-
err = compat_get_timex(&txc, utp);
953-
if (err)
954-
return err;
955-
956-
ret = do_adjtimex(&txc);
957-
958-
err = compat_put_timex(utp, &txc);
959-
if (err)
960-
return err;
961-
962-
return ret;
963-
}
964-
965928
#ifdef CONFIG_NUMA
966929
COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
967930
compat_uptr_t __user *, pages32,

‎kernel/time/posix-stubs.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ asmlinkage long sys_ni_posix_timers(void)
2828
}
2929

3030
#define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
31+
#define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
3132

3233
SYS_NI(timer_create);
3334
SYS_NI(timer_gettime);
@@ -40,6 +41,7 @@ SYS_NI(setitimer);
4041
#ifdef __ARCH_WANT_SYS_ALARM
4142
SYS_NI(alarm);
4243
#endif
44+
COMPAT_SYS_NI(clock_adjtime);
4345

4446
/*
4547
* We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC

‎kernel/time/posix-timers.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,33 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
10181018
return err;
10191019
}
10201020

1021+
#ifdef CONFIG_COMPAT
1022+
1023+
COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1024+
struct compat_timex __user *, utp)
1025+
{
1026+
const struct k_clock *kc = clockid_to_kclock(which_clock);
1027+
struct timex ktx;
1028+
int err;
1029+
1030+
if (!kc)
1031+
return -EINVAL;
1032+
if (!kc->clock_adj)
1033+
return -EOPNOTSUPP;
1034+
1035+
err = compat_get_timex(&ktx, utp);
1036+
if (err)
1037+
return err;
1038+
1039+
err = kc->clock_adj(which_clock, &ktx);
1040+
1041+
if (err >= 0)
1042+
err = compat_put_timex(utp, &ktx);
1043+
1044+
return err;
1045+
}
1046+
#endif
1047+
10211048
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
10221049
struct timespec __user *, tp)
10231050
{

‎kernel/time/time.c‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/ptrace.h>
4040

4141
#include <linux/uaccess.h>
42+
#include <linux/compat.h>
4243
#include <asm/unistd.h>
4344

4445
#include <generated/timeconst.h>
@@ -224,12 +225,33 @@ SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
224225
* structure. But bear in mind that the structures
225226
* may change
226227
*/
227-
if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
228+
if (copy_from_user(&txc, txc_p, sizeof(struct timex)))
228229
return -EFAULT;
229230
ret = do_adjtimex(&txc);
230231
return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
231232
}
232233

234+
#ifdef CONFIG_COMPAT
235+
236+
COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
237+
{
238+
struct timex txc;
239+
int err, ret;
240+
241+
err = compat_get_timex(&txc, utp);
242+
if (err)
243+
return err;
244+
245+
ret = do_adjtimex(&txc);
246+
247+
err = compat_put_timex(utp, &txc);
248+
if (err)
249+
return err;
250+
251+
return ret;
252+
}
253+
#endif
254+
233255
/*
234256
* Convert jiffies to milliseconds and back.
235257
*

0 commit comments

Comments
 (0)