我想知道 UNIX 信号的安全性。
SIGKILL
会杀死进程。那么,当非 root 用户的进程向 root 用户的进程发送信号时会发生什么?进程是否仍然执行信号处理程序?
我遵循公认的答案(gollum 的),然后输入man capabilites
,然后我找到了很多关于 Linux 内核的信息。来自man capabilities
:
NAME
capabilities - overview of Linux capabilities
DESCRIPTION
For the purpose of performing permission checks, traditional UNIX
implementations distinguish two categories of processes: privileged
processes (whose effective user ID is 0, referred to as superuser or
root), and unprivileged processes (whose effective UID is nonzero).
Privileged processes bypass all kernel permission checks, while
unprivileged processes are subject to full permission checking based
on the process's credentials (usually: effective UID, effective GID,
and supplementary group list).
Starting with kernel 2.2, Linux divides the privileges traditionally
associated with superuser into distinct units, known as capabilities,
which can be independently enabled and disabled. Capabilities are a
per-thread attribute.
Run Code Online (Sandbox Code Playgroud)
gol*_*lum 37
在 Linux 上,它取决于文件功能。
取以下简单的mykill.c
来源:
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
void exit_usage(const char *prog) {
printf("usage: %s -<signal> <pid>\n", prog);
exit(1);
}
int main(int argc, char **argv) {
pid_t pid;
int sig;
if (argc != 3)
exit_usage(argv[0]);
sig = atoi(argv[1]);
pid = atoi(argv[2]);
if (sig >= 0 || pid < 2)
exit_usage(argv[0]);
if (kill(pid, -sig) == -1) {
perror("failed");
return 1;
}
printf("successfully sent signal %d to process %d\n", -sig, pid);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建它:
gcc -Wall mykill.c -o /tmp/mykill
Run Code Online (Sandbox Code Playgroud)
现在作为 root 用户在后台启动睡眠过程:
root@horny:/root# /bin/sleep 3600 &
[1] 16098
Run Code Online (Sandbox Code Playgroud)
现在作为普通用户尝试杀死它:
demouser@horny:/home/demouser$ ps aux | grep sleep
root 16098 0.0 0.0 11652 696 pts/20 S 15:06 0:00 sleep 500
demouser@horny:/home/demouser$ /tmp/mykill -9 16098
failed: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
现在作为 root 用户更改/tmp/mykill
大写:
root@horny:/root# setcap cap_kill+ep /tmp/mykill
Run Code Online (Sandbox Code Playgroud)
并以普通用户身份重试:
demouser@horny:/home/demouser$ /tmp/mykill -9 16098
successfully sent signal 9 to process 16098
Run Code Online (Sandbox Code Playgroud)
最后请删除/tmp/mykill
明显的原因;)
Hau*_*ing 25
没有:
strace kill -HUP 1
[...]
kill(1, SIGHUP) = -1 EPERM (Operation not permitted)
[...]
Run Code Online (Sandbox Code Playgroud)
小智 5
kill(2)
手册页解释:
Linux 笔记
在不同的内核版本中,Linux 对非特权进程向另一个进程发送信号所需的权限实施了不同的规则。在内核 1.0 到 1.2.2 中,如果发送方的有效用户 ID 与接收方的有效用户 ID 匹配,或者发送方的实际用户 ID 与接收方的实际用户 ID 匹配,则可以发送信号。从内核 1.2.3 到 1.3.77,如果发送者的有效用户 ID 与接收者的真实或有效用户 ID 匹配,则可以发送信号。当前的规则符合 POSIX.1-2001,在内核 1.3.78 中被采用。
归档时间: |
|
查看次数: |
6576 次 |
最近记录: |