scoped_file.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/files/scoped_file.h"
  5. #include "base/check.h"
  6. #include "build/build_config.h"
  7. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include "base/posix/eintr_wrapper.h"
  11. #endif
  12. namespace base {
  13. namespace internal {
  14. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  15. // static
  16. void ScopedFDCloseTraits::Free(int fd) {
  17. // It's important to crash here.
  18. // There are security implications to not closing a file descriptor
  19. // properly. As file descriptors are "capabilities", keeping them open
  20. // would make the current process keep access to a resource. Much of
  21. // Chrome relies on being able to "drop" such access.
  22. // It's especially problematic on Linux with the setuid sandbox, where
  23. // a single open directory would bypass the entire security model.
  24. int ret = IGNORE_EINTR(close(fd));
  25. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_APPLE) || \
  26. BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_ANDROID)
  27. // NB: Some file descriptors can return errors from close() e.g. network
  28. // filesystems such as NFS and Linux input devices. On Linux, macOS, and
  29. // Fuchsia's POSIX layer, errors from close other than EBADF do not indicate
  30. // failure to actually close the fd.
  31. if (ret != 0 && errno != EBADF)
  32. ret = 0;
  33. #endif
  34. PCHECK(0 == ret);
  35. }
  36. #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  37. } // namespace internal
  38. } // namespace base