DeniseJoe
作者DeniseJoe·2012-03-07 13:20
系统架构师·IBM

SUID,SGID和粘滞位

字数 2378阅读 3410评论 0赞 0

1. suid:如果一个文件有suid位,则执行这个文件的时候,系统将忽视当前用户的权限,而直接以文件本身的owner身份来执行,例如:passwd命令最终是需要修改/etc/passwd文件的,但是/etc/passwd文件是权限是非owner(即root用户)不可写的,事实上每个user都需要运用passwd命令修改自己的密码的,也就是说每个用户其实都需要修改/etc/passwd文件,这就产生了矛盾。为了解决这个问题,我们发现在/usr/bin/passwd可执行文件中设置suid 之后,就可以忽视当前的用户,而直接以/usr/bin/passwd文件本身的owner也就是root用户去执行了。

$ which passwd
/usr/bin/passwd
$ ls -l /usr/bin/passwd
-r-sr-xr-x    1 root     security      41534 Feb 02 2011  /usr/bin/passwd

$ ls -l /etc/passwd
-rw-r--r--    1 root     security      13205 Mar 07 13:33 /etc/passwd

1.2 设置suid 权限: chmod u+s dir1/file1;  取消suid权限: chmod u-s dir1/file1

2.  sgid: 如果一个文件夹设置了sgid,则无论是哪个用户在此文件夹下创建了文件,文件的所属组都跟文件夹的所属组一样。例如:在这里我在/dir目录下创建了文件a b,事实上这两个文件的所属组应该是chenwend,但是由于我设置了sgid,所以所属组任然是/dir1目录的所属组 root。

[root@linux6 /]# mkdir dir1
[root@linux6 /]# ls -ld /dir1
drwxr-xr-x. 2 root root 4096 Mar  7 13:01 /dir1
[root@linux6 /]# chmod g+s /dir1
[root@linux6 /]# chmod o+w /dir1
[root@linux6 /]# su - chenwend
[chenwend@linux6 ~]$ cd /dir1
[chenwend@linux6 dir1]$ touch a b
[chenwend@linux6 dir1]$ ls -l a b
-rw-rw-r--.  1 chenwend root    0 Mar  7 13:02 a
-rw-rw-r--.  1 chenwend root    0 Mar  7 13:02 b

[chenwend@linux6 dir1]$ id
uid=500(chenwend) gid=500(chenwend) groups=500(chenwend) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[chenwend@linux6 dir1]$ touch /tmp/a
[chenwend@linux6 dir1]$ ls -l /tmp/a
-rw-rw-r--. 1 chenwend chenwend 0 Mar  7 13:04 /tmp/a

2.2 设置sgid的:chmod g+s dir1;  取消sgid: chmod g-s dir1

3. 粘滞位sticky bit: 如果一个文件夹设置了粘滞位,则其中的文件只有文件的owner或root才能删除。粘滞位将会自动屏蔽文件夹的写入权限。例如在/tmp目录下面,默认这个目录是对所有人可读可写的,但是这里会出现一个风险就是,有了写权限,任何人可以删除属于任何人的任何文件,为了避免这个风险,可以使用粘滞位,例如:系统中/tmp文件夹对所有人具备所有权限. 但是在这里user1却不能删除文件a。

[chenwend@linux6 dir1]$ ls -ld /tmp
drwxrwxrwt. 16 root root 4096 Mar  7 13:04 /tmp

[chenwend@linux6 tmp]$ touch a b
[chenwend@linux6 tmp]$ ls -l a b
-rw-rw-r--. 1 chenwend chenwend 0 Mar  7 13:13 a
-rw-rw-r--. 1 chenwend chenwend 0 Mar  7 13:13 b
[chenwend@linux6 tmp]$ su - user1
Password:
[user1@linux6 ~]$ cd /tmp
[user1@linux6 tmp]$ rm a
rm: remove write-protected regular empty file `a'? y
rm: cannot remove `a': Operation not permitted
[user1@linux6 tmp]$

3.2 设置粘滞位: chmod o+t dir1;  取消粘滞位: chmod o-t dir1


 





 

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

X社区推广