label_linux.go 5.2 KB

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