Skip to content

Commit 2a74dbb

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem updates from James Morris: "A quiet cycle for the security subsystem with just a few maintenance updates." * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: Smack: create a sysfs mount point for smackfs Smack: use select not depends in Kconfig Yama: remove locking from delete path Yama: add RCU to drop read locking drivers/char/tpm: remove tasklet and cleanup KEYS: Use keyring_alloc() to create special keyrings KEYS: Reduce initial permissions on keys KEYS: Make the session and process keyrings per-thread seccomp: Make syscall skipping and nr changes more consistent key: Fix resource leak keys: Fix unreachable code KEYS: Add payload preparsing opportunity prior to key instantiate or update
2 parents 770b6cb + e930723 commit 2a74dbb

20 files changed

Lines changed: 369 additions & 370 deletions

File tree

‎Documentation/prctl/seccomp_filter.txt‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,15 @@ SECCOMP_RET_KILL:
9595

9696
SECCOMP_RET_TRAP:
9797
Results in the kernel sending a SIGSYS signal to the triggering
98-
task without executing the system call. The kernel will
99-
rollback the register state to just before the system call
100-
entry such that a signal handler in the task will be able to
101-
inspect the ucontext_t->uc_mcontext registers and emulate
102-
system call success or failure upon return from the signal
103-
handler.
98+
task without executing the system call. siginfo->si_call_addr
99+
will show the address of the system call instruction, and
100+
siginfo->si_syscall and siginfo->si_arch will indicate which
101+
syscall was attempted. The program counter will be as though
102+
the syscall happened (i.e. it will not point to the syscall
103+
instruction). The return value register will contain an arch-
104+
dependent value -- if resuming execution, set it to something
105+
sensible. (The architecture dependency is because replacing
106+
it with -ENOSYS could overwrite some useful information.)
104107

105108
The SECCOMP_RET_DATA portion of the return value will be passed
106109
as si_errno.
@@ -123,6 +126,18 @@ SECCOMP_RET_TRACE:
123126
the BPF program return value will be available to the tracer
124127
via PTRACE_GETEVENTMSG.
125128

129+
The tracer can skip the system call by changing the syscall number
130+
to -1. Alternatively, the tracer can change the system call
131+
requested by changing the system call to a valid syscall number. If
132+
the tracer asks to skip the system call, then the system call will
133+
appear to return the value that the tracer puts in the return value
134+
register.
135+
136+
The seccomp check will not be run again after the tracer is
137+
notified. (This means that seccomp-based sandboxes MUST NOT
138+
allow use of ptrace, even of other sandboxed processes, without
139+
extreme care; ptracers can use this mechanism to escape.)
140+
126141
SECCOMP_RET_ALLOW:
127142
Results in the system call being executed.
128143

