chown删除了setuid位:bug还是功能?


14

重现步骤:

germar@host:~$ cd /tmp/
germar@host:/tmp$ touch test && chmod u+s test && ls -la test
-rwSr--r-- 1 germar germar 0 Nov  2 20:11 test
germar@host:/tmp$ chown germar:germar test && ls -la test
-rw-r--r-- 1 germar germar 0 Nov  2 20:11 test

经过Debian挤压和Ubuntu 12.04测试


在Fedora 17上也是如此。
— BenjiWiebe

Answers:


17

根据chown文档,这不是错误:

$ info coreutils 'chown invocation'

   The `chown' command sometimes clears the set-user-ID or set-group-ID
permission bits.  This behavior depends on the policy and functionality
of the underlying `chown' system call, which may make system-dependent
file mode modifications outside the control of the `chown' command.
For example, the `chown' command might not affect those bits when
invoked by a user with appropriate privileges, or when the bits signify
some function other than executable permission (e.g., mandatory
locking).  When in doubt, check the underlying system behavior.

谢谢jlliagre。我不知道info coreutils之前。我只阅读手册页并在网上搜索。
— Germar 2012年

12

这是设计使然,是标准行为。引用POSIX标准:

除非chown被具有适当特权的进程调用,否则在成功完成后,应清除常规文件的set-user-ID和set-group-ID位;其他文件类型的set-user-id和set-group-ID位可能会被清除。

(s顺便说一下,它是setuid(或group列中的setgid),而不是粘性的。)

此行为遵循基础系统调用的行为(除了在某些系统上,仅清除可执行文件的setxid位)。

删除setuid位的原因是,更改所有者还会更改哪个用户将成为该进程的有效用户ID。特别是,在用户可以放弃文件的系统上,cp /bin/sh foo; chmod u+s foo; chown joe foo将创建属于joe(一个巨大的安全漏洞)的setuid可执行文件。


为nitpick投票!SUID / SGID不是“粘性”位!
— 吉姆·丹尼斯

有关保留SUID / SGID的安全性的要点。直到我读完那句话,我一直为这种行为所困扰。我要补充一点,但是,即使以root身份运行,我也从未见过chown 无法清除这些位。我很好奇“适当的特权”意味着什么。
— greatlysuperiorman

1
@vastlysuperiorman在传统的Unix平台上,“适当的特权”表示用户ID0。但是POSIX允许系统定义自己的安全策略。例如,对于Linux上的许多操作,“适当的特权”被实现为一种功能(默认情况下,只有root才能获得)。在这种特殊情况下,无论特权如何,都会抑制chown上的setxid位,就像大多数(如果不是全部)Unix变体一样。但是Windows上的POSIX层可能会有所不同。
— 吉尔斯(Gilles)'所以别再邪恶了'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.