Proposing PR 1601 (commit 59e764c7) for resolute in the same upload as
PR 1530, which fixes LP: #2150073 and LP: #2160212.

Tested in fresh ubuntu:26.04 containers on a pty, as root and as a
NOPASSWD user, with upstream's regression test for issue 1598 (PR 1647:
the command runs "kill -TTOU $$" four times, stdout piped):

 * "sudo sh ~/inner.sh | cat" in an interactive bash: on 0.2.13-0ubuntu1.2, and 
with PR 1530 alone, the shell prints "Stopped", sudo and cat are in T, the 
command in T+, and three fg runs are needed. With PR 1530 and PR 1601: 
did-not-deadlock at once.
 * bash -c "set -m; sudo sh ~/inner.sh | cat", as upstream's test runs it: exit 
status 150 (stopped by SIGTTOU) without PR 1601, did-not-deadlock and 0 with it.

The Python reproducer in the description (apt-get -y full-upgrade with a
failing postinst) hangs on 0.2.13-0ubuntu1.2 too, but on 26.04 /bin/sh
does not exec apt-get, so that hang is the LP: #2150073 one (apt-get, a
child of the command, in T+), and PR 1530 alone fixes it.

The patch applies to 0.2.13-0ubuntu1.2 with no fuzz, on top of PR 1530
or without it, and makes src/exec/use_pty/parent.rs identical to 0.2.14.
The debdiff and the full test record are on LP: #2150073. The SRU
template is in this bug's description.

This bug is already Fix Released for the development release
(0.2.14-1ubuntu2). It needs a resolute task, so please accept the
resolute nomination.


** Description changed:

