Skip to content

Commit ead2541

Browse files
deepa-hubarndb
authored andcommitted
timex: use __kernel_timex internally
struct timex is not y2038 safe. Replace all uses of timex with y2038 safe __kernel_timex. Note that struct __kernel_timex is an ABI interface definition. We could define a new structure based on __kernel_timex that is only available internally instead. Right now, there isn't a strong motivation for this as the structure is isolated to a few defined struct timex interfaces and such a structure would be exactly the same as struct timex. The patch was generated by the following coccinelle script: virtual patch @Depends on patch forall@ identifier ts; expression e; @@ ( - struct timex ts; + struct __kernel_timex ts; | - struct timex ts = {}; + struct __kernel_timex ts = {}; | - struct timex ts = e; + struct __kernel_timex ts = e; | - struct timex *ts; + struct __kernel_timex *ts; | (memset \| copy_from_user \| copy_to_user \)(..., - sizeof(struct timex)) + sizeof(struct __kernel_timex)) ) @Depends on patch forall@ identifier ts; identifier fn; @@ fn(..., - struct timex *ts, + struct __kernel_timex *ts, ...) { ... } @Depends on patch forall@ identifier ts; identifier fn; @@ fn(..., - struct timex *ts) { + struct __kernel_timex *ts) { ... } Signed-off-by: Deepa Dinamani <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Arnd Bergmann <[email protected]>
1 parent 1a59639 commit ead2541

File tree

13 files changed

+38
-35
lines changed

13 files changed

+38
-35
lines changed

arch/alpha/kernel/osf_sys.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ struct timex32 {
12531253

12541254
SYSCALL_DEFINE1(old_adjtimex, struct timex32 __user *, txc_p)
12551255
{
1256-
struct timex txc;
1256+
struct __kernel_timex txc;
12571257
int ret;
12581258

12591259
/* copy relevant bits of struct timex. */
@@ -1270,7 +1270,8 @@ SYSCALL_DEFINE1(old_adjtimex, struct timex32 __user *, txc_p)
12701270
if (copy_to_user(txc_p, &txc, offsetof(struct timex32, time)) ||
12711271
(copy_to_user(&txc_p->tick, &txc.tick, sizeof(struct timex32) -
12721272
offsetof(struct timex32, tick))) ||
1273-
(put_tv_to_tv32(&txc_p->time, &txc.time)))
1273+
(put_user(txc.time.tv_sec, &txc_p->time.tv_sec)) ||
1274+
(put_user(txc.time.tv_usec, &txc_p->time.tv_usec)))
12741275
return -EFAULT;
12751276

12761277
return ret;

arch/sparc/kernel/sys_sparc_64.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ SYSCALL_DEFINE2(getdomainname, char __user *, name, int, len)
548548
SYSCALL_DEFINE1(sparc_adjtimex, struct timex __user *, txc_p)
549549
{
550550
struct timex txc; /* Local copy of parameter */
551-
struct timex *kt = (void *)&txc;
551+
struct __kernel_timex *kt = (void *)&txc;
552552
int ret;
553553

554554
/* Copy the user data space into the kernel copy
@@ -572,7 +572,7 @@ SYSCALL_DEFINE1(sparc_adjtimex, struct timex __user *, txc_p)
572572
SYSCALL_DEFINE2(sparc_clock_adjtime, const clockid_t, which_clock,struct timex __user *, txc_p)
573573
{
574574
struct timex txc; /* Local copy of parameter */
575-
struct timex *kt = (void *)&txc;
575+
struct __kernel_timex *kt = (void *)&txc;
576576
int ret;
577577

578578
if (!IS_ENABLED(CONFIG_POSIX_TIMERS)) {

drivers/ptp/ptp_clock.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp)
124124
return err;
125125
}
126126

127-
static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx)
127+
static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
128128
{
129129
struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
130130
struct ptp_clock_info *ops;

include/linux/posix-clock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct posix_clock;
5151
struct posix_clock_operations {
5252
struct module *owner;
5353

54-
int (*clock_adjtime)(struct posix_clock *pc, struct timex *tx);
54+
int (*clock_adjtime)(struct posix_clock *pc, struct __kernel_timex *tx);
5555

5656
int (*clock_gettime)(struct posix_clock *pc, struct timespec64 *ts);
5757

include/linux/time32.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ extern int get_old_itimerspec32(struct itimerspec64 *its,
6969
const struct old_itimerspec32 __user *uits);
7070
extern int put_old_itimerspec32(const struct itimerspec64 *its,
7171
struct old_itimerspec32 __user *uits);
72-
struct timex;
73-
int get_old_timex32(struct timex *, const struct old_timex32 __user *);
74-
int put_old_timex32(struct old_timex32 __user *, const struct timex *);
72+
struct __kernel_timex;
73+
int get_old_timex32(struct __kernel_timex *, const struct old_timex32 __user *);
74+
int put_old_timex32(struct old_timex32 __user *, const struct __kernel_timex *);
7575

7676
#if __BITS_PER_LONG == 64
7777

include/linux/timex.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ extern unsigned long tick_nsec; /* SHIFTED_HZ period (nsec) */
158158
#define NTP_INTERVAL_FREQ (HZ)
159159
#define NTP_INTERVAL_LENGTH (NSEC_PER_SEC/NTP_INTERVAL_FREQ)
160160

161-
extern int do_adjtimex(struct timex *);
162-
extern int do_clock_adjtime(const clockid_t which_clock, struct timex * ktx);
161+
extern int do_adjtimex(struct __kernel_timex *);
162+
extern int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx);
163163

164164
extern void hardpps(const struct timespec64 *, const struct timespec64 *);
165165

kernel/time/ntp.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ static inline int is_error_status(int status)
188188
&& (status & (STA_PPSWANDER|STA_PPSERROR)));
189189
}
190190

