使用PR_GET_NO_NEW_PRIVS 避免安全漏洞

本文介绍Linux内核3.5之后引入的安全特性PR_SET_NO_NEW_PRIVS,该特性限制进程及其子进程获取额外权限,如setuid或chroot等。通过一个简单的测试程序示例,展示了如何设置及验证这一限制。

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


PR_SET_NO_NEW_PRIVS
当一个进程或其子进程设置了
PR_SET_NO_NEW_PRIVS 属性,
则其不能访问一些无法share的
操作,如setuid, 和chroot.
这是kernel 3.5 以后加的feature.
主要用于安全目的.
下面这个程序编译并执行后, 就无法使用 sudo su 命令,

http://lwn.net/Articles/478062/
Test it like this:

---- begin test case

#include <sys/prctl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>


#define PR_SET_NO_NEW_PRIVS 36
#define PR_GET_NO_NEW_PRIVS 37

int main()
{
  int nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
  if (nnp == -EINVAL) {
    printf("Failed!\n");
    return 1;
  }

  printf("nnp was %d\n", nnp);

  if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
    printf("Failed!\n");
    return 1;
  }

  nnp = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
  if (nnp == -EINVAL) {
    printf("Failed!\n");
    return 1;
  }

  printf("nnp is %d\n", nnp);

  printf("here goes...\n");
  execlp("bash", "bash", NULL);
  printf("Failed to exec bash\n");
  return 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值