label_selinux.go 1.5 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. var err error
  11. if processLabel == "" { // SELinux is disabled
  12. return "", "", err
  13. }
  14. s := strings.Fields(options)
  15. l := len(s)
  16. if l > 0 {
  17. pcon := selinux.NewContext(processLabel)
  18. for i := 0; i < l; i++ {
  19. o := strings.Split(s[i], "=")
  20. pcon[o[0]] = o[1]
  21. }
  22. processLabel = pcon.Get()
  23. mountLabel, err = selinux.CopyLevel(processLabel, mountLabel)
  24. }
  25. return processLabel, mountLabel, err
  26. }
  27. func FormatMountLabel(src string, MountLabel string) string {
  28. var mountLabel string
  29. if src != "" {
  30. mountLabel = src
  31. if MountLabel != "" {
  32. mountLabel = fmt.Sprintf("%s,context=\"%s\"", mountLabel, MountLabel)
  33. }
  34. } else {
  35. if MountLabel != "" {
  36. mountLabel = fmt.Sprintf("context=\"%s\"", MountLabel)
  37. }
  38. }
  39. return mountLabel
  40. }
  41. func SetProcessLabel(processLabel string) error {
  42. if selinux.SelinuxEnabled() {
  43. return selinux.Setexeccon(processLabel)
  44. }
  45. return nil
  46. }
  47. func GetProcessLabel() (string, error) {
  48. if selinux.SelinuxEnabled() {
  49. return selinux.Getexeccon()
  50. }
  51. return "", nil
  52. }
  53. func SetFileLabel(path string, fileLabel string) error {
  54. if selinux.SelinuxEnabled() && fileLabel != "" {
  55. return selinux.Setfilecon(path, fileLabel)
  56. }
  57. return nil
  58. }
  59. func GetPidCon(pid int) (string, error) {
  60. return selinux.Getpidcon(pid)
  61. }
  62. func Init() {
  63. selinux.SelinuxEnabled()
  64. }