Skip to content

Commit 70ddf65

Browse files
0x7f454c46KAGA-KOKO
authored andcommitted
x86/vdso: Zap vvar pages when switching to a time namespace
The VVAR page layout depends on whether a task belongs to the root or non-root time namespace. Whenever a task changes its namespace, the VVAR page tables are cleared and then they will be re-faulted with a corresponding layout. Co-developed-by: Andrei Vagin <[email protected]> Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e6b28ec commit 70ddf65

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

arch/x86/entry/vdso/vma.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void __init init_vdso_image(const struct vdso_image *image)
5151
image->alt_len));
5252
}
5353

54+
static const struct vm_special_mapping vvar_mapping;
5455
struct linux_binprm;
5556

5657
static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
@@ -128,6 +129,32 @@ static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
128129

129130
return NULL;
130131
}
132+
133+
/*
134+
* The vvar page layout depends on whether a task belongs to the root or
135+
* non-root time namespace. Whenever a task changes its namespace, the VVAR
136+
* page tables are cleared and then they will re-faulted with a
137+
* corresponding layout.
138+
* See also the comment near timens_setup_vdso_data() for details.
139+
*/
140+
int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
141+
{
142+
struct mm_struct *mm = task->mm;
143+
struct vm_area_struct *vma;
144+
145+
if (down_write_killable(&mm->mmap_sem))
146+
return -EINTR;
147+
148+
for (vma = mm->mmap; vma; vma = vma->vm_next) {
149+
unsigned long size = vma->vm_end - vma->vm_start;
150+
151+
if (vma_is_special_mapping(vma, &vvar_mapping))
152+
zap_page_range(vma, vma->vm_start, size);
153+
}
154+
155+
up_write(&mm->mmap_sem);
156+
return 0;
157+
}
131158
#else
132159
static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma)
133160
{

include/linux/time_namespace.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ struct time_namespace {
3131
extern struct time_namespace init_time_ns;
3232

3333
#ifdef CONFIG_TIME_NS
34+
extern int vdso_join_timens(struct task_struct *task,
35+
struct time_namespace *ns);
36+
3437
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
3538
{
3639
kref_get(&ns->kref);
@@ -77,6 +80,12 @@ static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
7780
}
7881

7982
#else
83+
static inline int vdso_join_timens(struct task_struct *task,
84+
struct time_namespace *ns)
85+
{
86+
return 0;
87+
}
88+
8089
static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
8190
{
8291
return NULL;

kernel/time/namespace.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static void timens_put(struct ns_common *ns)
281281
static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)
282282
{
283283
struct time_namespace *ns = to_time_ns(new);
284+
int err;
284285

285286
if (!current_is_single_threaded())
286287
return -EUSERS;
@@ -291,6 +292,10 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)
291292

292293
timens_set_vvar_page(current, ns);
293294

295+
err = vdso_join_timens(current, ns);
296+
if (err)
297+
return err;
298+
294299
get_time_ns(ns);
295300
put_time_ns(nsproxy->time_ns);
296301
nsproxy->time_ns = ns;
@@ -305,13 +310,18 @@ int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
305310
{
306311
struct ns_common *nsc = &nsproxy->time_ns_for_children->ns;
307312
struct time_namespace *ns = to_time_ns(nsc);
313+
int err;
308314

309315
/* create_new_namespaces() already incremented the ref counter */
310316
if (nsproxy->time_ns == nsproxy->time_ns_for_children)
311317
return 0;
312318

313319
timens_set_vvar_page(tsk, ns);
314320

321+
err = vdso_join_timens(tsk, ns);
322+
if (err)
323+
return err;
324+
315325
get_time_ns(ns);
316326
put_time_ns(nsproxy->time_ns);
317327
nsproxy->time_ns = ns;

0 commit comments

Comments
 (0)