可执行文件加载与 Coredump

可执行文件加载与 Coredump

1. Exec 位于 VFS

exec.c 管理从 pathname/fd 加载新程序:

1
2
3
4
5
6
7
execve/execveat
-> do_execveat_common()
-> alloc_bprm()
-> bprm_execve()
-> prepare_binprm()
-> search_binary_handler()
-> linux_binfmt->load_binary()

成功 exec 替换当前进程地址空间、cred 和执行映像,而不是创建新进程。

2. linux_binprm

保存:

  • executable file;
  • filename/interpreter;
  • argv/env;
  • initial header buffer;
  • credentials;
  • stack limits;
  • recursion/interpreter state。

VFS permission、noexec mount、LSM、setuid/setgid/capability 都在加载阶段参与。

3. Binary format

linux_binfmt 通过 register_binfmt() 注册:

  • binfmt_elf.c
  • binfmt_script.c
  • binfmt_misc.c
  • binfmt_flat.c
  • ELF FDPIC(按配置)

search_binary_handler() 依次尝试匹配 handler。

4. ELF

ELF loader:

  • 校验 ELF header/program headers;
  • 打开 PT_INTERP 动态链接器;
  • 创建新 mm;
  • 映射 PT_LOAD;
  • 设置 brk、stack、auxv;
  • 处理 PIE/ASLR;
  • 最终 start_thread() 进入用户入口。

真实页通常在后续 page fault 时按需读入。

5. Script

#!interpreter optional-argbinfmt_script 解析,重写 argv 并让解释器成为下一执行文件。内核限制 shebang 长度/递归;用户输入路径仍需安全处理。

6. binfmt_misc

允许管理员按 magic/extension 注册用户态解释器,常用于 QEMU user emulation。配置接口本身具有高权限和代码执行风险。

7. Exec 并发

多线程 exec 需要:

  • 停止其他线程;
  • unshare files/sighand 等状态;
  • 关闭 CLOEXEC fd;
  • 处理 ptrace;
  • commit 新 cred;
  • 销毁旧 mm;
  • 保证失败发生在不可逆 point 前。

8. Coredump

coredump.c

1
2
3
4
5
6
fatal signal
-> do_coredump()
-> check dumpability/rlimit/pattern
-> open pipe/file target
-> binfmt->core_dump()
-> ELF core notes + VMAs/data

core_pattern 可指定文件名模板或 pipe helper。pipe helper 运行在高权限上下文,必须防命令注入和资源耗尽。

9. 安全与隐私

core 可能含密钥、用户数据和进程内存:

  • 配置 RLIMIT_CORE
  • 控制 dumpable;
  • 限制 core 文件权限和目录;
  • 容器考虑 namespace;
  • secretmem 等映射不应被普通 dump;
  • 上传前脱敏。

10. RK3588

嵌入式产品需在可诊断性和 flash 寿命间平衡。大型媒体进程 core 可能数 GiB,建议限额、压缩/流式收集、预留分区并防止写满系统盘。

文章互动

阅读 --

留言

0 条留言

正在加载留言…