删除 root 权限并仍然生成核心转储
Dropping root privileges and still generate coredumps
我注意到我的一些用户在崩溃后根本没有得到核心转储,即使他们配置中的其他一切似乎都是正确的。
多次阅读 core(5) 手册页后,我注意到了这一点:
[A core dump file is not produced if] The process is executing a set-user-ID (set-group-ID) program that is owned by a user (group) other than the real user (group) ID of the process.
我的守护进程不是 setuid root,但在很多配置中它是以 root 启动的,如果 .conf 文件指定了用户名,它会放弃特权,通常的组合是:
setgid(gid);
setuid(uid);
执行此操作时,不再生成核心转储。环境中的其他一切似乎都是正确的,删除这些调用(并保持 root 身份)让我像往常一样得到核心转储。
我试图改变 "real" uid/gid 作为手册页似乎建议的,通过调用不太便携的 setresgid/uid 似乎经常建议永久放弃特权:
setresgid(gid, gid, gid);
setresuid(uid, uid, uid);
我希望这样可以解决问题,但是...根本没有改善。仍然没有核心转储。
太...现在怎么办?
测试代码:
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc > 1) {
setgid(atoi(argv[2]));
setuid(atoi(argv[1]));
}
abort();
}
用法:
./a.out
作为任何用户,只需在没有 setgid/setuid 的情况下中止
./a.out 1000 100
(其中 1000 是 uid,100 是 gid)以 root 身份删除特权并观察核心转储不会发生。
- 额外的意外功能:传递一个参数,而不是两个参数,以获取 SIGSEGV 而不是 SIGABRT。
我已经在 arch linux、centos 6.5 和 openbsd 5.3
中测试过了
您必须为权限已更改的应用程序启用核心转储:
echo 2 > /proc/sys/fs/suid_dumpable
我建议你把它放在 /etc/rc.local
中。
例如,这是我那里的内容:
# This is to enable debugging as a normal user, rather than root
sysctl kernel.yama.ptrace_scope=0
# This is a convenient core file pattern
# '%e' is the name of the process
# '%p' is the pid of process
sysctl kernel.core_pattern="/tmp/core.%e.%p"
# Enable dump for processes with lowered privileges
echo 2 > /proc/sys/fs/suid_dumpable
# Remove limit for the size of core files
ulimit -c unlimited
编辑:
您可以看看这个整洁的库,它允许您手动将核心转储写入自定义文件:https://code.google.com/p/google-coredumper/
我相信这正是您所需要的。
要强制进程始终进行核心转储,请使用 prctl 系统调用。
prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
我注意到我的一些用户在崩溃后根本没有得到核心转储,即使他们配置中的其他一切似乎都是正确的。
多次阅读 core(5) 手册页后,我注意到了这一点:
[A core dump file is not produced if] The process is executing a set-user-ID (set-group-ID) program that is owned by a user (group) other than the real user (group) ID of the process.
我的守护进程不是 setuid root,但在很多配置中它是以 root 启动的,如果 .conf 文件指定了用户名,它会放弃特权,通常的组合是:
setgid(gid);
setuid(uid);
执行此操作时,不再生成核心转储。环境中的其他一切似乎都是正确的,删除这些调用(并保持 root 身份)让我像往常一样得到核心转储。
我试图改变 "real" uid/gid 作为手册页似乎建议的,通过调用不太便携的 setresgid/uid 似乎经常建议永久放弃特权:
setresgid(gid, gid, gid);
setresuid(uid, uid, uid);
我希望这样可以解决问题,但是...根本没有改善。仍然没有核心转储。
太...现在怎么办?
测试代码:
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc > 1) {
setgid(atoi(argv[2]));
setuid(atoi(argv[1]));
}
abort();
}
用法:
./a.out
作为任何用户,只需在没有 setgid/setuid 的情况下中止
./a.out 1000 100
(其中 1000 是 uid,100 是 gid)以 root 身份删除特权并观察核心转储不会发生。- 额外的意外功能:传递一个参数,而不是两个参数,以获取 SIGSEGV 而不是 SIGABRT。
我已经在 arch linux、centos 6.5 和 openbsd 5.3
中测试过了您必须为权限已更改的应用程序启用核心转储:
echo 2 > /proc/sys/fs/suid_dumpable
我建议你把它放在 /etc/rc.local
中。
例如,这是我那里的内容:
# This is to enable debugging as a normal user, rather than root
sysctl kernel.yama.ptrace_scope=0
# This is a convenient core file pattern
# '%e' is the name of the process
# '%p' is the pid of process
sysctl kernel.core_pattern="/tmp/core.%e.%p"
# Enable dump for processes with lowered privileges
echo 2 > /proc/sys/fs/suid_dumpable
# Remove limit for the size of core files
ulimit -c unlimited
编辑:
您可以看看这个整洁的库,它允许您手动将核心转储写入自定义文件:https://code.google.com/p/google-coredumper/
我相信这正是您所需要的。
要强制进程始终进行核心转储,请使用 prctl 系统调用。
prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);