《UNIX环境高级编程》笔记10--access函数

xiaoxiao2021-02-28  4

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

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

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

实践:

[java]  view plain  copy #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;   }   运行结果:

yan@yan-vm:~/apue$ ll a

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

yan@yan-vm:~/apue$ ./a.out

access: Permission denied

open: open ok!

使用yan运行执行access函数,因为a文件属于root,所以没有权限,因为access是使用实际用户ID和实际组ID进行测试的,但是

可以使用open函数以读的方式打开,因为设置了SUID。

注意:如果使用open函数以读写的方式打开,就会出现Permission denied,因为这样会有潜在的问题,如果用户在a中添加了恶意

代码,但是执行a时还是具有root的权限,那就不好了。

转载请注明原文地址: https://www.6miu.com/read-250262.html

最新回复(0)