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.

epoll: ep_unregister_pollwait() can use the freed pwq->whead

signalfd_cleanup() ensures that ->signalfd_wqh is not used, but
this is not enough. eppoll_entry->whead still points to the memory
we are going to free, ep_unregister_pollwait()->remove_wait_queue()
is obviously unsafe.

Change ep_poll_callback(POLLFREE) to set eppoll_entry->whead = NULL,
change ep_unregister_pollwait() to check pwq->whead != NULL under
rcu_read_lock() before remove_wait_queue(). We add the new helper,
ep_remove_wait_queue(), for this.

This works because sighand_cachep is SLAB_DESTROY_BY_RCU and because
->signalfd_wqh is initialized in sighand_ctor(), not in copy_sighand.
ep_unregister_pollwait()->remove_wait_queue() can play with already
freed and potentially reused ->sighand, but this is fine. This memory
must have the valid ->signalfd_wqh until rcu_read_unlock().

Reported-by: Maxime Bizon <mbizon@freebox.fr>
Cc: <stable@kernel.org>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Oleg Nesterov and committed by
Linus Torvalds
971316f0 d80e731e

+32 -4
+27 -3
fs/eventpoll.c
··· 320 320 return !list_empty(p); 321 321 } 322 322 323 + static inline struct eppoll_entry *ep_pwq_from_wait(wait_queue_t *p) 324 + { 325 + return container_of(p, struct eppoll_entry, wait); 326 + } 327 + 323 328 /* Get the "struct epitem" from a wait queue pointer */ 324 329 static inline struct epitem *ep_item_from_wait(wait_queue_t *p) 325 330 { ··· 472 467 put_cpu(); 473 468 } 474 469 470 + static void ep_remove_wait_queue(struct eppoll_entry *pwq) 471 + { 472 + wait_queue_head_t *whead; 473 + 474 + rcu_read_lock(); 475 + /* If it is cleared by POLLFREE, it should be rcu-safe */ 476 + whead = rcu_dereference(pwq->whead); 477 + if (whead) 478 + remove_wait_queue(whead, &pwq->wait); 479 + rcu_read_unlock(); 480 + } 481 + 475 482 /* 476 483 * This function unregisters poll callbacks from the associated file 477 484 * descriptor. Must be called with "mtx" held (or "epmutex" if called from ··· 498 481 pwq = list_first_entry(lsthead, struct eppoll_entry, llink); 499 482 500 483 list_del(&pwq->llink); 501 - remove_wait_queue(pwq->whead, &pwq->wait); 484 + ep_remove_wait_queue(pwq); 502 485 kmem_cache_free(pwq_cache, pwq); 503 486 } 504 487 } ··· 859 842 struct epitem *epi = ep_item_from_wait(wait); 860 843 struct eventpoll *ep = epi->ep; 861 844 862 - /* the caller holds eppoll_entry->whead->lock */ 863 - if ((unsigned long)key & POLLFREE) 845 + if ((unsigned long)key & POLLFREE) { 846 + ep_pwq_from_wait(wait)->whead = NULL; 847 + /* 848 + * whead = NULL above can race with ep_remove_wait_queue() 849 + * which can do another remove_wait_queue() after us, so we 850 + * can't use __remove_wait_queue(). whead->lock is held by 851 + * the caller. 852 + */ 864 853 list_del_init(&wait->task_list); 854 + } 865 855 866 856 spin_lock_irqsave(&ep->lock, flags); 867 857
+5 -1
fs/signalfd.c
··· 33 33 void signalfd_cleanup(struct sighand_struct *sighand) 34 34 { 35 35 wait_queue_head_t *wqh = &sighand->signalfd_wqh; 36 - 36 + /* 37 + * The lockless check can race with remove_wait_queue() in progress, 38 + * but in this case its caller should run under rcu_read_lock() and 39 + * sighand_cachep is SLAB_DESTROY_BY_RCU, we can safely return. 40 + */ 37 41 if (likely(!waitqueue_active(wqh))) 38 42 return; 39 43