From 2f074d33abd734fce740e464ec4df546d2bdc013 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 6 Mar 2017 13:05:17 -0700 Subject: [PATCH] Prevent sudo from receiving SIGTTOU when it tries to restore the controlling terminal. There appears to be a race with the shell (bash) which we may lose. --- src/exec_nopty.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/exec_nopty.c b/src/exec_nopty.c index c72eb3159..b6826ef70 100644 --- a/src/exec_nopty.c +++ b/src/exec_nopty.c @@ -391,11 +391,21 @@ dispatch_signal(struct exec_closure_nopty *ec, int signo, char *signame) } if (saved_pgrp != -1) { /* - * Restore command's process group if different. - * Otherwise, we cannot resume some shells. + * Restore foreground process group, if different. + * Otherwise, we cannot resume some shells (pdksh). + * + * There appears to be a race with shells that do restore + * the controlling group. Sudo can receive SIGTTOU + * if the shell has already changed the controlling tty. */ - if (saved_pgrp != ppgrp) + if (saved_pgrp != ppgrp) { + sigset_t set, oset; + sigemptyset(&set); + sigaddset(&set, SIGTTOU); + sigprocmask(SIG_BLOCK, &set, &oset); (void)tcsetpgrp(fd, saved_pgrp); + sigprocmask(SIG_SETMASK, &oset, NULL); + } close(fd); } } else { -- 2.40.0