git: e5e44726d5af - stable/13 - kern: restore signal mask before ast() for pselect/ppoll
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 10 Dec 2024 20:03:23 UTC
The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=e5e44726d5afdb260893a3b4e0be025f6dd74bd4 commit e5e44726d5afdb260893a3b4e0be025f6dd74bd4 Author: Kyle Evans <kevans@FreeBSD.org> AuthorDate: 2024-11-26 04:04:48 +0000 Commit: Kyle Evans <kevans@FreeBSD.org> CommitDate: 2024-12-10 20:03:14 +0000 kern: restore signal mask before ast() for pselect/ppoll It's possible to take a signal after pselect/ppoll have set their return value, but before we actually return to userland. This results in taking a signal without reflecting it in the return value, which weakens the guarantees provided by these functions. Switch both to restore the signal mask before we would deliver signals on return to userland. If a signal was received after the wait was over, then we'll just have the signal queued up for the next time it comes unblocked. The modified signal mask is retained if we were interrupted so that ast() actually handles the signal, at which point the signal mask is restored. des@ has a test case demonstrating the issue in D47738 which will follow. Reported by: des Reviewed by: des (earlier version), kib Sponsored by: Klara, Inc. Sponsored by: NetApp, Inc. Differential Revision: https://reviews.freebsd.org/D47741 (cherry picked from commit ccb973da1f1b65879eade8e65cdd2885e125f90e) --- sys/kern/sys_generic.c | 61 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index a0a13e77131b..c605e3f00fdd 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -975,17 +975,33 @@ kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, &td->td_oldsigmask, 0); if (error != 0) return (error); - td->td_pflags |= TDP_OLDMASK; + } + error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); + if (uset != NULL) { /* * Make sure that ast() is called on return to * usermode and TDP_OLDMASK is cleared, restoring old - * sigmask. + * sigmask. If we didn't get interrupted, then the caller is + * likely not expecting a signal to hit that should normally be + * blocked by its signal mask, so we restore the mask before + * any signals could be delivered. */ - thread_lock(td); - td->td_flags |= TDF_ASTPENDING; - thread_unlock(td); + if (error == EINTR) { + td->td_pflags |= TDP_OLDMASK; + thread_lock(td); + td->td_flags |= TDF_ASTPENDING; + thread_unlock(td); + } else { + int serror __diagused; + + /* *select(2) should never restart. */ + MPASS(error != ERESTART); + serror = kern_sigprocmask(td, SIG_SETMASK, + &td->td_oldsigmask, NULL, 0); + MPASS(serror == 0); + } } - error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits); + return (error); } @@ -1456,15 +1472,6 @@ kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds, &td->td_oldsigmask, 0); if (error) return (error); - td->td_pflags |= TDP_OLDMASK; - /* - * Make sure that ast() is called on return to - * usermode and TDP_OLDMASK is cleared, restoring old - * sigmask. - */ - thread_lock(td); - td->td_flags |= TDF_ASTPENDING; - thread_unlock(td); } seltdinit(td); @@ -1487,6 +1494,30 @@ kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds, error = EINTR; if (error == EWOULDBLOCK) error = 0; + + if (uset != NULL) { + /* + * Make sure that ast() is called on return to + * usermode and TDP_OLDMASK is cleared, restoring old + * sigmask. If we didn't get interrupted, then the caller is + * likely not expecting a signal to hit that should normally be + * blocked by its signal mask, so we restore the mask before + * any signals could be delivered. + */ + if (error == EINTR) { + td->td_pflags |= TDP_OLDMASK; + thread_lock(td); + td->td_flags |= TDF_ASTPENDING; + thread_unlock(td); + } else { + int serror __diagused; + + serror = kern_sigprocmask(td, SIG_SETMASK, + &td->td_oldsigmask, NULL, 0); + MPASS(serror == 0); + } + } + return (error); }