From f82dcd980d6fb3ac00cc3b94264abe43a47db74c Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Sat, 7 May 2016 23:11:52 +0000 Subject: [PATCH] seccomp.c: use print_array function * seccomp.c (decode_filter, decode_seccomp_fprog): Remove. (print_bpf_filter): New function. (print_seccomp_fprog): Use it via print_array. * tests/seccomp-filter-v.c (main): Update. --- seccomp.c | 66 ++++++++++++++++++---------------------- tests/seccomp-filter-v.c | 4 +-- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/seccomp.c b/seccomp.c index e62f99a0..5d972f97 100644 --- a/seccomp.c +++ b/seccomp.c @@ -108,6 +108,8 @@ decode_bpf_code(uint16_t code) } +#endif /* HAVE_LINUX_FILTER_H */ + static void decode_bpf_stmt(const struct bpf_filter *filter) { @@ -146,54 +148,44 @@ decode_bpf_jump(const struct bpf_filter *filter) #endif /* HAVE_LINUX_FILTER_H */ } -static void -decode_filter(const struct bpf_filter *filter) -{ - if (filter->jt || filter->jf) - decode_bpf_jump(filter); - else - decode_bpf_stmt(filter); -} - -#endif /* HAVE_LINUX_FILTER_H */ - #ifndef BPF_MAXINSNS # define BPF_MAXINSNS 4096 #endif -static void -decode_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr) +static bool +print_bpf_filter(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data) { - struct bpf_filter filter; - const unsigned long start_addr = addr; - unsigned int i = 0; - - for (; addr >= start_addr && i < len; ++i, addr += sizeof(filter)) { - if (i) { - tprints(", "); - if (i >= BPF_MAXINSNS) { - tprints("..."); - break; - } - } - if (umove_or_printaddr(tcp, addr, &filter)) - break; - if (!i) - tprints("["); - decode_filter(&filter); + const struct bpf_filter *filter = elem_buf; + unsigned int *pn = data; + + if ((*pn)++ >= BPF_MAXINSNS) { + tprints("..."); + return false; } - if (i) - tprints("]"); + + if (filter->jt || filter->jf) + decode_bpf_jump(filter); + else + decode_bpf_stmt(filter); + + return true; } static void -print_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr) +print_seccomp_fprog(struct tcb *tcp, unsigned long addr, unsigned short len) { tprintf("{len=%u, filter=", len); - if (abbrev(tcp) || !len) + + if (abbrev(tcp)) { printaddr(addr); - else - decode_seccomp_fprog(tcp, len, addr); + } else { + unsigned int insns = 0; + struct bpf_filter filter; + + print_array(tcp, addr, len, &filter, sizeof(filter), + umoven_or_printaddr, print_bpf_filter, &insns); + } + tprints("}"); } @@ -205,7 +197,7 @@ print_seccomp_filter(struct tcb *tcp, unsigned long addr) struct seccomp_fprog fprog; if (fetch_seccomp_fprog(tcp, addr, &fprog)) - print_seccomp_fprog(tcp, fprog.len, fprog.filter); + print_seccomp_fprog(tcp, fprog.filter, fprog.len); } static void diff --git a/tests/seccomp-filter-v.c b/tests/seccomp-filter-v.c index 9a706585..ef69c0dc 100644 --- a/tests/seccomp-filter-v.c +++ b/tests/seccomp-filter-v.c @@ -128,8 +128,8 @@ main(void) prog->len = 0; syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog); - tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=0, filter=%p})" - " = -1 EINVAL (%m)\n", prog->filter); + tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=0, filter=[]})" + " = -1 EINVAL (%m)\n"); unsigned int i; for (i = 0; i <= BPF_MAXINSNS; ++i) { -- 2.40.0