• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 Linaro Limited. All rights reserved.
4  * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
5  */
6 
7 /*
8  * clock_adjtime() syscall might have as execution path:
9  *
10  *   1) a regular POSIX clock (only REALTIME clock implements adjtime())
11  *      - will behave exactly like adjtimex() system call.
12  *      - only one being tested here.
13  *
14  *   2) a dynamic POSIX clock (which ops are implemented by PTP clocks)
15  *      - will trigger the PTP clock driver function "adjtime()"
16  *      - different implementations from one PTP clock to another
17  *      - might return EOPNOTSUPP (like ptp_kvm_caps, for example)
18  *      - no entry point for clock_adjtime(), missing "CLOCK_PTP" model
19  *
20  * so it is sane to check for the following errors:
21  *
22  *   EINVAL -  clock id being used does not exist
23  *
24  *   EFAULT - (struct timex *) does not point to valid memory
25  *
26  *   EINVAL - ADJ_OFFSET + .offset outside range -512000 < x < 512000
27  *            (after 2.6.26, kernels normalize to the limit if outside range)
28  *
29  *   EINVAL - ADJ_FREQUENCY + .freq outside range -32768000 < x < 3276800
30  *            (after 2.6.26, kernels normalize to the limit if outside range)
31  *
32  *   EINVAL - .tick outside permitted range (900000/HZ < .tick < 1100000/HZ)
33  *
34  *   EPERM  - .modes is neither 0 nor ADJ_OFFSET_SS_READ (CAP_SYS_TIME required)
35  *
36  *   EINVAL - .status other than those listed bellow
37  *
38  * For ADJ_STATUS, consider the following flags:
39  *
40  *      rw  STA_PLL - enable phase-locked loop updates (ADJ_OFFSET)
41  *      rw  STA_PPSFREQ - enable PPS (pulse-per-second) freq discipline
42  *      rw  STA_PPSTIME - enable PPS time discipline
43  *      rw  STA_FLL - select freq-locked loop mode.
44  *      rw  STA_INS - ins leap sec after the last sec of UTC day (all days)
45  *      rw  STA_DEL - del leap sec at last sec of UTC day (all days)
46  *      rw  STA_UNSYNC - clock unsynced
47  *      rw  STA_FREQHOLD - hold freq. ADJ_OFFSET made w/out auto small adjs
48  *      ro  STA_PPSSIGNAL - valid PPS (pulse-per-second) signal is present
49  *      ro  STA_PPSJITTER - PPS signal jitter exceeded.
50  *      ro  STA_PPSWANDER - PPS signal wander exceeded.
51  *      ro  STA_PPSERROR - PPS signal calibration error.
52  *      ro  STA_CLOKERR - clock HW fault.
53  *      ro  STA_NANO - 0 = us, 1 = ns (set = ADJ_NANO, cl = ADJ_MICRO)
54  *      rw  STA_MODE - mode: 0 = phased locked loop. 1 = freq locked loop
55  *      ro  STA_CLK - clock source. unused.
56  */
57 
58 #include "clock_adjtime.h"
59 
60 static long hz;
61 static struct tst_timex saved, ttxc;
62 static int supported;
63 static void *bad_addr;
64 
65 static void cleanup(void);
66 
67 struct test_case {
68 	clockid_t clktype;
69 	unsigned int modes;
70 	long lowlimit;
71 	long highlimit;
72 	long delta;
73 	int exp_err;
74 	int droproot;
75 };
76 
77 struct test_case tc[] = {
78 	{
79 	 .clktype = MAX_CLOCKS,
80 	 .exp_err = EINVAL,
81 	},
82 	{
83 	 .clktype = MAX_CLOCKS + 1,
84 	 .exp_err = EINVAL,
85 	},
86 	{
87 	 .clktype = CLOCK_REALTIME,
88 	 .modes = ADJ_ALL,
89 	 .exp_err = EFAULT,
90 	},
91 	{
92 	 .clktype = CLOCK_REALTIME,
93 	 .modes = ADJ_TICK,
94 	 .lowlimit = 900000,
95 	 .delta = 1,
96 	 .exp_err = EINVAL,
97 	},
98 	{
99 	 .clktype = CLOCK_REALTIME,
100 	 .modes = ADJ_TICK,
101 	 .highlimit = 1100000,
102 	 .delta = 1,
103 	 .exp_err = EINVAL,
104 	},
105 	{
106 	 .clktype = CLOCK_REALTIME,
107 	 .modes = ADJ_ALL,
108 	 .exp_err = EPERM,
109 	 .droproot = 1,
110 	},
111 };
112 
113 static struct test_variants {
114 	int (*clock_adjtime)(clockid_t clk_id, void *timex);
115 	enum tst_timex_type type;
116 	char *desc;
117 } variants[] = {
118 #if (__NR_clock_adjtime != __LTP__NR_INVALID_SYSCALL)
119 	{.clock_adjtime = sys_clock_adjtime, .type = TST_KERN_OLD_TIMEX, .desc = "syscall with old kernel spec"},
120 #endif
121 
122 #if (__NR_clock_adjtime64 != __LTP__NR_INVALID_SYSCALL)
123 	{.clock_adjtime = sys_clock_adjtime64, .type = TST_KERN_TIMEX, .desc = "syscall time64 with kernel spec"},
124 #endif
125 };
126 
verify_clock_adjtime(unsigned int i)127 static void verify_clock_adjtime(unsigned int i)
128 {
129 	struct test_variants *tv = &variants[tst_variant];
130 	uid_t whoami = 0;
131 	struct tst_timex *txcptr = &ttxc;
132 	struct passwd *nobody;
133 	static const char name[] = "nobody";
134 	int rval;
135 
136 	memset(txcptr, 0, sizeof(*txcptr));
137 
138 	txcptr->type = tv->type;
139 	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(txcptr));
140 	if (rval < 0) {
141 		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
142 		return;
143 	}
144 
145 	timex_show("GET", txcptr);
146 
147 	if (tc[i].droproot) {
148 		nobody = SAFE_GETPWNAM(name);
149 		whoami = nobody->pw_uid;
150 		SAFE_SETEUID(whoami);
151 	}
152 
153 	timex_set_field_uint(txcptr, ADJ_MODES, tc[i].modes);
154 
155 	if (tc[i].delta) {
156 		if (tc[i].lowlimit)
157 			timex_set_field_long(&ttxc, tc[i].modes, tc[i].lowlimit - tc[i].delta);
158 
159 		if (tc[i].highlimit)
160 			timex_set_field_long(&ttxc, tc[i].modes, tc[i].highlimit + tc[i].delta);
161 	}
162 
163 	/* special case: EFAULT for bad addresses */
164 	if (tc[i].exp_err == EFAULT) {
165 		TEST(tv->clock_adjtime(tc[i].clktype, bad_addr));
166 	} else {
167 		TEST(tv->clock_adjtime(tc[i].clktype, tst_timex_get(txcptr)));
168 		timex_show("TEST", txcptr);
169 	}
170 
171 	if (TST_RET >= 0) {
172 		tst_res(TFAIL, "clock_adjtime(): passed unexpectedly (mode=%x, "
173 				"uid=%d)", tc[i].modes, whoami);
174 		return;
175 	}
176 
177 	if (tc[i].exp_err != TST_ERR) {
178 		tst_res(TFAIL | TTERRNO, "clock_adjtime(): expected %d but "
179 				"failed with %d (mode=%x, uid=%d)",
180 				tc[i].exp_err, TST_ERR, tc[i].modes, whoami);
181 		return;
182 	}
183 
184 	tst_res(TPASS, "clock_adjtime(): failed as expected (mode=0x%x, "
185 			"uid=%d)", tc[i].modes, whoami);
186 
187 	if (tc[i].droproot)
188 		SAFE_SETEUID(0);
189 }
190 
setup(void)191 static void setup(void)
192 {
193 	struct test_variants *tv = &variants[tst_variant];
194 	size_t i;
195 	int rval;
196 
197 	tst_res(TINFO, "Testing variant: %s", tv->desc);
198 
199 	bad_addr = tst_get_bad_addr(NULL);
200 
201 	saved.type = tv->type;
202 	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(&saved));
203 	if (rval < 0) {
204 		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
205 		return;
206 	}
207 
208 	supported = 1;
209 
210 	if (rval != TIME_OK && rval != TIME_ERROR) {
211 		timex_show("SAVE_STATUS", &saved);
212 		tst_brk(TBROK | TERRNO, "clock has on-going leap changes, "
213 				"returned: %i", rval);
214 	}
215 
216 	hz = SAFE_SYSCONF(_SC_CLK_TCK);
217 
218 	/* fix high and low limits by dividing it per HZ value */
219 
220 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
221 		if (tc[i].modes == ADJ_TICK) {
222 			tc[i].highlimit /= hz;
223 			tc[i].lowlimit /= hz;
224 		}
225 	}
226 }
227 
cleanup(void)228 static void cleanup(void)
229 {
230 	struct test_variants *tv = &variants[tst_variant];
231 	unsigned int modes = ADJ_ALL;
232 	int rval;
233 
234 	if (supported == 0)
235 		return;
236 
237 	/* restore clock resolution based on original status flag */
238 
239 	if (timex_get_field_uint(&saved, ADJ_STATUS) & STA_NANO)
240 		modes |= ADJ_NANO;
241 	else
242 		modes |= ADJ_MICRO;
243 
244 	timex_set_field_uint(&saved, ADJ_MODES, modes);
245 
246 	/* restore original clock flags */
247 
248 	rval = tv->clock_adjtime(CLOCK_REALTIME, tst_timex_get(&saved));
249 	if (rval < 0) {
250 		tst_res(TFAIL | TERRNO, "clock_adjtime() failed %i", rval);
251 		return;
252 	}
253 }
254 
255 static struct tst_test test = {
256 	.test = verify_clock_adjtime,
257 	.setup = setup,
258 	.cleanup = cleanup,
259 	.tcnt = ARRAY_SIZE(tc),
260 	.test_variants = ARRAY_SIZE(variants),
261 	.needs_root = 1,
262 	.restore_wallclock = 1,
263 };
264