使用“find -exec”有哪些安全问题和竞争条件?

Dan*_*eny 14 shell security find

find手册页

-exec command ;
    There are unavoidable security problems
    surrounding use of the -exec action; you should use the
    -execdir option instead.

-execdir command {} +
    Like -exec, but the specified command is run from the
    subdirectory containing the matched file, which is not
    normally the directory in which you started find.  This a much
    more secure method for invoking commands, as it avoids race
    conditions during resolution of the paths to the matched
    files.
Run Code Online (Sandbox Code Playgroud)

这是什么意思?为什么从起始目录运行它会出现竞争条件?这些安全风险如何?

Dan*_*eny 13

这里找到详细信息:

-exec操作导致另一个程序运行。它将当时正在考虑的文件的名称传递给程序。被调用的程序通常会对该文件执行一些操作。再一次,这里存在可以利用的竞争条件。我们将作为一个具体的例子命令

 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
Run Code Online (Sandbox Code Playgroud)

在这个简单的例子中,我们只识别一个要删除的文件并调用 /bin/rm它来删除它。存在问题是因为在 find 决定它需要处理-exec操作的点与/bin/rm命令实际发出 unlink() 系统调用以从文件系统中删除文件的点之间存在时间间隔 。在这段时间内,攻击者可以重命名/tmp/umsp 目录,将其替换为指向/etc. 无法/bin/rm确定它正在处理 find 所考虑的同一个文件。一旦符号链接到位,攻击者就说服了 find 导致删除/etc/passwd文件,这不是实际调用的命令预期的效果。

不确定有人有多大可能利用它;但我想这就是答案!