c - 是否有一种机制可以操纵像 prctl() 这样的进程,但用于 forked/exec*()d 进程?

标签 c linux process

是否可以执行与 prctl(PR_SET_DUMPABLE, 0) 相同的操作,但对于其他进程,例如一个子进程,还是被 exec 执行的进程?下面的程序演示了可转储标志不是由 execv 的进程继承的。

#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv) {
    struct stat st;

    if (argc < 2) {
        fprintf(stderr, "usage: %s PROGRAM [ARGS...]\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (stat("/proc/self/fd", &st)) {
        perror("stat");
        return EXIT_FAILURE;
    }
    printf("before: owner of /proc/self/fd: %d\n", st.st_uid);

    if (prctl(PR_SET_DUMPABLE, 0) == -1) {
        perror("prctl");
        return EXIT_FAILURE;
    }

    if (stat("/proc/self/fd", &st)) {
        perror("stat");
        return EXIT_FAILURE;
    }
    printf("after: owner of /proc/self/fd: %d\n", st.st_uid);

    if (execv(argv[1], argv + 1) == -1) {
        perror("execv");
        return EXIT_FAILURE;
    }
}

$ ./a.out /usr/bin/stat -c 'in execed process: %u' /proc/self/fd
before: owner of /proc/self/fd: 1000
after: owner of /proc/self/fd: 0
in execed process: 1000

我想要这个的原因是我有一个程序作为普通用户运行并从 stdin 读取敏感数据(密码),我不希望其他人能够从 /读取这个过程数据过程。有其他替代方法吗?

最佳答案

I don't want others to be able to read this process data from /proc. Is there some alternative way to do this?

/proc 中per-process 子目录的内容通常由相应进程的有效uid 和有效gid 拥有。唯一documented不同的情况是进程的 dumpable 属性的值不同于 1。

The reason I want this is that I have a program which runs as a regular user and reads sensitive data (passwords) from stdin

如果有问题的程序以您可以选择的普通用户运行,那么最简单的做法就是选择一个具有专用主用户的专用用户团体。对于 /proc 访问的目的,这应该与将进程设置为不可转储或以 root 身份运行它一样好。也许更好,如果进程希望能够从它自己的 /proc 条目中读取。这也很自然。但是,它不满足关闭 dumpable 标志的主要目的——即防止进程转储核心。

如果程序本身在您的控制之下,那么您可以简单地修改它以发出适当的 prctl() 调用。我想这不是你的情况。

否则,程序无法修改且必须可由任意用户运行。根据the prctl() documentation ,除了调用 prctl() 之外,还有四种方法可以关闭进程的 dumpable 标志:

  • The process's effective user or group ID is changed.

  • The process's filesystem user or group ID is changed (see credentials(7)).

  • The process executes (execve(2)) a set-user-ID or set- group-ID program, resulting in a change of either the effective user ID or the effective group ID.

  • The process executes (execve(2)) a program that has file capabilities (see capabilities(7)), but only if the permitted capabilities gained exceed those already permitted for the process.

这些都描述了进程受访问控制的影响不同于由同一用户启动的普通进程的情况,这又回到了我最初的建议,即通过控制程序运行的用户来安排适当的访问控制.毕竟,谁在乎随机用户是否运行该程序并能够检索他们自己输入的 secret ?只有当其他 人被诱骗向程序泄露 secret 时,问题才会出现。但是,如果您正在考虑这种替代方案,那么您已经拒绝了简单的选择。

随着程序最终由非特权用户启动,更改其有效或文件系统凭据,即使通过包装程序,本身也不是一个可行的替代方案。然后,您需要为程序设置 SUID 或 SGID,或者为其分配您可以依赖的不同于用户正常功能的功能。请注意,SUID/SGID 目标,如果您这样做,则不需要是 root;它只需要与用户自己的不同。但是,这确实会再次返回到为程序运行指定的标识。

当然,capabilites 选项要求您的系统支持功能。如果没有,那么 SUID/SGID 是您唯一剩下的选择。这两者都由附加到文件系统中程序二进制文件的属性控制,因此它们不需要您修改程序本身。

关于c - 是否有一种机制可以操纵像 prctl() 这样的进程,但用于 forked/exec*()d 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54912342/

相关文章:

linux - Dns 不包含 `GetHostEntry' 的定义

Bash 获取在子 shell 中启动的进程的进程 ID

c - 进程 Hook 问题

c - fork 和流程管理

C 编程 - 乐透模拟

c - fork 和 exec 的区别

linux - 在 Fish Shell 中定义 Image Magick 的别名

linux - 将列表插入字符串

c - time_t now=time(NULL); 之间有区别吗?现在是time_t;是时候了);?

objective-c - 如何将 C 数组传递给 Objective-C 函数?