【问题标题】:File capabilities do not transfer to process once executed文件功能一旦执行就不会转移到进程
【发布时间】:2016-12-22 07:01:25
【问题描述】:

我正在尝试编写一个需要提升功能的程序(而不是简单地使用 sudo 运行它)。但是,我使用 setcap 设置的所有功能似乎都不会在执行后转移到进程中。此问题发生在多个可执行文件中并使用不同的功能。

此代码使用 cap_set_file() 将 CAP_NET_RAW 功能赋予作为 CLA 传递的文件。 (不要问我为什么需要这个。)

#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/capability.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#define handle_error(msg) \ 
    do { printf("%s: %s\n", msg, strerror(errno)); exit(EXIT_FAILURE); } while (0)

void print_cap_buf(cap_t cur) {
    char *buf;
    buf = cap_to_text(cur, NULL);
    printf("%s\n", buf);
    cap_free(buf);
}

void get_and_print_cap_buf() {
    cap_t cur = cap_get_proc();
    print_cap_buf(cur);
    cap_free(cur);
}

int main(int argc, char *argv[]) {
    cap_t file_cap;

    printf("Process capabilities: ");
    get_and_print_cap_buf(); // Print the current process capability list.

    file_cap = cap_from_text("cap_net_raw=ep");
    if (file_cap == NULL) handle_error("cap_from_text");

    printf("Capabilities to set in file: "); print_cap_buf(file_cap);

    if (argc == 2) {
        if ( cap_set_file(argv[1], file_cap) != 0) handle_error("cap_set_file");
    } else printf("No file specified.\n");

    cap_free(file_cap);
    return 0;
}

用gcc编译后:

gcc -Wall -pedantic -std=gnu99 test.c -o tt -lcap

我赋予它以下功能:

sudo setcap "cap_setfcap,cap_fowner,cap_net_raw=eip" tt 

使用getcap tt,输出为:

 $ getcap tt
tt = cap_fowner,cap_net_raw,cap_setfcap+eip

但是,当我运行程序时,我得到以下输出(test-client 是一个可执行文件,它创建一个原始以太网套接字):

 $ ./tt test-client
Process capabilities: =
Capabilities to set in file: = cap_net_raw+ep
cap_set_file: Operation not permitted

但是...当我使用 sudo 运行程序时,所有进程功能都可以正常运行。

 $ sudo ./tt test-client
Process capabilities: = cap_chown,cap_dac_override,cap_dac_read_search,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_linux_immutable,cap_net_bind_service,cap_net_broadcast,cap_net_admin,cap_net_raw,cap_ipc_lock,cap_ipc_owner,cap_sys_module,cap_sys_rawio,cap_sys_chroot,cap_sys_ptrace,cap_sys_pacct,cap_sys_admin,cap_sys_boot,cap_sys_nice,cap_sys_resource,cap_sys_time,cap_sys_tty_config,cap_mknod,cap_lease,cap_audit_write,cap_audit_control,cap_setfcap,cap_mac_override,cap_mac_admin,cap_syslog,cap_wake_alarm,cap_block_suspend,37+ep
Capabilities to set in file: = cap_net_raw+ep

并且目标文件“test-client”的功能设置正确。

但是,即使使用 CAP_NET_RAW,客户端在使用 EPERM 调用 socket() 时也会失败。我试过设置 CAP_NET_ADMIN 以防它也需要;同样的问题。我试过在上面的程序上使用 CAP_SETPCAP;没有骰子。我相当确定我已将其范围缩小到可执行文件的功能未进入运行进程的某些断开连接。

我在这里错过了什么?


编辑,第二天早上:

好的,所以我进行了更多测试,结果证明这段代码在 Raspberry Pi 上运行良好。我在我的主机上运行带有 LXTerminal 的 Lubuntu 16.04,这就是失败的那个。它在 LXTerminal 和纯文本 shell 中都失败了。也许这是一个操作系统错误?

Lubuntu 机器(cat /proc/version):

Linux version 4.4.0-34-generic (buildd@lgw01-20) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1) ) #53-Ubuntu SMP Wed Jul 27 16:06:39 UTC 2016

圆周率:

Linux version 4.4.11-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611) ) #888 SMP Mon May 23 20:10:33 BST 2016

再次编辑:--

在另一台机器上使用我安装的相同 USB 密钥进行测试。 /proc/version 略有不同:

Linux version 4.4.0-31-generic (buildd@lgw01-16) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1) ) #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016

工作正常。我很困惑。

【问题讨论】:

    标签: linux linux-capabilities


    【解决方案1】:

    多亏了这里的信息,我终于让它工作了:

    https://superuser.com/questions/865310/file-capabilities-setcap-not-being-applied-in-linux-mint-17-1

    事实证明,我的主目录被挂载为 nosuid,这会禁用所有功能标志。

    在没有 nosuid 的文件系统上运行程序时,它会按预期工作。


    对于未来的读者:如果您遇到此问题,请确保您的文件系统没有挂载为 nosuid。使用 mount 命令,检查与您存储数据的位置匹配的文件系统(在我的情况下为 /home/user),并查看是否设置了 nosuid 标志。

    $ mount
    ...
    /home/.ecryptfs/user/.Private on /home/user type ecryptfs (rw,nosuid,nodev,relatime,ecryptfs_fnek_sig=***,ecryptfs_sig=***,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_unlink_sigs)
    

    (这是一个 ecryptfs 系统,所以如果你在 Ubuntu 安装中选择“加密我的主目录”,你可能会遇到这个问题。我想不出一种方法来将它挂载为 suid,可能不会无论如何都想要。)

    我最终创建了一个新目录 /code(这是我的文件系统,我可以做我想做的事),它安装在没有 nosuid 的不同分区上。


    如果功能手册页引用了这一事实,那就太好了...(编辑:提交的补丁,现在可以了 :))

    【讨论】:

      【解决方案2】:

      只是一个数据点:您的代码可以在旧的 LTS 机器上运行:

      $ uname  -vr
      3.13.0-63-generic #103-Ubuntu SMP Fri Aug 14 21:42:59 UTC 2015
      
      $ ./tt test-client
      Process capabilities: = cap_fowner,cap_net_raw,cap_setfcap+ep
      Capabilities to set in file: = cap_net_raw+ep
      
      $ cat /etc/debian_version 
      jessie/sid
      

      也许这可能与用户进程的能力有关(调用./tt)?正如它在能力(7)中所说,能力是 每线程属性

      【讨论】:

        猜你喜欢
        • 2019-07-20
        • 2018-12-04
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        相关资源
        最近更新 更多