Lines Matching full:filter

38 #include <linux/filter.h>
62 /* The struct pid of the task whose filter triggered the notification */
65 /* The "cookie" for this request; this is unique for this filter. */
141 * filter->notify_lock.
153 * struct action_cache - per-filter cache of seccomp actions per
157 * filter will always allow the syscall, for the
160 * filter will always allow the syscall, for the
187 * A filter's reference count is incremented for each directly
188 * attached task, once for the dependent filter, and if
190 * the filter can be freed.
191 * @users: A filter's @users count is incremented for each directly
192 * attached task (filter installation, fork(), thread_sync),
193 * and once for the dependent filter (tracked in filter->prev).
195 * users of that filter exist. No new tasks can get associated with
196 * this filter after reaching 0. The @users count is always smaller
198 * the filter can be freed.
201 * @prev: points to a previously installed, or inherited, filter
209 * with current->seccomp.filter, the most recently attached or inherited filter.
259 * seccomp_check_filter - verify seccomp filter code
260 * @filter: filter to verify
261 * @flen: length of filter
263 * Takes a previously checked filter (by bpf_check_classic) and
264 * redirects all filter code that loads struct sk_buff data
270 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) in seccomp_check_filter() argument
274 struct sock_filter *ftest = &filter[pc]; in seccomp_check_filter()
354 * @sfilter: The seccomp filter
390 * unless filter returned SECCOMP_RET_ALLOW, in which case it will
400 /* Make sure cross-thread synced filter points somewhere sane. */ in seccomp_run_filters()
402 READ_ONCE(current->seccomp.filter); in seccomp_run_filters()
448 * filter) is set. in seccomp_assign_mode()
478 * seccomp filter.
498 is_ancestor(thread->seccomp.filter, in seccomp_can_sync_threads()
499 caller->seccomp.filter))) in seccomp_can_sync_threads()
513 static inline void seccomp_filter_free(struct seccomp_filter *filter) in seccomp_filter_free() argument
515 if (filter) { in seccomp_filter_free()
516 bpf_prog_destroy(filter->prog); in seccomp_filter_free()
517 kfree(filter); in seccomp_filter_free()
542 /* Notify about any unused filters in the task's former filter tree. */ in __seccomp_filter_release()
549 * seccomp_filter_release - Detach the task from its filter tree,
554 * it detaches it from its filter tree. As such, READ_ONCE() and
559 struct seccomp_filter *orig = tsk->seccomp.filter; in seccomp_filter_release()
564 /* Detach task from its filter tree. */ in seccomp_filter_release()
565 tsk->seccomp.filter = NULL; in seccomp_filter_release()
570 * seccomp_sync_threads: sets all threads to use current's filter
599 __seccomp_filter_release(thread->seccomp.filter); in seccomp_sync_threads()
601 /* Make our new filter tree visible. */ in seccomp_sync_threads()
602 smp_store_release(&thread->seccomp.filter, in seccomp_sync_threads()
603 caller->seccomp.filter); in seccomp_sync_threads()
629 * seccomp_prepare_filter: Prepares a seccomp filter for use.
632 * Returns filter on success or an ERR_PTR on failure.
651 * Installing a seccomp filter requires that the task has in seccomp_prepare_filter()
690 struct seccomp_filter *filter = ERR_PTR(-EFAULT); in seccomp_prepare_user_filter() local
698 fprog.filter = compat_ptr(fprog32.filter); in seccomp_prepare_user_filter()
703 filter = seccomp_prepare_filter(&fprog); in seccomp_prepare_user_filter()
705 return filter; in seccomp_prepare_user_filter()
710 * seccomp_is_const_allow - check if filter is constant allow with given data
726 struct sock_filter *insn = &fprog->filter[pc]; in seccomp_is_const_allow()
783 /* ran off the end of the filter?! */ in seccomp_is_const_allow()
797 /* The new filter must be as restrictive as the last. */ in seccomp_cache_prepare_bitmap()
818 * atomic clear_bit() not needed, filter not visible yet. in seccomp_cache_prepare_bitmap()
825 * seccomp_cache_prepare - emulate the filter to find cacheable syscalls
826 * @sfilter: The seccomp filter
851 * seccomp_attach_filter: validate and attach filter
852 * @flags: flags to change filter behavior
853 * @filter: seccomp filter to add to the current process
859 * seccomp mode or did not have an ancestral seccomp filter
863 struct seccomp_filter *filter) in seccomp_attach_filter() argument
870 /* Validate resulting filter length. */ in seccomp_attach_filter()
871 total_insns = filter->prog->len; in seccomp_attach_filter()
872 for (walker = current->seccomp.filter; walker; walker = walker->prev) in seccomp_attach_filter()
892 filter->log = true; in seccomp_attach_filter()
895 * If there is an existing filter, make it the prev and don't drop its in seccomp_attach_filter()
898 filter->prev = current->seccomp.filter; in seccomp_attach_filter()
899 seccomp_cache_prepare(filter); in seccomp_attach_filter()
900 current->seccomp.filter = filter; in seccomp_attach_filter()
903 /* Now that the new filter is in place, synchronize to all threads. */ in seccomp_attach_filter()
910 static void __get_seccomp_filter(struct seccomp_filter *filter) in __get_seccomp_filter() argument
912 refcount_inc(&filter->refs); in __get_seccomp_filter()
915 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
918 struct seccomp_filter *orig = tsk->seccomp.filter; in get_seccomp_filter()
1036 static u64 seccomp_next_notify_id(struct seccomp_filter *filter) in seccomp_next_notify_id() argument
1040 * filter. in seccomp_next_notify_id()
1042 lockdep_assert_held(&filter->notify_lock); in seccomp_next_notify_id()
1043 return filter->notif->next_id++; in seccomp_next_notify_id()
1196 /* Let the filter pass back 16 bits of data. */ in __seccomp_filter()
1255 * Note that the "match" filter will always be NULL for in __seccomp_filter()
1353 static void seccomp_notify_free(struct seccomp_filter *filter) in seccomp_notify_free() argument
1355 kfree(filter->notif); in seccomp_notify_free()
1356 filter->notif = NULL; in seccomp_notify_free()
1359 static void seccomp_notify_detach(struct seccomp_filter *filter) in seccomp_notify_detach() argument
1363 if (!filter) in seccomp_notify_detach()
1366 mutex_lock(&filter->notify_lock); in seccomp_notify_detach()
1372 list_for_each_entry(knotif, &filter->notif->notifications, list) { in seccomp_notify_detach()
1388 seccomp_notify_free(filter); in seccomp_notify_detach()
1389 mutex_unlock(&filter->notify_lock); in seccomp_notify_detach()
1394 struct seccomp_filter *filter = file->private_data; in seccomp_notify_release() local
1396 seccomp_notify_detach(filter); in seccomp_notify_release()
1397 __put_seccomp_filter(filter); in seccomp_notify_release()
1403 find_notification(struct seccomp_filter *filter, u64 id) in find_notification() argument
1407 lockdep_assert_held(&filter->notify_lock); in find_notification()
1409 list_for_each_entry(cur, &filter->notif->notifications, list) { in find_notification()
1418 static long seccomp_notify_recv(struct seccomp_filter *filter, in seccomp_notify_recv() argument
1434 ret = down_interruptible(&filter->notif->request); in seccomp_notify_recv()
1438 mutex_lock(&filter->notify_lock); in seccomp_notify_recv()
1439 list_for_each_entry(cur, &filter->notif->notifications, list) { in seccomp_notify_recv()
1461 wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM); in seccomp_notify_recv()
1464 mutex_unlock(&filter->notify_lock); in seccomp_notify_recv()
1475 mutex_lock(&filter->notify_lock); in seccomp_notify_recv()
1476 knotif = find_notification(filter, unotif.id); in seccomp_notify_recv()
1479 up(&filter->notif->request); in seccomp_notify_recv()
1481 mutex_unlock(&filter->notify_lock); in seccomp_notify_recv()
1487 static long seccomp_notify_send(struct seccomp_filter *filter, in seccomp_notify_send() argument
1504 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_send()
1508 knotif = find_notification(filter, resp.id); in seccomp_notify_send()
1527 mutex_unlock(&filter->notify_lock); in seccomp_notify_send()
1531 static long seccomp_notify_id_valid(struct seccomp_filter *filter, in seccomp_notify_id_valid() argument
1541 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_id_valid()
1545 knotif = find_notification(filter, id); in seccomp_notify_id_valid()
1551 mutex_unlock(&filter->notify_lock); in seccomp_notify_id_valid()
1555 static long seccomp_notify_addfd(struct seccomp_filter *filter, in seccomp_notify_addfd() argument
1593 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_addfd()
1597 knotif = find_notification(filter, addfd.id); in seccomp_notify_addfd()
1632 mutex_unlock(&filter->notify_lock); in seccomp_notify_addfd()
1648 mutex_lock(&filter->notify_lock); in seccomp_notify_addfd()
1662 mutex_unlock(&filter->notify_lock); in seccomp_notify_addfd()
1672 struct seccomp_filter *filter = file->private_data; in seccomp_notify_ioctl() local
1678 return seccomp_notify_recv(filter, buf); in seccomp_notify_ioctl()
1680 return seccomp_notify_send(filter, buf); in seccomp_notify_ioctl()
1683 return seccomp_notify_id_valid(filter, buf); in seccomp_notify_ioctl()
1690 return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd)); in seccomp_notify_ioctl()
1699 struct seccomp_filter *filter = file->private_data; in seccomp_notify_poll() local
1703 poll_wait(file, &filter->wqh, poll_tab); in seccomp_notify_poll()
1705 if (mutex_lock_interruptible(&filter->notify_lock) < 0) in seccomp_notify_poll()
1708 list_for_each_entry(cur, &filter->notif->notifications, list) { in seccomp_notify_poll()
1717 mutex_unlock(&filter->notify_lock); in seccomp_notify_poll()
1719 if (refcount_read(&filter->users) == 0) in seccomp_notify_poll()
1732 static struct file *init_listener(struct seccomp_filter *filter) in init_listener() argument
1737 filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL); in init_listener()
1738 if (!filter->notif) in init_listener()
1741 sema_init(&filter->notif->request, 0); in init_listener()
1742 filter->notif->next_id = get_random_u64(); in init_listener()
1743 INIT_LIST_HEAD(&filter->notif->notifications); in init_listener()
1746 filter, O_RDWR); in init_listener()
1751 __get_seccomp_filter(filter); in init_listener()
1755 seccomp_notify_free(filter); in init_listener()
1762 * If so, we'll want to reject this filter.
1766 * we use current->seccomp.filter.
1777 for (cur = current->seccomp.filter; cur; cur = cur->prev) { in has_duplicate_listener()
1786 * seccomp_set_mode_filter: internal function for setting seccomp filter
1787 * @flags: flags to change filter behavior
1788 * @filter: struct sock_fprog containing filter
1791 * Every filter successfully installed will be evaluated (in reverse order)
1799 const char __user *filter) in seccomp_set_mode_filter() argument
1823 /* Prepare the new filter before holding any locks. */ in seccomp_set_mode_filter()
1824 prepared = seccomp_prepare_user_filter(filter); in seccomp_set_mode_filter()
1864 /* Do not free the successfully attached filter. */ in seccomp_set_mode_filter()
1890 const char __user *filter) in seccomp_set_mode_filter() argument
1969 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
1973 long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter) in prctl_set_seccomp() argument
1982 * Setting strict mode through prctl always ignored filter, in prctl_set_seccomp()
1990 uargs = filter; in prctl_set_seccomp()
2004 struct seccomp_filter *orig, *filter; in get_nth_filter() local
2018 orig = task->seccomp.filter; in get_nth_filter()
2023 for (filter = orig; filter; filter = filter->prev) in get_nth_filter()
2027 filter = ERR_PTR(-ENOENT); in get_nth_filter()
2032 for (filter = orig; filter && count > 1; filter = filter->prev) in get_nth_filter()
2035 if (WARN_ON(count != 1 || !filter)) { in get_nth_filter()
2036 filter = ERR_PTR(-ENOENT); in get_nth_filter()
2040 __get_seccomp_filter(filter); in get_nth_filter()
2044 return filter; in get_nth_filter()
2050 struct seccomp_filter *filter; in seccomp_get_filter() local
2059 filter = get_nth_filter(task, filter_off); in seccomp_get_filter()
2060 if (IS_ERR(filter)) in seccomp_get_filter()
2061 return PTR_ERR(filter); in seccomp_get_filter()
2063 fprog = filter->prog->orig_prog; in seccomp_get_filter()
2065 /* This must be a new non-cBPF filter, since we save in seccomp_get_filter()
2066 * every cBPF filter's orig_prog above when in seccomp_get_filter()
2077 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) in seccomp_get_filter()
2081 __put_seccomp_filter(filter); in seccomp_get_filter()
2089 struct seccomp_filter *filter; in seccomp_get_metadata() local
2105 filter = get_nth_filter(task, kmd.filter_off); in seccomp_get_metadata()
2106 if (IS_ERR(filter)) in seccomp_get_metadata()
2107 return PTR_ERR(filter); in seccomp_get_metadata()
2109 if (filter->log) in seccomp_get_metadata()
2116 __put_seccomp_filter(filter); in seccomp_get_metadata()
2372 char *status = cached ? "ALLOW" : "FILTER"; in device_initcall()
2394 f = READ_ONCE(task->seccomp.filter); in proc_pid_seccomp_cache()
2400 /* prevent filter from being freed while we are printing it */ in proc_pid_seccomp_cache()