label_selinux.go 4.9 KB

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