label_selinux.go 3.9 KB

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