Skip to content

Commit 645ae5c

Browse files
author
James Morris
committed
Merge tag 'seccomp-v4.16-rc3' of https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux into fixes-v4.16-rc3
- Fix seccomp GET_METADATA to deal with field sizes correctly (Tycho Andersen) - Add selftest to make sure GET_METADATA doesn't regress (Tycho Andersen)
2 parents af3e79d + d057dc4 commit 645ae5c

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

include/uapi/linux/ptrace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ struct ptrace_peeksiginfo_args {
6969
#define PTRACE_SECCOMP_GET_METADATA 0x420d
7070

7171
struct seccomp_metadata {
72-
unsigned long filter_off; /* Input: which filter */
73-
unsigned int flags; /* Output: filter's flags */
72+
__u64 filter_off; /* Input: which filter */
73+
__u64 flags; /* Output: filter's flags */
7474
};
7575

7676
/* Read signals from a shared (process wide) queue */

kernel/seccomp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,14 +1076,16 @@ long seccomp_get_metadata(struct task_struct *task,
10761076

10771077
size = min_t(unsigned long, size, sizeof(kmd));
10781078

1079-
if (copy_from_user(&kmd, data, size))
1079+
if (size < sizeof(kmd.filter_off))
1080+
return -EINVAL;
1081+
1082+
if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
10801083
return -EFAULT;
10811084

10821085
filter = get_nth_filter(task, kmd.filter_off);
10831086
if (IS_ERR(filter))
10841087
return PTR_ERR(filter);
10851088

1086-
memset(&kmd, 0, sizeof(kmd));
10871089
if (filter->log)
10881090
kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
10891091

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ struct seccomp_data {
141141
#define SECCOMP_FILTER_FLAG_LOG 2
142142
#endif
143143

144+
#ifndef PTRACE_SECCOMP_GET_METADATA
145+
#define PTRACE_SECCOMP_GET_METADATA 0x420d
146+
147+
struct seccomp_metadata {
148+
__u64 filter_off; /* Input: which filter */
149+
__u64 flags; /* Output: filter's flags */
150+
};
151+
#endif
152+
144153
#ifndef seccomp
145154
int seccomp(unsigned int op, unsigned int flags, void *args)
146155
{
@@ -2845,6 +2854,58 @@ TEST(get_action_avail)
28452854
EXPECT_EQ(errno, EOPNOTSUPP);
28462855
}
28472856

2857+
TEST(get_metadata)
2858+
{
2859+
pid_t pid;
2860+
int pipefd[2];
2861+
char buf;
2862+
struct seccomp_metadata md;
2863+
2864+
ASSERT_EQ(0, pipe(pipefd));
2865+
2866+
pid = fork();
2867+
ASSERT_GE(pid, 0);
2868+
if (pid == 0) {
2869+
struct sock_filter filter[] = {
2870+
BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
2871+
};
2872+
struct sock_fprog prog = {
2873+
.len = (unsigned short)ARRAY_SIZE(filter),
2874+
.filter = filter,
2875+
};
2876+
2877+
/* one with log, one without */
2878+
ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER,
2879+
SECCOMP_FILTER_FLAG_LOG, &prog));
2880+
ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog));
2881+
2882+
ASSERT_EQ(0, close(pipefd[0]));
2883+
ASSERT_EQ(1, write(pipefd[1], "1", 1));
2884+
ASSERT_EQ(0, close(pipefd[1]));
2885+
2886+
while (1)
2887+
sleep(100);
2888+
}
2889+
2890+
ASSERT_EQ(0, close(pipefd[1]));
2891+
ASSERT_EQ(1, read(pipefd[0], &buf, 1));
2892+
2893+
ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid));
2894+
ASSERT_EQ(pid, waitpid(pid, NULL, 0));
2895+
2896+
md.filter_off = 0;
2897+
ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md));
2898+
EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG);
2899+
EXPECT_EQ(md.filter_off, 0);
2900+
2901+
md.filter_off = 1;
2902+
ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md));
2903+
EXPECT_EQ(md.flags, 0);
2904+
EXPECT_EQ(md.filter_off, 1);
2905+
2906+
ASSERT_EQ(0, kill(pid, SIGKILL));
2907+
}
2908+
28482909
/*
28492910
* TODO:
28502911
* - add microbenchmarks

0 commit comments

Comments
 (0)