计算机系统安全2018【1】第三章 UNIX 访问控制

版权声明:本文为博主原创文章,未经博主允许不得转载。如果你非要转载,麻烦加上我的原网址,谢谢。http://blog.csdn.net/qinglingLS https://blog.csdn.net/qinglingLS/article/details/86625426

1、运行二进制文件和脚本文件时,如何设置权限?

二进制文件,需要执行权限。

脚本文件,需要读和执行权限

二进制可执行文件已经是机器语言,不需要再编译运行,
脚本文件需要先读取,看他的运行环境和内容,所以还需要一个读权限。

2、目录的执行位有何用途?

目录的执行位,能够遍历文件的索引节点,找到相应的文件

有目录的执行位,才能操作相应的文件或目录

有了目录的执行位,还可以删除或者新建文件,这些不需要文件的权限
只要有目录权限即可。

3、读/home/abc/aaa.txt文件所需权限?删除/home/abc/aaa.txt需要什么权限?

读/home/abc/aaa.txt文件:需要路径和文件的读权限,路径的执行权限。

删除/home/abc:
目录 / /home /abc 具有读和执行权限
目录 /abc 具有写权限

删除文件时,不需要文件本身权限,但是需要目录的写权限!!

4、用fork创建进程,和用exec执行文件的进程,其euid,suid,ruid分别有何差别?

Fork创建的进程,子进程和父进程的euid,suid,ruid相同

exec执行设置setuid位的程序,euid为root,ruid是登录用户id,suid为0或登录用户id
在这里插入图片描述

fork是复制原来的进程,复制一个一模一样的,当然就是一样的euid,suid和ruid。只有返回的pid不同
exec是运行一个新的线程。一般做这件事的人都是root,euid(有效uid)是root,也就是0

5、函数setuid含义在不同操作系统版本以及不同情况下结果不同,请问如何避免歧义?

Setuid函数中不同操作系统,不同版本下含义不同。
使用seteuid,setreuid,setresuid函数,含义清晰。

也可以使用封装函数drop_priv_temp,drop_priv_perm,restore_priv等。或者自己封装函数。
不使用setuid函数,这个函数是有歧义的,使用后euid与设想的不同,造成程序以高权限运行。

在这里插入图片描述
写实验的时候按照上图,写了一个代码,贴在下面。,
用途:转换程序uid

Uid_change_test.c:

#include<sys/unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define Err_sys(info)                            \
    {                                    \
        fprintf(stderr, "%s:%s\n", info, strerror(errno));        \
        exit(EXIT_FAILURE);                        \
    }
#define err_sys(info)                            \
    {                                    \
   fprintf(stderr, "pid==0:%s:%s\n", info, strerror(errno));        \
        exit(EXIT_FAILURE);                        \
    }
#define ERROR_SYSCALL -1;
/**
* temporarily drop the uid to new_uid,and save uid now;
* setresuid:set real, effective and saved user or group ID
* return :
* ERROR_SYSCALL means:fail
* 0 means:success
**/
int drop_priv_temp(uid_t new_uid)
{
	if(setresuid(-1,new_uid,geteuid())<0){
	return ERROR_SYSCALL; }
	//to verify the change
	if(geteuid()!=new_uid){
	return ERROR_SYSCALL; }
	return 0;
}
/**
* permanetly drop the uid to new_uid,never come back;
* setresuid:set real, effective and saved user or group ID
* return :
* ERROR_SYSCALL means:fail
* 0 means:success
**/
int drop_priv_perm(uid_t new_uid)
{
  int ruid,euid,suid;
	if(setresuid(new_uid,new_uid,new_uid)<0){
	return ERROR_SYSCALL; }
	//to verify the change
	if(getresuid(&ruid,&euid,&suid)<0){
	return ERROR_SYSCALL; }
	if(ruid != new_uid || euid != new_uid ||
	suid != new_uid){
	  return ERROR_SYSCALL;}
	return 0;
}
/**
* save uid
* return :
* ERROR_SYSCALL means:fail
* 0 means:success
**/
int restore_priv()
{
  int ruid,euid,suid;
	//to verify the change
	if(getresuid(&ruid,&euid,&suid)<0){
	  return ERROR_SYSCALL; }
	if(setresuid(-1,suid,-1) < 0){
	  return ERROR_SYSCALL;}
	if(geteuid()!=suid){
	  return ERROR_SYSCALL;}
	return 0;
}
char *env_init[]={ "USER=sakura", "PATH=/tmp", NULL};
int main(int argc,char* argv[])
{ pid_t pid;
  uid_t new_uid=1000;
  int ruid,euid,suid;
  if((pid=fork())<0)
    {Err_sys("fork error");}
  else if(pid == 0)  {  /*specify path, specify environment */
    if ( execle("/home/sakura/echoall", "echoall", "myarg1","MY ARG2", (char *) 0 , env_init) < 0)
         {  err_sys("fork error"); } 
  }
  if(waitpid(pid,NULL,0)<0)
      err_sys("wait error");
   if(getresuid(&ruid,&euid,&suid)<0){
        err_sys("wait error"); }
   printf("ruid:%d,euid:%d,suid:%d\n",ruid,euid,suid) ;   
   drop_priv_temp(new_uid);
   if(getresuid(&ruid,&euid,&suid)<0){
        err_sys("wait error"); }
   printf("ruid:%d,euid:%d,suid:%d\n",ruid,euid,suid) ;   
   restore_priv();
   if(getresuid(&ruid,&euid,&suid)<0){
   err_sys("wait error"); }
   printf("ruid:%d,euid:%d,suid:%d\n",ruid,euid,suid) ;    
   
  if((pid=fork())<0)
    {Err_sys("fork error");}
  else if (pid == 0)  {  /*specify path, specify environment */
    if( execlp("/home/sakura/echoall", "echoall", "only 1 args", (char *) 0 < 0))
        err_sys("fork error"); 
         } 
  exit(0);

}

