Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

ioctl_tiocsctty passes the address of a zero instead of the documented 0 argument

Aperta Adatta ai principianti
#1,703 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 11 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
linux, rust

Direzione di ricerca

Il bug è in src/process/ioctl.rs: la chiamata documentata è alla riga 13, e l'adattatore degli argomenti alle righe 45-47 passa l'indirizzo di una variabile locale sullo stack 0u32 invece del valore 0. Inizia leggendo quell'adattatore e il commento di documentazione di ioctl_tiocsctty, poi esegui il repro strace dell'issue per confermare l'argomento errato. È completato quando l'adattatore passa il letterale 0, il commento cita ioctl_tty(2) e l'output di strace mostra TIOCSCTTY, 0.

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

Descrizione

rustix::process::ioctl_tiocsctty is documented as performing ioctl(fd, TIOCSCTTY, 0):

https://github.com/bytecodealliance/rustix/blob/1e2954a46ecffa6b30426ad7b1833b226530dcf4/src/process/ioctl.rs#L13

but the argument adapter passes the address of a stack-local 0u32:

https://github.com/bytecodealliance/rustix/blob/1e2954a46ecffa6b30426ad7b1833b226530dcf4/src/process/ioctl.rs#L45-L47

Linux from 2.6.12 requires the third arg to be exactly 1 for the option to steal the terminal, and only uses that arg anyway when CAP_SYS_ADMIN. On BSDs the arg is ignored. The Linux manpage says to use 0 otherwise; the comment in the code has tty_ioctl(4) but that's since been renamed to ioctl_tty(2) and TIOCSCTTY(2const) has the detail:

Make the given terminal the controlling terminal of the
calling process.  The calling process must be a session
leader and not have a controlling terminal already.  For
this case, arg should be specified as zero.

If this terminal is already the controlling terminal of a
different session group, then the ioctl fails with EPERM,
unless the caller has the CAP_SYS_ADMIN capability and arg
equals 1, in which case the terminal is stolen, and all
processes that had it as controlling terminal lose it.

From kernel source:

static int tiocsctty(struct tty_struct *tty,
                     struct file *file, int arg)
{
    // ...
    if (tty->ctrl.session) {
        if (arg == 1 && capable(CAP_SYS_ADMIN)) {
            session_clear_tty(tty->ctrl.session);
        } else {
            ret = -EPERM;
            goto unlock;
        }
    }
    // ...
}

This isn't unsafe exactly, but it's still confusing. Also, it might be worth exposing the steal variant.

Tiny repro:

#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
package.edition = "2024"
dependencies.rustix = { version = "1.1.5", features = ["process"] }
---
fn main() {
    let _ = rustix::process::ioctl_tiocsctty(std::io::stdin());
}

With strace:

$ ./repro.rs >/dev/null # compile
$ sudo strace -e ioctl ~/.cargo/build/*/*/target/debug/repro # run with CAP_SYS_ADMIN
ioctl(0, TIOCSCTTY, -527932840)         = -1 ENOTTY (Inappropriate ioctl for device)

The return may vary and isn't relevant: -527932840 is an address and is neither 0 nor 1, the two permitted values. A silver lining is that it's also impossible for arg to be 1 under the current code, so it's technically harmless.

Lingua principale
Rust
Stelle
2.1k
Fork
308
Merge medio
14g 5h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bytecodealliance/rustix

Tutte le issue di bytecodealliance/rustix

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.