label_linux.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package label
  2. import (
  3. "os"
  4. "os/user"
  5. "strings"
  6. "github.com/opencontainers/selinux/go-selinux"
  7. "github.com/pkg/errors"
  8. )
  9. // Valid Label Options
  10. var validOptions = map[string]bool{
  11. "disable": true,
  12. "type": true,
  13. "filetype": true,
  14. "user": true,
  15. "role": true,
  16. "level": true,
  17. }
  18. var ErrIncompatibleLabel = errors.New("Bad SELinux option z and Z can not be used together")
  19. // InitLabels returns the process label and file labels to be used within
  20. // the container. A list of options can be passed into this function to alter
  21. // the labels. The labels returned will include a random MCS String, that is
  22. // guaranteed to be unique.
  23. // If the disabled flag is passed in, the process label will not be set, but the mount label will be set
  24. // to the container_file label with the maximum category. This label is not usable by any confined label.
  25. func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
  26. if !selinux.GetEnabled() {
  27. return "", "", nil
  28. }
  29. processLabel, mountLabel := selinux.ContainerLabels()
  30. if processLabel != "" {
  31. defer func() {
  32. if retErr != nil {
  33. selinux.ReleaseLabel(mountLabel)
  34. }
  35. }()
  36. pcon, err := selinux.NewContext(processLabel)
  37. if err != nil {
  38. return "", "", err
  39. }
  40. mcsLevel := pcon["level"]
  41. mcon, err := selinux.NewContext(mountLabel)
  42. if err != nil {
  43. return "", "", err
  44. }
  45. for _, opt := range options {
  46. if opt == "disable" {
  47. selinux.ReleaseLabel(mountLabel)
  48. return "", selinux.PrivContainerMountLabel(), nil
  49. }
  50. if i := strings.Index(opt, ":"); i == -1 {
  51. return "", "", errors.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type, filetype' followed by ':' and a value", opt)
  52. }
  53. con := strings.SplitN(opt, ":", 2)
  54. if !validOptions[con[0]] {
  55. return "", "", errors.Errorf("Bad label option %q, valid options 'disable, user, role, level, type, filetype'", con[0])
  56. }
  57. if con[0] == "filetype" {
  58. mcon["type"] = con[1]
  59. continue
  60. }
  61. pcon[con[0]] = con[1]
  62. if con[0] == "level" || con[0] == "user" {
  63. mcon[con[0]] = con[1]
  64. }
  65. }
  66. if pcon.Get() != processLabel {
  67. if pcon["level"] != mcsLevel {
  68. selinux.ReleaseLabel(processLabel)
  69. }
  70. processLabel = pcon.Get()
  71. selinux.ReserveLabel(processLabel)
  72. }
  73. mountLabel = mcon.Get()
  74. }
  75. return processLabel, mountLabel, nil
  76. }
  77. // Deprecated: The GenLabels function is only to be used during the transition
  78. // to the official API. Use InitLabels(strings.Fields(options)) instead.
  79. func GenLabels(options string) (string, string, error) {
  80. return InitLabels(strings.Fields(options))
  81. }
  82. // SetFileLabel modifies the "path" label to the specified file label
  83. func SetFileLabel(path string, fileLabel string) error {
  84. if !selinux.GetEnabled() || fileLabel == "" {
  85. return nil
  86. }
  87. return selinux.SetFileLabel(path, fileLabel)
  88. }
  89. // SetFileCreateLabel tells the kernel the label for all files to be created
  90. func SetFileCreateLabel(fileLabel string) error {
  91. if !selinux.GetEnabled() {
  92. return nil
  93. }
  94. return selinux.SetFSCreateLabel(fileLabel)
  95. }
  96. // Relabel changes the label of path to the filelabel string.
  97. // It changes the MCS label to s0 if shared is true.
  98. // This will allow all containers to share the content.
  99. func Relabel(path string, fileLabel string, shared bool) error {
  100. if !selinux.GetEnabled() || fileLabel == "" {
  101. return nil
  102. }
  103. exclude_paths := map[string]bool{
  104. "/": true,
  105. "/bin": true,
  106. "/boot": true,
  107. "/dev": true,
  108. "/etc": true,
  109. "/etc/passwd": true,
  110. "/etc/pki": true,
  111. "/etc/shadow": true,
  112. "/home": true,
  113. "/lib": true,
  114. "/lib64": true,
  115. "/media": true,
  116. "/opt": true,
  117. "/proc": true,
  118. "/root": true,
  119. "/run": true,
  120. "/sbin": true,
  121. "/srv": true,
  122. "/sys": true,
  123. "/tmp": true,
  124. "/usr": true,
  125. "/var": true,
  126. "/var/lib": true,
  127. "/var/log": true,
  128. }
  129. if home := os.Getenv("HOME"); home != "" {
  130. exclude_paths[home] = true
  131. }
  132. if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
  133. if usr, err := user.Lookup(sudoUser); err == nil {
  134. exclude_paths[usr.HomeDir] = true
  135. }
  136. }
  137. if path != "/" {
  138. path = strings.TrimSuffix(path, "/")
  139. }
  140. if exclude_paths[path] {
  141. return errors.Errorf("SELinux relabeling of %s is not allowed", path)
  142. }
  143. if shared {
  144. c, err := selinux.NewContext(fileLabel)
  145. if err != nil {
  146. return err
  147. }
  148. c["level"] = "s0"
  149. fileLabel = c.Get()
  150. }
  151. if err := selinux.Chcon(path, fileLabel, true); err != nil {
  152. return err
  153. }
  154. return nil
  155. }
  156. // DisableSecOpt returns a security opt that can disable labeling
  157. // support for future container processes
  158. // Deprecated: use selinux.DisableSecOpt
  159. var DisableSecOpt = selinux.DisableSecOpt
  160. // Validate checks that the label does not include unexpected options
  161. func Validate(label string) error {
  162. if strings.Contains(label, "z") && strings.Contains(label, "Z") {
  163. return ErrIncompatibleLabel
  164. }
  165. return nil
  166. }
  167. // RelabelNeeded checks whether the user requested a relabel
  168. func RelabelNeeded(label string) bool {
  169. return strings.Contains(label, "z") || strings.Contains(label, "Z")
  170. }
  171. // IsShared checks that the label includes a "shared" mark
  172. func IsShared(label string) bool {
  173. return strings.Contains(label, "z")
  174. }