6

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

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

#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

工作正常。我很混乱。

4

2 回答 2

6

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

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 的不同分区上。


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

于 2016-08-18T21:12:40.750 回答
1

只是一个数据点:您的代码在较旧的 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)中所说,能力是每个线程的属性

于 2016-08-16T21:55:20.280 回答