核心文件大小限制对进程具有非确定性影响

Ram*_*Ram 14 c linux coredump

我正在运行自定义2.6.27内核,我只是注意到在段错误期间生成的核心文件大于为进程设置的硬核文件大小限制.

更令人讨厌的是,核心文件有时只会被截断(但不会受到ulimit设置的限制).

例如,这是我将尝试崩溃的程序:

int main(int argc, char **argv)
{
    // Get the hard and soft limit from command line
    struct rlimit new = {atoi(argv[1]), atoi(argv[1])};

    // Create some memory so as to beef up the core file size
    void *p = malloc(10 * 1024 * 1024);

    if (!p)
        return 1;

    if (setrlimit(RLIMIT_CORE, &new)) // Set the hard and soft limit
        return 2;                     // for core files produced by this
                                      // process

    while (1);

    free(p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是执行:

Linux# ./a.out 1446462 &    ## Set hard and soft limit to ~1.4 MB
[1] 14802
Linux# ./a.out 1446462 &
[2] 14803
Linux# ./a.out 1446462 &
[3] 14804
Linux# ./a.out 1446462 &
[4] 14807

Linux# cat /proc/14802/limits | grep core
Max core file size        1446462              1446462              bytes

Linux# killall -QUIT a.out

Linux# ls -l
total 15708
-rwxr-xr-x 1 root root     4624 Aug  1 18:28 a.out
-rw------- 1 root root 12013568 Aug  1 18:39 core.14802         <=== truncated core
-rw------- 1 root root 12017664 Aug  1 18:39 core.14803
-rw------- 1 root root 12013568 Aug  1 18:39 core.14804         <=== truncated core
-rw------- 1 root root 12017664 Aug  1 18:39 core.14807
[1]   Quit                    (core dumped) ./a.out 1446462
[2]   Quit                    (core dumped) ./a.out 1446462
[3]   Quit                    (core dumped) ./a.out 1446462
[4]   Quit                    (core dumped) ./a.out 1446462
Run Code Online (Sandbox Code Playgroud)

所以这里发生了多件事.我将每个进程的硬限制设置为大约1.4 MB.

  1. 生成的核心文件远远超过此设置限制.为什么?
  2. 产生的4个核心文件中的2个被截断,但只是4096字节.这里发生了什么?

我知道核心文件包含分配的完整堆栈和堆内存等.对于这样一个简单的程序(最多给出或占用几个字节),这不应该是非常恒定的,因此在多个实例之间产生一致的核心吗?

EDITS:

1 请求的输出du

Linux# du core.*
1428    core.14802
1428    core.14803
1428    core.14804
1428    core.14807

Linux# du -b core.*
12013568    core.14802
12017664    core.14803
12013568    core.14804
12017664    core.14807
Run Code Online (Sandbox Code Playgroud)

2memset()malloc()明确统治后 添加内容,因为核心文件现在全部被截断为1449984(仍3522超过限制的字节数).

那么为什么核心之前如此之大,它们包含了什么?无论是什么,它都没有受到过程的限制.

3新程序也显示了一些有趣的行为:

Linux# ./a.out 12017664 &
[1] 26586
Linux# ./a.out 12017664 &
[2] 26589
Linux# ./a.out 12017664 &
[3] 26590
Linux# ./a.out 12017663 &        ## 1 byte smaller
[4] 26653
Linux# ./a.out 12017663 &        ## 1 byte smaller
[5] 26666
Linux# ./a.out 12017663 &        ## 1 byte smaller
[6] 26667

Linux# killall -QUIT a.out

Linux# ls -l
total ..
-rwxr-xr-x 1 root root     4742 Aug  1 19:47 a.out
-rw------- 1 root root 12017664 Aug  1 19:47 core.26586
-rw------- 1 root root 12017664 Aug  1 19:47 core.26589
-rw------- 1 root root 12017664 Aug  1 19:47 core.26590
-rw------- 1 root root  1994752 Aug  1 19:47 core.26653           <== ???
-rw------- 1 root root  9875456 Aug  1 19:47 core.26666           <== ???
-rw------- 1 root root  9707520 Aug  1 19:47 core.26667           <== ???
[1]   Quit                    (core dumped) ./a.out 12017664
[2]   Quit                    (core dumped) ./a.out 12017664
[3]   Quit                    (core dumped) ./a.out 12017664
[4]   Quit                    (core dumped) ./a.out 12017663
[5]   Quit                    (core dumped) ./a.out 12017663
[6]   Quit                    (core dumped) ./a.out 12017663
Run Code Online (Sandbox Code Playgroud)

Pao*_*ini 7

核心倾销的实施可以在fs/binfmt_elf.c.我将遵循3.12及更高版本中的代码(它随着提交9b56d5438而改变)但逻辑非常相似.

代码最初决定转储VMA(虚拟内存区域)的数量vma_dump_size.对于诸如brk堆之类的匿名VMA ,它返回VMA的完整大小.在此步骤中,不涉及核心限制.

编写核心转储的第一阶段然后PT_LOAD为每个VMA 写入一个标头.这基本上是一个指针,指示在ELF文件的其余部分中查找数据的位置.实际数据由for循环写入,实际上是第二阶段.

在第二阶段,elf_core_dump重复调用get_dump_page以获取struct page必须转储的程序地址空间的每个页面的指针.get_dump_page是一个常见的实用功能mm/gup.c.评论get_dump_page是有帮助的:

 * Returns NULL on any kind of failure - a hole must then be inserted into
 * the corefile, to preserve alignment with its headers; and also returns
 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
 * allowing a hole to be left in the corefile to save diskspace.
Run Code Online (Sandbox Code Playgroud)

事实上,如果返回,则在(在你的内核中,在3.12+中)elf_core_dump调用一个函数.这个函数调用lseek在转储中留下一个洞(实际上因为这是它直接在指针上调用的内核). 主要的区别是,确实不遵守ulimit,而新的确实如此.fs/coredump.cdump_seekdump_skipget_dump_pageNULLfile->f_op->llseekstruct filedump_seekdump_skip

至于为什么第二个程序有奇怪的行为,可能是因为ASLR(地址空间随机化).截断哪个VMA取决于VMA的相对顺序,这是随机的.您可以尝试禁用它

echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
Run Code Online (Sandbox Code Playgroud)

并查看您的结果是否更均匀.要重新启用ASLR,请使用

echo 2 | sudo tee /proc/sys/kernel/randomize_va_space
Run Code Online (Sandbox Code Playgroud)