label_linux.go 4.9 KB

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