Skip to content

Commit 8010094

Browse files
Fix a handful of audit-related issue
This is sort of a mix between a new feature and a bug fix. I've managed to screw up merging this patch set a handful of times but I think it's OK this time around. The main new feature here is audit support for RISC-V, with some fixes to audit-related bugs that cropped up along the way: * The addition of NR_syscalls into unistd.h, which is necessary for CONFIG_FTRACE_SYSCALLS. * The definition of CREATE_TRACE_POINTS so __tracepoint_sys_{enter,exit} get defined. * A fix for trace_sys_exit() so we can enable CONFIG_HAVE_SYSCALL_TRACEPOINTS.
2 parents 2cffc95 + 5aeb1b3 commit 8010094

8 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎arch/riscv/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ config RISCV
2828
select GENERIC_STRNLEN_USER
2929
select GENERIC_SMP_IDLE_THREAD
3030
select GENERIC_ATOMIC64 if !64BIT || !RISCV_ISA_A
31+
select HAVE_ARCH_AUDITSYSCALL
3132
select HAVE_MEMBLOCK_NODE_MAP
3233
select HAVE_DMA_CONTIGUOUS
3334
select HAVE_FUTEX_CMPXCHG if FUTEX
3435
select HAVE_GENERIC_DMA_COHERENT
3536
select HAVE_PERF_EVENTS
37+
select HAVE_SYSCALL_TRACEPOINTS
3638
select IRQ_DOMAIN
3739
select RISCV_ISA_A if SMP
3840
select SPARSE_IRQ

‎arch/riscv/include/asm/ptrace.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ static inline void frame_pointer_set(struct pt_regs *regs,
113113
SET_FP(regs, val);
114114
}
115115

116+
static inline unsigned long regs_return_value(struct pt_regs *regs)
117+
{
118+
return regs->a0;
119+
}
120+
116121
#endif /* __ASSEMBLY__ */
117122

118123
#endif /* _ASM_RISCV_PTRACE_H */

‎arch/riscv/include/asm/syscall.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef _ASM_RISCV_SYSCALL_H
1919
#define _ASM_RISCV_SYSCALL_H
2020

21+
#include <uapi/linux/audit.h>
2122
#include <linux/sched.h>
2223
#include <linux/err.h>
2324

@@ -99,4 +100,13 @@ static inline void syscall_set_arguments(struct task_struct *task,
99100
memcpy(&regs->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0));
100101
}
101102

103+
static inline int syscall_get_arch(void)
104+
{
105+
#ifdef CONFIG_64BIT
106+
return AUDIT_ARCH_RISCV64;
107+
#else
108+
return AUDIT_ARCH_RISCV32;
109+
#endif
110+
}
111+
102112
#endif /* _ASM_RISCV_SYSCALL_H */

‎arch/riscv/include/asm/thread_info.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ struct thread_info {
8080
#define TIF_RESTORE_SIGMASK 4 /* restore signal mask in do_signal() */
8181
#define TIF_MEMDIE 5 /* is terminating due to OOM killer */
8282
#define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
83+
#define TIF_SYSCALL_AUDIT 7 /* syscall auditing */
8384

8485
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
8586
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
8687
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
8788
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
89+
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
90+
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
8891

8992
#define _TIF_WORK_MASK \
9093
(_TIF_NOTIFY_RESUME | _TIF_SIGPENDING | _TIF_NEED_RESCHED)
9194

95+
#define _TIF_SYSCALL_WORK \
96+
(_TIF_SYSCALL_TRACE | _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_AUDIT)
97+
9298
#endif /* _ASM_RISCV_THREAD_INFO_H */

‎arch/riscv/include/asm/unistd.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@
1919
#define __ARCH_WANT_SYS_CLONE
2020

2121
#include <uapi/asm/unistd.h>
22+
23+
#define NR_syscalls (__NR_syscalls)

‎arch/riscv/kernel/entry.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ handle_syscall:
201201
REG_S s2, PT_SEPC(sp)
202202
/* Trace syscalls, but only if requested by the user. */
203203
REG_L t0, TASK_TI_FLAGS(tp)
204-
andi t0, t0, _TIF_SYSCALL_TRACE
204+
andi t0, t0, _TIF_SYSCALL_WORK
205205
bnez t0, handle_syscall_trace_enter
206206
check_syscall_nr:
207207
/* Check to make sure we don't jump to a bogus syscall number. */
@@ -221,7 +221,7 @@ ret_from_syscall:
221221
REG_S a0, PT_A0(sp)
222222
/* Trace syscalls, but only if requested by the user. */
223223
REG_L t0, TASK_TI_FLAGS(tp)
224-
andi t0, t0, _TIF_SYSCALL_TRACE
224+
andi t0, t0, _TIF_SYSCALL_WORK
225225
bnez t0, handle_syscall_trace_exit
226226

227227
ret_from_exception:

‎arch/riscv/kernel/ptrace.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
#include <asm/ptrace.h>
1919
#include <asm/syscall.h>
2020
#include <asm/thread_info.h>
21+
#include <linux/audit.h>
2122
#include <linux/ptrace.h>
2223
#include <linux/elf.h>
2324
#include <linux/regset.h>
2425
#include <linux/sched.h>
2526
#include <linux/sched/task_stack.h>
2627
#include <linux/tracehook.h>
28+
29+
#define CREATE_TRACE_POINTS
2730
#include <trace/events/syscalls.h>
2831

2932
enum riscv_regset {
@@ -163,15 +166,19 @@ void do_syscall_trace_enter(struct pt_regs *regs)
163166
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
164167
trace_sys_enter(regs, syscall_get_nr(current, regs));
165168
#endif
169+
170+
audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
166171
}
167172

168173
void do_syscall_trace_exit(struct pt_regs *regs)
169174
{
175+
audit_syscall_exit(regs);
176+
170177
if (test_thread_flag(TIF_SYSCALL_TRACE))
171178
tracehook_report_syscall_exit(regs, 0);
172179

173180
#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
174181
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
175-
trace_sys_exit(regs, regs->regs[0]);
182+
trace_sys_exit(regs, regs_return_value(regs));
176183
#endif
177184
}

‎include/uapi/linux/audit.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ enum {
400400
/* do not define AUDIT_ARCH_PPCLE since it is not supported by audit */
401401
#define AUDIT_ARCH_PPC64 (EM_PPC64|__AUDIT_ARCH_64BIT)
402402
#define AUDIT_ARCH_PPC64LE (EM_PPC64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
403+
#define AUDIT_ARCH_RISCV32 (EM_RISCV|__AUDIT_ARCH_LE)
404+
#define AUDIT_ARCH_RISCV64 (EM_RISCV|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
403405
#define AUDIT_ARCH_S390 (EM_S390)
404406
#define AUDIT_ARCH_S390X (EM_S390|__AUDIT_ARCH_64BIT)
405407
#define AUDIT_ARCH_SH (EM_SH)

0 commit comments

Comments
 (0)