注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

setuid()与setgid()的使用 (中)  

2013-09-02 15:16:43|  分类: Linux内核 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
实例1

#include <stdio.h>
void show_ids(){
printf("real uid.uid= %d\n",getuid());
printf("effect uid.euid= %d\n",geteuid());
}
int main(int argc,char * argv[])
{
  printf("at the begin\n");
  show_ids();
int uid=atoi(argv[1]);
printf("try to set uid=%d \n",uid);
if(setuid(uid)) {
show_ids();
printf("su: permission denied\n");
return 1;
}
printf("at the end\n");
show_ids();
  return 0;
}

编译运行
root@ubuntu:/home/robin/share/test-su# gcc test2.c -o test2.exe
root@ubuntu:/home/robin/share/test-su# ./test2.exe 1234
at the begin
real uid.uid= 0
effect uid.euid= 0
try to set uid=1234 
at the end
real uid.uid= 1234
effect uid.euid= 1234
root@ubuntu:/home/robin/share/test-su# exit
robin@ubuntu:~/share/test-su$ ./test2.exe 1234
at the begin
real uid.uid= 1000
effect uid.euid= 1000
try to set uid=1234 
real uid.uid= 1000
effect uid.euid= 1000
su: permission denied
robin@ubuntu:~/share/test-su$ gcc test2.c -o test2.exe
robin@ubuntu:~/share/test-su$ ./test2.exe 1234
at the begin
real uid.uid= 1000
effect uid.euid= 1000
try to set uid=1234 
real uid.uid= 1000
effect uid.euid= 1000
su: permission denied
robin@ubuntu:~/share/test-su$ su
密码: 
root@ubuntu:/home/robin/share/test-su# chmod 6777 test2.exe
root@ubuntu:/home/robin/share/test-su# exit
robin@ubuntu:~/share/test-su$ ./test2.exe 1234
at the begin
real uid.uid= 1000
effect uid.euid= 1000
try to set uid=1234 
real uid.uid= 1000
effect uid.euid= 1000
su: permission denied
robin@ubuntu:~/share/test-su$ 
robin@ubuntu:~/share/test-su$ ls -l
-rwxr--r-- 1 robin robin  426 2013-08-29 16:13 test2.c
-rwsrwsrwx 1 robin robin 8789 2013-08-29 16:16 test2.exe
robin@ubuntu:~/share/test-su$ su
密码: 
root@ubuntu:/home/robin/share/test-su# ./test2.exe 1234
at the begin
real uid.uid= 0
effect uid.euid= 1000
try to set uid=1234 
real uid.uid= 0
effect uid.euid= 1000
su: permission denied
实例2
代码同实例1
编译运行
root@ubuntu:/home/robin/share/test-su# gcc test2.c -o test2.exe
root@ubuntu:/home/robin/share/test-su# ls -l
-rwxr--r-- 1 robin robin  426 2013-08-29 16:13 test2.c
-rwxr-xr-x 1 root  root  8789 2013-08-29 16:58 test2.exe
root@ubuntu:/home/robin/share/test-su# ./test2.exe 1234
at the begin
real uid.uid= 0
effect uid.euid= 0
try to set uid=1234 
at the end
real uid.uid= 1234
effect uid.euid= 1234
root@ubuntu:/home/robin/share/test-su# exit
robin@ubuntu:~/share/test-su$ ./test2.exe 1234
at the begin
real uid.uid= 1000
effect uid.euid= 1000
try to set uid=1234 
real uid.uid= 1000
effect uid.euid= 1000
su: permission denied
robin@ubuntu:~/share/test-su$ su
密码: 
root@ubuntu:/home/robin/share/test-su# chmod 6777 test2.exe
root@ubuntu:/home/robin/share/test-su# ls -l
-rwxr--r-- 1 robin robin  426 2013-08-29 16:13 test2.c
-rwsrwsrwx 1 root  root  8789 2013-08-29 16:58 test2.exe
root@ubuntu:/home/robin/share/test-su# exit
robin@ubuntu:~/share/test-su$ ./test2.exe 1234
at the begin
real uid.uid= 1000
effect uid.euid= 0
try to set uid=1234 
at the end
real uid.uid= 1234
effect uid.euid= 1234
结束!

  评论这张
 
阅读(628)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017