Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

seccomp: don't use semaphore and wait_queue together

The main reason is to use new wake_up helpers that will be added in the
following patches. But here are a few other reasons:

* if we use two different ways, we always need to call them both. This
patch fixes seccomp_notify_recv where we forgot to call wake_up_poll
in the error path.

* If we use one primitive, we can control how many waiters are woken up
for each request. Our goal is to wake up just one that will handle a
request. Right now, wake_up_poll can wake up one waiter and
up(&match->notif->request) can wake up one more.

Signed-off-by: Andrei Vagin <avagin@google.com>
Acked-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Link: https://lore.kernel.org/r/20230308073201.3102738-2-avagin@google.com
Signed-off-by: Kees Cook <keescook@chromium.org>

authored by

Andrei Vagin and committed by
Kees Cook
4943b66d fdf0eaf1

+36 -5
+36 -5
kernel/seccomp.c
··· 145 145 * @notifications: A list of struct seccomp_knotif elements. 146 146 */ 147 147 struct notification { 148 - struct semaphore request; 148 + atomic_t requests; 149 149 u64 next_id; 150 150 struct list_head notifications; 151 151 }; ··· 1116 1116 list_add_tail(&n.list, &match->notif->notifications); 1117 1117 INIT_LIST_HEAD(&n.addfd); 1118 1118 1119 - up(&match->notif->request); 1119 + atomic_inc(&match->notif->requests); 1120 1120 wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM); 1121 1121 1122 1122 /* ··· 1450 1450 return NULL; 1451 1451 } 1452 1452 1453 + static int recv_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync, 1454 + void *key) 1455 + { 1456 + /* Avoid a wakeup if event not interesting for us. */ 1457 + if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR))) 1458 + return 0; 1459 + return autoremove_wake_function(wait, mode, sync, key); 1460 + } 1461 + 1462 + static int recv_wait_event(struct seccomp_filter *filter) 1463 + { 1464 + DEFINE_WAIT_FUNC(wait, recv_wake_function); 1465 + int ret; 1466 + 1467 + if (atomic_dec_if_positive(&filter->notif->requests) >= 0) 1468 + return 0; 1469 + 1470 + for (;;) { 1471 + ret = prepare_to_wait_event(&filter->wqh, &wait, TASK_INTERRUPTIBLE); 1472 + 1473 + if (atomic_dec_if_positive(&filter->notif->requests) >= 0) 1474 + break; 1475 + 1476 + if (ret) 1477 + return ret; 1478 + 1479 + schedule(); 1480 + } 1481 + finish_wait(&filter->wqh, &wait); 1482 + return 0; 1483 + } 1453 1484 1454 1485 static long seccomp_notify_recv(struct seccomp_filter *filter, 1455 1486 void __user *buf) ··· 1498 1467 1499 1468 memset(&unotif, 0, sizeof(unotif)); 1500 1469 1501 - ret = down_interruptible(&filter->notif->request); 1470 + ret = recv_wait_event(filter); 1502 1471 if (ret < 0) 1503 1472 return ret; 1504 1473 ··· 1546 1515 if (should_sleep_killable(filter, knotif)) 1547 1516 complete(&knotif->ready); 1548 1517 knotif->state = SECCOMP_NOTIFY_INIT; 1549 - up(&filter->notif->request); 1518 + atomic_inc(&filter->notif->requests); 1519 + wake_up_poll(&filter->wqh, EPOLLIN | EPOLLRDNORM); 1550 1520 } 1551 1521 mutex_unlock(&filter->notify_lock); 1552 1522 } ··· 1809 1777 if (!filter->notif) 1810 1778 goto out; 1811 1779 1812 - sema_init(&filter->notif->request, 0); 1813 1780 filter->notif->next_id = get_random_u64(); 1814 1781 INIT_LIST_HEAD(&filter->notif->notifications); 1815 1782