Skip to content

Commit c6792d4

Browse files
committed
Merge tag 'core_urgent_for_v5.11_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull syscall entry fixes from Borislav Petkov: - For syscall user dispatch, separate prctl operation from syscall redirection range specification before the API has been made official in 5.11. - Ensure tasks using the generic syscall code do trap after returning from a syscall when single-stepping is requested. * tag 'core_urgent_for_v5.11_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: entry: Use different define for selector variable in SUD entry: Ensure trap after single-step on system call return
2 parents 6fed85d + 36a6c84 commit c6792d4

File tree

10 files changed

+33
-27
lines changed

10 files changed

+33
-27
lines changed

Documentation/admin-guide/syscall-user-dispatch.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ trampoline code on the vDSO, that trampoline is never intercepted.
7070
[selector] is a pointer to a char-sized region in the process memory
7171
region, that provides a quick way to enable disable syscall redirection
7272
thread-wide, without the need to invoke the kernel directly. selector
73-
can be set to PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF. Any other
74-
value should terminate the program with a SIGSYS.
73+
can be set to SYSCALL_DISPATCH_FILTER_ALLOW or SYSCALL_DISPATCH_FILTER_BLOCK.
74+
Any other value should terminate the program with a SIGSYS.
7575

7676
Security Notes
7777
--------------

arch/x86/include/asm/entry-common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ static __always_inline void arch_check_user_regs(struct pt_regs *regs)
4343
}
4444
#define arch_check_user_regs arch_check_user_regs
4545

46-
#define ARCH_SYSCALL_EXIT_WORK (_TIF_SINGLESTEP)
47-
4846
static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
4947
unsigned long ti_work)
5048
{

arch/x86/kernel/step.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ static int enable_single_step(struct task_struct *child)
127127
regs->flags |= X86_EFLAGS_TF;
128128

129129
/*
130-
* Always set TIF_SINGLESTEP - this guarantees that
131-
* we single-step system calls etc.. This will also
130+
* Always set TIF_SINGLESTEP. This will also
132131
* cause us to set TF when returning to user mode.
133132
*/
134133
set_tsk_thread_flag(child, TIF_SINGLESTEP);
135134

135+
/*
136+
* Ensure that a trap is triggered once stepping out of a system
137+
* call prior to executing any user instruction.
138+
*/
139+
set_task_syscall_work(child, SYSCALL_EXIT_TRAP);
140+
136141
oflags = regs->flags;
137142

138143
/* Set TF on the kernel stack.. */
@@ -230,6 +235,7 @@ void user_disable_single_step(struct task_struct *child)
230235

231236
/* Always clear TIF_SINGLESTEP... */
232237
clear_tsk_thread_flag(child, TIF_SINGLESTEP);
238+
clear_task_syscall_work(child, SYSCALL_EXIT_TRAP);
233239

234240
/* But touch TF only if it was set by us.. */
235241
if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))

include/linux/entry-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
SYSCALL_WORK_SYSCALL_TRACE | \
4747
SYSCALL_WORK_SYSCALL_AUDIT | \
4848
SYSCALL_WORK_SYSCALL_USER_DISPATCH | \
49+
SYSCALL_WORK_SYSCALL_EXIT_TRAP | \
4950
ARCH_SYSCALL_WORK_EXIT)
5051

5152
/*

include/linux/thread_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum syscall_work_bit {
4343
SYSCALL_WORK_BIT_SYSCALL_EMU,
4444
SYSCALL_WORK_BIT_SYSCALL_AUDIT,
4545
SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
46+
SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
4647
};
4748

4849
#define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
@@ -51,6 +52,7 @@ enum syscall_work_bit {
5152
#define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
5253
#define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
5354
#define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
55+
#define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
5456
#endif
5557

5658
#include <asm/thread_info.h>

include/uapi/linux/prctl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,8 @@ struct prctl_mm_map {
251251
#define PR_SET_SYSCALL_USER_DISPATCH 59
252252
# define PR_SYS_DISPATCH_OFF 0
253253
# define PR_SYS_DISPATCH_ON 1
254+
/* The control values for the user space selector when dispatch is enabled */
255+
# define SYSCALL_DISPATCH_FILTER_ALLOW 0
256+
# define SYSCALL_DISPATCH_FILTER_BLOCK 1
254257

255258
#endif /* _LINUX_PRCTL_H */

kernel/entry/common.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,18 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)
209209
lockdep_sys_exit();
210210
}
211211

212-
#ifndef _TIF_SINGLESTEP
213-
static inline bool report_single_step(unsigned long work)
214-
{
215-
return false;
216-
}
217-
#else
218212
/*
219213
* If SYSCALL_EMU is set, then the only reason to report is when
220-
* TIF_SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
214+
* SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
221215
* instruction has been already reported in syscall_enter_from_user_mode().
222216
*/
223217
static inline bool report_single_step(unsigned long work)
224218
{
225219
if (work & SYSCALL_WORK_SYSCALL_EMU)
226220
return false;
227221

228-
return !!(current_thread_info()->flags & _TIF_SINGLESTEP);
222+
return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
229223
}
230-
#endif
231-
232224

