Mat*_*t S 6 linux linux-capabilities
我正在尝试编写一个需要提升功能的程序(而不是简单地使用 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;
}
Run Code Online (Sandbox Code Playgroud)
用gcc编译后:
gcc -Wall -pedantic -std=gnu99 test.c -o tt -lcap
Run Code Online (Sandbox Code Playgroud)
我赋予它以下功能:
sudo setcap "cap_setfcap,cap_fowner,cap_net_raw=eip" tt
Run Code Online (Sandbox Code Playgroud)
并使用 getcap tt,输出为:
$ getcap tt
tt = cap_fowner,cap_net_raw,cap_setfcap+eip
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该程序时,我得到以下输出(test-client 是一个可执行文件,它创建一个原始以太网套接字):
$ ./tt test-client
Process capabilities: =
Capabilities to set in file: = cap_net_raw+ep
cap_set_file: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
但是......当我使用 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
Run Code Online (Sandbox Code Playgroud)
并且目标文件“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
Run Code Online (Sandbox Code Playgroud)
圆周率:
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
Run Code Online (Sandbox Code Playgroud)
再次编辑:--
使用我以前安装的相同 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
Run Code Online (Sandbox Code Playgroud)
工作正常。我很困惑。
多亏了在这里找到的信息,我终于让它起作用了:
https://superuser.com/questions/865310/file-capabilities-setcap-not-being-applied-in-linux-mint-17-1
在没有 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)
Run Code Online (Sandbox Code Playgroud)
(这是一个 ecryptfs 系统,所以如果你在 Ubuntu 安装中选择了“加密我的主目录”,你可能会遇到这个问题。我想不出将它挂载为 suid 的方法,而且可能无论如何都不想.)
我最终创建了一个新目录 /code(这是我的文件系统,我可以做我想做的事),它安装在没有 nosuid 的不同分区上。
如果功能的手册页引用了这个事实,那就太好了……(编辑:已提交补丁,现在可以了:))
| 归档时间: |
|
| 查看次数: |
1051 次 |
| 最近记录: |