getpgid: UB when the kernel returns process group 0 (kernel threads / other PID namespace)

Ouverte
#1,696 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
30/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
rust

Piste de recherche

Read src/backend/linux_raw/process/syscalls.rs and src/backend/libc/process/syscalls.rs, starting at getpgid and comparing how each handles the kernel result. Then check the public Pid API and related wrappers such as getsid to understand the impact of the proposed choices. Done means a safe, consistent way to represent or handle a returned 0 is agreed on and applied; the issue does not specify which fix to choose.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

getpgid can return a process group ID of 0 on Linux. rustix then builds a Pid from that value with Pid::from_raw_unchecked, which calls NonZeroI32::new_unchecked(0). That is undefined behavior in release builds. Debug builds hit the debug_assert!(pgid > 0) instead.

Where: src/backend/linux_raw/process/syscalls.rs, getpgid, unchanged in 1.1.4 and on main. The libc backend (src/backend/libc/process/syscalls.rs) does the same without the debug assertion.

When the kernel returns 0: getpgid(2) reports the group ID as seen from the caller's PID namespace. Two cases give 0:

  • kernel threads, for example PID 2 (kthreadd) and its children;
  • any process whose process group is not visible in the caller's namespace.

A process that scans /proc and calls getpgid on every PID will meet both cases.

Reproduction (Linux, debug build):

let pgid = rustix::process::getpgid(rustix::process::Pid::from_raw(2));
// panics on the debug_assert; in release it creates a Pid holding 0 (UB)

Possible fixes:

  • return Result<Option<Pid>>, with None for 0;
  • map 0 to an error;
  • document it and return a type that may hold 0.

Other wrappers that hand a kernel-provided ID to from_raw_unchecked (for example getsid) may have the same issue.

We found this while running a test suite natively on Ubuntu 26.04. As a workaround we now call libc::getpgid directly and treat 0 as "no visible group".

Langage dominant
Rust
Étoiles
2.1k
Forks
322
Merge moyen
6 j 15 h
PR mergées (30 j)
11

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de bytecodealliance/rustix

Toutes les issues de bytecodealliance/rustix

Issues similaires

Plus d'issues Rust

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.