access函数

access函数是按照实际用户ID和实际组ID进行访问测试的。函数的定义如下: 

#include <unistd.h>  
int access(const char* pathname, int mode);  
//若成功返回0,若出错则返回-1.  

其中mode是下面所列常量的按位或。

实践: 

#include <unistd.h>  
#include <stdio.h>  
#include <fcntl.h>  
  
int main(void){  
        if(access("a",F_OK|R_OK|W_OK) < 0){  
                perror("access");  
        }  
  
        int fd = -1;  
        if((fd = open("a",O_RD))<0){  
                perror("open");  
                return -1;  
        }  
        printf("open ok!\n");    
        return 0;  
}  

运行结果: 

$ ll a

-rwsr--r-- 1 root root 0 Apr 24 23:49 a*

$ ./a.out

access: Permission denied

open: open ok!

使用yan用户运行执行access函数,因为a文件属于root,所以没有权限,因为access是使用实际用户ID和实际组ID进行测试的(???),但是可以使用open函数以读的方式打开,因为设置了SUID。  //不太懂,yan是其他用户,不应该也有读权限吗???,测试以后结果确实是这样的.注意:如果使用open函数以读写的方式打开,就会出现Permission denied,因为这样会有潜在的问题,如果用户在a中添加了恶意代码,但是执行a时还是具有root的权限,那就不好了。

posted @ 2015-06-24 10:16  neteasefans  阅读(266)  评论(0编辑  收藏  举报