label_selinux.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // +build selinux,linux
  2. package label
  3. import (
  4. "fmt"
  5. "strings"
  6. "github.com/docker/libcontainer/selinux"
  7. )
  8. // InitLabels returns the process label and file labels to be used within
  9. // the container. A list of options can be passed into this function to alter
  10. // the labels. The labels returned will include a random MCS String, that is
  11. // guaranteed to be unique.
  12. func InitLabels(options []string) (string, string, error) {
  13. if !selinux.SelinuxEnabled() {
  14. return "", "", nil
  15. }
  16. processLabel, mountLabel := selinux.GetLxcContexts()
  17. if processLabel != "" {
  18. pcon := selinux.NewContext(processLabel)
  19. mcon := selinux.NewContext(mountLabel)
  20. for _, opt := range options {
  21. if opt == "disable" {
  22. return "", "", nil
  23. }
  24. if i := strings.Index(opt, ":"); i == -1 {
  25. return "", "", fmt.Errorf("Bad SELinux Option")
  26. }
  27. con := strings.SplitN(opt, ":", 2)
  28. pcon[con[0]] = con[1]
  29. if con[0] == "level" || con[0] == "user" {
  30. mcon[con[0]] = con[1]
  31. }
  32. }
  33. processLabel = pcon.Get()
  34. mountLabel = mcon.Get()
  35. }
  36. return processLabel, mountLabel, nil
  37. }
  38. // DEPRECATED: The GenLabels function is only to be used during the transition to the official API.
  39. func GenLabels(options string) (string, string, error) {
  40. return InitLabels(strings.Fields(options))
  41. }
  42. // FormatMountLabel returns a string to be used by the mount command.
  43. // The format of this string will be used to alter the labeling of the mountpoint.
  44. // The string returned is suitable to be used as the options field of the mount command.
  45. // If you need to have additional mount point options, you can pass them in as
  46. // the first parameter. Second parameter is the label that you wish to apply
  47. // to all content in the mount point.
  48. func FormatMountLabel(src, mountLabel string) string {
  49. if mountLabel != "" {
  50. switch src {
  51. case "":
  52. src = fmt.Sprintf("context=%q", mountLabel)
  53. default:
  54. src = fmt.Sprintf("%s,context=%q", src, mountLabel)
  55. }
  56. }
  57. return src
  58. }
  59. // SetProcessLabel takes a process label and tells the kernel to assign the
  60. // label to the next program executed by the current process.
  61. func SetProcessLabel(processLabel string) error {
  62. if processLabel == "" {
  63. return nil
  64. }
  65. return selinux.Setexeccon(processLabel)
  66. }
  67. // GetProcessLabel returns the process label that the kernel will assign
  68. // to the next program executed by the current process. If "" is returned
  69. // this indicates that the default labeling will happen for the process.
  70. func GetProcessLabel() (string, error) {
  71. return selinux.Getexeccon()
  72. }
  73. // SetFileLabel modifies the "path" label to the specified file label
  74. func SetFileLabel(path string, fileLabel string) error {
  75. if selinux.SelinuxEnabled() && fileLabel != "" {
  76. return selinux.Setfilecon(path, fileLabel)
  77. }
  78. return nil
  79. }
  80. // Change the label of path to the filelabel string. If the relabel string
  81. // is "z", relabel will change the MCS label to s0. This will allow all
  82. // containers to share the content. If the relabel string is a "Z" then
  83. // the MCS label should continue to be used. SELinux will use this field
  84. // to make sure the content can not be shared by other containes.
  85. func Relabel(path string, fileLabel string, relabel string) error {
  86. if fileLabel == "" {
  87. return nil
  88. }
  89. if relabel == "z" {
  90. c := selinux.NewContext(fileLabel)
  91. c["level"] = "s0"
  92. fileLabel = c.Get()
  93. }
  94. return selinux.Chcon(path, fileLabel, true)
  95. }
  96. // GetPidLabel will return the label of the process running with the specified pid
  97. func GetPidLabel(pid int) (string, error) {
  98. return selinux.Getpidcon(pid)
  99. }
  100. // Init initialises the labeling system
  101. func Init() {
  102. selinux.SelinuxEnabled()
  103. }
  104. // ReserveLabel will record the fact that the MCS label has already been used.
  105. // This will prevent InitLabels from using the MCS label in a newly created
  106. // container
  107. func ReserveLabel(label string) error {
  108. selinux.ReserveLabel(label)
  109. return nil
  110. }
  111. // UnreserveLabel will remove the reservation of the MCS label.
  112. // This will allow InitLabels to use the MCS label in a newly created
  113. // containers
  114. func UnreserveLabel(label string) error {
  115. selinux.FreeLxcContexts(label)
  116. return nil
  117. }
  118. // DupSecOpt takes an process label and returns security options that
  119. // can be used to set duplicate labels on future container processes
  120. func DupSecOpt(src string) []string {
  121. return selinux.DupSecOpt(src)
  122. }
  123. // DisableSecOpt returns a security opt that can disable labeling
  124. // support for future container processes
  125. func DisableSecOpt() []string {
  126. return selinux.DisableSecOpt()
  127. }