Skip to content

Commit 5651721

Browse files
redpigtorvalds
authored andcommitted
x86/vsyscall: allow seccomp filter in vsyscall=emulate
If a seccomp filter program is installed, older static binaries and distributions with older libc implementations (glibc 2.13 and earlier) that rely on vsyscall use will be terminated regardless of the filter program policy when executing time, gettimeofday, or getcpu. This is only the case when vsyscall emulation is in use (vsyscall=emulate is the default). This patch emulates system call entry inside a vsyscall=emulate by populating regs->ax and regs->orig_ax with the system call number prior to calling into seccomp such that all seccomp-dependencies function normally. Additionally, system call return behavior is emulated in line with other vsyscall entrypoints for the trace/trap cases. [ v2: fixed ip and sp on SECCOMP_RET_TRAP/TRACE (thanks to luto@mit.edu) ] Reported-and-tested-by: Owen Kibel <qmewlo@gmail.com> Signed-off-by: Will Drewry <wad@chromium.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ac7d181 commit 5651721

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

‎arch/x86/kernel/vsyscall_64.c‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ static int addr_to_vsyscall_nr(unsigned long addr)
139139
return nr;
140140
}
141141

142+
static int vsyscall_seccomp(struct task_struct *tsk, int syscall_nr)
143+
{
144+
if (!seccomp_mode(&tsk->seccomp))
145+
return 0;
146+
task_pt_regs(tsk)->orig_ax = syscall_nr;
147+
task_pt_regs(tsk)->ax = syscall_nr;
148+
return __secure_computing(syscall_nr);
149+
}
150+
142151
static bool write_ok_or_segv(unsigned long ptr, size_t size)
143152
{
144153
/*
@@ -174,6 +183,7 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
174183
int vsyscall_nr;
175184
int prev_sig_on_uaccess_error;
176185
long ret;
186+
int skip;
177187

178188
/*
179189
* No point in checking CS -- the only way to get here is a user mode
@@ -205,9 +215,6 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
205215
}
206216

207217
tsk = current;
208-
if (seccomp_mode(&tsk->seccomp))
209-
do_exit(SIGKILL);
210-
211218
/*
212219
* With a real vsyscall, page faults cause SIGSEGV. We want to
213220
* preserve that behavior to make writing exploits harder.
@@ -222,8 +229,13 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
222229
* address 0".
223230
*/
224231
ret = -EFAULT;
232+
skip = 0;
225233
switch (vsyscall_nr) {
226234
case 0:
235+
skip = vsyscall_seccomp(tsk, __NR_gettimeofday);
236+
if (skip)
237+
break;
238+
227239
if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
228240
!write_ok_or_segv(regs->si, sizeof(struct timezone)))
229241
break;
@@ -234,13 +246,21 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
234246
break;
235247

236248
case 1:
249+
skip = vsyscall_seccomp(tsk, __NR_time);
250+
if (skip)
251+
break;
252+
237253
if (!write_ok_or_segv(regs->di, sizeof(time_t)))
238254
break;
239255

240256
ret = sys_time((time_t __user *)regs->di);
241257
break;
242258

243259
case 2:
260+
skip = vsyscall_seccomp(tsk, __NR_getcpu);
261+
if (skip)
262+
break;
263+
244264
if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
245265
!write_ok_or_segv(regs->si, sizeof(unsigned)))
246266
break;
@@ -253,6 +273,12 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
253273

254274
current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
255275

276+
if (skip) {
277+
if ((long)regs->ax <= 0L) /* seccomp errno emulation */
278+
goto do_ret;
279+
goto done; /* seccomp trace/trap */
280+
}
281+
256282
if (ret == -EFAULT) {
257283
/* Bad news -- userspace fed a bad pointer to a vsyscall. */
258284
warn_bad_vsyscall(KERN_INFO, regs,
@@ -271,10 +297,11 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
271297

272298
regs->ax = ret;
273299

300+
do_ret:
274301
/* Emulate a ret instruction. */
275302
regs->ip = caller;
276303
regs->sp += 8;
277-
304+
done:
278305
return true;
279306

280307
sigsegv:

0 commit comments

Comments
 (0)