man对于 int kill(pid_t pid, int sig);
说:
If pid equals -1, then sig is sent to every process for which the
calling process has permission to send signals, except for process 1
(init), but see below.
这是否意味着如果我的程序以 root 权限运行并且意外地(由于内存损坏或黑客攻击)提供 -1
作为 pid
参数 - 这将对整个系统造成完整的 DoS?
如果是这样,是否建议在调用这个潜在的灾难性调用之前始终对 pid
参数值执行双重检查? (只是说)
Does this mean that if my program is run with root permissions and it
accidentally (due to memory corruption or a hack) provides -1 as pid
argument - this will cause a complete DoS for the entire system?
是的,这种情况是可能的。但它发生的可能性非常小。因为没有以 root 权限运行的程序会做这样的事情。如果恶意用户/二进制文件以某种方式获得了 root 权限,那么发送信号只是问题之一。
If so, is it recommended to always perform a double check for the pid
argument value before calling this potentially disastrous call?
那只是 super 偏执的想法。进行灾难性事件的方法有数千种。您不妨担心:
如果没有在系统启动时运行的恶意守护进程会怎样:
kill(-1, SIGKILL);
您如何知道您创建的库函数是否不会调用 reboot(2)
并重新启动您的系统?
等等
此外,PID 不仅仅是用户提供的需要清理的值。 PID 主要是使用系统调用或库调用在程序中获取的值。因此,“意外”使用 -1 的可能性为零。基本上,您某人/程序拥有 root 权限并决定搞砸您的系统,那么您无能为力。