15
Analyzing the linux API I noticed that an interesting structure is possible:
#include <unistd.h>
#include <stdlib.h>
int main()
{
while (1)
{
if (fork()) exit(0); // Altera meu pid
setpgid(0, 0); // Cria um novo grupo de processos, o atual sendo o único membro
// Agora executar alguma operação "maligna":
usleep(1000);
}
}
What happens here is that every iteration the process will create a clone child and then kill itself. For all intents and purposes, this is the equivalent of changing your own ID. Then I use the setpgid
to open a new group of processes including only the current process, without the original process before the clone. So at each iteration the group ID is also changed. Then I run a short operation and change my Ids again.
This type of process is known as comet process by system administrators.
The problem is that it is very difficult to kill a process like this, because the kill
needs a process ID or a group ID and the time it takes for me to identify its ID and send a Kill, will be enough time for it to change.
Why this process does not appear listed?
When I executeps -A | grep teste
(assuming my executable is calledteste
), sometimes the process doesn’t show up. That doesn’t make sense to me. I understand that some moments there are 2 processes with the same name (the clone and the relative who has not yet killed himself), and that most of the time there is only one. But zero doesn’t make sense. He’s not dead at any point. If I repeat the command a considerable number of times the process is listed in some. Why?How to kill this process?
Although I can list and get his ID (after many attempts) the time it takes to callkill
is enough for him to clone himself and commit suicide. I can try with thekillall teste
, but the command fails on almost every call. How to effectively kill it?The fact that this structure is possible represents a flaw in the API design?
What I see here is an API that allows the process ID to fluctuate and at the same time depend on it being stable as the only way to kill a process. Wouldn’t that be a flaw in the original design? It could be considered a security flaw until?
Unfortunately the
killall
failure stating that the process does not exist (probably changed ID right after he resolved the name and before he sent the signal). However, if I repeat this command a few million times in sequence, one will succeed and kill my process. This is the only alternative?– Guilherme Bernal
Very interesting discussion, curious to say the least! Okay, what if the binary name of this comet is init , what the solution?
– cemdorst
Root can’t kill the
init
on linux, @cemdorst– motobói
I think you can. #pkill init does not work?
– cemdorst