191-
static inline void pps_fill_timex(struct timex *txc)
191+
static inline void pps_fill_timex(struct __kernel_timex *txc)
192192
{
193193
txc->ppsfreq = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) *
194194
PPM_SCALE_INV, NTP_SCALE_SHIFT);
195195
txc->jitter = pps_jitter;
196196
if (!(time_status & STA_NANO))
197-
txc->jitter /= NSEC_PER_USEC;
197+
txc->jitter = pps_jitter / NSEC_PER_USEC;
198198
txc->shift = pps_shift;
199199
txc->stabil = pps_stabil;
200200
txc->jitcnt = pps_jitcnt;
@@ -220,7 +220,7 @@ static inline int is_error_status(int status)
220220
return status & (STA_UNSYNC|STA_CLOCKERR);
221221
}
222222

223-
static inline void pps_fill_timex(struct timex *txc)
223+
static inline void pps_fill_timex(struct __kernel_timex *txc)
224224
{
225225
/* PPS is not implemented, so these are zero */
226226
txc->ppsfreq = 0;
@@ -633,7 +633,7 @@ void ntp_notify_cmos_timer(void)
633633
/*
634634
* Propagate a new txc->status value into the NTP state:
635635
*/
636-
static inline void process_adj_status(const struct timex *txc)
636+
static inline void process_adj_status(const struct __kernel_timex *txc)
637637
{
638638
if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
639639
time_state = TIME_OK;
@@ -656,7 +656,8 @@ static inline void process_adj_status(const struct timex *txc)
656656
}
657657

658658

659-
static inline void process_adjtimex_modes(const struct timex *txc, s32 *time_tai)
659+
static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
660+
s32 *time_tai)
660661
{
661662
if (txc->modes & ADJ_STATUS)
662663
process_adj_status(txc);
@@ -707,7 +708,8 @@ static inline void process_adjtimex_modes(const struct timex *txc, s32 *time_tai
707708
* adjtimex mainly allows reading (and writing, if superuser) of
708709
* kernel time-keeping variables. used by xntpd.
709710
*/
710-
int __do_adjtimex(struct timex *txc, const struct timespec64 *ts, s32 *time_tai)
711+
int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
712+
s32 *time_tai)
711713
{
712714
int result;
713715

@@ -729,7 +731,7 @@ int __do_adjtimex(struct timex *txc, const struct timespec64 *ts, s32 *time_tai)
729731
txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
730732
NTP_SCALE_SHIFT);
731733
if (!(time_status & STA_NANO))
732-
txc->offset /= NSEC_PER_USEC;
734+
txc->offset = (u32)txc->offset / NSEC_PER_USEC;
733735
}
734736

735737
result = time_state; /* mostly `TIME_OK' */
@@ -754,7 +756,7 @@ int __do_adjtimex(struct timex *txc, const struct timespec64 *ts, s32 *time_tai)
754756
txc->time.tv_sec = (time_t)ts->tv_sec;
755757
txc->time.tv_usec = ts->tv_nsec;
756758
if (!(time_status & STA_NANO))
757-
txc->time.tv_usec /= NSEC_PER_USEC;
759+
txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
758760

759761
/* Handle leapsec adjustments */
760762
if (unlikely(ts->tv_sec >= ntp_next_leap_sec)) {

kernel/time/ntp_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ extern void ntp_clear(void);
88
extern u64 ntp_tick_length(void);
99
extern ktime_t ntp_get_next_leap(void);
1010
extern int second_overflow(time64_t secs);
11-
extern int __do_adjtimex(struct timex *txc, const struct timespec64 *ts, s32 *time_tai);
11+
extern int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, s32 *time_tai);
1212
extern void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts);
1313
#endif /* _LINUX_NTP_INTERNAL_H */

kernel/time/posix-clock.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static void put_clock_desc(struct posix_clock_desc *cd)
228228
fput(cd->fp);
229229
}
230230

