1、特殊文件权限示例

[root@localhost test]# ls -ld /bin/passwd /tmp/

-rwsr-xr-x.  1 root root 27832 Jun 10  2014 /bin/passwd

drwxrwxrwt. 10 root root   247 Oct 28 22:17 /tmp/


2、特殊权限介绍:SUID、SGID、STICKY

安全上下文:

    进程以某用户的身份进行运行,进程是发起此进程用户的代理,因此以此用户的身份和权限完成所有操作。

权限的匹配模型:

    进程的属主,是否为被访问文件的属主,如果是,则应用属主权限;否则,判断进程的属主,是否属于访问文件的属组,如果是,则应用属组权限;否则应用其他用户权限;


SUID:

    默认情况下,用户发起的进程,进程的属主是其发起者;因此,其以发起者的身份运行。


SUID的功能:

    用户运行某程序时,如果此程序拥有SUID权限,那些程序运行为进程时,其进程属主不是发起者,而是程序文件自己的属主。

SGID的功能:

    当目录属组有写权限且有SGID权限时,那么所有属于此目录的属组且以属组身份在此目录中新建文件或目录时,此文件的属组不是用户的基本组,而是此目录的属组。

管理SUID权限:

    chmod u+s file    

    chmod u-s file

管理SGID权限:

    chmod g+s file

    chmod g-s file

SUID权限展示位置:在属主的执行权限位

    如果属主原本有执行权限,显示为小写“s”;

    如果属主原本没有执行权限,则显示为大写“S”

SUID权限展示位置:在属组的执行权限位

    如果属组原本有执行权限,显示为小写“s”;

    如果属组原本没有执行权限,则显示为大写“S”

[root@localhost test]# cp /bin/ls /tmp/test/sls

[root@localhost test]# ll sls 

-rwxr-xr-x. 1 root root 117656 Oct 29 21:50 sls

[root@localhost test]# chmod u+s sls

[root@localhost test]# ll sls 

-rwsr-xr-x. 1 root root 117656 Oct 29 21:50 sls

[root@localhost test]# su user

bash-4.2$ /tmp/test/sls -l sls

-rwsr-xr-x. 1 root root 117656 Oct 29 21:50 sls



[root@localhost tmp]# ls -ld test/

drwxrwxrwx. 2 user user 128 Oct 29 21:50 test/

[root@localhost tmp]# chmod g+s test/

[root@localhost tmp]# ll -d test/

drwxrwsrwx. 2 user user 128 Oct 29 21:50 test/

[root@localhost tmp]# su lily

bash-4.2$ touch file

bash-4.2$ ls -l  /tmp/test/file 

-rw-r--r--. 1 lily user 0 Oct 29 22:05 /tmp/test/file //有SUID权限


bash-4.2$ ls -ld  /tmp/test1/lily 

drwxr-xr-x. 2 lily lily 6 Oct 29 22:08 /tmp/test1/lily  //没有SUID权限

STICKY功能:

    对于属组或全局可写的目录,组内的所有用户或系统上的所有用户对在此目录中都能创建新文件或删除所有已有的文件;如果为此目录设置sticky权限,则每个用户都能创建新文件,且只能删除自己的文件。



注意:默认系统上的/tmp和/var/tmp目录默认均有sticky权限;

管理STICKY权限:

    chmod o+t file

    chmod o-t file


STICKY权限展示位置:在其他用户的执行权限位

    如果其他用户原本有执行权限,显示为小写“t”

    如果其他用户原本没有执行权限,则显示为大写“T”


[root@localhost tmp]# mkdir sticky

[root@localhost tmp]# chmod o+t sticky/

[root@localhost tmp]# ls sticky/ -ld

drwxrwxrwt. 2 root root 6 Oct 29 22:15 sticky/

[root@localhost tmp]# 

[root@localhost tmp]# su lily

bash-4.2$ cd /tmp/sticky

bash-4.2$ touch lily

bash-4.2$ cd /tmp/sticky1

bash-4.2$ touch  lily1

bash-4.2$ exit


[root@localhost tmp]# su lucy

[lucy@localhost tmp]$ cd /tmp/sticky

[lucy@localhost sticky]$ touch lucy

[lucy@localhost sticky]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:18 lily

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:21 lucy

[lucy@localhost sticky]$ rm lily 

rm: remove write-protected regular empty file ‘lily’? y

rm: cannot remove ‘lily’: Operation not permitted

[lucy@localhost sticky]$ 


[lucy@localhost sticky]$ cd /tmp/sticky1

[lucy@localhost sticky1]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:19 lily1

[lucy@localhost sticky1]$ touch lucy1

[lucy@localhost sticky1]$ ll

total 0

-rw-r--r--. 1 lily lily 0 Oct 29 22:19 lily1

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:23 lucy1

[lucy@localhost sticky1]$ rm lily1 

rm: remove write-protected regular empty file ‘lily1’? y

[lucy@localhost sticky1]$ ll

total 0

-rw-rw-r--. 1 lucy lucy 0 Oct 29 22:23 lucy1

[lucy@localhost sticky1]$ 












管理特殊权限的另一种方法:(八进制数字表示,0-7)

    SUID    SGID    STICKY

    2^2    2^1      2^0

    特殊权限位在最高位置展示,如:chmod 1777 file1

基于八进制方式赋权时,可于默认的三位八进制数字左侧在加一位八进制数字。

umask默认为四位。



3、facl:file access control lists,文件访问控制列表

文件的额外赋权机制:

    在原有的u、g、o之外,另一层让普通用户能控制赋权给另外的用户或组的赋权机制。


查看文件的facl:

    getfacl [-aceEsRLPtpndvh] file ...

    getfacl [-aceEsRLPtpndvh] -

如: 

[root@localhost tmp]# getfacl aaa

# file: aaa

# owner: root

# group: root

user::rw-

group::r--

other::r--


设置文件的facl:

    setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...

    setfacl --restore=file


    setfacl -m u:USERNAME:MODE FILE

    setfacl -m g:GROUPNAME:MODE FILE 

如:

[root@localhost test]# setfacl -m u:user:rwx a.dan 

[root@localhost test]# getfacl a.dan 

# file: a.dan

# owner: root

# group: root

user::rw-

user:user:rwx

group::r--

mask::rwx

other::r--

 


撤销文件的facl:

    setfacl -x u:USERNAME FILE  

    setfacl -x g:GROUPNAME FILE 

如:

[root@localhost test]# setfacl -x g:root b.dan 


如果文件设置了facl,用ls -l命令则会显现“+”

[root@localhost test]# ll

total 0

-rw-r--r--. 1 basher basher 0 Oct 28 21:28 abfstab.dan

-rw-rwxr--+ 1 root   root   0 Oct 28 21:28 a.dan



有了facl的安全上下文:

    先是检查属主,再检查属主facl

    先是检查属组,再检查属组facl

    然后在检查其他;