在程序中需要执行root操作的地方调用seteuid函数获取root权限,在程序初始化时、执行完root操作之后再seteuid回普通ID--安全起见
然后把生成的程序设为root所有,并设置“set-user-id”位。
fisherman:/home/wangyao/tempcode# cat t_euid.c
#include
#include
#include
int main()
{
int oldeuid = geteuid();
printf("oldeuid=%d\n",oldeuid);
seteuid(0);
printf("seteuid(0)\neuid=%d\n",geteuid());
/*Do somthing*/
unlink("/root/a");
seteuid(oldeuid);
printf("uid=%d\n",geteuid());
return 0;
} |
fisherman:/home/wangyao/tempcode# gcc -o t_euid t_euid.c
fisherman:/home/wangyao/tempcode# chmod +s t_euid
fisherman:/home/wangyao/tempcode# touch /root/a
fisherman:/home/wangyao/tempcode# ll /root/a
-rw-r--r-- 1 root root 0 2007-10-12 17:01 /root/a
wangyao@fisherman:~/tempcode$ ll /root/a
-rw-r--r-- 1 root root 0 2007-10-12 17:01 /root/a
wangyao@fisherman:~/tempcode$ ./t_euid
oldeuid=0
seteuid(0)
euid=0
uid=0
wangyao@fisherman:~/tempcode$ ll /root/a
ls: /root/a: 没有那个文件或目录
阅读(1578) | 评论(0) | 转发(0) |