News for hackers. Stuff that matters.
Re:IPC – Unix Signals by jlokier on Monday October 09, 2023 @12:26PM (#37822044)
> A new pid is always more than the previously assigned pid during an OS uptime

This isn't true. The pid increments most of the time, but occasionally the new pid jumps down to a seemingly arbitrary value to start a new range. It is switching between unused ranges. This happens on my Linux machines all the time.

However, it doesn't matter for what the article describes: it doesn't depend on the pid always increasing, it only depends on the pid for each process being unique.

Pid reuse doesn't matter often, but it matters occasionally with how sigals are often used. Many scripts are written to signal a process that might have terminated already, e.g. using a pid stored in an environment variable or file. This usually just returns an error, but if there has been enough system activity meanwhile, the pid may belong to a new process, which the script does not intend to signal, so a random process may be killed unexpectedly.

Re:IPC – Unix Signals by duped on Monday October 09, 2023 @12:48PM (#37822326)
I feel a bit nerd sniped: On Linux PIDs are only unique within a PID namespace. But that doesn't matter because the entire point of a PID namespace is to isolate processes.

But it's worth mentioning because it can mitigate the problem you've laid out. If you have a long running process that may need to kill its children, you can start with within a new PID namespace such that it may only kill its children (or descendant).

Re:IPC – Unix Signals by rcr on Monday October 09, 2023 @01:23PM (#37822820)
This doesn't entirely solve the problem of potentially killing the wrong process though
Re:IPC – Unix Signals by duped on Monday October 09, 2023 @02:32PM (#37823596)
No but it is a lot less likely for a PID to get reused on accident within a new PID namespace.
Re:IPC – Unix Signals by rcr on Monday October 09, 2023 @07:46PM (#37826891)
It's really no less likely, namespace or not. There are better mechanisms for handling this situation, pidfds for example.
Re:IPC – Unix Signals by remram on Monday October 09, 2023 @04:13PM (#37824911)
That should only be a problem if you're running as root. Running as a dedicated user is also a mitigation.
Re:IPC – Unix Signals by rcr on Monday October 09, 2023 @01:23PM (#37822808)
Linux introduced pidfds for this reason.
Re:IPC – Unix Signals by goodyduru on Monday October 09, 2023 @06:03PM (#37826032)
Author here. Thanks for the feedback! Corrected the error.
Re:IPC – Unix Signals by somat on Monday October 09, 2023 @11:07PM (#37828113)
Also note that openbsd uses random pids.
Re:IPC – Unix Signals by Lammy on Tuesday October 10, 2023 @01:21AM (#37828771)
And FreeBSD if so configured:

  $ sysctl kern.pid_max kern.randompid
  kern.pid_max: 99999
  kern.randompid: 594

D12238 sez “If set to a value of (100 <= kern.randompid <= (PID_MAX - 100)), it will be used as modulus when kern_fork.c tries to allocate new PID. Formula is lastpid += arc4random() % kern.randompid”