Debian Codegraph @

arch/um/kernel/skas/mmu.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015 Thomas Meyer ([email protected])
4 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
5 */
6
7 #include <linux/mm.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10
11 #include <shared/irq_kern.h>
12 #include <asm/pgalloc.h>
13 #include <asm/sections.h>
14 #include <asm/mmu_context.h>
15 #include <as-layout.h>
16 #include <os.h>
17 #include <skas.h>
18 #include <stub-data.h>
19
20 /* Ensure the stub_data struct covers the allocated area */
21 static_assert(sizeof(struct stub_data) == STUB_DATA_PAGES * UM_KERN_PAGE_SIZE);
22
23 static spinlock_t mm_list_lock;
24 static struct list_head mm_list;
25
26 struct mutex *__get_turnstile(struct mm_id *mm_id)
27 {
28 struct mm_context *ctx = container_of(mm_id, struct mm_context, id);
29
30 return &ctx->turnstile;
31 }
32
33 void enter_turnstile(struct mm_id *mm_id)
34 {
35 mutex_lock(__get_turnstile(mm_id));
36 }
37
38 void exit_turnstile(struct mm_id *mm_id)
39 {
40 mutex_unlock(__get_turnstile(mm_id));
41 }
42
43 int init_new_context(struct task_struct *task, struct mm_struct *mm)
44 {
45 struct mm_id *new_id = &mm->context.id;
46 unsigned long stack = 0;
47 int ret = -ENOMEM;
48
49 mutex_init(&mm->context.turnstile);
50 spin_lock_init(&mm->context.sync_tlb_lock);
51
52 stack = __get_free_pages(GFP_KERNEL | __GFP_ZERO, ilog2(STUB_DATA_PAGES));
53 if (stack == 0)
54 goto out;
55
56 new_id->stack = stack;
57 new_id->syscall_data_len = 0;
58 new_id->syscall_fd_num = 0;
59
60 scoped_guard(spinlock_irqsave, &mm_list_lock) {
61 /* Insert into list, used for lookups when the child dies */
62 list_add(&mm->context.list, &mm_list);
63 }
64
65 ret = start_userspace(new_id);
66 if (ret < 0)
67 goto out_free;
68
69 /* Ensure the new MM is clean and nothing unwanted is mapped */
70 unmap(new_id, 0, STUB_START);
71
72 return 0;
73
74 out_free:
75 free_pages(new_id->stack, ilog2(STUB_DATA_PAGES));
76 out:
77 return ret;
78 }
79
80 void destroy_context(struct mm_struct *mm)
81 {
82 struct mm_context *mmu = &mm->context;
83
84 /*
85 * If init_new_context wasn't called, this will be
86 * zero, resulting in a kill(0), which will result in the
87 * whole UML suddenly dying. Also, cover negative and
88 * 1 cases, since they shouldn't happen either.
89 *
90 * Negative cases happen if the child died unexpectedly.
91 */
92 if (mmu->id.pid >= 0 && mmu->id.pid < 2) {
93 printk(KERN_ERR "corrupt mm_context - pid = %d\n",
94 mmu->id.pid);
95 return;
96 }
97
98 scoped_guard(spinlock_irqsave, &mm_list_lock)
99 list_del(&mm->context.list);
100
101 if (mmu->id.pid > 0) {
102 os_kill_ptraced_process(mmu->id.pid, 1);
103 mmu->id.pid = -1;
104 }
105
106 if (using_seccomp && mmu->id.sock)
107 os_close_file(mmu->id.sock);
108
109 free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
110 }
111
112 static irqreturn_t mm_sigchld_irq(int irq, void* dev)
113 {
114 struct mm_context *mm_context;
115 pid_t pid;
116
117 guard(spinlock)(&mm_list_lock);
118
119 while ((pid = os_reap_child()) > 0) {
120 /*
121 * A child died, check if we have an MM with the PID. This is
122 * only relevant in SECCOMP mode (as ptrace will fail anyway).
123 *
124 * See wait_stub_done_seccomp for more details.
125 */
126 list_for_each_entry(mm_context, &mm_list, list) {
127 if (mm_context->id.pid == pid) {
128 struct stub_data *stub_data;
129 printk("Unexpectedly lost MM child! Affected tasks will segfault.");
130
131 /* Marks the MM as dead */
132 mm_context->id.pid = -1;
133
134 stub_data = (void *)mm_context->id.stack;
135 stub_data->futex = FUTEX_IN_KERN;
136 #if IS_ENABLED(CONFIG_SMP)
137 os_futex_wake(&stub_data->futex);
138 #endif
139
140 /*
141 * NOTE: Currently executing syscalls by
142 * affected tasks may finish normally.
143 */
144 break;
145 }
146 }
147 }
148
149 return IRQ_HANDLED;
150 }
151
152 static int __init init_child_tracking(void)
153 {
154 int err;
155
156 spin_lock_init(&mm_list_lock);
157 INIT_LIST_HEAD(&mm_list);
158
159 err = request_irq(SIGCHLD_IRQ, mm_sigchld_irq, 0, "SIGCHLD", NULL);
160 if (err < 0)
161 panic("Failed to register SIGCHLD IRQ: %d", err);
162
163 return 0;
164 }
165 early_initcall(init_child_tracking)