setuid(###)后跟getuid()在C程序中失败并设置了权限位

时间:2018-03-01 23:06:47

标签: c setuid

我正在尝试使用setuid非常简单的事情,然后在C程序中使用getuid,除非用户是c程序的所有者,否则该程序无效。除了thistuid之外,我还使用chmod 7777设置权限位。这是C程序......

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int x;

  printf("Setting uid to 1111\n");
  x=setuid(1111);
  printf("setuid returned... %d\n",x);
  printf("Testing state of uid using getuid()\n");
  printf("uid: %d\n",getuid());

  return 0;
}

鉴于1111是C程序所有者的uid(由id -u返回)。 在gcc之后,chmod 7777就生成了二进制文件。

作为所有者的测试工作正常...

% suid
Setting uid to 1111
setuid returned... 0
Testing state of uid using getuid()
uid: 1112

并检查二进制文件的权限......

ls -l suid
-rwsrwsrwx 1 katman design 6852 Mar  1 17:56 suid

然后我成为另一个用户(同一组BTW)并再次运行“suid”可执行文件......

% suid
Setting uid to 1111
setuid returned... 0
Testing state of uid using getuid()
uid: 57849

如果所有者授予了执行此权限的权限,为什么setuid不能正常工作?

也...

问:如果我可以使用它,我听说可以执行system()调用从这样的C程序运行cshell脚本,shell脚本应该作为C程序的所有者运行。这个uid被“传递”给了孩子们。对错吗?

问:如果cshell脚本运行一个perl脚本,而脚本又运行系统调用等等......所有这些后代shell都将作为启动的父C二进制文件的所有者运行整个级联吗? IOW,是孩子们“继承”的吗?

0 个答案:

没有答案
相关问题