233225
static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
234226
{

kernel/entry/syscall_user_dispatch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ bool syscall_user_dispatch(struct pt_regs *regs)
5050
if (unlikely(__get_user(state, sd->selector)))
5151
do_exit(SIGSEGV);
5252

53-
if (likely(state == PR_SYS_DISPATCH_OFF))
53+
if (likely(state == SYSCALL_DISPATCH_FILTER_ALLOW))
5454
return false;
5555

56-
if (state != PR_SYS_DISPATCH_ON)
56+
if (state != SYSCALL_DISPATCH_FILTER_BLOCK)
5757
do_exit(SIGSYS);
5858
}
5959

tools/testing/selftests/syscall_user_dispatch/sud_benchmark.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# define PR_SET_SYSCALL_USER_DISPATCH 59
2323
# define PR_SYS_DISPATCH_OFF 0
2424
# define PR_SYS_DISPATCH_ON 1
25+
# define SYSCALL_DISPATCH_FILTER_ALLOW 0
26+
# define SYSCALL_DISPATCH_FILTER_BLOCK 1
2527
#endif
2628

2729
#ifdef __NR_syscalls
@@ -55,8 +57,8 @@ unsigned long trapped_call_count = 0;
5557
unsigned long native_call_count = 0;
5658

5759
char selector;
58-
#define SYSCALL_BLOCK (selector = PR_SYS_DISPATCH_ON)
59-
#define SYSCALL_UNBLOCK (selector = PR_SYS_DISPATCH_OFF)
60+
#define SYSCALL_BLOCK (selector = SYSCALL_DISPATCH_FILTER_BLOCK)
61+
#define SYSCALL_UNBLOCK (selector = SYSCALL_DISPATCH_FILTER_ALLOW)
6062

6163
#define CALIBRATION_STEP 100000
6264
#define CALIBRATE_TO_SECS 5
@@ -170,7 +172,7 @@ int main(void)
170172
syscall(MAGIC_SYSCALL_1);
171173

172174
#ifdef TEST_BLOCKED_RETURN
173-
if (selector == PR_SYS_DISPATCH_OFF) {
175+
if (selector == SYSCALL_DISPATCH_FILTER_ALLOW) {
174176
fprintf(stderr, "Failed to return with selector blocked.\n");
175177
exit(-1);
176178
}

tools/testing/selftests/syscall_user_dispatch/sud_test.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# define PR_SET_SYSCALL_USER_DISPATCH 59
1919
# define PR_SYS_DISPATCH_OFF 0
2020
# define PR_SYS_DISPATCH_ON 1
21+
# define SYSCALL_DISPATCH_FILTER_ALLOW 0
22+
# define SYSCALL_DISPATCH_FILTER_BLOCK 1
2123
#endif
2224

2325
#ifndef SYS_USER_DISPATCH
@@ -30,8 +32,8 @@
3032
# define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
3133
#endif
3234

33-
#define SYSCALL_DISPATCH_ON(x) ((x) = 1)
34-
#define SYSCALL_DISPATCH_OFF(x) ((x) = 0)
35+
#define SYSCALL_DISPATCH_ON(x) ((x) = SYSCALL_DISPATCH_FILTER_BLOCK)
36+
#define SYSCALL_DISPATCH_OFF(x) ((x) = SYSCALL_DISPATCH_FILTER_ALLOW)
3537

3638
/* Test Summary:
3739
*
@@ -56,7 +58,7 @@
5658

5759
TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS)
5860
{
59-
char sel = 0;
61+
char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
6062
struct sysinfo info;
6163
int ret;
6264

@@ -79,7 +81,7 @@ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS)
7981

8082
TEST(bad_prctl_param)
8183
{
82-
char sel = 0;
84+
char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
8385
int op;
8486

8587
/* Invalid op */
@@ -220,7 +222,7 @@ TEST_SIGNAL(bad_selector, SIGSYS)
220222
sigset_t mask;
221223
struct sysinfo info;
222224

223-
glob_sel = 0;
225+
glob_sel = SYSCALL_DISPATCH_FILTER_ALLOW;
224226
nr_syscalls_emulated = 0;
225227
si_code = 0;
226228
si_errno = 0;
@@ -288,7 +290,7 @@ TEST(direct_dispatch_range)
288290
{
289291
int ret = 0;
290292
struct sysinfo info;
291-
char sel = 0;
293+
char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
292294

293295
/*
294296
* Instead of calculating libc addresses; allow the entire

0 commit comments

Comments
 (0)