Skip to content

Commit d057dc4

Browse files
tych0kees
authored andcommitted
seccomp: add a selftest for get_metadata
Let's test that we get the flags correctly, and that we preserve the filter index across the ptrace(PTRACE_SECCOMP_GET_METADATA) correctly. Signed-off-by: Tycho Andersen <tycho@tycho.ws> CC: Kees Cook <keescook@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 63bb004 commit d057dc4

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

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)