label_selinux.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if processLabel == "" {
  80. return nil
  81. }
  82. return selinux.SetExecLabel(processLabel)
  83. }
  84. // ProcessLabel returns the process label that the kernel will assign
  85. // to the next program executed by the current process. If "" is returned
  86. // this indicates that the default labeling will happen for the process.
  87. func ProcessLabel() (string, error) {
  88. return selinux.ExecLabel()
  89. }
  90. // GetFileLabel returns the label for specified path
  91. func FileLabel(path string) (string, error) {
  92. return selinux.FileLabel(path)
  93. }
  94. // SetFileLabel modifies the "path" label to the specified file label
  95. func SetFileLabel(path string, fileLabel string) error {
  96. if selinux.GetEnabled() && fileLabel != "" {
  97. return selinux.SetFileLabel(path, fileLabel)
  98. }
  99. return nil
  100. }
  101. // SetFileCreateLabel tells the kernel the label for all files to be created
  102. func SetFileCreateLabel(fileLabel string) error {
  103. if selinux.GetEnabled() {
  104. return selinux.SetFSCreateLabel(fileLabel)
  105. }
  106. return nil
  107. }
  108. // Relabel changes the label of path to the filelabel string.
  109. // It changes the MCS label to s0 if shared is true.
  110. // This will allow all containers to share the content.
  111. func Relabel(path string, fileLabel string, shared bool) error {
  112. if !selinux.GetEnabled() {
  113. return nil
  114. }
  115. if fileLabel == "" {
  116. return nil
  117. }
  118. exclude_paths := map[string]bool{"/": true, "/usr": true, "/etc": true}
  119. if exclude_paths[path] {
  120. return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
  121. }
  122. if shared {
  123. c := selinux.NewContext(fileLabel)
  124. c["level"] = "s0"
  125. fileLabel = c.Get()
  126. }
  127. if err := selinux.Chcon(path, fileLabel, true); err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. // PidLabel will return the label of the process running with the specified pid
  133. func PidLabel(pid int) (string, error) {
  134. return selinux.PidLabel(pid)
  135. }
  136. // Init initialises the labeling system
  137. func Init() {
  138. selinux.GetEnabled()
  139. }
  140. // ReserveLabel will record the fact that the MCS label has already been used.
  141. // This will prevent InitLabels from using the MCS label in a newly created
  142. // container
  143. func ReserveLabel(label string) error {
  144. selinux.ReserveLabel(label)
  145. return nil
  146. }
  147. // ReleaseLabel will remove the reservation of the MCS label.
  148. // This will allow InitLabels to use the MCS label in a newly created
  149. // containers
  150. func ReleaseLabel(label string) error {
  151. selinux.ReleaseLabel(label)
  152. return nil
  153. }
  154. // DupSecOpt takes a process label and returns security options that
  155. // can be used to set duplicate labels on future container processes
  156. func DupSecOpt(src string) []string {
  157. return selinux.DupSecOpt(src)
  158. }
  159. // DisableSecOpt returns a security opt that can disable labeling
  160. // support for future container processes
  161. func DisableSecOpt() []string {
  162. return selinux.DisableSecOpt()
  163. }
  164. // Validate checks that the label does not include unexpected options
  165. func Validate(label string) error {
  166. if strings.Contains(label, "z") && strings.Contains(label, "Z") {
  167. return ErrIncompatibleLabel
  168. }
  169. return nil
  170. }
  171. // RelabelNeeded checks whether the user requested a relabel
  172. func RelabelNeeded(label string) bool {
  173. return strings.Contains(label, "z") || strings.Contains(label, "Z")
  174. }
  175. // IsShared checks that the label includes a "shared" mark
  176. func IsShared(label string) bool {
  177. return strings.Contains(label, "z")
  178. }