Skip to content

Commit 1d9d02f

Browse files
aarcangeLinus Torvalds
authored andcommitted
move seccomp from /proc to a prctl
This reduces the memory footprint and it enforces that only the current task can enable seccomp on itself (this is a requirement for a strightforward [modulo preempt ;) ] TIF_NOTSC implementation). Signed-off-by: Andrea Arcangeli <andrea@cpushare.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent be0ef95 commit 1d9d02f

5 files changed

Lines changed: 51 additions & 74 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
#include <linux/mount.h>
6868
#include <linux/security.h>
6969
#include <linux/ptrace.h>
70-
#include <linux/seccomp.h>
7170
#include <linux/cpuset.h>
7271
#include <linux/audit.h>
7372
#include <linux/poll.h>
@@ -817,71 +816,6 @@ static const struct file_operations proc_loginuid_operations = {
817816
};
818817
#endif
819818

820-
#ifdef CONFIG_SECCOMP
821-
static ssize_t seccomp_read(struct file *file, char __user *buf,
822-
size_t count, loff_t *ppos)
823-
{
824-
struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
825-
char __buf[20];
826-
size_t len;
827-
828-
if (!tsk)
829-
return -ESRCH;
830-
/* no need to print the trailing zero, so use only len */
831-
len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
832-
put_task_struct(tsk);
833-
834-
return simple_read_from_buffer(buf, count, ppos, __buf, len);
835-
}
836-
837-
static ssize_t seccomp_write(struct file *file, const char __user *buf,
838-
size_t count, loff_t *ppos)
839-
{
840-
struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
841-
char __buf[20], *end;
842-
unsigned int seccomp_mode;
843-
ssize_t result;
844-
845-
result = -ESRCH;
846-
if (!tsk)
847-
goto out_no_task;
848-
849-
/* can set it only once to be even more secure */
850-
result = -EPERM;
851-
if (unlikely(tsk->seccomp.mode))
852-
goto out;
853-
854-
result = -EFAULT;
855-
memset(__buf, 0, sizeof(__buf));
856-
count = min(count, sizeof(__buf) - 1);
857-
if (copy_from_user(__buf, buf, count))
858-
goto out;
859-
860-
seccomp_mode = simple_strtoul(__buf, &end, 0);
861-
if (*end == '\n')
862-
end++;
863-
result = -EINVAL;
864-
if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
865-
tsk->seccomp.mode = seccomp_mode;
866-
set_tsk_thread_flag(tsk, TIF_SECCOMP);
867-
} else
868-
goto out;
869-
result = -EIO;
870-
if (unlikely(!(end - __buf)))
871-
goto out;
872-
result = end - __buf;
873-
out:
874-
put_task_struct(tsk);
875-
out_no_task:
876-
return result;
877-
}
878-
879-
static const struct file_operations proc_seccomp_operations = {
880-
.read = seccomp_read,
881-
.write = seccomp_write,
882-
};
883-
#endif /* CONFIG_SECCOMP */
884-
885819
#ifdef CONFIG_FAULT_INJECTION
886820
static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
887821
size_t count, loff_t *ppos)
@@ -2042,9 +1976,6 @@ static const struct pid_entry tgid_base_stuff[] = {
20421976
REG("numa_maps", S_IRUGO, numa_maps),
20431977
#endif
20441978
REG("mem", S_IRUSR|S_IWUSR, mem),
2045-
#ifdef CONFIG_SECCOMP
2046-
REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2047-
#endif
20481979
LNK("cwd", cwd),
20491980
LNK("root", root),
20501981
LNK("exe", exe),
@@ -2329,9 +2260,6 @@ static const struct pid_entry tid_base_stuff[] = {
23292260
REG("numa_maps", S_IRUGO, numa_maps),
23302261
#endif
23312262
REG("mem", S_IRUSR|S_IWUSR, mem),
2332-
#ifdef CONFIG_SECCOMP
2333-
REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2334-
#endif
23352263
LNK("cwd", cwd),
23362264
LNK("root", root),
23372265
LNK("exe", exe),

‎include/linux/prctl.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@
5959
# define PR_ENDIAN_LITTLE 1 /* True little endian mode */
6060
# define PR_ENDIAN_PPC_LITTLE 2 /* "PowerPC" pseudo little endian */
6161

62+
/* Get/set process seccomp mode */
63+
#define PR_GET_SECCOMP 21
64+
#define PR_SET_SECCOMP 22
65+
6266
#endif /* _LINUX_PRCTL_H */

‎include/linux/seccomp.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#ifdef CONFIG_SECCOMP
66

7-
#define NR_SECCOMP_MODES 1
8-
97
#include <linux/thread_info.h>
108
#include <asm/seccomp.h>
119

@@ -23,6 +21,9 @@ static inline int has_secure_computing(struct thread_info *ti)
2321
return unlikely(test_ti_thread_flag(ti, TIF_SECCOMP));
2422
}
2523

24+
extern long prctl_get_seccomp(void);
25+
extern long prctl_set_seccomp(unsigned long);
26+
2627
#else /* CONFIG_SECCOMP */
2728

2829
typedef struct { } seccomp_t;
@@ -34,6 +35,16 @@ static inline int has_secure_computing(struct thread_info *ti)
3435
return 0;
3536
}
3637

38+
static inline long prctl_get_seccomp(void)
39+
{
40+
return -EINVAL;
41+
}
42+
43+
static inline long prctl_set_seccomp(unsigned long arg2)
44+
{
45+
return -EINVAL;
46+
}
47+
3748
#endif /* CONFIG_SECCOMP */
3849

3950
#endif /* _LINUX_SECCOMP_H */

‎kernel/seccomp.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/sched.h>
1111

1212
/* #define SECCOMP_DEBUG 1 */
13+
#define NR_SECCOMP_MODES 1
1314

1415
/*
1516
* Secure computing mode 1 allows only read/write/exit/sigreturn.
@@ -54,3 +55,28 @@ void __secure_computing(int this_syscall)
5455
#endif
5556
do_exit(SIGKILL);
5657
}
58+
59+
long prctl_get_seccomp(void)
60+
{
61+
return current->seccomp.mode;
62+
}
63+
64+
long prctl_set_seccomp(unsigned long seccomp_mode)
65+
{
66+
long ret;
67+
68+
/* can set it only once to be even more secure */
69+
ret = -EPERM;
70+
if (unlikely(current->seccomp.mode))
71+
goto out;
72+
73+
ret = -EINVAL;
74+
if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
75+
current->seccomp.mode = seccomp_mode;
76+
set_thread_flag(TIF_SECCOMP);
77+
ret = 0;
78+
}
79+
80+
out:
81+
return ret;
82+
}

‎kernel/sys.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/cn_proc.h>
3232
#include <linux/getcpu.h>
3333
#include <linux/task_io_accounting_ops.h>
34+
#include <linux/seccomp.h>
3435

3536
#include <linux/compat.h>
3637
#include <linux/syscalls.h>
@@ -2242,6 +2243,13 @@ asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
22422243
error = SET_ENDIAN(current, arg2);
22432244
break;
22442245

2246+
case PR_GET_SECCOMP:
2247+
error = prctl_get_seccomp();
2248+
break;
2249+
case PR_SET_SECCOMP:
2250+
error = prctl_set_seccomp(arg2);
2251+
break;
2252+
22452253
default:
22462254
error = -EINVAL;
22472255
break;

0 commit comments

Comments
 (0)