selinux_linux.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // +build linux
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package selinux
  15. import (
  16. selinux "github.com/opencontainers/selinux/go-selinux"
  17. )
  18. // SELinuxEnabled returns whether SELinux is enabled on the system. SELinux
  19. // has a tri-state:
  20. //
  21. // 1. disabled: SELinux Kernel modules not loaded, SELinux policy is not
  22. // checked during Kernel MAC checks
  23. // 2. enforcing: Enabled; SELinux policy violations are denied and logged
  24. // in the audit log
  25. // 3. permissive: Enabled, but SELinux policy violations are permitted and
  26. // logged in the audit log
  27. //
  28. // SELinuxEnabled returns true if SELinux is enforcing or permissive, and
  29. // false if it is disabled.
  30. func SELinuxEnabled() bool {
  31. return selinux.GetEnabled()
  32. }
  33. // realSELinuxRunner is the real implementation of SELinuxRunner interface for
  34. // Linux.
  35. type realSELinuxRunner struct{}
  36. var _ SELinuxRunner = &realSELinuxRunner{}
  37. func (_ *realSELinuxRunner) Getfilecon(path string) (string, error) {
  38. if !SELinuxEnabled() {
  39. return "", nil
  40. }
  41. return selinux.FileLabel(path)
  42. }
  43. // SetFileLabel applies the SELinux label on the path or returns an error.
  44. func SetFileLabel(path string, label string) error {
  45. return selinux.SetFileLabel(path, label)
  46. }