label_selinux.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // +build selinux,linux
  2. package label
  3. import (
  4. "fmt"
  5. "os"
  6. "os/user"
  7. "strings"
  8. "github.com/opencontainers/selinux/go-selinux"
  9. )
  10. // Valid Label Options
  11. var validOptions = map[string]bool{
  12. "disable": true,
  13. "type": true,
  14. "filetype": true,
  15. "user": true,
  16. "role": true,
  17. "level": true,
  18. }
  19. var ErrIncompatibleLabel = fmt.Errorf("Bad SELinux option z and Z can not be used together")
  20. // InitLabels returns the process label and file labels to be used within
  21. // the container. A list of options can be passed into this function to alter
  22. // the labels. The labels returned will include a random MCS String, that is
  23. // guaranteed to be unique.
  24. func InitLabels(options []string) (plabel string, mlabel string, Err error) {
  25. if !selinux.GetEnabled() {
  26. return "", "", nil
  27. }
  28. processLabel, mountLabel := selinux.ContainerLabels()
  29. if processLabel != "" {
  30. defer func() {
  31. if Err != nil {
  32. ReleaseLabel(mountLabel)
  33. }
  34. }()
  35. pcon, err := selinux.NewContext(processLabel)
  36. if err != nil {
  37. return "", "", err
  38. }
  39. mcon, err := selinux.NewContext(mountLabel)
  40. if err != nil {
  41. return "", "", err
  42. }
  43. for _, opt := range options {
  44. if opt == "disable" {
  45. return "", mountLabel, nil
  46. }
  47. if i := strings.Index(opt, ":"); i == -1 {
  48. return "", "", fmt.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type, filetype' followed by ':' and a value", opt)
  49. }
  50. con := strings.SplitN(opt, ":", 2)
  51. if !validOptions[con[0]] {
  52. return "", "", fmt.Errorf("Bad label option %q, valid options 'disable, user, role, level, type, filetype'", con[0])
  53. }
  54. if con[0] == "filetype" {
  55. mcon["type"] = con[1]
  56. }
  57. pcon[con[0]] = con[1]
  58. if con[0] == "level" || con[0] == "user" {
  59. mcon[con[0]] = con[1]
  60. }
  61. }
  62. _ = ReleaseLabel(processLabel)
  63. processLabel = pcon.Get()
  64. mountLabel = mcon.Get()
  65. _ = ReserveLabel(processLabel)
  66. }
  67. return processLabel, mountLabel, nil
  68. }
  69. func ROMountLabel() string {
  70. return selinux.ROFileLabel()
  71. }
  72. // DEPRECATED: The GenLabels function is only to be used during the transition to the official API.
  73. func GenLabels(options string) (string, string, error) {
  74. return InitLabels(strings.Fields(options))
  75. }
  76. // FormatMountLabel returns a string to be used by the mount command.
  77. // The format of this string will be used to alter the labeling of the mountpoint.
  78. // The string returned is suitable to be used as the options field of the mount command.
  79. // If you need to have additional mount point options, you can pass them in as
  80. // the first parameter. Second parameter is the label that you wish to apply
  81. // to all content in the mount point.
  82. func FormatMountLabel(src, mountLabel string) string {
  83. if mountLabel != "" {
  84. switch src {
  85. case "":
  86. src = fmt.Sprintf("context=%q", mountLabel)
  87. default:
  88. src = fmt.Sprintf("%s,context=%q", src, mountLabel)
  89. }
  90. }
  91. return src
  92. }
  93. // SetProcessLabel takes a process label and tells the kernel to assign the
  94. // label to the next program executed by the current process.
  95. func SetProcessLabel(processLabel string) error {
  96. return selinux.SetExecLabel(processLabel)
  97. }
  98. // SetSocketLabel takes a process label and tells the kernel to assign the
  99. // label to the next socket that gets created
  100. func SetSocketLabel(processLabel string) error {
  101. return selinux.SetSocketLabel(processLabel)
  102. }
  103. // SocketLabel retrieves the current default socket label setting
  104. func SocketLabel() (string, error) {
  105. return selinux.SocketLabel()
  106. }
  107. // SetKeyLabel takes a process label and tells the kernel to assign the
  108. // label to the next kernel keyring that gets created
  109. func SetKeyLabel(processLabel string) error {
  110. return selinux.SetKeyLabel(processLabel)
  111. }
  112. // KeyLabel retrieves the current default kernel keyring label setting
  113. func KeyLabel() (string, error) {
  114. return selinux.KeyLabel()
  115. }
  116. // ProcessLabel returns the process label that the kernel will assign
  117. // to the next program executed by the current process. If "" is returned
  118. // this indicates that the default labeling will happen for the process.
  119. func ProcessLabel() (string, error) {
  120. return selinux.ExecLabel()
  121. }
  122. // FileLabel returns the label for specified path
  123. func FileLabel(path string) (string, error) {
  124. return selinux.FileLabel(path)
  125. }
  126. // SetFileLabel modifies the "path" label to the specified file label
  127. func SetFileLabel(path string, fileLabel string) error {
  128. if selinux.GetEnabled() && fileLabel != "" {
  129. return selinux.SetFileLabel(path, fileLabel)
  130. }
  131. return nil
  132. }
  133. // SetFileCreateLabel tells the kernel the label for all files to be created
  134. func SetFileCreateLabel(fileLabel string) error {
  135. if selinux.GetEnabled() {
  136. return selinux.SetFSCreateLabel(fileLabel)
  137. }
  138. return nil
  139. }
  140. // Relabel changes the label of path to the filelabel string.
  141. // It changes the MCS label to s0 if shared is true.
  142. // This will allow all containers to share the content.
  143. func Relabel(path string, fileLabel string, shared bool) error {
  144. if !selinux.GetEnabled() {
  145. return nil
  146. }
  147. if fileLabel == "" {
  148. return nil
  149. }
  150. exclude_paths := map[string]bool{
  151. "/": true,
  152. "/bin": true,
  153. "/boot": true,
  154. "/dev": true,
  155. "/etc": true,
  156. "/etc/passwd": true,
  157. "/etc/pki": true,
  158. "/etc/shadow": true,
  159. "/home": true,
  160. "/lib": true,
  161. "/lib64": true,
  162. "/media": true,
  163. "/opt": true,
  164. "/proc": true,
  165. "/root": true,
  166. "/run": true,
  167. "/sbin": true,
  168. "/srv": true,
  169. "/sys": true,
  170. "/tmp": true,
  171. "/usr": true,
  172. "/var": true,
  173. "/var/lib": true,
  174. "/var/log": true,
  175. }
  176. if home := os.Getenv("HOME"); home != "" {
  177. exclude_paths[home] = true
  178. }
  179. if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
  180. if usr, err := user.Lookup(sudoUser); err == nil {
  181. exclude_paths[usr.HomeDir] = true
  182. }
  183. }
  184. if path != "/" {
  185. path = strings.TrimSuffix(path, "/")
  186. }
  187. if exclude_paths[path] {
  188. return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
  189. }
  190. if shared {
  191. c, err := selinux.NewContext(fileLabel)
  192. if err != nil {
  193. return err
  194. }
  195. c["level"] = "s0"
  196. fileLabel = c.Get()
  197. }
  198. if err := selinux.Chcon(path, fileLabel, true); err != nil {
  199. return err
  200. }
  201. return nil
  202. }
  203. // PidLabel will return the label of the process running with the specified pid
  204. func PidLabel(pid int) (string, error) {
  205. return selinux.PidLabel(pid)
  206. }
  207. // Init initialises the labeling system
  208. func Init() {
  209. selinux.GetEnabled()
  210. }
  211. // ClearLabels will clear all reserved labels
  212. func ClearLabels() {
  213. selinux.ClearLabels()
  214. }
  215. // ReserveLabel will record the fact that the MCS label has already been used.
  216. // This will prevent InitLabels from using the MCS label in a newly created
  217. // container
  218. func ReserveLabel(label string) error {
  219. selinux.ReserveLabel(label)
  220. return nil
  221. }
  222. // ReleaseLabel will remove the reservation of the MCS label.
  223. // This will allow InitLabels to use the MCS label in a newly created
  224. // containers
  225. func ReleaseLabel(label string) error {
  226. selinux.ReleaseLabel(label)
  227. return nil
  228. }
  229. // DupSecOpt takes a process label and returns security options that
  230. // can be used to set duplicate labels on future container processes
  231. func DupSecOpt(src string) ([]string, error) {
  232. return selinux.DupSecOpt(src)
  233. }
  234. // DisableSecOpt returns a security opt that can disable labeling
  235. // support for future container processes
  236. func DisableSecOpt() []string {
  237. return selinux.DisableSecOpt()
  238. }
  239. // Validate checks that the label does not include unexpected options
  240. func Validate(label string) error {
  241. if strings.Contains(label, "z") && strings.Contains(label, "Z") {
  242. return ErrIncompatibleLabel
  243. }
  244. return nil
  245. }
  246. // RelabelNeeded checks whether the user requested a relabel
  247. func RelabelNeeded(label string) bool {
  248. return strings.Contains(label, "z") || strings.Contains(label, "Z")
  249. }
  250. // IsShared checks that the label includes a "shared" mark
  251. func IsShared(label string) bool {
  252. return strings.Contains(label, "z")
  253. }