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. */
136 * filter->notify_lock.
150 * A filter's reference count is incremented for each directly
151 * attached task, once for the dependent filter, and if
153 * the filter can be freed.
154 * @users: A filter's @users count is incremented for each directly
155 * attached task (filter installation, fork(), thread_sync),
156 * and once for the dependent filter (tracked in filter->prev).
158 * users of that filter exist. No new tasks can get associated with
159 * this filter after reaching 0. The @users count is always smaller
161 * the filter can be freed.
163 * @prev: points to a previously installed, or inherited, filter
171 * with current->seccomp.filter, the most recently attached or inherited filter.
220 * seccomp_check_filter - verify seccomp filter code
221 * @filter: filter to verify
222 * @flen: length of filter
224 * Takes a previously checked filter (by bpf_check_classic) and
225 * redirects all filter code that loads struct sk_buff data
231 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) in seccomp_check_filter() argument
235 struct sock_filter *ftest = &filter[pc]; in seccomp_check_filter()
305 * unless filter returned SECCOMP_RET_ALLOW, in which case it will
315 /* Make sure cross-thread synced filter points somewhere sane. */ in seccomp_run_filters()
317 READ_ONCE(current->seccomp.filter); in seccomp_run_filters()
360 * filter) is set. in seccomp_assign_mode()
390 * seccomp filter.
410 is_ancestor(thread->seccomp.filter, in seccomp_can_sync_threads()
411 caller->seccomp.filter))) in seccomp_can_sync_threads()
425 static inline void seccomp_filter_free(struct seccomp_filter *filter) in seccomp_filter_free() argument
427 if (filter) { in seccomp_filter_free()
428 bpf_prog_destroy(filter->prog); in seccomp_filter_free()
429 kfree(filter); in seccomp_filter_free()
454 /* Notify about any unused filters in the task's former filter tree. */ in __seccomp_filter_release()
461 * seccomp_filter_release - Detach the task from its filter tree,
466 * it detaches it from its filter tree. As such, READ_ONCE() and
471 struct seccomp_filter *orig = tsk->seccomp.filter; in seccomp_filter_release()
473 /* Detach task from its filter tree. */ in seccomp_filter_release()
474 tsk->seccomp.filter = NULL; in seccomp_filter_release()
479 * seccomp_sync_threads: sets all threads to use current's filter
508 __seccomp_filter_release(thread->seccomp.filter); in seccomp_sync_threads()
510 /* Make our new filter tree visible. */ in seccomp_sync_threads()
511 smp_store_release(&thread->seccomp.filter, in seccomp_sync_threads()
512 caller->seccomp.filter); in seccomp_sync_threads()
538 * seccomp_prepare_filter: Prepares a seccomp filter for use.
541 * Returns filter on success or an ERR_PTR on failure.
555 * Installing a seccomp filter requires that the task has in seccomp_prepare_filter()
594 struct seccomp_filter *filter = ERR_PTR(-EFAULT); in seccomp_prepare_user_filter() local
602 fprog.filter = compat_ptr(fprog32.filter); in seccomp_prepare_user_filter()
607 filter = seccomp_prepare_filter(&fprog); in seccomp_prepare_user_filter()
609 return filter; in seccomp_prepare_user_filter()
613 * seccomp_attach_filter: validate and attach filter
614 * @flags: flags to change filter behavior
615 * @filter: seccomp filter to add to the current process
621 * seccomp mode or did not have an ancestral seccomp filter
625 struct seccomp_filter *filter) in seccomp_attach_filter() argument
632 /* Validate resulting filter length. */ in seccomp_attach_filter()
633 total_insns = filter->prog->len; in seccomp_attach_filter()
634 for (walker = current->seccomp.filter; walker; walker = walker->prev) in seccomp_attach_filter()
654 filter->log = true; in seccomp_attach_filter()
657 * If there is an existing filter, make it the prev and don't drop its in seccomp_attach_filter()
660 filter->prev = current->seccomp.filter; in seccomp_attach_filter()
661 current->seccomp.filter = filter; in seccomp_attach_filter()
664 /* Now that the new filter is in place, synchronize to all threads. */ in seccomp_attach_filter()
671 static void __get_seccomp_filter(struct seccomp_filter *filter) in __get_seccomp_filter() argument
673 refcount_inc(&filter->refs); in __get_seccomp_filter()
676 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
679 struct seccomp_filter *orig = tsk->seccomp.filter; in get_seccomp_filter()
700 * @reason: filter-supplied reason code to send to userland (via si_errno)
821 static u64 seccomp_next_notify_id(struct seccomp_filter *filter) in seccomp_next_notify_id() argument
825 * filter. in seccomp_next_notify_id()
827 lockdep_assert_held(&filter->notify_lock); in seccomp_next_notify_id()
828 return filter->notif->next_id++; in seccomp_next_notify_id()
956 /* Let the filter pass back 16 bits of data. */ in __seccomp_filter()
1015 * Note that the "match" filter will always be NULL for in __seccomp_filter()
1115 static void seccomp_notify_free(struct seccomp_filter *filter) in seccomp_notify_free() argument
1117 kfree(filter->notif); in seccomp_notify_free()
1118 filter->notif = NULL; in seccomp_notify_free()
1121 static void seccomp_notify_detach(struct seccomp_filter *filter) in seccomp_notify_detach() argument
1125 if (!filter) in seccomp_notify_detach()
1128 mutex_lock(&filter->notify_lock); in seccomp_notify_detach()
1134 list_for_each_entry(knotif, &filter->notif->notifications, list) { in seccomp_notify_detach()
1150 seccomp_notify_free(filter); in seccomp_notify_detach()
1151 mutex_unlock(&filter->notify_lock); in seccomp_notify_detach()
1156 struct seccomp_filter *filter = file->private_data; in seccomp_notify_release() local
1158 seccomp_notify_detach(filter); in seccomp_notify_release()
1159 __put_seccomp_filter(filter); in seccomp_notify_release()
1165 find_notification(struct seccomp_filter *filter, u64 id) in find_notification() argument
1169 lockdep_assert_held(&filter->notify_lock); in find_notification()
1171 list_for_each_entry(cur, &filter->notif->notifications, list) { in find_notification()
1180 static long seccomp_notify_recv(struct seccomp_filter *filter, in seccomp_notify_recv() argument
1196 ret = down_interruptible(&filter->notif->request); in seccomp_notify_recv()
1200 mutex_lock(&filter->notify_lock); in seccomp_notify_recv()
1201 list_for_each_entry(cur, &filter->notif->notifications, list) { in seccomp_notify_recv()
1223 wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM); in seccomp_notify_recv()
1226 mutex_unlock(&filter->notify_lock); in seccomp_notify_recv()
1237 mutex_lock(&filter->notify_lock); in seccomp_notify_recv()
1238 knotif = find_notification(filter, unotif.id); in seccomp_notify_recv()
1241 up(&filter->notif->request); in seccomp_notify_recv()
1243 mutex_unlock(&filter->notify_lock); in seccomp_notify_recv()
1249 static long seccomp_notify_send(struct seccomp_filter *filter, in seccomp_notify_send() argument
1266 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_send()
1270 knotif = find_notification(filter, resp.id); in seccomp_notify_send()
1289 mutex_unlock(&filter->notify_lock); in seccomp_notify_send()
1293 static long seccomp_notify_id_valid(struct seccomp_filter *filter, in seccomp_notify_id_valid() argument
1303 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_id_valid()
1307 knotif = find_notification(filter, id); in seccomp_notify_id_valid()
1313 mutex_unlock(&filter->notify_lock); in seccomp_notify_id_valid()
1317 static long seccomp_notify_addfd(struct seccomp_filter *filter, in seccomp_notify_addfd() argument
1354 ret = mutex_lock_interruptible(&filter->notify_lock); in seccomp_notify_addfd()
1358 knotif = find_notification(filter, addfd.id); in seccomp_notify_addfd()
1376 mutex_unlock(&filter->notify_lock); in seccomp_notify_addfd()
1392 mutex_lock(&filter->notify_lock); in seccomp_notify_addfd()
1406 mutex_unlock(&filter->notify_lock); in seccomp_notify_addfd()
1416 struct seccomp_filter *filter = file->private_data; in seccomp_notify_ioctl() local
1422 return seccomp_notify_recv(filter, buf); in seccomp_notify_ioctl()
1424 return seccomp_notify_send(filter, buf); in seccomp_notify_ioctl()
1427 return seccomp_notify_id_valid(filter, buf); in seccomp_notify_ioctl()
1434 return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd)); in seccomp_notify_ioctl()
1443 struct seccomp_filter *filter = file->private_data; in seccomp_notify_poll() local
1447 poll_wait(file, &filter->wqh, poll_tab); in seccomp_notify_poll()
1449 if (mutex_lock_interruptible(&filter->notify_lock) < 0) in seccomp_notify_poll()
1452 list_for_each_entry(cur, &filter->notif->notifications, list) { in seccomp_notify_poll()
1461 mutex_unlock(&filter->notify_lock); in seccomp_notify_poll()
1463 if (refcount_read(&filter->users) == 0) in seccomp_notify_poll()
1476 static struct file *init_listener(struct seccomp_filter *filter) in init_listener() argument
1481 filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL); in init_listener()
1482 if (!filter->notif) in init_listener()
1485 sema_init(&filter->notif->request, 0); in init_listener()
1486 filter->notif->next_id = get_random_u64(); in init_listener()
1487 INIT_LIST_HEAD(&filter->notif->notifications); in init_listener()
1490 filter, O_RDWR); in init_listener()
1495 __get_seccomp_filter(filter); in init_listener()
1499 seccomp_notify_free(filter); in init_listener()
1506 * If so, we'll want to reject this filter.
1510 * we use current->seccomp.filter.
1521 for (cur = current->seccomp.filter; cur; cur = cur->prev) { in has_duplicate_listener()
1530 * seccomp_set_mode_filter: internal function for setting seccomp filter
1531 * @flags: flags to change filter behavior
1532 * @filter: struct sock_fprog containing filter
1535 * Every filter successfully installed will be evaluated (in reverse order)
1543 const char __user *filter) in seccomp_set_mode_filter() argument
1567 /* Prepare the new filter before holding any locks. */ in seccomp_set_mode_filter()
1568 prepared = seccomp_prepare_user_filter(filter); in seccomp_set_mode_filter()
1608 /* Do not free the successfully attached filter. */ in seccomp_set_mode_filter()
1634 const char __user *filter) in seccomp_set_mode_filter() argument
1713 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
1717 long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter) in prctl_set_seccomp() argument
1726 * Setting strict mode through prctl always ignored filter, in prctl_set_seccomp()
1734 uargs = filter; in prctl_set_seccomp()
1748 struct seccomp_filter *orig, *filter; in get_nth_filter() local
1762 orig = task->seccomp.filter; in get_nth_filter()
1767 for (filter = orig; filter; filter = filter->prev) in get_nth_filter()
1771 filter = ERR_PTR(-ENOENT); in get_nth_filter()
1776 for (filter = orig; filter && count > 1; filter = filter->prev) in get_nth_filter()
1779 if (WARN_ON(count != 1 || !filter)) { in get_nth_filter()
1780 filter = ERR_PTR(-ENOENT); in get_nth_filter()
1784 __get_seccomp_filter(filter); in get_nth_filter()
1788 return filter; in get_nth_filter()
1794 struct seccomp_filter *filter; in seccomp_get_filter() local
1803 filter = get_nth_filter(task, filter_off); in seccomp_get_filter()
1804 if (IS_ERR(filter)) in seccomp_get_filter()
1805 return PTR_ERR(filter); in seccomp_get_filter()
1807 fprog = filter->prog->orig_prog; in seccomp_get_filter()
1809 /* This must be a new non-cBPF filter, since we save in seccomp_get_filter()
1810 * every cBPF filter's orig_prog above when in seccomp_get_filter()
1821 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) in seccomp_get_filter()
1825 __put_seccomp_filter(filter); in seccomp_get_filter()
1833 struct seccomp_filter *filter; in seccomp_get_metadata() local
1849 filter = get_nth_filter(task, kmd.filter_off); in seccomp_get_metadata()
1850 if (IS_ERR(filter)) in seccomp_get_metadata()
1851 return PTR_ERR(filter); in seccomp_get_metadata()
1853 if (filter->log) in seccomp_get_metadata()
1860 __put_seccomp_filter(filter); in seccomp_get_metadata()