xref: /openbmc/linux/kernel/time/namespace.c (revision 74be2d3b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author: Andrei Vagin <avagin@openvz.org>
4  * Author: Dmitry Safonov <dima@arista.com>
5  */
6 
7 #include <linux/time_namespace.h>
8 #include <linux/user_namespace.h>
9 #include <linux/sched/signal.h>
10 #include <linux/sched/task.h>
11 #include <linux/clocksource.h>
12 #include <linux/seq_file.h>
13 #include <linux/proc_ns.h>
14 #include <linux/export.h>
15 #include <linux/time.h>
16 #include <linux/slab.h>
17 #include <linux/cred.h>
18 #include <linux/err.h>
19 #include <linux/mm.h>
20 
21 #include <vdso/datapage.h>
22 
23 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
24 				struct timens_offsets *ns_offsets)
25 {
26 	ktime_t offset;
27 
28 	switch (clockid) {
29 	case CLOCK_MONOTONIC:
30 		offset = timespec64_to_ktime(ns_offsets->monotonic);
31 		break;
32 	case CLOCK_BOOTTIME:
33 	case CLOCK_BOOTTIME_ALARM:
34 		offset = timespec64_to_ktime(ns_offsets->boottime);
35 		break;
36 	default:
37 		return tim;
38 	}
39 
40 	/*
41 	 * Check that @tim value is in [offset, KTIME_MAX + offset]
42 	 * and subtract offset.
43 	 */
44 	if (tim < offset) {
45 		/*
46 		 * User can specify @tim *absolute* value - if it's lesser than
47 		 * the time namespace's offset - it's already expired.
48 		 */
49 		tim = 0;
50 	} else {
51 		tim = ktime_sub(tim, offset);
52 		if (unlikely(tim > KTIME_MAX))
53 			tim = KTIME_MAX;
54 	}
55 
56 	return tim;
57 }
58 
59 static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
60 {
61 	return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);
62 }
63 
64 static void dec_time_namespaces(struct ucounts *ucounts)
65 {
66 	dec_ucount(ucounts, UCOUNT_TIME_NAMESPACES);
67 }
68 
69 /**
70  * clone_time_ns - Clone a time namespace
71  * @user_ns:	User namespace which owns a new namespace.
72  * @old_ns:	Namespace to clone
73  *
74  * Clone @old_ns and set the clone refcount to 1
75  *
76  * Return: The new namespace or ERR_PTR.
77  */
78 static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
79 					  struct time_namespace *old_ns)
80 {
81 	struct time_namespace *ns;
82 	struct ucounts *ucounts;
83 	int err;
84 
85 	err = -ENOSPC;
86 	ucounts = inc_time_namespaces(user_ns);
87 	if (!ucounts)
88 		goto fail;
89 
90 	err = -ENOMEM;
91 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
92 	if (!ns)
93 		goto fail_dec;
94 
95 	kref_init(&ns->kref);
96 
97 	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
98 	if (!ns->vvar_page)
99 		goto fail_free;
100 
101 	err = ns_alloc_inum(&ns->ns);
102 	if (err)
103 		goto fail_free_page;
104 
105 	ns->ucounts = ucounts;
106 	ns->ns.ops = &timens_operations;
107 	ns->user_ns = get_user_ns(user_ns);
108 	ns->offsets = old_ns->offsets;
109 	ns->frozen_offsets = false;
110 	return ns;
111 
112 fail_free_page:
113 	__free_page(ns->vvar_page);
114 fail_free:
115 	kfree(ns);
116 fail_dec:
117 	dec_time_namespaces(ucounts);
118 fail:
119 	return ERR_PTR(err);
120 }
121 
122 /**
123  * copy_time_ns - Create timens_for_children from @old_ns
124  * @flags:	Cloning flags
125  * @user_ns:	User namespace which owns a new namespace.
126  * @old_ns:	Namespace to clone
127  *
128  * If CLONE_NEWTIME specified in @flags, creates a new timens_for_children;
129  * adds a refcounter to @old_ns otherwise.
130  *
131  * Return: timens_for_children namespace or ERR_PTR.
132  */
133 struct time_namespace *copy_time_ns(unsigned long flags,
134 	struct user_namespace *user_ns, struct time_namespace *old_ns)
135 {
136 	if (!(flags & CLONE_NEWTIME))
137 		return get_time_ns(old_ns);
138 
139 	return clone_time_ns(user_ns, old_ns);
140 }
141 
142 static struct timens_offset offset_from_ts(struct timespec64 off)
143 {
144 	struct timens_offset ret;
145 
146 	ret.sec = off.tv_sec;
147 	ret.nsec = off.tv_nsec;
148 
149 	return ret;
150 }
151 
152 /*
153  * A time namespace VVAR page has the same layout as the VVAR page which
154  * contains the system wide VDSO data.
155  *
156  * For a normal task the VVAR pages are installed in the normal ordering:
157  *     VVAR
158  *     PVCLOCK
159  *     HVCLOCK
160  *     TIMENS   <- Not really required
161  *
162  * Now for a timens task the pages are installed in the following order:
163  *     TIMENS
164  *     PVCLOCK
165  *     HVCLOCK
166  *     VVAR
167  *
168  * The check for vdso_data->clock_mode is in the unlikely path of
169  * the seq begin magic. So for the non-timens case most of the time
170  * 'seq' is even, so the branch is not taken.
171  *
172  * If 'seq' is odd, i.e. a concurrent update is in progress, the extra check
173  * for vdso_data->clock_mode is a non-issue. The task is spin waiting for the
174  * update to finish and for 'seq' to become even anyway.
175  *
176  * Timens page has vdso_data->clock_mode set to VDSO_CLOCKMODE_TIMENS which
177  * enforces the time namespace handling path.
178  */
179 static void timens_setup_vdso_data(struct vdso_data *vdata,
180 				   struct time_namespace *ns)
181 {
182 	struct timens_offset *offset = vdata->offset;
183 	struct timens_offset monotonic = offset_from_ts(ns->offsets.monotonic);
184 	struct timens_offset boottime = offset_from_ts(ns->offsets.boottime);
185 
186 	vdata->seq			= 1;
187 	vdata->clock_mode		= VDSO_CLOCKMODE_TIMENS;
188 	offset[CLOCK_MONOTONIC]		= monotonic;
189 	offset[CLOCK_MONOTONIC_RAW]	= monotonic;
190 	offset[CLOCK_MONOTONIC_COARSE]	= monotonic;
191 	offset[CLOCK_BOOTTIME]		= boottime;
192 	offset[CLOCK_BOOTTIME_ALARM]	= boottime;
193 }
194 
195 /*
196  * Protects possibly multiple offsets writers racing each other
197  * and tasks entering the namespace.
198  */
199 static DEFINE_MUTEX(offset_lock);
200 
201 static void timens_set_vvar_page(struct task_struct *task,
202 				struct time_namespace *ns)
203 {
204 	struct vdso_data *vdata;
205 	unsigned int i;
206 
207 	if (ns == &init_time_ns)
208 		return;
209 
210 	/* Fast-path, taken by every task in namespace except the first. */
211 	if (likely(ns->frozen_offsets))
212 		return;
213 
214 	mutex_lock(&offset_lock);
215 	/* Nothing to-do: vvar_page has been already initialized. */
216 	if (ns->frozen_offsets)
217 		goto out;
218 
219 	ns->frozen_offsets = true;
220 	vdata = arch_get_vdso_data(page_address(ns->vvar_page));
221 
222 	for (i = 0; i < CS_BASES; i++)
223 		timens_setup_vdso_data(&vdata[i], ns);
224 
225 out:
226 	mutex_unlock(&offset_lock);
227 }
228 
229 void free_time_ns(struct kref *kref)
230 {
231 	struct time_namespace *ns;
232 
233 	ns = container_of(kref, struct time_namespace, kref);
234 	dec_time_namespaces(ns->ucounts);
235 	put_user_ns(ns->user_ns);
236 	ns_free_inum(&ns->ns);
237 	__free_page(ns->vvar_page);
238 	kfree(ns);
239 }
240 
241 static struct time_namespace *to_time_ns(struct ns_common *ns)
242 {
243 	return container_of(ns, struct time_namespace, ns);
244 }
245 
246 static struct ns_common *timens_get(struct task_struct *task)
247 {
248 	struct time_namespace *ns = NULL;
249 	struct nsproxy *nsproxy;
250 
251 	task_lock(task);
252 	nsproxy = task->nsproxy;
253 	if (nsproxy) {
254 		ns = nsproxy->time_ns;
255 		get_time_ns(ns);
256 	}
257 	task_unlock(task);
258 
259 	return ns ? &ns->ns : NULL;
260 }
261 
262 static struct ns_common *timens_for_children_get(struct task_struct *task)
263 {
264 	struct time_namespace *ns = NULL;
265 	struct nsproxy *nsproxy;
266 
267 	task_lock(task);
268 	nsproxy = task->nsproxy;
269 	if (nsproxy) {
270 		ns = nsproxy->time_ns_for_children;
271 		get_time_ns(ns);
272 	}
273 	task_unlock(task);
274 
275 	return ns ? &ns->ns : NULL;
276 }
277 
278 static void timens_put(struct ns_common *ns)
279 {
280 	put_time_ns(to_time_ns(ns));
281 }
282 
283 static int timens_install(struct nsset *nsset, struct ns_common *new)
284 {
285 	struct nsproxy *nsproxy = nsset->nsproxy;
286 	struct time_namespace *ns = to_time_ns(new);
287 	int err;
288 
289 	if (!current_is_single_threaded())
290 		return -EUSERS;
291 
292 	if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
293 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
294 		return -EPERM;
295 
296 	timens_set_vvar_page(current, ns);
297 
298 	err = vdso_join_timens(current, ns);
299 	if (err)
300 		return err;
301 
302 	get_time_ns(ns);
303 	put_time_ns(nsproxy->time_ns);
304 	nsproxy->time_ns = ns;
305 
306 	get_time_ns(ns);
307 	put_time_ns(nsproxy->time_ns_for_children);
308 	nsproxy->time_ns_for_children = ns;
309 	return 0;
310 }
311 
312 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
313 {
314 	struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
315 	struct time_namespace *ns = to_time_ns(nsc);
316 	int err;
317 
318 	/* create_new_namespaces() already incremented the ref counter */
319 	if (nsproxy->time_ns == nsproxy->time_ns_for_children)
320 		return 0;
321 
322 	timens_set_vvar_page(tsk, ns);
323 
324 	err = vdso_join_timens(tsk, ns);
325 	if (err)
326 		return err;
327 
328 	get_time_ns(ns);
329 	put_time_ns(nsproxy->time_ns);
330 	nsproxy->time_ns = ns;
331 
332 	return 0;
333 }
334 
335 static struct user_namespace *timens_owner(struct ns_common *ns)
336 {
337 	return to_time_ns(ns)->user_ns;
338 }
339 
340 static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
341 {
342 	char *clock;
343 
344 	switch (clockid) {
345 	case CLOCK_BOOTTIME:
346 		clock = "boottime";
347 		break;
348 	case CLOCK_MONOTONIC:
349 		clock = "monotonic";
350 		break;
351 	default:
352 		clock = "unknown";
353 		break;
354 	}
355 	seq_printf(m, "%-10s %10lld %9ld\n", clock, ts->tv_sec, ts->tv_nsec);
356 }
357 
358 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
359 {
360 	struct ns_common *ns;
361 	struct time_namespace *time_ns;
362 
363 	ns = timens_for_children_get(p);
364 	if (!ns)
365 		return;
366 	time_ns = to_time_ns(ns);
367 
368 	show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
369 	show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
370 	put_time_ns(time_ns);
371 }
372 
373 int proc_timens_set_offset(struct file *file, struct task_struct *p,
374 			   struct proc_timens_offset *offsets, int noffsets)
375 {
376 	struct ns_common *ns;
377 	struct time_namespace *time_ns;
378 	struct timespec64 tp;
379 	int i, err;
380 
381 	ns = timens_for_children_get(p);
382 	if (!ns)
383 		return -ESRCH;
384 	time_ns = to_time_ns(ns);
385 
386 	if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
387 		put_time_ns(time_ns);
388 		return -EPERM;
389 	}
390 
391 	for (i = 0; i < noffsets; i++) {
392 		struct proc_timens_offset *off = &offsets[i];
393 
394 		switch (off->clockid) {
395 		case CLOCK_MONOTONIC:
396 			ktime_get_ts64(&tp);
397 			break;
398 		case CLOCK_BOOTTIME:
399 			ktime_get_boottime_ts64(&tp);
400 			break;
401 		default:
402 			err = -EINVAL;
403 			goto out;
404 		}
405 
406 		err = -ERANGE;
407 
408 		if (off->val.tv_sec > KTIME_SEC_MAX ||
409 		    off->val.tv_sec < -KTIME_SEC_MAX)
410 			goto out;
411 
412 		tp = timespec64_add(tp, off->val);
413 		/*
414 		 * KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
415 		 * still unreachable.
416 		 */
417 		if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
418 			goto out;
419 	}
420 
421 	mutex_lock(&offset_lock);
422 	if (time_ns->frozen_offsets) {
423 		err = -EACCES;
424 		goto out_unlock;
425 	}
426 
427 	err = 0;
428 	/* Don't report errors after this line */
429 	for (i = 0; i < noffsets; i++) {
430 		struct proc_timens_offset *off = &offsets[i];
431 		struct timespec64 *offset = NULL;
432 
433 		switch (off->clockid) {
434 		case CLOCK_MONOTONIC:
435 			offset = &time_ns->offsets.monotonic;
436 			break;
437 		case CLOCK_BOOTTIME:
438 			offset = &time_ns->offsets.boottime;
439 			break;
440 		}
441 
442 		*offset = off->val;
443 	}
444 
445 out_unlock:
446 	mutex_unlock(&offset_lock);
447 out:
448 	put_time_ns(time_ns);
449 
450 	return err;
451 }
452 
453 const struct proc_ns_operations timens_operations = {
454 	.name		= "time",
455 	.type		= CLONE_NEWTIME,
456 	.get		= timens_get,
457 	.put		= timens_put,
458 	.install	= timens_install,
459 	.owner		= timens_owner,
460 };
461 
462 const struct proc_ns_operations timens_for_children_operations = {
463 	.name		= "time_for_children",
464 	.real_ns_name	= "time",
465 	.type		= CLONE_NEWTIME,
466 	.get		= timens_for_children_get,
467 	.put		= timens_put,
468 	.install	= timens_install,
469 	.owner		= timens_owner,
470 };
471 
472 struct time_namespace init_time_ns = {
473 	.kref		= KREF_INIT(3),
474 	.user_ns	= &init_user_ns,
475 	.ns.inum	= PROC_TIME_INIT_INO,
476 	.ns.ops		= &timens_operations,
477 	.frozen_offsets	= true,
478 };
479 
480 static int __init time_ns_init(void)
481 {
482 	return 0;
483 }
484 subsys_initcall(time_ns_init);
485