【问题标题】:Coredump file not generated when changing user更改用户时未生成 Coredump 文件
【发布时间】:2016-01-13 19:53:56
【问题描述】:

以下代码生成核心转储文件:

#include <iostream>
#include <string>
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>

int main() {
    int b = 0;
    int a = 140/b;

    return 0;
}   

输出:Floating point exception (core dumped)

Coredump 生成在/opt/cores

$ ls -al /opt/cores
total 188
drwxrwxrwx  2 root root   4096 Jan 13 16:46 .
drwxr-xr-x 28 root root   4096 Jan 12 11:57 ..
-rw-------  1 root root 344064 Jan 13 16:46 core.prueba.6776.8

但是,这不会生成核心转储文件:

#include <iostream>
#include <string>
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>

int main() {
    std::string usr = "nobody";
    std::string grp = "oinstall";

    group* gp = getgrnam(grp.data());
    passwd* user = getpwnam(usr.data());
    if (gp && user && setgid(gp->gr_gid) == 0 && setuid(user->pw_uid) == 0) {
        std::cout << "changed!" << std::endl;
    } else {
        std::cout << "not changed =(" << std::endl;
    }   
    struct rlimit rlim;
    rlim.rlim_cur = RLIM_INFINITY;
    rlim.rlim_max = RLIM_INFINITY;
    if (setrlimit(RLIMIT_CORE, &rlim) != 0) {
        std::cout << "setrlimit error" << std::endl;
    }   

    getrlimit(RLIMIT_CORE, &rlim);

    std::cout << "rlim_cur: " << (int)rlim.rlim_cur <<", rlim_max: " << (int)rlim.rlim_max << std::endl;
    int b = 0;
    int a = 140/b;

    return 0;
}  

输出:

changed!
rlim_cur: -1, rlim_max: -1
Floating point exception

我已经用更改的用户运行了第一段代码,它生成了 coredump 文件,因此该目录具有正确的权限。问题是当我在代码中更改用户时。有什么线索吗?

这发生在 Linux(CentOS 6、CentOS 7、RHEL 6)上。

在 Solaris 中工作正常。

【问题讨论】:

标签: core


【解决方案1】:

setuid() 手册页有这样的注释:

如果 uid 与旧的有效 UID 不同,则该过程将是 禁止离开核心转储。

这是一种安全机制,你可以阅读更多关于为什么here

您需要启用fs.suid_dumpable 以让您的进程进行核心转储:

 sysctl -w fs.suid_dumpable=2

【讨论】:

  • 谢谢!我设法在程序中找到解决方案,而不是更改系统。
【解决方案2】:

关于安全机制的观点是正确的。

解决方案是在 更改用户后添加:

prctl(PR_SET_DUMPABLE, 1, 0,0,0);

现在核心转储已生成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多