Skip to content

Commit 3dbc4f5

Browse files
committed
Merge branch 'next-seccomp' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull seccomp updates from James Morris: "Add support for retrieving seccomp metadata" * 'next-seccomp' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: ptrace, seccomp: add support for retrieving seccomp metadata seccomp: hoist out filter resolving logic
2 parents ae0cb7b + 26025bf commit 3dbc4f5

File tree

4 files changed

+94
-30
lines changed

4 files changed

+94
-30
lines changed

include/linux/seccomp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,19 @@ static inline void get_seccomp_filter(struct task_struct *tsk)
9595
#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
9696
extern long seccomp_get_filter(struct task_struct *task,
9797
unsigned long filter_off, void __user *data);
98+
extern long seccomp_get_metadata(struct task_struct *task,
99+
unsigned long filter_off, void __user *data);
98100
#else
99101
static inline long seccomp_get_filter(struct task_struct *task,
100102
unsigned long n, void __user *data)
101103
{
102104
return -EINVAL;
103105
}
106+
static inline long seccomp_get_metadata(struct task_struct *task,
107+
unsigned long filter_off,
108+
void __user *data)
109+
{
110+
return -EINVAL;
111+
}
104112
#endif /* CONFIG_SECCOMP_FILTER && CONFIG_CHECKPOINT_RESTORE */
105113
#endif /* _LINUX_SECCOMP_H */

include/uapi/linux/ptrace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ struct ptrace_peeksiginfo_args {
6666
#define PTRACE_SETSIGMASK 0x420b
6767

6868
#define PTRACE_SECCOMP_GET_FILTER 0x420c
69+
#define PTRACE_SECCOMP_GET_METADATA 0x420d
70+
71+
struct seccomp_metadata {
72+
unsigned long filter_off; /* Input: which filter */
73+
unsigned int flags; /* Output: filter's flags */
74+
};
6975

7076
/* Read signals from a shared (process wide) queue */
7177
#define PTRACE_PEEKSIGINFO_SHARED (1 << 0)

kernel/ptrace.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ int ptrace_request(struct task_struct *child, long request,
10921092
ret = seccomp_get_filter(child, addr, datavp);
10931093
break;
10941094

1095+
case PTRACE_SECCOMP_GET_METADATA:
1096+
ret = seccomp_get_metadata(child, addr, datavp);
1097+
break;
1098+
10951099
default:
10961100
break;
10971101
}

kernel/seccomp.c

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -978,49 +978,68 @@ long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
978978
}
979979

980980
#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
981-
long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
982-
void __user *data)
981+
static struct seccomp_filter *get_nth_filter(struct task_struct *task,
982+
unsigned long filter_off)
983983
{
984-
struct seccomp_filter *filter;
985-
struct sock_fprog_kern *fprog;
986-
long ret;
987-
unsigned long count = 0;
988-
989-
if (!capable(CAP_SYS_ADMIN) ||
990-
current->seccomp.mode != SECCOMP_MODE_DISABLED) {
991-
return -EACCES;
992-
}
984+
struct seccomp_filter *orig, *filter;
985+
unsigned long count;
993986

987+
/*
988+
* Note: this is only correct because the caller should be the (ptrace)
989+
* tracer of the task, otherwise lock_task_sighand is needed.
990+
*/
994991
spin_lock_irq(&task->sighand->siglock);
992+
995993
if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
996-
ret = -EINVAL;
997-
goto out;
994+
spin_unlock_irq(&task->sighand->siglock);
995+
return ERR_PTR(-EINVAL);
998996
}
999997

1000-
filter = task->seccomp.filter;
1001-
while (filter) {
1002-
filter = filter->prev;
998+
orig = task->seccomp.filter;
999+
__get_seccomp_filter(orig);
1000+
spin_unlock_irq(&task->sighand->siglock);
1001+
1002+
count = 0;
1003+
for (filter = orig; filter; filter = filter->prev)
10031004
count++;
1004-
}
10051005

10061006
if (filter_off >= count) {
1007-
ret = -ENOENT;
1007+
filter = ERR_PTR(-ENOENT);
10081008
goto out;
10091009
}
1010-
count -= filter_off;
10111010

1012-
filter = task->seccomp.filter;
1013-
while (filter && count > 1) {
1014-
filter = filter->prev;
1011+
count -= filter_off;
1012+
for (filter = orig; filter && count > 1; filter = filter->prev)
10151013
count--;
1016-
}
10171014

10181015
if (WARN_ON(count != 1 || !filter)) {
1019-
/* The filter tree shouldn't shrink while we're using it. */
1020-
ret = -ENOENT;
1016+
filter = ERR_PTR(-ENOENT);
10211017
goto out;
10221018
}
10231019

1020+
__get_seccomp_filter(filter);
1021+
1022+
out:
1023+
__put_seccomp_filter(orig);
1024+
return filter;
1025+
}
1026+
1027+
long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
1028+
void __user *data)
1029+
{
1030+
struct seccomp_filter *filter;
1031+
struct sock_fprog_kern *fprog;
1032+
long ret;
1033+
1034+
if (!capable(CAP_SYS_ADMIN) ||
1035+
current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1036+
return -EACCES;
1037+
}
1038+
1039+
filter = get_nth_filter(task, filter_off);
1040+
if (IS_ERR(filter))
1041+
return PTR_ERR(filter);
1042+
10241043
fprog = filter->prog->orig_prog;
10251044
if (!fprog) {
10261045
/* This must be a new non-cBPF filter, since we save
@@ -1035,17 +1054,44 @@ long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
10351054
if (!data)
10361055
goto out;
10371056

1038-
__get_seccomp_filter(filter);
1039-
spin_unlock_irq(&task->sighand->siglock);
1040-
10411057
if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
10421058
ret = -EFAULT;
10431059

1060+
out:
10441061
__put_seccomp_filter(filter);
10451062
return ret;
1063+
}
10461064

1047-
out:
1048-
spin_unlock_irq(&task->sighand->siglock);
1065+
long seccomp_get_metadata(struct task_struct *task,
1066+
unsigned long size, void __user *data)
1067+
{
1068+
long ret;
1069+
struct seccomp_filter *filter;
1070+
struct seccomp_metadata kmd = {};
1071+
1072+
if (!capable(CAP_SYS_ADMIN) ||
1073+
current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1074+
return -EACCES;
1075+
}
1076+
1077+
size = min_t(unsigned long, size, sizeof(kmd));
1078+
1079+
if (copy_from_user(&kmd, data, size))
1080+
return -EFAULT;
1081+
1082+
filter = get_nth_filter(task, kmd.filter_off);
1083+
if (IS_ERR(filter))
1084+
return PTR_ERR(filter);
1085+
1086+
memset(&kmd, 0, sizeof(kmd));
1087+
if (filter->log)
1088+
kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
1089+
1090+
ret = size;
1091+
if (copy_to_user(data, &kmd, size))
1092+
ret = -EFAULT;
1093+
1094+
__put_seccomp_filter(filter);
10491095
return ret;
10501096
}
10511097
#endif

0 commit comments

Comments
 (0)