+ [ Impact ]
+ 
+  * On 26.04, with use_pty (the default) and sudo's stdout piped, a
+ command that the terminal stops a second time by SIGTTOU or SIGTTIN is
+ never resumed. Instead sudo stops itself and its whole job: an
+ interactive shell prints "Stopped" and gives its prompt back, and a
+ script or a program that runs sudo with a pipe, as in the report, waits
+ forever.
+ 
+  * Cause: in suspend_pty() (src/exec/use_pty/parent.rs), sudo returns
+ SIGCONT_FG only while the user's terminal is not yet in raw mode. The
+ first stop puts the terminal in raw mode and resumes the command. On the
+ second stop the terminal is already raw, so sudo skips the resume, takes
+ the suspend path and sends the stop signal to its own process group.
+ Nothing resumes it.
+ 
+  * Fix: upstream PR 1601 (commit 59e764c7, in 0.2.14 and later) moves
+ the resume out of the "if !term_raw" block, so sudo returns SIGCONT_FG
+ whenever it is in the foreground of the user's terminal, as sudo.ws
+ does.
+ 
+ [ Test Plan ]
+ 
+ Run this on a clean Ubuntu 26.04 install, where /usr/bin/sudo is sudo-rs
+ (the default). Use a real terminal (the local console, a desktop
+ terminal or "ssh -t") and a user that may run sudo. Open two terminals,
+ A and B, as that user.
+ 
+ Steps:
+ 
+  a. Do the setup below in terminal A.
+  b. Run tests 1 to 3 with 0.2.13-0ubuntu1.2 to see the bug.
+  c. Install the update from -proposed (below).
+  d. Run tests 1 to 3 again in the same terminals.
+ 
+ Setup, in terminal A. First run this line on its own. It may ask for
+ your password:
+ 
+     sudo -v
+ 
+ sudo-rs keeps the password for 15 minutes. If the tests take longer, run
+ "sudo -v" again before the next test, so that no password prompt lands
+ inside a test. Then paste this block as it is, with no spaces in front
+ of the lines:
+ 
+ cat > ~/inner.sh <<'EOF'
+ for _ in 1 2 3 4; do
+     kill -TTOU $$
+ done
+ echo did-not-deadlock
+ EOF
+ sudo --version
+ 
+ inner.sh is the script of upstream's regression test for this bug
+ (upstream PR 1647): the command stops itself with SIGTTOU four times.
+ "sudo --version" prints sudo-rs 0.2.13-0ubuntu1.2 before the update.
+ 
+ To look for a stopped process, run this in terminal B:
+ 
+     ps -eo pid,ppid,stat,wchan:32,args | awk '$3 ~ /^T/'
+ 
+ Bug tests, in terminal A:
+ 
+  1. The command is stopped twice, stdout piped:
+ 
+         sudo sh ~/inner.sh | cat
+ 
+     Expected: did-not-deadlock, then the prompt.
+ 
+     With 0.2.13-0ubuntu1.2 the shell prints "[1]+  Stopped  sudo sh
+ ~/inner.sh | cat" and the prompt instead. Terminal B lists sudo and cat
+ in state T, and "sh .../inner.sh" in state T+. Run fg. It stops again.
+ After the third fg, did-not-deadlock appears.
+ 
+  2. The same, as upstream's test runs it, with job control in a non-
+ interactive bash:
+ 
+         bash -c "set -m; sudo sh ~/inner.sh | cat"; echo exit=$?
+ 
+     Expected: did-not-deadlock, then exit=0.
+ 
+     With 0.2.13-0ubuntu1.2 only exit=150 appears (128 plus 22, SIGTTOU):
+ bash saw the pipeline stopped and quit, and sudo and its command were
+ then killed.
+ 
+ With 0.2.13-0ubuntu1.3 tests 1 and 2 pass and terminal B lists nothing.
+ 
+ Regression test, in terminal A:
+ 
+  3. Ctrl-Z and fg still suspend and resume sudo:
+ 
+         sudo sh -c 'read v && echo got-$v'
+ 
+     Press Ctrl-Z. The shell prints "Stopped". Run fg, type hello and
+ press Enter. Expected on both versions: got-hello.
+ 
+ Install the update: enable -proposed as
+ https://ubuntu.com/project/docs/SRU/reference/sru-verification/
+ describes, then run:
+ 
+     sudo apt install -t resolute-proposed sudo-rs
+     apt policy sudo-rs
+     sudo --version
+ 
+ "sudo --version" must print sudo-rs 0.2.13-0ubuntu1.3.
+ 
+ [ Where problems could occur ]
+ 
+  * The change is in the parent's handling of a command that the terminal
+ stops with SIGTTOU or SIGTTIN, while sudo is in the foreground of the
+ user's terminal. sudo now always resumes the command in the foreground
+ there. Before, it did so only the first time, and after that it restored
+ the user's terminal and stopped itself. If the change is wrong, it would
+ show as a command under sudo that loops between stopped and running, or
+ a terminal left in raw mode after sudo ends.
+ 
+  * A command that keeps sending itself SIGTTOU or SIGTTIN is now resumed
+ every time, as with sudo.ws, instead of stopping sudo after the first
+ time.
+ 
+  * Not changed: Ctrl-Z and other SIGTSTP stops (test 3), the case where
+ sudo itself is in the background of the user's terminal (it still
+ suspends, as before), and the no_pty path (Defaults !use_pty, or no
+ terminal).
+ 
+  * The same change has shipped in upstream 0.2.14 and 0.2.15, and in
+ 26.10 and 27.04. suspend_pty() in 0.2.14 and 0.2.15 is identical to the
+ fix commit.
+ 
+ [ Other Info ]
+ 
+  * Upstream: issue 1598, PR 1601, commit 
59e764c737aac6ea6ff7af36dbf36397c3916b46, merged 2026-05-26, first released in 
0.2.14. Regression test: upstream PR 1647 
(sigttou_in_foreground_does_not_deadlock), in test-framework/, which is not in 
the crates.io tarball.
+    https://github.com/trifectatechfoundation/sudo-rs/pull/1601
+ 
+  * The patch is the upstream diff unchanged, with a DEP-3 header. It
+ applies to 0.2.13-0ubuntu1.2 with no fuzz or offset, on top of the PR
+ 1530 patch or without it. It touches src/exec/use_pty/parent.rs only.
+ After it, parent.rs is identical to the one in upstream 0.2.14.
+ 
+  * The same upload fixes LP: #2150073 and LP: #2160212 (upstream PR
+ 1530). The debdiff and the full test record are on LP: #2150073.
+ 
+  * Fixed in the development release: this bug was closed by
+ 0.2.14-1ubuntu2 in 26.10 (stonking), and tenacious, the current
+ development series, has 0.2.14-1ubuntu4.1. noble has rust-sudo-rs
+ 0.2.2-1 in universe, which is not the default sudo there. It has the
+ same nested return but was not tested, and no noble task is proposed.
+ 
+  * The report's Python reproducer (apt-get -y full-upgrade with a
+ failing postinst) was also run. On 26.04 /bin/sh does not exec apt-get,
+ so apt-get is a child of the command, and it stops after the dpkg error
+ for the reason of LP: #2150073. It hangs on 0.2.13-0ubuntu1.2 and passes
+ on 0.2.13-0ubuntu1.3, and also on a build with only the PR 1530 patch.
+ The second stop that this bug is about is what tests 1 and 2 show.
+ 
+  * Testing done: tests 1 and 2 ran in fresh ubuntu:26.04 containers on a
+ real pty, as root and as a NOPASSWD user, on three .deb builds: the
+ archive 0.2.13-0ubuntu1.2, a local build with only the PR 1530 patch,
+ and 0.2.13-0ubuntu1.3 with both patches. Tests 1 and 2 stop on the first
+ two and pass on 0.2.13-0ubuntu1.3. In test 1 sudo and cat were in state
+ T and the command in T+, and three fg runs were needed. Test 2 printed
+ exit=150. Upstream's test as its framework runs it (bash -c as the top
+ process of a tty) gave the same: exit status 150 on the first two
+ builds, did-not-deadlock and 0 on 0.2.13-0ubuntu1.3. Test 3 passes on
+ all three. The containers ran on a 6.18 kernel, not a 26.04 VM with its
+ 7.0 kernel.
+ 
+ [ Original Description ]
+ 
  ## Summary
  
  When a command running under `sudo` with `use_pty` receives SIGTTOU
  twice in succession, the parent process fails to send SIGCONT_FG on the
  second occurrence. This causes the command to remain permanently stopped
  (T state), deadlocking the entire process tree.
  
  The bug is a misplaced `return` in `suspend_pty()` in
  `src/exec/use_pty/parent.rs`. The `return Some(SIGCONT_FG)` is nested
  inside `if !self.term_raw`, so when `term_raw` is already `true` (set
  during the first SIGTTOU handling), the early return is skipped and the
  code falls through to the suspend path, which sends SIGTTOU to the
  parent process group via `killpg()`.
  
  Original sudo (sudo.ws) does not have this issue — its equivalent `ret =
  SIGCONT_FG; break;` is at the `if (ec->foreground)` level, always
  executing when the parent is the foreground process.
  
  ## Environment
  
  - sudo-rs 0.2.13-0ubuntu1
  - `Defaults use_pty` in `/etc/sudoers`
  - Linux (tested on 6.x kernel, riscv64)
  
  ## Steps to Reproduce
  
  Prerequisites:
  - `Defaults use_pty` enabled in `/etc/sudoers`
  - A command that triggers SIGTTOU twice (e.g., a dpkg post-install script 
failure during `apt-get full-upgrade`)
  
  Minimal reproduction using Python:
  
  ```python
  import subprocess, threading
  
  proc = subprocess.Popen(
-     ["sudo", "/bin/sh", "-c", "LC_ALL=C apt-get -y full-upgrade"],
-     stdin=None,              # inherit terminal
-     stdout=subprocess.PIPE,  # pipe stdout
-     stderr=subprocess.STDOUT,
-     text=True,
+     ["sudo", "/bin/sh", "-c", "LC_ALL=C apt-get -y full-upgrade"],
+     stdin=None,              # inherit terminal
+     stdout=subprocess.PIPE,  # pipe stdout
+     stderr=subprocess.STDOUT,
+     text=True,
  )
  
  def drain():
-     for line in proc.stdout:
-         print(line, end="")
+     for line in proc.stdout:
+         print(line, end="")
  
  threading.Thread(target=drain, daemon=True).start()
  proc.wait(timeout=120)  # hangs here
  ```
  
  When a dpkg trigger/script fails, `sh` receives SIGTTOU from the kernel
  and stops. The monitor sends SIGCONT (via SIGCONT_FG from parent). `sh`
  resumes but receives SIGTTOU again. This time, no SIGCONT is sent — the
  command stays stopped permanently.
  
  Workaround: use `stdin=subprocess.DEVNULL` to prevent sudo from
  detecting a terminal on stdin.
  
  Switching to original sudo (sudo.ws) via `update-alternatives`
  eliminates the issue entirely.
  
  ## Root Cause
  
  In `src/exec/use_pty/parent.rs`, the `suspend_pty()` function handles
  SIGTTOU/SIGTTIN at lines 576-603:
  
  ```rust
  if let SIGTTOU | SIGTTIN = signal {
-     if !self.foreground && self.check_foreground().is_err() {
-         return None;
-     }
- 
-     if self.foreground {
-         dev_info!(...);
-         if !self.term_raw {
-             if self.tty_pipe
-                 .left_mut()
-                 .set_raw_mode(false, self.preserve_oflag)
-                 .is_ok()
-             {
-                 self.term_raw = true;
-             }
-             // Resume command in the foreground
-             self.tty_pipe.enable_input(registry);
-             return Some(SIGCONT_FG);  // BUG: only reached when term_raw was 
false
-         }
-         // When term_raw is already true, falls through!
-     }
+     if !self.foreground && self.check_foreground().is_err() {
+         return None;
+     }
+ 
+     if self.foreground {
+         dev_info!(...);
+         if !self.term_raw {
+             if self.tty_pipe
+                 .left_mut()
+                 .set_raw_mode(false, self.preserve_oflag)
+                 .is_ok()
+             {
+                 self.term_raw = true;
+             }
+             // Resume command in the foreground
+             self.tty_pipe.enable_input(registry);
+             return Some(SIGCONT_FG);  // BUG: only reached when term_raw was 
false
+         }
+         // When term_raw is already true, falls through!
+     }
  }
  
  // Falls through to suspend path:
  // ...
  // killpg(self.parent_pgrp, signal)  ← deadlocks the process tree
  ```
  
  The equivalent code in original sudo (`src/exec_pty.c:236-246`) does not
  have this problem:
  
  ```c
  if (ec->foreground) {
-     if (!ec->term_raw) {
-         if (sudo_term_raw(io_fds[SFD_USERTTY], term_raw_flags))
-             ec->term_raw = true;
-     }
-     ret = SIGCONT_FG;   /* always reached when foreground is true */
-     break;
+     if (!ec->term_raw) {
+         if (sudo_term_raw(io_fds[SFD_USERTTY], term_raw_flags))
+             ec->term_raw = true;
+     }
+     ret = SIGCONT_FG;   /* always reached when foreground is true */
+     break;
  }
  ```
  
  The `ret = SIGCONT_FG; break;` is at the `if (ec->foreground)` scope, so
  it executes regardless of `term_raw` state.
  
  ## Sequence of Events
  
  ```
  stdout=PIPE → foreground=false, term_raw=false at startup
  
  1st SIGTTOU:
-   command stops (SIGTTOU from kernel)
-   → monitor: on_stop() → sends Stop(SIGTTOU) to parent
-   → parent: suspend_pty(SIGTTOU)
-     → check_foreground() → foreground=true (sudo IS fg process on user tty)
-     → term_raw=false → enters if(!term_raw) block
-     → set_raw_mode() → term_raw=true
-     → returns SIGCONT_FG ✓
-   → monitor: tcsetpgrp(command_pgrp) + kill(SIGCONT)
-   → command resumes
+   command stops (SIGTTOU from kernel)
+   → monitor: on_stop() → sends Stop(SIGTTOU) to parent
+   → parent: suspend_pty(SIGTTOU)
+     → check_foreground() → foreground=true (sudo IS fg process on user tty)
+     → term_raw=false → enters if(!term_raw) block
+     → set_raw_mode() → term_raw=true
+     → returns SIGCONT_FG ✓
+   → monitor: tcsetpgrp(command_pgrp) + kill(SIGCONT)
+   → command resumes
  
  2nd SIGTTOU:
-   command stops again (SIGTTOU from kernel)
-   → monitor: on_stop() → sends Stop(SIGTTOU) to parent
-   → parent: suspend_pty(SIGTTOU)
-     → foreground=true, term_raw=true
-     → if(!term_raw) is FALSE → skips the block with return SIGCONT_FG
-     → falls through to suspend path
-     → killpg(parent_pgrp, SIGTTOU) → stops entire parent process group
-     → DEADLOCK: nobody sends SIGCONT to resume ✗
+   command stops again (SIGTTOU from kernel)
+   → monitor: on_stop() → sends Stop(SIGTTOU) to parent
+   → parent: suspend_pty(SIGTTOU)
+     → foreground=true, term_raw=true
+     → if(!term_raw) is FALSE → skips the block with return SIGCONT_FG
+     → falls through to suspend path
+     → killpg(parent_pgrp, SIGTTOU) → stops entire parent process group
+     → DEADLOCK: nobody sends SIGCONT to resume ✗
  ```
  
  ## Suggested Fix
  
  Move `return Some(SIGCONT_FG)` and `enable_input()` outside the `if
  !self.term_raw` block to match sudo.ws behavior:
  
  ```rust
  if self.foreground {
-     dev_info!(...);
-     if !self.term_raw {
-         if self.tty_pipe
-             .left_mut()
-             .set_raw_mode(false, self.preserve_oflag)
-             .is_ok()
-         {
-             self.term_raw = true;
-         }
-     }
-     // Resume command in the foreground (always, regardless of term_raw)
-     self.tty_pipe.enable_input(registry);
-     return Some(SIGCONT_FG);
+     dev_info!(...);
+     if !self.term_raw {
+         if self.tty_pipe
+             .left_mut()
+             .set_raw_mode(false, self.preserve_oflag)
+             .is_ok()
+         {
+             self.term_raw = true;
+         }
+     }
+     // Resume command in the foreground (always, regardless of term_raw)
+     self.tty_pipe.enable_input(registry);
+     return Some(SIGCONT_FG);
  }
  ```

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2146860

Title:
  command permanently stop

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/rust-sudo-rs/+bug/2146860/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to