Skip to content

Commit e95a4f8

Browse files
committed
csky: Add SECCOMP_FILTER supported
secure_computing() is called first in syscall_trace_enter() so that a system call will be aborted quickly without doing succeeding syscall tracing if seccomp rules want to deny that system call. TODO: - Update https://github.com/seccomp/libseccomp csky support Signed-off-by: Guo Ren <[email protected]> Cc: Arnd Bergmann <[email protected]>
1 parent c23dd24 commit e95a4f8

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

arch/csky/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config CSKY
3838
select GX6605S_TIMER if CPU_CK610
3939
select HAVE_ARCH_TRACEHOOK
4040
select HAVE_ARCH_AUDITSYSCALL
41+
select HAVE_ARCH_SECCOMP_FILTER
4142
select HAVE_COPY_THREAD_TLS
4243
select HAVE_DEBUG_BUGVERBOSE
4344
select HAVE_DYNAMIC_FTRACE
@@ -296,3 +297,16 @@ endmenu
296297
source "arch/csky/Kconfig.platforms"
297298

298299
source "kernel/Kconfig.hz"
300+
301+
config SECCOMP
302+
bool "Enable seccomp to safely compute untrusted bytecode"
303+
help
304+
This kernel feature is useful for number crunching applications
305+
that may need to compute untrusted bytecode during their
306+
execution. By using pipes or other transports made available to
307+
the process as file descriptors supporting the read/write
308+
syscalls, it's possible to isolate those applications in
309+
their own address space using seccomp. Once seccomp is
310+
enabled via prctl(PR_SET_SECCOMP), it cannot be disabled
311+
and the task is only allowed to execute a few safe syscalls
312+
defined by each seccomp mode.

arch/csky/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ generic-y += gpio.h
44
generic-y += kvm_para.h
55
generic-y += local64.h
66
generic-y += qrwlock.h
7+
generic-y += seccomp.h
78
generic-y += user.h
89
generic-y += vmlinux.lds.h

arch/csky/include/asm/thread_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ static inline struct thread_info *current_thread_info(void)
8585
_TIF_NOTIFY_RESUME | _TIF_UPROBE)
8686

8787
#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
88-
_TIF_SYSCALL_TRACEPOINT)
88+
_TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP)
8989

9090
#endif /* _ASM_CSKY_THREAD_INFO_H */

arch/csky/kernel/entry.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ ENTRY(csky_systemcall)
168168
csky_syscall_trace:
169169
mov a0, sp /* sp = pt_regs pointer */
170170
jbsr syscall_trace_enter
171+
cmpnei a0, 0
172+
bt 1f
171173
/* Prepare args before do system call */
172174
ldw a0, (sp, LSAVE_A0)
173175
ldw a1, (sp, LSAVE_A1)
@@ -188,6 +190,7 @@ csky_syscall_trace:
188190
#endif
189191
stw a0, (sp, LSAVE_A0) /* Save return value */
190192

193+
1:
191194
#ifdef CONFIG_DEBUG_RSEQ
192195
mov a0, sp
193196
jbsr rseq_syscall

arch/csky/kernel/ptrace.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,20 @@ long arch_ptrace(struct task_struct *child, long request,
320320
return ret;
321321
}
322322

323-
asmlinkage void syscall_trace_enter(struct pt_regs *regs)
323+
asmlinkage int syscall_trace_enter(struct pt_regs *regs)
324324
{
325325
if (test_thread_flag(TIF_SYSCALL_TRACE))
326326
if (tracehook_report_syscall_entry(regs))
327-
syscall_set_nr(current, regs, -1);
327+
return -1;
328+
329+
if (secure_computing() == -1)
330+
return -1;
328331

329332
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
330333
trace_sys_enter(regs, syscall_get_nr(current, regs));
331334

332335
audit_syscall_entry(regs_syscallid(regs), regs->a0, regs->a1, regs->a2, regs->a3);
336+
return 0;
333337
}
334338

335339
asmlinkage void syscall_trace_exit(struct pt_regs *regs)

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ struct seccomp_data {
116116
# define __NR_seccomp 277
117117
# elif defined(__riscv)
118118
# define __NR_seccomp 277
119+
# elif defined(__csky__)
120+
# define __NR_seccomp 277
119121
# elif defined(__hppa__)
120122
# define __NR_seccomp 338
121123
# elif defined(__powerpc__)
@@ -1603,6 +1605,14 @@ TEST_F(TRACE_poke, getpid_runs_normally)
16031605
# define ARCH_REGS struct user_regs_struct
16041606
# define SYSCALL_NUM a7
16051607
# define SYSCALL_RET a0
1608+
#elif defined(__csky__)
1609+
# define ARCH_REGS struct pt_regs
1610+
#if defined(__CSKYABIV2__)
1611+
# define SYSCALL_NUM regs[3]
1612+
#else
1613+
# define SYSCALL_NUM regs[9]
1614+
#endif
1615+
# define SYSCALL_RET a0
16061616
#elif defined(__hppa__)
16071617
# define ARCH_REGS struct user_regs_struct
16081618
# define SYSCALL_NUM gr[20]
@@ -1693,7 +1703,8 @@ void change_syscall(struct __test_metadata *_metadata,
16931703
EXPECT_EQ(0, ret) {}
16941704

16951705
#if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
1696-
defined(__s390__) || defined(__hppa__) || defined(__riscv)
1706+
defined(__s390__) || defined(__hppa__) || defined(__riscv) || \
1707+
defined(__csky__)
16971708
{
16981709
regs.SYSCALL_NUM = syscall;
16991710
}

0 commit comments

Comments
 (0)