1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Linaro Limited. All rights reserved.
4 * Author: Viresh Kumar<viresh.kumar@linaro.org>
5 *
6 * Check Year 2038 related vulnerabilities.
7 */
8
9 #include <signal.h>
10 #include "config.h"
11 #include "time64_variants.h"
12 #include "tst_timer.h"
13 #include "tst_safe_clocks.h"
14
15 #define TIMER_DELTA 3
16 #define ALLOWED_DELTA (50 * 1000) /* 50 ms */
17
18 static struct tst_ts start, end;
19 static struct tst_its its;
20
21 static struct time64_variants variants[] = {
22 #if (__NR_clock_settime != __LTP__NR_INVALID_SYSCALL)
23 { .clock_gettime = sys_clock_gettime, .clock_settime = sys_clock_settime, .timer_settime = sys_timer_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
24 #endif
25
26 #if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
27 { .clock_gettime = sys_clock_gettime64, .clock_settime = sys_clock_settime64, .timer_settime = sys_timer_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
28 #endif
29 };
30
setup(void)31 static void setup(void)
32 {
33 struct time64_variants *tv = &variants[tst_variant];
34
35 tst_res(TINFO, "Testing variant: %s", tv->desc);
36 start.type = end.type = its.type = tv->ts_type;
37
38 /* Check if the kernel is y2038 safe */
39 if (tv->ts_type == TST_KERN_OLD_TIMESPEC &&
40 sizeof(start.ts.kern_old_ts.tv_sec) == 4) {
41 tst_brk(TCONF, "Not Y2038 safe to run test");
42 }
43 }
44
run(void)45 static void run(void)
46 {
47 struct time64_variants *tv = &variants[tst_variant];
48 long long time = 0x7FFFFFFE; /* Time just before y2038 */
49 struct sigevent ev = {
50 .sigev_notify = SIGEV_SIGNAL,
51 .sigev_signo = SIGABRT,
52 };
53 long long diff;
54 kernel_timer_t timer;
55 sigset_t set;
56 int sig, ret;
57
58 SAFE_SIGEMPTYSET(&set);
59 SAFE_SIGADDSET(&set, SIGABRT);
60 SAFE_SIGPROCMASK(SIG_BLOCK, &set, NULL);
61
62 TEST(tst_syscall(__NR_timer_create, CLOCK_REALTIME, &ev, &timer));
63 if (TST_RET != 0)
64 tst_brk(TBROK | TTERRNO, "timer_create() failed");
65
66 tst_ts_set_sec(&start, time);
67 tst_ts_set_nsec(&start, 0);
68
69 ret = tv->clock_settime(CLOCK_REALTIME, tst_ts_get(&start));
70 if (ret == -1)
71 tst_brk(TBROK | TERRNO, "clock_settime() failed");
72
73 tst_its_set_interval_sec(&its, 0);
74 tst_its_set_interval_nsec(&its, 0);
75 tst_its_set_value_sec(&its, time + TIMER_DELTA);
76 tst_its_set_value_nsec(&its, 0);
77
78 TEST(tv->timer_settime(timer, TIMER_ABSTIME, tst_its_get(&its), NULL));
79 if (TST_RET == -1)
80 tst_brk(TBROK | TTERRNO, "timer_settime() failed");
81
82 SAFE_SIGWAIT(&set, &sig);
83
84 ret = tv->clock_gettime(CLOCK_REALTIME, tst_ts_get(&end));
85 if (ret == -1)
86 tst_brk(TBROK | TERRNO, "clock_gettime() failed");
87
88 if (sig != SIGABRT) {
89 tst_res(TFAIL, "clock_settime(): Y2038 test failed");
90 return;
91 }
92
93 diff = tst_ts_diff_ms(end, start);
94
95 if (diff < TIMER_DELTA * 1000) {
96 tst_res(TFAIL, "Timer expired too soon, after %llims", diff);
97 return;
98 }
99
100 if (diff > TIMER_DELTA * 1000 + ALLOWED_DELTA)
101 tst_res(TINFO, "Time expired too late, after %llims", diff);
102 else
103 tst_res(TPASS, "clock_settime(): Y2038 test passed");
104 }
105
106 static struct tst_test test = {
107 .test_all = run,
108 .test_variants = ARRAY_SIZE(variants),
109 .setup = setup,
110 .needs_root = 1,
111 .restore_wallclock = 1,
112 };
113