k8s_safeopen_linux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package safepath
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. import (
  15. "context"
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "github.com/containerd/log"
  20. "github.com/docker/docker/internal/unix_noeintr"
  21. "golang.org/x/sys/unix"
  22. )
  23. // kubernetesSafeOpen open path formed by concatenation of the base directory
  24. // and its subpath and return its fd.
  25. // Symlinks are disallowed (pathname must already resolve symlinks) and the path
  26. // path must be within the base directory.
  27. // This is minimally modified code from https://github.com/kubernetes/kubernetes/blob/55fb1805a1217b91b36fa8fe8f2bf3a28af2454d/pkg/volume/util/subpath/subpath_linux.go#L530
  28. func kubernetesSafeOpen(base, subpath string) (int, error) {
  29. // syscall.Openat flags used to traverse directories not following symlinks
  30. const nofollowFlags = unix.O_RDONLY | unix.O_NOFOLLOW
  31. // flags for getting file descriptor without following the symlink
  32. const openFDFlags = unix.O_NOFOLLOW | unix.O_PATH
  33. pathname := filepath.Join(base, subpath)
  34. segments := strings.Split(subpath, string(filepath.Separator))
  35. // Assumption: base is the only directory that we have under control.
  36. // Base dir is not allowed to be a symlink.
  37. parentFD, err := unix_noeintr.Open(base, nofollowFlags|unix.O_CLOEXEC, 0)
  38. if err != nil {
  39. return -1, &ErrNotAccessible{Path: base, Cause: err}
  40. }
  41. defer func() {
  42. if parentFD != -1 {
  43. if err = unix_noeintr.Close(parentFD); err != nil {
  44. log.G(context.TODO()).Errorf("Closing FD %v failed for safeopen(%v): %v", parentFD, pathname, err)
  45. }
  46. }
  47. }()
  48. childFD := -1
  49. defer func() {
  50. if childFD != -1 {
  51. if err = unix_noeintr.Close(childFD); err != nil {
  52. log.G(context.TODO()).Errorf("Closing FD %v failed for safeopen(%v): %v", childFD, pathname, err)
  53. }
  54. }
  55. }()
  56. currentPath := base
  57. // Follow the segments one by one using openat() to make
  58. // sure the user cannot change already existing directories into symlinks.
  59. for _, seg := range segments {
  60. var deviceStat unix.Stat_t
  61. currentPath = filepath.Join(currentPath, seg)
  62. if !isLocalTo(currentPath, base) {
  63. return -1, &ErrEscapesBase{Base: currentPath, Subpath: seg}
  64. }
  65. // Trigger auto mount if it's an auto-mounted directory, ignore error if not a directory.
  66. // Notice the trailing slash is mandatory, see "automount" in openat(2) and open_by_handle_at(2).
  67. unix_noeintr.Fstatat(parentFD, seg+"/", &deviceStat, unix.AT_SYMLINK_NOFOLLOW)
  68. log.G(context.TODO()).Debugf("Opening path %s", currentPath)
  69. childFD, err = unix_noeintr.Openat(parentFD, seg, openFDFlags|unix.O_CLOEXEC, 0)
  70. if err != nil {
  71. return -1, &ErrNotAccessible{Path: currentPath, Cause: err}
  72. }
  73. err := unix_noeintr.Fstat(childFD, &deviceStat)
  74. if err != nil {
  75. return -1, fmt.Errorf("error running fstat on %s with %v", currentPath, err)
  76. }
  77. fileFmt := deviceStat.Mode & unix.S_IFMT
  78. if fileFmt == unix.S_IFLNK {
  79. return -1, fmt.Errorf("unexpected symlink found %s", currentPath)
  80. }
  81. // Close parentFD
  82. if err = unix_noeintr.Close(parentFD); err != nil {
  83. return -1, fmt.Errorf("closing fd for %q failed: %v", filepath.Dir(currentPath), err)
  84. }
  85. // Set child to new parent
  86. parentFD = childFD
  87. childFD = -1
  88. }
  89. // We made it to the end, return this fd, don't close it
  90. finalFD := parentFD
  91. parentFD = -1
  92. return finalFD, nil
  93. }