@@ -161,3 +176,50 @@ architecture supports both ptrace_event and seccomp, it will be able to
161176
support seccomp filter with minor fixup: SIGSYS support and seccomp return
162177
value checking. Then it must just add CONFIG_HAVE_ARCH_SECCOMP_FILTER
163178
to its arch-specific Kconfig.
179+
180+
181+
182+
Caveats
183+
-------
184+
185+
The vDSO can cause some system calls to run entirely in userspace,
186+
leading to surprises when you run programs on different machines that
187+
fall back to real syscalls. To minimize these surprises on x86, make
188+
sure you test with
189+
/sys/devices/system/clocksource/clocksource0/current_clocksource set to
190+
something like acpi_pm.
191+
192+
On x86-64, vsyscall emulation is enabled by default. (vsyscalls are
193+
legacy variants on vDSO calls.) Currently, emulated vsyscalls will honor seccomp, with a few oddities:
194+
195+
- A return value of SECCOMP_RET_TRAP will set a si_call_addr pointing to
196+
the vsyscall entry for the given call and not the address after the
197+
'syscall' instruction. Any code which wants to restart the call
198+
should be aware that (a) a ret instruction has been emulated and (b)
199+
trying to resume the syscall will again trigger the standard vsyscall
200+
emulation security checks, making resuming the syscall mostly
201+
pointless.
202+
203+
- A return value of SECCOMP_RET_TRACE will signal the tracer as usual,
204+
but the syscall may not be changed to another system call using the
205+
orig_rax register. It may only be changed to -1 order to skip the
206+
currently emulated call. Any other change MAY terminate the process.
207+
The rip value seen by the tracer will be the syscall entry address;
208+
this is different from normal behavior. The tracer MUST NOT modify
209+
rip or rsp. (Do not rely on other changes terminating the process.
210+
They might work. For example, on some kernels, choosing a syscall
211+
that only exists in future kernels will be correctly emulated (by
212+
returning -ENOSYS).
213+
214+
To detect this quirky behavior, check for addr & ~0x0C00 ==
215+
0xFFFFFFFFFF600000. (For SECCOMP_RET_TRACE, use rip. For
216+
SECCOMP_RET_TRAP, use siginfo->si_call_addr.) Do not check any other
217+
condition: future kernels may improve vsyscall emulation and current
218+
kernels in vsyscall=native mode will behave differently, but the
219+
instructions at 0xF...F600{0,4,8,C}00 will not be system calls in these
220+
cases.
221+
222+
Note that modern systems are unlikely to use vsyscalls at all -- they
223+
are a legacy feature and they are considerably slower than standard
224+
syscalls. New code will use the vDSO, and vDSO-issued system calls
225+
are indistinguishable from normal system calls.

‎Documentation/security/keys.txt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,23 @@ payload contents" for more information.
994994
reference pointer if successful.
995995

996996

997+
(*) A keyring can be created by:
998+
999+
struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
1000+
const struct cred *cred,
1001+
key_perm_t perm,
1002+
unsigned long flags,
1003+
struct key *dest);
1004+
1005+
This creates a keyring with the given attributes and returns it. If dest
1006+
is not NULL, the new keyring will be linked into the keyring to which it
1007+
points. No permission checks are made upon the destination keyring.
1008+
1009+
Error EDQUOT can be returned if the keyring would overload the quota (pass
1010+
KEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accounted
1011+
towards the user's quota). Error ENOMEM can also be returned.
1012+
1013+
9971014
(*) To check the validity of a key, this function can be called:
9981015

9991016
int validate_key(struct key *key);

‎arch/x86/kernel/vsyscall_64.c‎

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,6 @@ static int addr_to_vsyscall_nr(unsigned long addr)
145145
return nr;
146146
}
147147

