label_selinux.go 5.7 KB

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