bytecodealliance / bytecodealliance/rustix

`rustix::termios::tcgetpgrp` is unsound on macOS

Aperta
#1,678 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
2.1k
Fork
296
Merge medio
6g 22h
PR unite (30g)
4

Descrizione

The [libc backend for `rustix::termios::tcgetpgrp`](https://docs.rs/crate/rustix/latest/source/src/backend/libc/termios/syscalls.rs#166) passes the result of libc's `tcgetpgrp()` directly to `Pid::from_raw_unchecked` on non-Linux platforms; only Linux has a check to make sure the PID isn't 0. However, `tcgetpgrp()` can return 0 on non-Linux platforms too, notably macOS.

Specifically, if there is no process running on the terminal (e.g., immediately after creating a PTY), `tcgetpgrp()` will return 0. On macOS, this means 0 will get passed to `Pid::from_raw_unchecked`, which is UB.

### Reproduction

Must be run on macOS. Might fail on other non-Linux platforms but I haven't tested.

```sh
cargo new rustix-issue
cd rustix-issue
```

Put the following in `Cargo.toml`:

```toml
[package]
name = "rustix-issue"
version = "0.1.0"
edition = "2024"

[dependencies.rustix]
version = "1.1.4"
features = ["pty", "termios"]
```

Put the following in `src/main.rs`:

```rust
use rustix::pty::{OpenptFlags, openpt};
use rustix::termios::tcgetpgrp;

fn main() {
let fd = openpt(OpenptFlags::RDWR | OpenptFlags::NOCTTY).expect("openpt");
match tcgetpgrp(&fd) {
Ok(pid) => println!("tcgetpgrp() -> {pid}"),
Err(e) => println!("tcgetpgrp() -> {e}"),
}
}
```

Then run `cargo run`. As I don't use macOS I asked @ellie to run this; here's the output:

```
thread 'main' (14903657) panicked at /Users/ellie/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.1.4/src/pid.rs:61:9:
assertion failed: raw > 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

We hit the debug assertion in [`Pid::from_raw_unchecked`](https://docs.rs/crate/rustix/latest/source/src/pid.rs#60), which indicates UB.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia da src/backend/libc/termios/syscalls.rs intorno a tcgetpgrp e confronta la gestione Linux con i percorsi non Linux; leggi anche pid.rs intorno a Pid::from_raw_unchecked. Riproduci il caso PTY di macOS riportato nell’issue e verifica la copertura rilevante di termios; il lavoro è completato quando il risultato zero non raggiunge più il costruttore di PID non verificato senza interrompere il comportamento esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
rust
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
72/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.