hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/x86/entry/vsyscall/vsyscall_64.c
....@@ -42,9 +42,11 @@
4242 #define CREATE_TRACE_POINTS
4343 #include "vsyscall_trace.h"
4444
45
-static enum { EMULATE, NONE } vsyscall_mode =
45
+static enum { EMULATE, XONLY, NONE } vsyscall_mode __ro_after_init =
4646 #ifdef CONFIG_LEGACY_VSYSCALL_NONE
4747 NONE;
48
+#elif defined(CONFIG_LEGACY_VSYSCALL_XONLY)
49
+ XONLY;
4850 #else
4951 EMULATE;
5052 #endif
....@@ -54,6 +56,8 @@
5456 if (str) {
5557 if (!strcmp("emulate", str))
5658 vsyscall_mode = EMULATE;
59
+ else if (!strcmp("xonly", str))
60
+ vsyscall_mode = XONLY;
5761 else if (!strcmp("none", str))
5862 vsyscall_mode = NONE;
5963 else
....@@ -99,28 +103,22 @@
99103 * sig_on_uaccess_err, this could go away.
100104 */
101105
102
- if (!access_ok(VERIFY_WRITE, (void __user *)ptr, size)) {
103
- siginfo_t info;
106
+ if (!access_ok((void __user *)ptr, size)) {
104107 struct thread_struct *thread = &current->thread;
105108
106
- thread->error_code = 6; /* user fault, no page, write */
109
+ thread->error_code = X86_PF_USER | X86_PF_WRITE;
107110 thread->cr2 = ptr;
108111 thread->trap_nr = X86_TRAP_PF;
109112
110
- clear_siginfo(&info);
111
- info.si_signo = SIGSEGV;
112
- info.si_errno = 0;
113
- info.si_code = SEGV_MAPERR;
114
- info.si_addr = (void __user *)ptr;
115
-
116
- force_sig_info(SIGSEGV, &info, current);
113
+ force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)ptr);
117114 return false;
118115 } else {
119116 return true;
120117 }
121118 }
122119
123
-bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
120
+bool emulate_vsyscall(unsigned long error_code,
121
+ struct pt_regs *regs, unsigned long address)
124122 {
125123 struct task_struct *tsk;
126124 unsigned long caller;
....@@ -128,6 +126,22 @@
128126 int prev_sig_on_uaccess_err;
129127 long ret;
130128 unsigned long orig_dx;
129
+
130
+ /* Write faults or kernel-privilege faults never get fixed up. */
131
+ if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)
132
+ return false;
133
+
134
+ if (!(error_code & X86_PF_INSTR)) {
135
+ /* Failed vsyscall read */
136
+ if (vsyscall_mode == EMULATE)
137
+ return false;
138
+
139
+ /*
140
+ * User code tried and failed to read the vsyscall page.
141
+ */
142
+ warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");
143
+ return false;
144
+ }
131145
132146 /*
133147 * No point in checking CS -- the only way to get here is a user mode
....@@ -170,7 +184,7 @@
170184 */
171185 switch (vsyscall_nr) {
172186 case 0:
173
- if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
187
+ if (!write_ok_or_segv(regs->di, sizeof(struct __kernel_old_timeval)) ||
174188 !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
175189 ret = -EFAULT;
176190 goto check_fault;
....@@ -180,7 +194,7 @@
180194 break;
181195
182196 case 1:
183
- if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
197
+ if (!write_ok_or_segv(regs->di, sizeof(__kernel_old_time_t))) {
184198 ret = -EFAULT;
185199 goto check_fault;
186200 }
....@@ -208,7 +222,7 @@
208222 */
209223 regs->orig_ax = syscall_nr;
210224 regs->ax = -ENOSYS;
211
- tmp = secure_computing(NULL);
225
+ tmp = secure_computing();
212226 if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
213227 warn_bad_vsyscall(KERN_DEBUG, regs,
214228 "seccomp tried to change syscall nr or ip");
....@@ -275,7 +289,7 @@
275289 return true;
276290
277291 sigsegv:
278
- force_sig(SIGSEGV, current);
292
+ force_sig(SIGSEGV);
279293 return true;
280294 }
281295
....@@ -291,7 +305,7 @@
291305 static const struct vm_operations_struct gate_vma_ops = {
292306 .name = gate_vma_name,
293307 };
294
-static struct vm_area_struct gate_vma = {
308
+static struct vm_area_struct gate_vma __ro_after_init = {
295309 .vm_start = VSYSCALL_ADDR,
296310 .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
297311 .vm_page_prot = PAGE_READONLY_EXEC,
....@@ -364,12 +378,20 @@
364378 extern char __vsyscall_page;
365379 unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
366380
367
- if (vsyscall_mode != NONE) {
381
+ /*
382
+ * For full emulation, the page needs to exist for real. In
383
+ * execute-only mode, there is no PTE at all backing the vsyscall
384
+ * page.
385
+ */
386
+ if (vsyscall_mode == EMULATE) {
368387 __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
369388 PAGE_KERNEL_VVAR);
370389 set_vsyscall_pgtable_user_bits(swapper_pg_dir);
371390 }
372391
392
+ if (vsyscall_mode == XONLY)
393
+ gate_vma.vm_flags = VM_EXEC;
394
+
373395 BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
374396 (unsigned long)VSYSCALL_ADDR);
375397 }