让设置 suid 的程序也产生 coredump

本文探讨了Linux系统中SUID程序产生core dump文件的不同模式。默认情况下,改变了凭证的进程不会产生core dump,但可通过调整fs.suid_dumpable参数启用三种模式:传统模式、调试模式及安全模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

被 suid 的程序要产生 coredump 文件,还需要额外的设置。

[url]http://www.linuxinsight.com/proc_sys_fs_suid_dumpable.html[/url]

[quote]
suid_dumpable
Submitted by admin on Thu, 2006-06-01 01:45

The value in this file determines whether core dump files are produced for set-user-ID or otherwise protected/tainted binaries. Three different integer values can be specified:

* 0 (default) - This provides the traditional behaviour. A core dump will not be produced for a process which has changed credentials (by calling seteuid(2), setgid(2), or similar, or by executing a set-user-ID or set-group-ID program) or whose binary does not have read permission enabled.
* 1 ("debug") - All processes dump core when possible. The core dump is owned by the file system user ID of the dumping process and no security is applied. This is intended for system debugging situations only. Ptrace is unchecked.
* 2 ("suidsafe") - Any binary which normally would not be dumped (see "0" above) is dumped readable by root only. This allows the user to remove the core dump file but not to read it. For security reasons core dumps in this mode will not overwrite one another or other files. This mode is appropriate when administrators are attempting to debug problems in a normal environment.
[/quote]
<think>我们已经在之前的回答中详细说明了如何永久修改core dump文件大小限制。现在用户的新问题是:如何测试core dump文件的生成机制?即如何模拟程序崩溃以生成core dump文件,从而验证配置是否生效。 根据用户提供的引用[1]和引用[2],我们了解到: 1. 当进程接收到某些信号(如SIGSEGV, SIGABRT等)时,会触发core dump生成。 2. 需要确保core dump功能已开启(通过ulimit -c检查)且路径可写。 因此,测试步骤应包括: 1. 确保已配置core dump生成(如通过ulimit -c unlimited)。 2. 编写一个会崩溃的程序(如触发段错误)或使用命令模拟崩溃。 3. 运行程序,检查是否在预期位置生成了core dump文件。 具体测试方法如下: ### 测试方法1:使用简单C程序触发段错误 ```c // 文件名:test_core.c #include <stdio.h> int main() { int *p = NULL; *p = 42; // 对空指针解引用,触发段错误 return 0; } ``` 编译并运行: ```bash gcc -g test_core.c -o test_core # 编译时加入调试信息 ./test_core # 运行程序,应产生段错误 ls -l ./core.* # 检查当前目录下的core文件(取决于core_pattern配置) ``` ### 测试方法2:使用Shell命令模拟崩溃 ```bash # 方法1:使用kill命令发送信号 sleep 100 & # 后台运行一个长时间进程 kill -SIGSEGV $! # 向该进程发送段错误信号 # 方法2:使用bash内置命令触发段错误 kill -SEGV $$ # 向当前shell进程发送信号(会终止当前shell) # 或者更安全的方式(在子shell中运行): ( kill -SEGV $$ ) # 在子shell中触发,避免影响当前shell ``` ### 测试方法3:使用Python脚本 ```python # 文件名:test_core.py import os import signal os.kill(os.getpid(), signal.SIGSEGV) # 向自身发送段错误信号 ``` 运行: ```bash python3 test_core.py ``` ### 验证core dump文件生成 - 检查core_pattern配置的目录(默认可能是当前目录或/var/lib/systemd/coredump) ```bash cat /proc/sys/kernel/core_pattern ``` - 使用coredumpctl工具(systemd系统): ```bash coredumpctl list # 列出所有core dump ``` ### 注意事项 1. 确保程序崩溃时的工作目录有写入权限(或core_pattern指定的目录)。 2. 如果使用`sudo`运行程序,core文件可能生成在root的家目录。 3. 测试前确保core dump已开启(`ulimit -c unlimited`在当前终端生效)。 ### 测试步骤总结 1. 配置core dump(如`ulimit -c unlimited`或通过永久配置)。 2. 编写或选择一种方法触发崩溃。 3. 运行程序,观察是否生成core文件。 4. 使用调试工具(如gdb)分析core文件,验证其有效性: ```bash gdb ./test_core core.pid # 使用gdb加载core文件 (gdb) bt # 查看堆栈回溯 ``` §§相关问题§§ 1. 如何分析core dump文件以定位程序崩溃原因? 2. 为什么有时候程序崩溃了却没有生成core dump文件? 3. 如何配置core dump文件名称包含进程名和时间戳? 4. 在多线程程序中,core dump会记录所有线程的信息吗?</think>### 测试 core dump 文件生成机制的方法 以下是验证 core dump 生成机制的完整测试流程,包含多种测试场景: #### 📝 测试准备 ```bash # 1. 确保 core dump 功能已启用 ulimit -c unlimited # 临时解除限制 echo "/tmp/core-%e-%p-%t" | sudo tee /proc/sys/kernel/core_pattern # 设置生成路径 # 2. 安装调试工具 sudo yum install gdb -y # CentOS sudo apt install gdb -y # Ubuntu ``` #### ⚙️ 测试方法 1:直接触发崩溃(推荐) ```bash # 创建测试程序 (C语言) cat << EOF > test_core.c #include <stdio.h> int main() { int *p = NULL; *p = 42; // 故意解引用空指针 return 0; } EOF # 编译并运行 gcc -g test_core.c -o test_core ./test_core # 应产生段错误 SIGSEGV # 验证 core dump ls -lh /tmp/core-test_core* # 检查生成文件 ``` #### ⚙️ 测试方法 2:使用 kill 命令模拟 ```bash # 启动后台进程 sleep 1000 & PID=$! # 发送崩溃信号 kill -SIGSEGV $PID # 模拟段错误 kill -SIGABRT $PID # 模拟断言失败 # 检查生成结果 coredumpctl list $PID # systemd 系统 ls /tmp/core-sleep* # 传统系统 ``` #### ⚙️ 测试方法 3:Python 脚本测试 ```python # test_core.py import os, signal os.kill(os.getpid(), signal.SIGSEGV) # 自毁信号 ``` ```bash python3 test_core.py ls /tmp/core-python* ``` #### 🔍 验证与分析 ```bash # 1. 检查文件生成 file /tmp/core-test_core* # 应显示 "ELF core dump" # 2. 使用 GDB 分析 gdb ./test_core /tmp/core-test_core* (gdb) bt # 查看调用栈 (gdb) info registers # 检查寄存器状态 ``` #### 🧪 测试场景矩阵 | 测试场景 | 触发方式 | 预期信号 | 验证方法 | |--------------------------|---------------------------|------------|------------------------| | 空指针解引用 | `*NULL = value` | SIGSEGV | GDB 查看崩溃地址 | | 除零错误 | `int x=1/0;` | SIGFPE | 检查算术异常 | | 非法指令 | `asm("ud2");` | SIGILL | 反汇编验证 | | 内存越界 | `buffer[1000]=0` (小数组) | SIGSEGV | AddressSanitizer 工具 | | 主动终止 | `abort()` | SIGABRT | 检查断言消息 | | 总线错误 | 未对齐内存访问 | SIGBUS | 架构相关测试 | #### ⚠️ 常见问题排查 1. **无 core 文件生成**: - 检查 `ulimit -c` 是否返回 `unlimited` - 确认 `cat /proc/sys/kernel/core_pattern` 路径可写 - 检查磁盘空间 `df -h /tmp` 2. **文件权限问题**: ```bash sudo sysctl -w kernel.core_uses_pid=1 # 添加 PID 到文件名 sudo sysctl -w fs.suid_dumpable=2 # 允许 setuid 程序生成 ``` 3. **容器环境测试**: ```bash docker run --ulimit core=-1 -it alpine sh -c "sleep 10 & kill -ABRT $!" ``` > 📌 测试提示:使用 `coredumpctl debug` 可直接调试最新生成的 core 文件[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值