1 // SPDX-License-Identifier: GPL-2.0
3 * Author: Andrei Vagin <avagin@openvz.org>
4 * Author: Dmitry Safonov <dima@arista.com>
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/seq_file.h>
12 #include <linux/proc_ns.h>
13 #include <linux/export.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/cred.h>
17 #include <linux/err.h>
20 #include <vdso/datapage.h>
22 ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
23 struct timens_offsets *ns_offsets)
29 offset = timespec64_to_ktime(ns_offsets->monotonic);
32 case CLOCK_BOOTTIME_ALARM:
33 offset = timespec64_to_ktime(ns_offsets->boottime);
40 * Check that @tim value is in [offset, KTIME_MAX + offset]
41 * and subtract offset.
45 * User can specify @tim *absolute* value - if it's lesser than
46 * the time namespace's offset - it's already expired.
50 tim = ktime_sub(tim, offset);
51 if (unlikely(tim > KTIME_MAX))
58 static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
60 return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);
63 static void dec_time_namespaces(struct ucounts *ucounts)
65 dec_ucount(ucounts, UCOUNT_TIME_NAMESPACES);
69 * clone_time_ns - Clone a time namespace
70 * @user_ns: User namespace which owns a new namespace.
71 * @old_ns: Namespace to clone
73 * Clone @old_ns and set the clone refcount to 1
75 * Return: The new namespace or ERR_PTR.
77 static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
78 struct time_namespace *old_ns)
80 struct time_namespace *ns;
81 struct ucounts *ucounts;
85 ucounts = inc_time_namespaces(user_ns);
90 ns = kmalloc(sizeof(*ns), GFP_KERNEL);
96 ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
100 err = ns_alloc_inum(&ns->ns);
104 ns->ucounts = ucounts;
105 ns->ns.ops = &timens_operations;
106 ns->user_ns = get_user_ns(user_ns);
107 ns->offsets = old_ns->offsets;
108 ns->frozen_offsets = false;
112 __free_page(ns->vvar_page);
116 dec_time_namespaces(ucounts);
122 * copy_time_ns - Create timens_for_children from @old_ns
123 * @flags: Cloning flags
124 * @user_ns: User namespace which owns a new namespace.
125 * @old_ns: Namespace to clone
127 * If CLONE_NEWTIME specified in @flags, creates a new timens_for_children;
128 * adds a refcounter to @old_ns otherwise.
130 * Return: timens_for_children namespace or ERR_PTR.
132 struct time_namespace *copy_time_ns(unsigned long flags,
133 struct user_namespace *user_ns, struct time_namespace *old_ns)
135 if (!(flags & CLONE_NEWTIME))
136 return get_time_ns(old_ns);
138 return clone_time_ns(user_ns, old_ns);
141 static struct timens_offset offset_from_ts(struct timespec64 off)
143 struct timens_offset ret;
145 ret.sec = off.tv_sec;
146 ret.nsec = off.tv_nsec;
152 * A time namespace VVAR page has the same layout as the VVAR page which
153 * contains the system wide VDSO data.
155 * For a normal task the VVAR pages are installed in the normal ordering:
159 * TIMENS <- Not really required
161 * Now for a timens task the pages are installed in the following order:
167 * The check for vdso_data->clock_mode is in the unlikely path of
168 * the seq begin magic. So for the non-timens case most of the time
169 * 'seq' is even, so the branch is not taken.
171 * If 'seq' is odd, i.e. a concurrent update is in progress, the extra check
172 * for vdso_data->clock_mode is a non-issue. The task is spin waiting for the
173 * update to finish and for 'seq' to become even anyway.
175 * Timens page has vdso_data->clock_mode set to VCLOCK_TIMENS which enforces
176 * the time namespace handling path.
178 static void timens_setup_vdso_data(struct vdso_data *vdata,
179 struct time_namespace *ns)
181 struct timens_offset *offset = vdata->offset;
182 struct timens_offset monotonic = offset_from_ts(ns->offsets.monotonic);
183 struct timens_offset boottime = offset_from_ts(ns->offsets.boottime);
186 vdata->clock_mode = VCLOCK_TIMENS;
187 offset[CLOCK_MONOTONIC] = monotonic;
188 offset[CLOCK_MONOTONIC_RAW] = monotonic;
189 offset[CLOCK_MONOTONIC_COARSE] = monotonic;
190 offset[CLOCK_BOOTTIME] = boottime;
191 offset[CLOCK_BOOTTIME_ALARM] = boottime;
195 * Protects possibly multiple offsets writers racing each other
196 * and tasks entering the namespace.
198 static DEFINE_MUTEX(offset_lock);
200 static void timens_set_vvar_page(struct task_struct *task,
201 struct time_namespace *ns)
203 struct vdso_data *vdata;
206 if (ns == &init_time_ns)
209 /* Fast-path, taken by every task in namespace except the first. */
210 if (likely(ns->frozen_offsets))
213 mutex_lock(&offset_lock);
214 /* Nothing to-do: vvar_page has been already initialized. */
215 if (ns->frozen_offsets)
218 ns->frozen_offsets = true;
219 vdata = arch_get_vdso_data(page_address(ns->vvar_page));
221 for (i = 0; i < CS_BASES; i++)
222 timens_setup_vdso_data(&vdata[i], ns);
225 mutex_unlock(&offset_lock);
228 void free_time_ns(struct kref *kref)
230 struct time_namespace *ns;
232 ns = container_of(kref, struct time_namespace, kref);
233 dec_time_namespaces(ns->ucounts);
234 put_user_ns(ns->user_ns);
235 ns_free_inum(&ns->ns);
236 __free_page(ns->vvar_page);
240 static struct time_namespace *to_time_ns(struct ns_common *ns)
242 return container_of(ns, struct time_namespace, ns);
245 static struct ns_common *timens_get(struct task_struct *task)
247 struct time_namespace *ns = NULL;
248 struct nsproxy *nsproxy;
251 nsproxy = task->nsproxy;
253 ns = nsproxy->time_ns;
258 return ns ? &ns->ns : NULL;
261 static struct ns_common *timens_for_children_get(struct task_struct *task)
263 struct time_namespace *ns = NULL;
264 struct nsproxy *nsproxy;
267 nsproxy = task->nsproxy;
269 ns = nsproxy->time_ns_for_children;
274 return ns ? &ns->ns : NULL;
277 static void timens_put(struct ns_common *ns)
279 put_time_ns(to_time_ns(ns));
282 static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)
284 struct time_namespace *ns = to_time_ns(new);
287 if (!current_is_single_threaded())
290 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
291 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
294 timens_set_vvar_page(current, ns);
296 err = vdso_join_timens(current, ns);
301 put_time_ns(nsproxy->time_ns);
302 nsproxy->time_ns = ns;
305 put_time_ns(nsproxy->time_ns_for_children);
306 nsproxy->time_ns_for_children = ns;
310 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
312 struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
313 struct time_namespace *ns = to_time_ns(nsc);
316 /* create_new_namespaces() already incremented the ref counter */
317 if (nsproxy->time_ns == nsproxy->time_ns_for_children)
320 timens_set_vvar_page(tsk, ns);
322 err = vdso_join_timens(tsk, ns);
327 put_time_ns(nsproxy->time_ns);
328 nsproxy->time_ns = ns;
333 static struct user_namespace *timens_owner(struct ns_common *ns)
335 return to_time_ns(ns)->user_ns;
338 static void show_offset(struct seq_file *m, int clockid, struct timespec64 *ts)
340 seq_printf(m, "%d %lld %ld\n", clockid, ts->tv_sec, ts->tv_nsec);
343 void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m)
345 struct ns_common *ns;
346 struct time_namespace *time_ns;
348 ns = timens_for_children_get(p);
351 time_ns = to_time_ns(ns);
353 show_offset(m, CLOCK_MONOTONIC, &time_ns->offsets.monotonic);
354 show_offset(m, CLOCK_BOOTTIME, &time_ns->offsets.boottime);
355 put_time_ns(time_ns);
358 int proc_timens_set_offset(struct file *file, struct task_struct *p,
359 struct proc_timens_offset *offsets, int noffsets)
361 struct ns_common *ns;
362 struct time_namespace *time_ns;
363 struct timespec64 tp;
366 ns = timens_for_children_get(p);
369 time_ns = to_time_ns(ns);
371 if (!file_ns_capable(file, time_ns->user_ns, CAP_SYS_TIME)) {
372 put_time_ns(time_ns);
376 for (i = 0; i < noffsets; i++) {
377 struct proc_timens_offset *off = &offsets[i];
379 switch (off->clockid) {
380 case CLOCK_MONOTONIC:
384 ktime_get_boottime_ts64(&tp);
393 if (off->val.tv_sec > KTIME_SEC_MAX ||
394 off->val.tv_sec < -KTIME_SEC_MAX)
397 tp = timespec64_add(tp, off->val);
399 * KTIME_SEC_MAX is divided by 2 to be sure that KTIME_MAX is
402 if (tp.tv_sec < 0 || tp.tv_sec > KTIME_SEC_MAX / 2)
406 mutex_lock(&offset_lock);
407 if (time_ns->frozen_offsets) {
413 /* Don't report errors after this line */
414 for (i = 0; i < noffsets; i++) {
415 struct proc_timens_offset *off = &offsets[i];
416 struct timespec64 *offset = NULL;
418 switch (off->clockid) {
419 case CLOCK_MONOTONIC:
420 offset = &time_ns->offsets.monotonic;
423 offset = &time_ns->offsets.boottime;
431 mutex_unlock(&offset_lock);
433 put_time_ns(time_ns);
438 const struct proc_ns_operations timens_operations = {
440 .type = CLONE_NEWTIME,
443 .install = timens_install,
444 .owner = timens_owner,
447 const struct proc_ns_operations timens_for_children_operations = {
448 .name = "time_for_children",
449 .type = CLONE_NEWTIME,
450 .get = timens_for_children_get,
452 .install = timens_install,
453 .owner = timens_owner,
456 struct time_namespace init_time_ns = {
457 .kref = KREF_INIT(3),
458 .user_ns = &init_user_ns,
459 .ns.inum = PROC_TIME_INIT_INO,
460 .ns.ops = &timens_operations,
461 .frozen_offsets = true,
464 static int __init time_ns_init(void)
468 subsys_initcall(time_ns_init);