Echoall.c
int main(int argc,char *argv[])
{   int i;
      char  **ptr;
      extern char **environ;
      
      for (i=0;i<argc; i++)   /* echo all command-line args*/
 printf("argv[%d]: %s\n", i, argv[i]);
for (ptr=environ; *ptr!=0;  ptr++)   /*  all env strings*/
printf("%s\n", *ptr);
exit(0);
}

6、文件、目录的权限设置

读文件,需要文件的读权限,层层目录的读和执行权限。
 运行可执行文件,需要文件的执行权限,层层目录的读和执行权限。
  脚本文件,运行时需要读和执行权限。 
  硬连接,删除时,需要找到文件的inode指针 ,对inode指针里的链接数进行修改,
  当硬连接数大于1的时候不能删除该文件。
  硬连接数为1的时候删除该文件。

1.write the file /d1/d2/f3
x on / and /d1
and /d2, w on f3
2. delete the file /d1/d2/f3
x and w on
/d1/d2, x on / and /d1
3. execute the file /d1/d2/f3, which is a binary file
x on f3 and / and
/d1 and /d1/d2,
4. execute the file /d1/d2/f3, which is a shell script(脚本)
r and x on
f3, x on
/ and /d1 and /d2
5.
list the file names under the
directory /d1/d2s
x on / and /d1
, r on /d2s
6. delete the directory /d1/d2, where the directory is empty
x on / and
/d1, w on /d1
7.
delete the directory /d1/d2, where /d1/d2 contains one file /d1/d2/f3
x on / and /d1, w
on /d1, w and x on /d2
8.
delete the directory /d1/d2, where /d1/d2 contains a subdirectory /d1/d2/d3, which contains one file /d1/d2/d3/f4
x on / , w and x on /d1, w and x on /d1/d2, w and x on
/d1/d2/d3
9.
create the directory /d1/d2/d3, when
/d1/d2 exists, and /d1/d2/d3 does not
x on / and
/d1, x and w and r on /d1/d2
10.
rename a file from /d1/d2/f3 to
/d1/d2/f4
改文件名,需要文件夹的w权限
x on / and
/d1, w and x on /d2
11.
create a hard link /d1/d2/f3, which
points to /d4/f5

/d2文件夹有写权限, f5文件有写权限

x on / and /d1
, x and w on /d2,

r and x on /d4, w on f5

remove /d1/d2/f3, which is a hard link
pointing to /d4/f5
回答:/d2有wx权限,f5有w权限
x on / and /d1
, w and x on /d1/d2 , 对目录d2要有写和执行权限
r and x on /d4,w on f5对文件有写权限才能修改连接数
13. create a
symbolic link /d1/d2/f3, which points to the directory /d4
Symbolic link 符号链接

回答:目录d2有wx权限,f3有rx权限

x on / and /d1 and /d2, w on /d2, r x on f3

14
read the file /d1/d2/f3/f5, where /d1/d2/f3 is
a symbolic link pointing to the directory /d4, and /d4 contains a file /d4/f5。读文件/d1/d2/f3/f5,/d1/d2/f3是符号链接执向目录/d4,/d4目录下包含文件d5

回答:符号链接文件f3有rx权限,/d4有x权限,f5有r权限

x on / and /d1
and /d2

rx on f3 , rx on /d4 ,
r on f5

15
delete the file /d1/d2/f3/f5, in the same
setting as above

回答:

x on / and /d1
and /d1/d2 and /d4 , w on f3 and /d4

猜你喜欢

转载自blog.csdn.net/qinglingLS/article/details/86625426