Skip to content

Commit dac494b

Browse files
benzeajmberg
authored andcommitted
um: Add stub side of SECCOMP/futex based process handling
This adds the stub side for the new seccomp process management code. In this case we do register save/restore through the signal handler mcontext. Add special code for handling TLS, which for x86_64 means setting the FS_BASE/GS_BASE registers while for i386 it means calling the set_thread_area syscall. Co-authored-by: Johannes Berg <[email protected]> Signed-off-by: Benjamin Berg <[email protected]> Signed-off-by: Benjamin Berg <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Johannes Berg <[email protected]>
1 parent 247ed9e commit dac494b

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

arch/um/include/shared/common-offsets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ DEFINE(UM_THREAD_SIZE, THREAD_SIZE);
1414

1515
DEFINE(UM_NSEC_PER_SEC, NSEC_PER_SEC);
1616
DEFINE(UM_NSEC_PER_USEC, NSEC_PER_USEC);
17+
18+
DEFINE(UM_KERN_GDT_ENTRY_TLS_ENTRIES, GDT_ENTRY_TLS_ENTRIES);

arch/um/include/shared/skas/stub-data.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <linux/compiler_types.h>
1212
#include <as-layout.h>
1313
#include <sysdep/tls.h>
14+
#include <sysdep/stub-data.h>
15+
16+
#define FUTEX_IN_CHILD 0
17+
#define FUTEX_IN_KERN 1
1418