231-
static int pc_clock_adjtime(clockid_t id, struct timex *tx)
231+
static int pc_clock_adjtime(clockid_t id, struct __kernel_timex *tx)
232232
{
233233
struct posix_clock_desc cd;
234234
int err;

kernel/time/posix-timers.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static int posix_clock_realtime_set(const clockid_t which_clock,
179179
}
180180

181181
static int posix_clock_realtime_adj(const clockid_t which_clock,
182-
struct timex *t)
182+
struct __kernel_timex *t)
183183
{
184184
return do_adjtimex(t);
185185
}
@@ -1047,7 +1047,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
10471047
return error;
10481048
}
10491049

1050-
int do_clock_adjtime(const clockid_t which_clock, struct timex * ktx)
1050+
int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
10511051
{
10521052
const struct k_clock *kc = clockid_to_kclock(which_clock);
10531053

@@ -1062,7 +1062,7 @@ int do_clock_adjtime(const clockid_t which_clock, struct timex * ktx)
10621062
SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
10631063
struct timex __user *, utx)
10641064
{
1065-
struct timex ktx;
1065+
struct __kernel_timex ktx;
10661066
int err;
10671067

10681068
if (copy_from_user(&ktx, utx, sizeof(ktx)))
@@ -1132,7 +1132,7 @@ COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
11321132
COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
11331133
struct old_timex32 __user *, utp)
11341134
{
1135-
struct timex ktx;
1135+
struct __kernel_timex ktx;
11361136
int err;
11371137

11381138
err = get_old_timex32(&ktx, utp);

kernel/time/posix-timers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct k_clock {
88
const struct timespec64 *tp);
99
int (*clock_get)(const clockid_t which_clock,
1010
struct timespec64 *tp);
11-
int (*clock_adj)(const clockid_t which_clock, struct timex *tx);
11+
int (*clock_adj)(const clockid_t which_clock, struct __kernel_timex *tx);
1212
int (*timer_create)(struct k_itimer *timer);
1313
int (*nsleep)(const clockid_t which_clock, int flags,
1414
const struct timespec64 *);

kernel/time/time.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,25 @@ COMPAT_SYSCALL_DEFINE2(settimeofday, struct old_timeval32 __user *, tv,
265265

266266
SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
267267
{
268-
struct timex txc; /* Local copy of parameter */
268+
struct __kernel_timex txc; /* Local copy of parameter */
269269
int ret;
270270

271271
/* Copy the user data space into the kernel copy
272272
* structure. But bear in mind that the structures
273273
* may change
274274
*/
275-
if (copy_from_user(&txc, txc_p, sizeof(struct timex)))
275+
if (copy_from_user(&txc, txc_p, sizeof(struct __kernel_timex)))
276276
return -EFAULT;
277277
ret = do_adjtimex(&txc);
278-
return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
278+
return copy_to_user(txc_p, &txc, sizeof(struct __kernel_timex)) ? -EFAULT : ret;
279279
}
280280

281281
#ifdef CONFIG_COMPAT_32BIT_TIME
282-
int get_old_timex32(struct timex *txc, const struct old_timex32 __user *utp)
282+
int get_old_timex32(struct __kernel_timex *txc, const struct old_timex32 __user *utp)
283283
{
284284
struct old_timex32 tx32;
285285

286-
memset(txc, 0, sizeof(struct timex));
286+
memset(txc, 0, sizeof(struct __kernel_timex));
287287
if (copy_from_user(&tx32, utp, sizeof(struct old_timex32)))
288288
return -EFAULT;
289289

@@ -311,7 +311,7 @@ int get_old_timex32(struct timex *txc, const struct old_timex32 __user *utp)
311311
return 0;
312312
}
313313

314-
int put_old_timex32(struct old_timex32 __user *utp, const struct timex *txc)
314+
int put_old_timex32(struct old_timex32 __user *utp, const struct __kernel_timex *txc)
315315
{
316316
struct old_timex32 tx32;
317317

@@ -344,7 +344,7 @@ int put_old_timex32(struct old_timex32 __user *utp, const struct timex *txc)
344344

345345
COMPAT_SYSCALL_DEFINE1(adjtimex, struct old_timex32 __user *, utp)
346346
{
347-
struct timex txc;
347+
struct __kernel_timex txc;
348348
int err, ret;
349349

350350
err = get_old_timex32(&txc, utp);

kernel/time/timekeeping.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
22342234
/**
22352235
* timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex
22362236
*/
2237-
static int timekeeping_validate_timex(const struct timex *txc)
2237+
static int timekeeping_validate_timex(const struct __kernel_timex *txc)
22382238
{
22392239
if (txc->modes & ADJ_ADJTIME) {
22402240
/* singleshot must not be used with any other mode bits */
@@ -2300,7 +2300,7 @@ static int timekeeping_validate_timex(const struct timex *txc)
23002300
/**
23012301
* do_adjtimex() - Accessor function to NTP __do_adjtimex function
23022302
*/
2303-
int do_adjtimex(struct timex *txc)
2303+
int do_adjtimex(struct __kernel_timex *txc)
23042304
{
23052305
struct timekeeper *tk = &tk_core.timekeeper;
23062306
unsigned long flags;

0 commit comments

Comments
 (0)