label_selinux.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. processLabel = pcon.Get()
  46. mountLabel = mcon.Get()
  47. }
  48. return processLabel, mountLabel, nil
  49. }
  50. func ROMountLabel() string {
  51. return selinux.ROFileLabel()
  52. }
  53. // DEPRECATED: The GenLabels function is only to be used during the transition to the official API.
  54. func GenLabels(options string) (string, string, error) {
  55. return InitLabels(strings.Fields(options))
  56. }
  57. // FormatMountLabel returns a string to be used by the mount command.
  58. // The format of this string will be used to alter the labeling of the mountpoint.
  59. // The string returned is suitable to be used as the options field of the mount command.
  60. // If you need to have additional mount point options, you can pass them in as
  61. // the first parameter. Second parameter is the label that you wish to apply
  62. // to all content in the mount point.
  63. func FormatMountLabel(src, mountLabel string) string {
  64. if mountLabel != "" {
  65. switch src {
  66. case "":
  67. src = fmt.Sprintf("context=%q", mountLabel)
  68. default:
  69. src = fmt.Sprintf("%s,context=%q", src, mountLabel)
  70. }
  71. }
  72. return src
  73. }
  74. // SetProcessLabel takes a process label and tells the kernel to assign the
  75. // label to the next program executed by the current process.
  76. func SetProcessLabel(processLabel string) error {
  77. if processLabel == "" {
  78. return nil
  79. }
  80. return selinux.SetExecLabel(processLabel)
  81. }
  82. // ProcessLabel returns the process label that the kernel will assign
  83. // to the next program executed by the current process. If "" is returned
  84. // this indicates that the default labeling will happen for the process.
  85. func ProcessLabel() (string, error) {
  86. return selinux.ExecLabel()
  87. }
  88. // GetFileLabel returns the label for specified path
  89. func FileLabel(path string) (string, error) {
  90. return selinux.FileLabel(path)
  91. }
  92. // SetFileLabel modifies the "path" label to the specified file label
  93. func SetFileLabel(path string, fileLabel string) error {
  94. if selinux.GetEnabled() && fileLabel != "" {
  95. return selinux.SetFileLabel(path, fileLabel)
  96. }
  97. return nil
  98. }
  99. // SetFileCreateLabel tells the kernel the label for all files to be created
  100. func SetFileCreateLabel(fileLabel string) error {
  101. if selinux.GetEnabled() {
  102. return selinux.SetFSCreateLabel(fileLabel)
  103. }
  104. return nil
  105. }
  106. // Relabel changes the label of path to the filelabel string.
  107. // It changes the MCS label to s0 if shared is true.
  108. // This will allow all containers to share the content.
  109. func Relabel(path string, fileLabel string, shared bool) error {
  110. if !selinux.GetEnabled() {
  111. return nil
  112. }
  113. if fileLabel == "" {
  114. return nil
  115. }
  116. exclude_paths := map[string]bool{"/": true, "/usr": true, "/etc": true}
  117. if exclude_paths[path] {
  118. return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
  119. }
  120. if shared {
  121. c := selinux.NewContext(fileLabel)
  122. c["level"] = "s0"
  123. fileLabel = c.Get()
  124. }
  125. if err := selinux.Chcon(path, fileLabel, true); err != nil {
  126. return err
  127. }
  128. return nil
  129. }
  130. // PidLabel will return the label of the process running with the specified pid
  131. func PidLabel(pid int) (string, error) {
  132. return selinux.PidLabel(pid)
  133. }
  134. // Init initialises the labeling system
  135. func Init() {
  136. selinux.GetEnabled()
  137. }
  138. // ReserveLabel will record the fact that the MCS label has already been used.
  139. // This will prevent InitLabels from using the MCS label in a newly created
  140. // container
  141. func ReserveLabel(label string) error {
  142. selinux.ReserveLabel(label)
  143. return nil
  144. }
  145. // ReleaseLabel will remove the reservation of the MCS label.
  146. // This will allow InitLabels to use the MCS label in a newly created
  147. // containers
  148. func ReleaseLabel(label string) error {
  149. selinux.ReleaseLabel(label)
  150. return nil
  151. }
  152. // DupSecOpt takes a process label and returns security options that
  153. // can be used to set duplicate labels on future container processes
  154. func DupSecOpt(src string) []string {
  155. return selinux.DupSecOpt(src)
  156. }
  157. // DisableSecOpt returns a security opt that can disable labeling
  158. // support for future container processes
  159. func DisableSecOpt() []string {
  160. return selinux.DisableSecOpt()
  161. }
  162. // Validate checks that the label does not include unexpected options
  163. func Validate(label string) error {
  164. if strings.Contains(label, "z") && strings.Contains(label, "Z") {
  165. return ErrIncompatibleLabel
  166. }
  167. return nil
  168. }
  169. // RelabelNeeded checks whether the user requested a relabel
  170. func RelabelNeeded(label string) bool {
  171. return strings.Contains(label, "z") || strings.Contains(label, "Z")
  172. }
  173. // IsShared checks that the label includes a "shared" mark
  174. func IsShared(label string) bool {
  175. return strings.Contains(label, "z")
  176. }