label_selinux.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // +build selinux,linux
  2. package label
  3. import (
  4. "fmt"
  5. "strings"
  6. "github.com/opencontainers/selinux/go-selinux"
  7. )
  8. // Valid Label Options
  9. var validOptions = map[string]bool{
  10. "disable": true,
  11. "type": true,
  12. "user": true,
  13. "role": true,
  14. "level": true,
  15. }
  16. var ErrIncompatibleLabel = fmt.Errorf("Bad SELinux option z and Z can not be used together")
  17. // InitLabels returns the process label and file labels to be used within
  18. // the container. A list of options can be passed into this function to alter
  19. // the labels. The labels returned will include a random MCS String, that is
  20. // guaranteed to be unique.
  21. func InitLabels(options []string) (string, string, error) {
  22. if !selinux.GetEnabled() {
  23. return "", "", nil
  24. }
  25. processLabel, mountLabel := selinux.ContainerLabels()
  26. if processLabel != "" {
  27. pcon := selinux.NewContext(processLabel)
  28. mcon := selinux.NewContext(mountLabel)
  29. for _, opt := range options {
  30. if opt == "disable" {
  31. return "", "", nil
  32. }
  33. if i := strings.Index(opt, ":"); i == -1 {
  34. return "", "", fmt.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type' followed by ':' and a value", opt)
  35. }
  36. con := strings.SplitN(opt, ":", 2)
  37. if !validOptions[con[0]] {
  38. return "", "", fmt.Errorf("Bad label option %q, valid options 'disable, user, role, level, type'", con[0])
  39. }
  40. pcon[con[0]] = con[1]
  41. if con[0] == "level" || con[0] == "user" {
  42. mcon[con[0]] = con[1]
  43. }
  44. }
  45. _ = ReleaseLabel(processLabel)
  46. processLabel = pcon.Get()
  47. mountLabel = mcon.Get()
  48. _ = ReserveLabel(processLabel)
  49. }
  50. return processLabel, mountLabel, nil
  51. }
  52. func ROMountLabel() string {
  53. return selinux.ROFileLabel()
  54. }
  55. // DEPRECATED: The GenLabels function is only to be used during the transition to the official API.
  56. func GenLabels(options string) (string, string, error) {
  57. return InitLabels(strings.Fields(options))
  58. }
  59. // FormatMountLabel returns a string to be used by the mount command.
  60. // The format of this string will be used to alter the labeling of the mountpoint.
  61. // The string returned is suitable to be used as the options field of the mount command.
  62. // If you need to have additional mount point options, you can pass them in as
  63. // the first parameter. Second parameter is the label that you wish to apply
  64. // to all content in the mount point.
  65. func FormatMountLabel(src, mountLabel string) string {
  66. if mountLabel != "" {
  67. switch src {
  68. case "":
  69. src = fmt.Sprintf("context=%q", mountLabel)
  70. default:
  71. src = fmt.Sprintf("%s,context=%q", src, mountLabel)
  72. }
  73. }
  74. return src
  75. }
  76. // SetProcessLabel takes a process label and tells the kernel to assign the
  77. // label to the next program executed by the current process.
  78. func SetProcessLabel(processLabel string) error {
  79. return selinux.SetExecLabel(processLabel)
  80. }
  81. // ProcessLabel returns the process label that the kernel will assign
  82. // to the next program executed by the current process. If "" is returned
  83. // this indicates that the default labeling will happen for the process.
  84. func ProcessLabel() (string, error) {
  85. return selinux.ExecLabel()
  86. }
  87. // GetFileLabel returns the label for specified path
  88. func FileLabel(path string) (string, error) {
  89. return selinux.FileLabel(path)
  90. }
  91. // SetFileLabel modifies the "path" label to the specified file label
  92. func SetFileLabel(path string, fileLabel string) error {
  93. if selinux.GetEnabled() && fileLabel != "" {
  94. return selinux.SetFileLabel(path, fileLabel)
  95. }
  96. return nil
  97. }
  98. // SetFileCreateLabel tells the kernel the label for all files to be created
  99. func SetFileCreateLabel(fileLabel string) error {
  100. if selinux.GetEnabled() {
  101. return selinux.SetFSCreateLabel(fileLabel)
  102. }
  103. return nil
  104. }
  105. // Relabel changes the label of path to the filelabel string.
  106. // It changes the MCS label to s0 if shared is true.
  107. // This will allow all containers to share the content.
  108. func Relabel(path string, fileLabel string, shared bool) error {
  109. if !selinux.GetEnabled() {
  110. return nil
  111. }
  112. if fileLabel == "" {
  113. return nil
  114. }
  115. exclude_paths := map[string]bool{"/": true, "/usr": true, "/etc": true, "/tmp": true, "/home": true, "/run": true, "/var": true, "/root": true}
  116. if exclude_paths[path] {
  117. return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
  118. }
  119. if shared {
  120. c := selinux.NewContext(fileLabel)
  121. c["level"] = "s0"
  122. fileLabel = c.Get()
  123. }
  124. if err := selinux.Chcon(path, fileLabel, true); err != nil {
  125. return err
  126. }
  127. return nil
  128. }
  129. // PidLabel will return the label of the process running with the specified pid
  130. func PidLabel(pid int) (string, error) {
  131. return selinux.PidLabel(pid)
  132. }
  133. // Init initialises the labeling system
  134. func Init() {
  135. selinux.GetEnabled()
  136. }
  137. // ReserveLabel will record the fact that the MCS label has already been used.
  138. // This will prevent InitLabels from using the MCS label in a newly created
  139. // container
  140. func ReserveLabel(label string) error {
  141. selinux.ReserveLabel(label)
  142. return nil
  143. }
  144. // ReleaseLabel will remove the reservation of the MCS label.
  145. // This will allow InitLabels to use the MCS label in a newly created
  146. // containers
  147. func ReleaseLabel(label string) error {
  148. selinux.ReleaseLabel(label)
  149. return nil
  150. }
  151. // DupSecOpt takes a process label and returns security options that
  152. // can be used to set duplicate labels on future container processes
  153. func DupSecOpt(src string) []string {
  154. return selinux.DupSecOpt(src)
  155. }
  156. // DisableSecOpt returns a security opt that can disable labeling
  157. // support for future container processes
  158. func DisableSecOpt() []string {
  159. return selinux.DisableSecOpt()
  160. }
  161. // Validate checks that the label does not include unexpected options
  162. func Validate(label string) error {
  163. if strings.Contains(label, "z") && strings.Contains(label, "Z") {
  164. return ErrIncompatibleLabel
  165. }
  166. return nil
  167. }
  168. // RelabelNeeded checks whether the user requested a relabel
  169. func RelabelNeeded(label string) bool {
  170. return strings.Contains(label, "z") || strings.Contains(label, "Z")
  171. }
  172. // IsShared checks that the label includes a "shared" mark
  173. func IsShared(label string) bool {
  174. return strings.Contains(label, "z")
  175. }