gpt4 book ai didi

linux - 一旦执行,文件功能不会传输到进程

转载 作者:太空狗 更新时间:2023-10-29 11:07:39 26 4
gpt4 key购买 nike

我正在尝试编写一个需要提升功能的程序(而不是简单地使用 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

但是,当我运行该程序时,我得到以下输出(测试客户端是创建原始以太网套接字的可执行文件):

 $ ./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 key 进行了测试。 /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

工作正常。我很困惑。

最佳答案

我终于让这个工作了,多亏了在这里找到的信息:

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

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

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


对于 future 的读者:如果您遇到这个问题,请确保您的文件系统没有挂载为 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 的不同分区上。


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

关于linux - 一旦执行,文件功能不会传输到进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com