如何使用 open() 安全地设置标志?

How to securly set flags with open()?

本题中:

在评论中:

问题

If I create a file using 'S_IRUSR' as the mode bit in the first open(), then others call open() using ''O_RDONLY" as flag will not be able to read the file

回答

The same user will be able to read the file. And, just a technicality, it's not that the other users won't be able to read the file, it's that they won't be able to open the file

所以我问过打开(或创建)新文件时设置权限有什么意义,这就是我在这里问的原因。 还有提到:

They may, as an extreme example, have write access to the directory meaning they could rename your file, copy it to the original name, have access to that then delete your renamed one. Permissions do work but you have to ensure they're set up correctly (like securing the entire path leading to a file, not just the file itself.

所以我不明白"set flags correctly"是什么意思。而且还不明白权限设置的目的,当它不安全时。有人可以解释一下吗?

我会尽量简短地解释。如果您在文件系统中的某处执行 'ls -la' ,您会看到类似于下面的文件列表的内容。它显示了 Linux 和类似操作系统上文件系统权限的基础知识。 运行 'man chmod' 详细解释;您还应该看到 'man 3 chmod' 函数描述。

正确设置文件和目录的权限以及其他设置对于安全性至关重要。基本上,用户应该有最小的权限来完成他的合法任务。

drwx r-x r-x  2 root root    4096 Dec 14 23:55 .
drwx r-x r-x 24 root root    4096 Dec  5 19:02 ..
-rwx r-x r-x  1 root root 1113504 Jun  7  2019 file1
-rwx r-x r-x  1 root root  748968 Aug 29  2018 file2
-rwx r-x r-x  1 root root   34888 Jul  4  2019 file3
...
-rwx r-x r-x  1 root root   34888 Jul  4  2019 file_n
 ^^^ ^^^  ^^^ 
  |   |   |  
  |   |   +---------------------- permissions other users  
  |   +-------------------------- permissions for group members
  +------------------------------ permissions for owner
  d  - is a directory
  r  - read permission
  w  - write perrmision
  x  - execute permission

So I have asked what is the point of setting permission when opening (resp. creating) new file, and that's why I am asking here. There is also mentioned:

They may, as an extreme example, have write access to the directory meaning they could rename your file, copy it to the original name, have access to that then delete your renamed one. permissions do work but you have to ensure they're set up correctly (like securing the entire path leading to a file, not just the file itself.

引文中的评论部分不正确。确实,对目录具有写权限但对文件没有任何权限的人可以重命名文件或将其删除(in / from that directory,见下文)。确实,在扫清道路之后,同一个人可以使用其他人的原始名称创建一个他们可读的新文件。然而,un真实的是,这样的人可以复制原始文件,无论是原始名称还是任何其他名称,或者可以使用如此机动地阅读原文件。

这里的一个关键点是目录 D 中文件 F 的目录条目只是一种特殊文件中的数据:从名称到底层文件系统中条目的映射。该条目是 D 的一部分,而不是 F 的一部分,因此对其进行操作需要访问 D,不要 F。人们通常所说的文件名实际上根本不是该文件的一部分,也不一定是唯一的,因为同一个底层文件可以在多个地方链接到目录树,具有相同或不同的名称。

第二个关键点与第一个相关:文件的权限存在于文件系统中,而不是目录列表中,它们可以有效地控制对文件内容的访问。这实际上对于目录和其他类型的文件是一样的:用户需要对给定目录的读取权限才能读取其内容,并且需要对它的写入权限才能修改其内容。因此,文件的访问控制属性确实起到了有用的作用,需要适当地设置文件才能充分满足它们的预期目的

问题第一部分的答案是双重的:

  • 文件的访问控制属性是该文件的文件系统元数据的固有部分,因此无法避免在创建文件时将它们设置为某物
  • 当您打开一个尚不存在但您愿意创建的文件(名称)时,您不能延迟权限设置,因为系统将使用该文件的初始权限来检查打开新文件的权限-创建的文件。无法从请求的打开模式派生文件的适当权限。

第二部分的答案要简单得多:您必须在尝试打开文件时指定访问模式,因为这是 OS 强制执行访问控制的方式和时间.此外,您可以指定比允许的模式更严格的模式作为内部控制,以防止错误地对文件执行意外操作。

I do not understand what "set flags correcly" means.

评论对我来说似乎很清楚,尤其是考虑到它所附的示例,但如果将包含它的较大语句重写为 "you need to take these considerations into account, with respect not just to the file itself but also to every directory in the path to it, to ensure that file permissions correctly address your security goals".[=12=,您可能会更清楚]