capset capget 设置和获得进程权限

本文深入探讨了Linux环境下使用capset和capget进行进程权限管理的方法,包括宏定义、数据结构以及实际操作示例,强调了执行权限要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



capset 和capget 分别用来设置和获取进程权限:


       int capget(cap_user_header_t hdrp, cap_user_data_t datap);

       int capset(cap_user_header_t hdrp, const cap_user_data_t datap);

相关的宏和数据结构

 

           #define _LINUX_CAPABILITY_VERSION_1  0x19980330
           #define _LINUX_CAPABILITY_U32S_1     1

           #define _LINUX_CAPABILITY_VERSION_2  0x20071026
           #define _LINUX_CAPABILITY_U32S_2     2

           typedef struct __user_cap_header_struct {
              __u32 version;
              int pid;
           } *cap_user_header_t;

           typedef struct __user_cap_data_struct {
              __u32 effective;
              __u32 permitted;
              __u32 inheritable;
           } *cap_user_data_t;


例子:

#undef _POSIX_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/capability.h>
#include <errno.h>

int main()
{
    struct __user_cap_header_struct cap_header;

    struct __user_cap_data_struct   cap_data;

    cap_header.pid = getpid() ;
    cap_header.version = _LINUX_CAPABILITY_VERSION_1;

    if( capget(&cap_header, &cap_data) < 0)
    {
        printf("%s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    printf("capheader: %x  %d\n", cap_header.version, cap_header.pid);
    printf("capdata: %x  %x  %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);

    __u32 cap_mask  = 0;
    cap_mask |= (1 << CAP_NET_BIND_SERVICE);
    cap_data.effective = cap_mask;//类似于权限的集合
    cap_data.permitted = cap_mask;//0001000000
    cap_data.inheritable = 0;//子进程不继承特权

    if( capset(&cap_header, &cap_data) < 0)
    {
        printf("%s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    printf("%d\n", capget(&cap_header, &cap_data));
    printf("capheader: %x  %d\n", cap_header.version, cap_header.pid);
    printf("capdata: %x  %x  %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);
    return 0;
}


---》必须以root权限或者sudo才能执行:

普通用户:

capheader: 19980330  6092
capdata: 0  0  0
Operation not permitted


root:

capheader: 19980330  6098
capdata: ffffffff  ffffffff  0
0
capheader: 19980330  6098
capdata: 400  400  0



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值