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 with each iteration the process will create a child clone and kill itself next. For all intents and purposes, this is the equivalent of exchanging your own ID. Then I use setpgid
to open a new group of processes including only the current process , without the original process before the clone. So with 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, since kill
needs a process ID or a group ID and the time it takes to identify the ID of the group and send a kill will be enough time for it to change.
-
Why is this process not listed?
When I runps -A | grep teste
(assuming my executable is calledteste
), sometimes the process does not appear. It makes no sense to me. I understand that in some moments there is a process with the same name (the clone and the relative that has not yet killed himself), and that most of the time there is only one. But zero does not make sense. He is at no time dead. If I repeat the command a considerable number of times the process is listed in some. Why? -
How to kill this process?
Even though I can list and get his ID (after many attempts) the time it takes to callkill
is enough for him to clone and commit suicide. I can try withkillall teste
, but the command fails on almost all calls. How to effectively kill him? -
Does this structure represent a flaw in API design?
What I see here is an API that allows the process ID to float and at the same time depend on it being stable as the only way to kill a process. Would not that be a flaw in the original design? Could it be considered a security breach until?