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/proc_ns.h>
12 #include <linux/export.h>
13 #include <linux/time.h>
14 #include <linux/slab.h>
15 #include <linux/cred.h>
16 #include <linux/err.h>
18 static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
20 return inc_ucount(ns, current_euid(), UCOUNT_TIME_NAMESPACES);
23 static void dec_time_namespaces(struct ucounts *ucounts)
25 dec_ucount(ucounts, UCOUNT_TIME_NAMESPACES);
29 * clone_time_ns - Clone a time namespace
30 * @user_ns: User namespace which owns a new namespace.
31 * @old_ns: Namespace to clone
33 * Clone @old_ns and set the clone refcount to 1
35 * Return: The new namespace or ERR_PTR.
37 static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
38 struct time_namespace *old_ns)
40 struct time_namespace *ns;
41 struct ucounts *ucounts;
45 ucounts = inc_time_namespaces(user_ns);
50 ns = kmalloc(sizeof(*ns), GFP_KERNEL);
56 err = ns_alloc_inum(&ns->ns);
60 ns->ucounts = ucounts;
61 ns->ns.ops = &timens_operations;
62 ns->user_ns = get_user_ns(user_ns);
68 dec_time_namespaces(ucounts);
74 * copy_time_ns - Create timens_for_children from @old_ns
75 * @flags: Cloning flags
76 * @user_ns: User namespace which owns a new namespace.
77 * @old_ns: Namespace to clone
79 * If CLONE_NEWTIME specified in @flags, creates a new timens_for_children;
80 * adds a refcounter to @old_ns otherwise.
82 * Return: timens_for_children namespace or ERR_PTR.
84 struct time_namespace *copy_time_ns(unsigned long flags,
85 struct user_namespace *user_ns, struct time_namespace *old_ns)
87 if (!(flags & CLONE_NEWTIME))
88 return get_time_ns(old_ns);
90 return clone_time_ns(user_ns, old_ns);
93 void free_time_ns(struct kref *kref)
95 struct time_namespace *ns;
97 ns = container_of(kref, struct time_namespace, kref);
98 dec_time_namespaces(ns->ucounts);
99 put_user_ns(ns->user_ns);
100 ns_free_inum(&ns->ns);
104 static struct time_namespace *to_time_ns(struct ns_common *ns)
106 return container_of(ns, struct time_namespace, ns);
109 static struct ns_common *timens_get(struct task_struct *task)
111 struct time_namespace *ns = NULL;
112 struct nsproxy *nsproxy;
115 nsproxy = task->nsproxy;
117 ns = nsproxy->time_ns;
122 return ns ? &ns->ns : NULL;
125 static struct ns_common *timens_for_children_get(struct task_struct *task)
127 struct time_namespace *ns = NULL;
128 struct nsproxy *nsproxy;
131 nsproxy = task->nsproxy;
133 ns = nsproxy->time_ns_for_children;
138 return ns ? &ns->ns : NULL;
141 static void timens_put(struct ns_common *ns)
143 put_time_ns(to_time_ns(ns));
146 static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)
148 struct time_namespace *ns = to_time_ns(new);
150 if (!current_is_single_threaded())
153 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
154 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
158 put_time_ns(nsproxy->time_ns);
159 nsproxy->time_ns = ns;
162 put_time_ns(nsproxy->time_ns_for_children);
163 nsproxy->time_ns_for_children = ns;
167 int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
169 struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
170 struct time_namespace *ns = to_time_ns(nsc);
172 /* create_new_namespaces() already incremented the ref counter */
173 if (nsproxy->time_ns == nsproxy->time_ns_for_children)
177 put_time_ns(nsproxy->time_ns);
178 nsproxy->time_ns = ns;
183 static struct user_namespace *timens_owner(struct ns_common *ns)
185 return to_time_ns(ns)->user_ns;
188 const struct proc_ns_operations timens_operations = {
190 .type = CLONE_NEWTIME,
193 .install = timens_install,
194 .owner = timens_owner,
197 const struct proc_ns_operations timens_for_children_operations = {
198 .name = "time_for_children",
199 .type = CLONE_NEWTIME,
200 .get = timens_for_children_get,
202 .install = timens_install,
203 .owner = timens_owner,
206 struct time_namespace init_time_ns = {
207 .kref = KREF_INIT(3),
208 .user_ns = &init_user_ns,
209 .ns.inum = PROC_TIME_INIT_INO,
210 .ns.ops = &timens_operations,
213 static int __init time_ns_init(void)
217 subsys_initcall(time_ns_init);