copy_windows.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. winio "github.com/Microsoft/go-winio"
  8. "github.com/docker/docker/pkg/idtools"
  9. "github.com/docker/docker/pkg/reexec"
  10. "github.com/docker/docker/pkg/system"
  11. "github.com/pkg/errors"
  12. "golang.org/x/sys/windows"
  13. )
  14. var pathBlacklist = map[string]bool{
  15. "c:\\": true,
  16. "c:\\windows": true,
  17. }
  18. func init() {
  19. reexec.Register("windows-fix-permissions", fixPermissionsReexec)
  20. }
  21. func fixPermissions(source, destination string, identity idtools.Identity, _ bool) error {
  22. if identity.SID == "" {
  23. return nil
  24. }
  25. cmd := reexec.Command("windows-fix-permissions", source, destination, identity.SID)
  26. output, err := cmd.CombinedOutput()
  27. return errors.Wrapf(err, "failed to exec windows-fix-permissions: %s", output)
  28. }
  29. func fixPermissionsReexec() {
  30. err := fixPermissionsWindows(os.Args[1], os.Args[2], os.Args[3])
  31. if err != nil {
  32. fmt.Fprint(os.Stderr, err)
  33. os.Exit(1)
  34. }
  35. }
  36. func fixPermissionsWindows(source, destination, SID string) error {
  37. privileges := []string{winio.SeRestorePrivilege, system.SeTakeOwnershipPrivilege}
  38. err := winio.EnableProcessPrivileges(privileges)
  39. if err != nil {
  40. return err
  41. }
  42. defer winio.DisableProcessPrivileges(privileges)
  43. sid, err := windows.StringToSid(SID)
  44. if err != nil {
  45. return err
  46. }
  47. // Owners on *nix have read/write/delete/read control and write DAC.
  48. // Add an ACE that grants this to the user/group specified with the
  49. // chown option. Currently Windows is not honoring the owner change,
  50. // however, they are aware of this and it should be fixed at some
  51. // point.
  52. sddlString := system.SddlAdministratorsLocalSystem
  53. sddlString += "(A;OICI;GRGWGXRCWDSD;;;" + SID + ")"
  54. securityDescriptor, err := windows.SecurityDescriptorFromString(sddlString)
  55. if err != nil {
  56. return err
  57. }
  58. dacl, _, err := securityDescriptor.DACL()
  59. if err != nil {
  60. return err
  61. }
  62. return windows.SetNamedSecurityInfo(destination, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION, sid, nil, dacl, nil)
  63. }
  64. func validateCopySourcePath(imageSource *imageMount, origPath, platform string) error {
  65. // validate windows paths from other images + LCOW
  66. if imageSource == nil || platform != "windows" {
  67. return nil
  68. }
  69. origPath = filepath.FromSlash(origPath)
  70. p := strings.ToLower(filepath.Clean(origPath))
  71. if !filepath.IsAbs(p) {
  72. if filepath.VolumeName(p) != "" {
  73. if p[len(p)-2:] == ":." { // case where clean returns weird c:. paths
  74. p = p[:len(p)-1]
  75. }
  76. p += "\\"
  77. } else {
  78. p = filepath.Join("c:\\", p)
  79. }
  80. }
  81. if _, blacklisted := pathBlacklist[p]; blacklisted {
  82. return errors.New("copy from c:\\ or c:\\windows is not allowed on windows")
  83. }
  84. return nil
  85. }