c语言中将用户从root更改为nobody后无法生成核心文件

标签 c linux root core

c语言中将用户从root更改为nobody后,我确定程序核心转储,但始终无法生成核心文件。 我确定没有人有权在当前目录中生成文件。 ulimit -c 是无限制的,我使用:

system("echo 'tesstestestestestetestestet!!!!!!' >  hahahahhaahahah");

将用户从 root 更改为 nobody 后,文件 haha​​hahhaahahah 已创建!

所以,我很困惑!

这是我的c文件:

#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
#if 1
    struct passwd *pw;

    //char *username = "root";
    char *username = "nobody";
    if (getuid() == 0 || geteuid() == 0)
    {
        if (username == 0 || *username == '\0')
        {
            fprintf(stderr, "can't run as root without the -u switch\n");
            exit(-1);
        }
        if ((pw = getpwnam(username)) == NULL)
        {
            fprintf(stderr, "can't find the user %s to switch to\n", username);
            exit(-1);
        }
        if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
        {
            fprintf(stderr, "failed to assume identity of user %s\n", username);
            exit(-1);
        }
    }
#endif

    printf("now user change to group id %d, user id %d\n", getgid(), getuid());

    system("echo 'tesstestestestestetestestet!!!!!!' >  hahahahhaahahah");
    char *test_a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    char *test_b;
    strcpy(test_b, test_a);
    *(char *)1=1;
    printf("test_b:%s\n", test_b);
}

最佳答案

仔细阅读 core(5)手册页:

There are various circumstances in which a core dump file is not produced:

.... 跳过手册页中的一些文本 ....

  • 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(2) 之后系统调用,核心转储。(出于安全原因)

另请参阅特定于 Linux 的 prctl(2)系统调用,使用 PR_SET_DUMPABLE

另请阅读 http://advancedlinuxprogramming.com/

注意。拥有一个nobody 可写目录可能不是一个好主意。 nobody 用户通常不应拥有任何文件或目录!

关于c语言中将用户从root更改为nobody后无法生成核心文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17204782/

相关文章:

将字符串转换为 unsigned long int

c - 在 C 中操作字符串

c - 为什么我在下面的程序中针对指针而不是数组出现段错误?

c++ - 更有效地使用 fork() 和写时复制内存共享

arrays - Bash 从配置文件中解析数组

javascript - 从外部根文件夹访问js

c - 在 OS X 中读取其他进程的内存?

linux - Linux脚本中两个$有什么区别

scipy.optimize.fsolve 'proper array of floats' 错误

linux - 如何删除 root 拥有的名称中包含空格的文件?