History

seccomp: Report number of loaded filters in /proc/$pid/status

A common question asked when debugging seccomp filters is "how many filters are attached to your process?" Provide a way to easily answer this question through /proc/$pid/status with a "Seccomp_filters" line.

Kees Cook committed 6 years ago

Commit c818c031 parent e4d05024 files · +10 −0Browse files

Unified · Split

4 files
  1. fs/proc/array.c +2 −0
  2. include/linux/seccomp.h +2 −0
  3. init/init_task.c +3 −0
  4. kernel/seccomp.c +3 −0
fs/proc/array.c+2−0
@@ -341,6 +341,8 @@ static inline void task_seccomp(struct seq_file *m, struct task_struct *p) · 340 unmodified lines
341341 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
342342 #ifdef CONFIG_SECCOMP
343343 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
344+ seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
345+ atomic_read(&p->seccomp.filter_count));
344346 #endif
345347 seq_puts(m, "\nSpeculation_Store_Bypass:\t");
346348 switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
include/linux/seccomp.h+2−0
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #ifdef CONFIG_SECCOMP
1414
1515 #include <linux/thread_info.h>
16+#include <linux/atomic.h>
1617 #include <asm/seccomp.h>
1718
1819 struct seccomp_filter;
@@ -29,6 +30,7 @@ struct seccomp_filter; · 10 unmodified lines
2930 */
3031 struct seccomp {
3132 int mode;
33+ atomic_t filter_count;
3234 struct seccomp_filter *filter;
3335 };
3436
init/init_task.c+3−0
@@ -204,6 +204,9 @@ struct task_struct init_task · 203 unmodified lines
204204 #ifdef CONFIG_SECURITY
205205 .security = NULL,
206206 #endif
207+#ifdef CONFIG_SECCOMP
208+ .seccomp = { .filter_count = ATOMIC_INIT(0) },
209+#endif
207210 };
208211 EXPORT_SYMBOL(init_task);
209212
kernel/seccomp.c+3−0
@@ -398,6 +398,8 @@ static inline void seccomp_sync_threads(unsigned long flags) · 397 unmodified lines
398398 put_seccomp_filter(thread);
399399 smp_store_release(&thread->seccomp.filter,
400400 caller->seccomp.filter);
401+ atomic_set(&thread->seccomp.filter_count,
402+ atomic_read(&thread->seccomp.filter_count));
401403
402404 /*
403405 * Don't let an unprivileged task work around
@@ -544,6 +546,7 @@ static long seccomp_attach_filter(unsigned int flags, · 140 unmodified lines
544546 */
545547 filter->prev = current->seccomp.filter;
546548 current->seccomp.filter = filter;
549+ atomic_inc(&current->seccomp.filter_count);
547550
548551 /* Now that the new filter is in place, synchronize to all threads. */
549552 if (flags & SECCOMP_FILTER_FLAG_TSYNC)