修改文件时,Linux 文件功能丢失。这是预期的行为吗?

Kes*_*pta 5 linux xattr capabilities

当我修改文件时,我之前设置的文件功能会丢失。这是预期的行为吗?

我首先设置了一个文件能力:

$ setcap CAP_NET_RAW+ep ./test.txt
$ getcap ./test.txt
./test.txt = cap_net_raw+ep
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,我发现文件功能已设置。

然后我修改文件。

$ echo hello >> ./test.txt
Run Code Online (Sandbox Code Playgroud)

现在,当我检查文件功能时,找不到任何功能。

$ getcap ./test.txt
Run Code Online (Sandbox Code Playgroud)

meu*_*euh 6

是的,这是预期的行为。我没有说明它的文件,但你可以在2007 年的这个补丁中看到

当具有 posix 功能的文件被覆盖时,应该删除文件功能,如 setuid 位。

此补丁引入了 security_inode_killpriv()。这目前仅针对功能定义,并在更改 inode 时调用以通知安全模块它可能想要清除附加到该 inode 的任何特权。能力模块检查是否为 inode 定义了任何文件能力,如果是,则清除它们。

security_inode_killpriv今天仍在内核中,notify_change 在“响应写入或截断”中更改 inode 时被调用 :请参阅dentry_needs_remove_privs

 /* Return mask of changes for notify_change() that need to be done as a
  * response to write or truncate... */
 int dentry_needs_remove_privs(struct dentry *dentry)
Run Code Online (Sandbox Code Playgroud)