label_selinux.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // +build selinux,linux
  2. package label
  3. import (
  4. "fmt"
  5. "github.com/dotcloud/docker/pkg/selinux"
  6. "strings"
  7. )
  8. func GenLabels(options string) (string, string, error) {
  9. processLabel, mountLabel := selinux.GetLxcContexts()
  10. if processLabel == "" { // SELinux is disabled
  11. return "", "", nil
  12. }
  13. var (
  14. err error
  15. s = strings.Fields(options)
  16. l = len(s)
  17. )
  18. if l > 0 {
  19. pcon := selinux.NewContext(processLabel)
  20. for i := 0; i < l; i++ {
  21. o := strings.Split(s[i], "=")
  22. pcon[o[0]] = o[1]
  23. }
  24. processLabel = pcon.Get()
  25. mountLabel, err = selinux.CopyLevel(processLabel, mountLabel)
  26. }
  27. return processLabel, mountLabel, err
  28. }
  29. func FormatMountLabel(src string, mountLabel string) string {
  30. if mountLabel != "" {
  31. switch src {
  32. case "":
  33. src = fmt.Sprintf("%s,context=%s", src, mountLabel)
  34. default:
  35. src = fmt.Sprintf("context=%s", mountLabel)
  36. }
  37. }
  38. return src
  39. }
  40. func SetProcessLabel(processLabel string) error {
  41. if selinux.SelinuxEnabled() {
  42. return selinux.Setexeccon(processLabel)
  43. }
  44. return nil
  45. }
  46. func GetProcessLabel() (string, error) {
  47. if selinux.SelinuxEnabled() {
  48. return selinux.Getexeccon()
  49. }
  50. return "", nil
  51. }
  52. func SetFileLabel(path string, fileLabel string) error {
  53. if selinux.SelinuxEnabled() && fileLabel != "" {
  54. return selinux.Setfilecon(path, fileLabel)
  55. }
  56. return nil
  57. }
  58. func GetPidCon(pid int) (string, error) {
  59. return selinux.Getpidcon(pid)
  60. }
  61. func Init() {
  62. selinux.SelinuxEnabled()
  63. }