ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux-文件功能一旦执行就不会转移到进程

2019-11-18 09:50:54  阅读:199  来源: 互联网

标签:linux-capabilities linux


我正在尝试编写需要提高功能的程序(而不是简单地使用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内部以及纯文本外壳中均失败.也许是操作系统错误?

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

PI:

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

工作正常.我很混乱.

解决方法:

多亏了以下信息,我终于可以使用它了:

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的另一个分区上.

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

标签:linux-capabilities,linux
来源: https://codeday.me/bug/20191118/2027016.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有