file_util_posix: refactor CopyDirectory

Rearrange the loop so that we can use continue to go to the next
file. Also factor the copying bits of CopyFile so that CopyDirectory
can use them with files that are already open, so it can open/fstat
the files itself and be resilient to time-of-check/time-of-use
attacks (such as the attacker replacing a file that was just stated
but not yet opened with a pipe that it will block reading from).

Bug: 767138
Test: existing + new unit tests
Change-Id: I0c2164b0165850834323d860d1d390ac895737e9
Reviewed-on: https://chromium-review.googlesource.com/677563
Commit-Queue: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504813}

CrOS-Libchrome-Original-Commit: bca21fc58cb2e2275fc00e8eb5700047749f7fce
3 files changed
tree: 54b479ba6dc078e7f8e6b10aaf3f7b34083b451a
  1. base/
  2. build/
  3. components/
  4. dbus/
  5. device/
  6. ipc/
  7. mojo/
  8. testing/
  9. third_party/
  10. ui/