label_selinux.go 6.0 KB

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