148-
#ifdef CONFIG_SECCOMP
149-
static int vsyscall_seccomp(struct task_struct *tsk, int syscall_nr)
150-
{
151-
if (!seccomp_mode(&tsk->seccomp))
152-
return 0;
153-
task_pt_regs(tsk)->orig_ax = syscall_nr;
154-
task_pt_regs(tsk)->ax = syscall_nr;
155-
return __secure_computing(syscall_nr);
156-
}
157-
#else
158-
#define vsyscall_seccomp(_tsk, _nr) 0
159-
#endif
160-
161148
static bool write_ok_or_segv(unsigned long ptr, size_t size)
162149
{
163150
/*
@@ -190,10 +177,9 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
190177
{
191178
struct task_struct *tsk;
192179
unsigned long caller;
193-
int vsyscall_nr;
180+
int vsyscall_nr, syscall_nr, tmp;
194181
int prev_sig_on_uaccess_error;
195182
long ret;
196-
int skip;
197183

198184
/*
199185
* No point in checking CS -- the only way to get here is a user mode
@@ -225,56 +211,84 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
225211
}
226212

227213
tsk = current;
228-
/*
229-
* With a real vsyscall, page faults cause SIGSEGV. We want to
230-
* preserve that behavior to make writing exploits harder.
231-
*/
232-
prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
233-
current_thread_info()->sig_on_uaccess_error = 1;
234214

235215
/*
216+
* Check for access_ok violations and find the syscall nr.
217+
*
236218
* NULL is a valid user pointer (in the access_ok sense) on 32-bit and
237219
* 64-bit, so we don't need to special-case it here. For all the
238220
* vsyscalls, NULL means "don't write anything" not "write it at
239221
* address 0".
240222
*/
241-
ret = -EFAULT;
242-
skip = 0;
243223
switch (vsyscall_nr) {
244224
case 0:
245-
skip = vsyscall_seccomp(tsk, __NR_gettimeofday);
246-
if (skip)
247-
break;
248-
249225
if (!write_ok_or_segv(regs->di, sizeof(struct timeval)) ||
250-
!write_ok_or_segv(regs->si, sizeof(struct timezone)))
251-
break;
226+
!write_ok_or_segv(regs->si, sizeof(struct timezone))) {
227+
ret = -EFAULT;
228+
goto check_fault;
229+
}
230+
231+
syscall_nr = __NR_gettimeofday;
232+
break;
233+
234+
case 1:
235+
if (!write_ok_or_segv(regs->di, sizeof(time_t))) {
236+
ret = -EFAULT;
237+
goto check_fault;
238+
}
239+
240+
syscall_nr = __NR_time;
241+
break;
242+
243+
case 2:
244+
if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
245+
!write_ok_or_segv(regs->si, sizeof(unsigned))) {
246+
ret = -EFAULT;
247+
goto check_fault;
248+
}
249+
250+
syscall_nr = __NR_getcpu;
251+
break;
252+
}
253+
254+
/*
255+
* Handle seccomp. regs->ip must be the original value.
256+
* See seccomp_send_sigsys and Documentation/prctl/seccomp_filter.txt.
257+
*
258+
* We could optimize the seccomp disabled case, but performance
259+
* here doesn't matter.
260+
*/
261+
regs->orig_ax = syscall_nr;
262+
regs->ax = -ENOSYS;
263+
tmp = secure_computing(syscall_nr);
264+
if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
265+
warn_bad_vsyscall(KERN_DEBUG, regs,
266+
"seccomp tried to change syscall nr or ip");
267+
do_exit(SIGSYS);
268+
}
269+
if (tmp)
270+
goto do_ret; /* skip requested */
252271

272+
/*
273+
* With a real vsyscall, page faults cause SIGSEGV. We want to
274+
* preserve that behavior to make writing exploits harder.
275+
*/
276+
prev_sig_on_uaccess_error = current_thread_info()->sig_on_uaccess_error;
277+
current_thread_info()->sig_on_uaccess_error = 1;
278+
279+
ret = -EFAULT;
280+
switch (vsyscall_nr) {
281+
case 0:
253282
ret = sys_gettimeofday(
254283
(struct timeval __user *)regs->di,
255284
(struct timezone __user *)regs->si);
256285
break;
257286

258287
case 1:
259-
skip = vsyscall_seccomp(tsk, __NR_time);
260-
if (skip)
261-
break;
262-
263-
if (!write_ok_or_segv(regs->di, sizeof(time_t)))
264-
break;
265-
266288
ret = sys_time((time_t __user *)regs->di);
267289
break;
268290

269291
case 2:
270-
skip = vsyscall_seccomp(tsk, __NR_getcpu);
271-
if (skip)
272-
break;
273-
274-
if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
275-
!write_ok_or_segv(regs->si, sizeof(unsigned)))
276-
break;
277-
278292
ret = sys_getcpu((unsigned __user *)regs->di,
279293
(unsigned __user *)regs->si,
280294
NULL);
@@ -283,12 +297,7 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
283297

284298
current_thread_info()->sig_on_uaccess_error = prev_sig_on_uaccess_error;
285299

286-
if (skip) {
287-
if ((long)regs->ax <= 0L) /* seccomp errno emulation */
288-
goto do_ret;
289-
goto done; /* seccomp trace/trap */
290-
}
291-
300+
check_fault:
292301
if (ret == -EFAULT) {
293302
/* Bad news -- userspace fed a bad pointer to a vsyscall. */
294303
warn_bad_vsyscall(KERN_INFO, regs,
@@ -311,7 +320,6 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
311320
/* Emulate a ret instruction. */
312321
regs->ip = caller;
313322
regs->sp += 8;
314-
done:
315323
return true;
316324

317325
sigsegv:

0 commit comments

Comments
 (0)