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