应该在find命令中使用-execdir代替-exec

21 篇文章 2 订阅

    没事的时候读读 Linux 的 man 文档能学到不少新东西,注意到以前没注意过的细节。

    比如刚才在看 find 命令的文档时就发现了下面这 2 段话:

-exec command ;
Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the  command  until  an
argument  consisting  of  `;'  is  encountered.   The string `{}' is replaced by the current file name being processed everywhere it
occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.  Both  of  these  con‐
structions  might  need  to be escaped (with a `\') or quoted to protect them from expansion by the shell.  See the EXAMPLES section
for examples of the use of the -exec option.  The specified command is run once for each matched file.  The command is  executed  in
the  starting  directory.   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.
 As with the -exec action, the `+' form of -execdir will build a command line to process more than
one matched file, but any given invocation of command will only list files that exist in the same subdirectory.   If  you  use  this
option,  you  must  ensure  that your $PATH environment variable does not reference `.'; otherwise, an attacker can run any commands
they like by leaving an appropriately-named file in a directory in which you will run -execdir.  The same applies to having  entries
in $PATH which are empty or which are not absolute directory names.


    注意看红字。

    第 1 段话的意思是可以在 -exec 参数后接上花括号 {} 来表示每一个被找到的对象并执行操作,但每次执行操作的调用都是你执行 find 命令的目录,这可能导致竞态(find命令正在解析的路径和执行操作的命令使用的路径相同)的发生所以存在安全风险。因此应该使用 -execdir 参数替代 -exec。

    第 2 段话的意思是 -execdir 参数的作用和 -exec 相同,区别在于前者每次对被找到的对象执行操作时都是在这些对象所在的目录下执行的,因此可以避免竞态。

    同样的,也应该使用 -okdir 参数替代 -ok 参数使用。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值