1519
struct stub_init_data {
1620
unsigned long stub_start;
@@ -52,6 +56,16 @@ struct stub_data {
5256
/* 128 leaves enough room for additional fields in the struct */
5357
struct stub_syscall syscall_data[(UM_KERN_PAGE_SIZE - 128) / sizeof(struct stub_syscall)] __aligned(16);
5458

59+
/* data shared with signal handler (only used in seccomp mode) */
60+
short restart_wait;
61+
unsigned int futex;
62+
int signal;
63+
unsigned short si_offset;
64+
unsigned short mctx_offset;
65+
66+
/* seccomp architecture specific state restore */
67+
struct stub_data_arch arch_data;
68+
5569
/* Stack for our signal handlers and for calling into . */
5670
unsigned char sigstack[UM_KERN_PAGE_SIZE] __aligned(UM_KERN_PAGE_SIZE);
5771
};

arch/um/kernel/skas/stub.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include <sysdep/stub.h>
77

8+
#include <linux/futex.h>
9+
#include <errno.h>
10+
811
static __always_inline int syscall_handler(struct stub_data *d)
912
{
1013
int i;
@@ -57,3 +60,49 @@ stub_syscall_handler(void)
5760

5861
trap_myself();
5962
}
63+
64+
void __section(".__syscall_stub")
65+
stub_signal_interrupt(int sig, siginfo_t *info, void *p)
66+
{
67+
struct stub_data *d = get_stub_data();
68+
ucontext_t *uc = p;
69+
long res;
70+
71+
d->signal = sig;
72+
d->si_offset = (unsigned long)info - (unsigned long)&d->sigstack[0];
73+
d->mctx_offset = (unsigned long)&uc->uc_mcontext - (unsigned long)&d->sigstack[0];
74+
75+
restart_wait:
76+
d->futex = FUTEX_IN_KERN;
77+
do {
78+
res = stub_syscall3(__NR_futex, (unsigned long)&d->futex,
79+
FUTEX_WAKE, 1);
80+
} while (res == -EINTR);
81+
do {
82+
res = stub_syscall4(__NR_futex, (unsigned long)&d->futex,
83+
FUTEX_WAIT, FUTEX_IN_KERN, 0);
84+
} while (res == -EINTR || d->futex == FUTEX_IN_KERN);
85+
86+
if (res < 0 && res != -EAGAIN)
87+
stub_syscall1(__NR_exit_group, 1);
88+
89+
/* Try running queued syscalls. */
90+
if (syscall_handler(d) < 0 || d->restart_wait) {
91+
/* Report SIGSYS if we restart. */
92+
d->signal = SIGSYS;
93+
d->restart_wait = 0;
94+
goto restart_wait;
95+
}
96+
97+
/* Restore arch dependent state that is not part of the mcontext */
98+
stub_seccomp_restore_state(&d->arch_data);
99+
100+
/* Return so that the host modified mcontext is restored. */
101+
}
102+
103+
void __section(".__syscall_stub")
104+
stub_signal_restorer(void)
105+
{
106+
/* We must not have anything on the stack when doing rt_sigreturn */
107+
stub_syscall0(__NR_rt_sigreturn);
108+
}

arch/x86/um/shared/sysdep/stub-data.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ARCH_STUB_DATA_H
3+
#define __ARCH_STUB_DATA_H
4+
5+
#ifdef __i386__
6+
#include <generated/asm-offsets.h>
7+
#include <asm/ldt.h>
8+
9+
struct stub_data_arch {
10+
int sync;
11+
struct user_desc tls[UM_KERN_GDT_ENTRY_TLS_ENTRIES];
12+
};
13+
#else
14+
#define STUB_SYNC_FS_BASE (1 << 0)
15+
#define STUB_SYNC_GS_BASE (1 << 1)
16+
struct stub_data_arch {
17+
int sync;
18+
unsigned long fs_base;
19+
unsigned long gs_base;
20+
};
21+
#endif
22+
23+
#endif /* __ARCH_STUB_DATA_H */

arch/x86/um/shared/sysdep/stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
extern void stub_segv_handler(int, siginfo_t *, void *);
1515
extern void stub_syscall_handler(void);
16+
extern void stub_signal_interrupt(int, siginfo_t *, void *);
17+
extern void stub_signal_restorer(void);

arch/x86/um/shared/sysdep/stub_32.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,17 @@ static __always_inline void *get_stub_data(void)
131131
"call *%%eax ;" \
132132
:: "i" ((1 + STUB_DATA_PAGES) * UM_KERN_PAGE_SIZE), \
133133
"i" (&fn))
134+
135+
static __always_inline void
136+
stub_seccomp_restore_state(struct stub_data_arch *arch)
137+
{
138+
for (int i = 0; i < sizeof(arch->tls) / sizeof(arch->tls[0]); i++) {
139+
if (arch->sync & (1 << i))
140+
stub_syscall1(__NR_set_thread_area,
141+
(unsigned long) &arch->tls[i]);
142+
}
143+
144+
arch->sync = 0;
145+
}
146+
134147
#endif

arch/x86/um/shared/sysdep/stub_64.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sysdep/ptrace_user.h>
1111
#include <generated/asm-offsets.h>
1212
#include <linux/stddef.h>
13+
#include <asm/prctl.h>
1314

1415
#define STUB_MMAP_NR __NR_mmap
1516
#define MMAP_OFFSET(o) (o)
@@ -134,4 +135,20 @@ static __always_inline void *get_stub_data(void)
134135
"call *%%rax ;" \
135136
:: "i" ((1 + STUB_DATA_PAGES) * UM_KERN_PAGE_SIZE), \
136137
"i" (&fn))
138+
139+
static __always_inline void
140+
stub_seccomp_restore_state(struct stub_data_arch *arch)
141+
{
142+
/*
143+
* We could use _writefsbase_u64/_writegsbase_u64 if the host reports
144+
* support in the hwcaps (HWCAP2_FSGSBASE).
145+
*/
146+
if (arch->sync & STUB_SYNC_FS_BASE)
147+
stub_syscall2(__NR_arch_prctl, ARCH_SET_FS, arch->fs_base);
148+
if (arch->sync & STUB_SYNC_GS_BASE)
149+
stub_syscall2(__NR_arch_prctl, ARCH_SET_GS, arch->gs_base);
150+
151+
arch->sync = 0;
152+
}
153+
137154
#endif

0 commit comments

Comments
 (0)