1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 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 spinlock_t mm_list_lock; 24 struct list_head mm_list; 25 26 int init_new_context(struct task_struct *task, struct mm_struct *mm) 27 { 28 struct mm_id *new_id = &mm->context.id; 29 unsigned long stack = 0; 30 int ret = -ENOMEM; 31 32 stack = __get_free_pages(GFP_KERNEL | __GFP_ZERO, ilog2(STUB_DATA_PAGES)); 33 if (stack == 0) 34 goto out; 35 36 new_id->stack = stack; 37 38 scoped_guard(spinlock_irqsave, &mm_list_lock) { 39 /* Insert into list, used for lookups when the child dies */ 40 list_add(&mm->context.list, &mm_list); 41 } 42 43 ret = start_userspace(new_id); 44 if (ret < 0) 45 goto out_free; 46 47 /* Ensure the new MM is clean and nothing unwanted is mapped */ 48 unmap(new_id, 0, STUB_START); 49 50 return 0; 51 52 out_free: 53 free_pages(new_id->stack, ilog2(STUB_DATA_PAGES)); 54 out: 55 return ret; 56 } 57 58 void destroy_context(struct mm_struct *mm) 59 { 60 struct mm_context *mmu = &mm->context; 61 62 /* 63 * If init_new_context wasn't called, this will be 64 * zero, resulting in a kill(0), which will result in the 65 * whole UML suddenly dying. Also, cover negative and 66 * 1 cases, since they shouldn't happen either. 67 * 68 * Negative cases happen if the child died unexpectedly. 69 */ 70 if (mmu->id.pid >= 0 && mmu->id.pid < 2) { 71 printk(KERN_ERR "corrupt mm_context - pid = %d\n", 72 mmu->id.pid); 73 return; 74 } 75 76 if (mmu->id.pid > 0) { 77 os_kill_ptraced_process(mmu->id.pid, 1); 78 mmu->id.pid = -1; 79 } 80 81 if (using_seccomp && mmu->id.sock) 82 os_close_file(mmu->id.sock); 83 84 free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES)); 85 86 guard(spinlock_irqsave)(&mm_list_lock); 87 88 list_del(&mm->context.list); 89 } 90 91 static irqreturn_t mm_sigchld_irq(int irq, void* dev) 92 { 93 struct mm_context *mm_context; 94 pid_t pid; 95 96 guard(spinlock)(&mm_list_lock); 97 98 while ((pid = os_reap_child()) > 0) { 99 /* 100 * A child died, check if we have an MM with the PID. This is 101 * only relevant in SECCOMP mode (as ptrace will fail anyway). 102 * 103 * See wait_stub_done_seccomp for more details. 104 */ 105 list_for_each_entry(mm_context, &mm_list, list) { 106 if (mm_context->id.pid == pid) { 107 struct stub_data *stub_data; 108 printk("Unexpectedly lost MM child! Affected tasks will segfault."); 109 110 /* Marks the MM as dead */ 111 mm_context->id.pid = -1; 112 113 /* 114 * NOTE: If SMP is implemented, a futex_wake 115 * needs to be added here. 116 */ 117 stub_data = (void *)mm_context->id.stack; 118 stub_data->futex = FUTEX_IN_KERN; 119 120 /* 121 * NOTE: Currently executing syscalls by 122 * affected tasks may finish normally. 123 */ 124 break; 125 } 126 } 127 } 128 129 return IRQ_HANDLED; 130 } 131 132 static int __init init_child_tracking(void) 133 { 134 int err; 135 136 spin_lock_init(&mm_list_lock); 137 INIT_LIST_HEAD(&mm_list); 138 139 err = request_irq(SIGCHLD_IRQ, mm_sigchld_irq, 0, "SIGCHLD", NULL); 140 if (err < 0) 141 panic("Failed to register SIGCHLD IRQ: %d", err); 142 143 return 0; 144 } 145 early_initcall(init_child_tracking) 146