Skip to content

Commit 76c1288

Browse files
author
Christian Brauner
committed
nsproxy: support CLONE_NEWTIME with setns()
So far setns() was missing time namespace support. This was partially due to it simply not being implemented but also because vdso_join_timens() could still fail which made switching to multiple namespaces atomically problematic. This is now fixed so support CLONE_NEWTIME with setns() Signed-off-by: Christian Brauner <[email protected]> Reviewed-by: Andrei Vagin <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Michael Kerrisk <[email protected]> Cc: Serge Hallyn <[email protected]> Cc: Dmitry Safonov <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5cfea9a commit 76c1288

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

include/linux/time_namespace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern struct time_namespace init_time_ns;
3333
#ifdef CONFIG_TIME_NS
3434
extern int vdso_join_timens(struct task_struct *task,
3535
struct time_namespace *ns);
36+
extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
3637

3738
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
3839
{
@@ -96,6 +97,11 @@ static inline int vdso_join_timens(struct task_struct *task,
9697
return 0;
9798
}
9899

100+
static inline void timens_commit(struct task_struct *tsk,
101+
struct time_namespace *ns)
102+
{
103+
}
104+
99105
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
100106
{
101107
return NULL;

kernel/nsproxy.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ void exit_task_namespaces(struct task_struct *p)
262262
static int check_setns_flags(unsigned long flags)
263263
{
264264
if (!flags || (flags & ~(CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
265-
CLONE_NEWNET | CLONE_NEWUSER | CLONE_NEWPID |
266-
CLONE_NEWCGROUP)))
265+
CLONE_NEWNET | CLONE_NEWTIME | CLONE_NEWUSER |
266+
CLONE_NEWPID | CLONE_NEWCGROUP)))
267267
return -EINVAL;
268268

269269
#ifndef CONFIG_USER_NS
@@ -290,6 +290,10 @@ static int check_setns_flags(unsigned long flags)
290290
if (flags & CLONE_NEWNET)
291291
return -EINVAL;
292292
#endif
293+
#ifndef CONFIG_TIME_NS
294+
if (flags & CLONE_NEWTIME)
295+
return -EINVAL;
296+
#endif
293297

294298
return 0;
295299
}
@@ -464,6 +468,14 @@ static int validate_nsset(struct nsset *nsset, struct pid *pid)
464468
}
465469
#endif
466470

471+
#ifdef CONFIG_TIME_NS
472+
if (flags & CLONE_NEWTIME) {
473+
ret = validate_ns(nsset, &nsp->time_ns->ns);
474+
if (ret)
475+
goto out;
476+
}
477+
#endif
478+
467479
out:
468480
if (pid_ns)
469481
put_pid_ns(pid_ns);
@@ -507,6 +519,11 @@ static void commit_nsset(struct nsset *nsset)
507519
exit_sem(me);
508520
#endif
509521

522+
#ifdef CONFIG_TIME_NS
523+
if (flags & CLONE_NEWTIME)
524+
timens_commit(me, nsset->nsproxy->time_ns);
525+
#endif
526+
510527
/* transfer ownership */
511528
switch_task_namespaces(me, nsset->nsproxy);
512529
nsset->nsproxy = NULL;

kernel/time/namespace.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static void timens_put(struct ns_common *ns)
280280
put_time_ns(to_time_ns(ns));
281281
}
282282

283-
static void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
283+
void timens_commit(struct task_struct *tsk, struct time_namespace *ns)
284284
{
285285
timens_set_vvar_page(tsk, ns);
286286
vdso_join_timens(tsk, ns);
@@ -298,9 +298,6 @@ static int timens_install(struct nsset *nsset, struct ns_common *new)
298298
!ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
299299
return -EPERM;
300300

301-
302-
timens_commit(current, ns);
303-
304301
get_time_ns(ns);
305302
put_time_ns(nsproxy->time_ns);
306303
nsproxy->time_ns = ns;

0 commit comments

Comments
 (0)