selinux.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package selinux
  2. import (
  3. "github.com/pkg/errors"
  4. )
  5. const (
  6. // Enforcing constant indicate SELinux is in enforcing mode
  7. Enforcing = 1
  8. // Permissive constant to indicate SELinux is in permissive mode
  9. Permissive = 0
  10. // Disabled constant to indicate SELinux is disabled
  11. Disabled = -1
  12. // DefaultCategoryRange is the upper bound on the category range
  13. DefaultCategoryRange = uint32(1024)
  14. )
  15. var (
  16. // ErrMCSAlreadyExists is returned when trying to allocate a duplicate MCS.
  17. ErrMCSAlreadyExists = errors.New("MCS label already exists")
  18. // ErrEmptyPath is returned when an empty path has been specified.
  19. ErrEmptyPath = errors.New("empty path")
  20. // InvalidLabel is returned when an invalid label is specified.
  21. InvalidLabel = errors.New("Invalid Label")
  22. // ErrIncomparable is returned two levels are not comparable
  23. ErrIncomparable = errors.New("incomparable levels")
  24. // ErrLevelSyntax is returned when a sensitivity or category do not have correct syntax in a level
  25. ErrLevelSyntax = errors.New("invalid level syntax")
  26. // CategoryRange allows the upper bound on the category range to be adjusted
  27. CategoryRange = DefaultCategoryRange
  28. )
  29. // Context is a representation of the SELinux label broken into 4 parts
  30. type Context map[string]string
  31. // SetDisabled disables SELinux support for the package
  32. func SetDisabled() {
  33. setDisabled()
  34. }
  35. // GetEnabled returns whether SELinux is currently enabled.
  36. func GetEnabled() bool {
  37. return getEnabled()
  38. }
  39. // ClassIndex returns the int index for an object class in the loaded policy,
  40. // or -1 and an error
  41. func ClassIndex(class string) (int, error) {
  42. return classIndex(class)
  43. }
  44. // SetFileLabel sets the SELinux label for this path or returns an error.
  45. func SetFileLabel(fpath string, label string) error {
  46. return setFileLabel(fpath, label)
  47. }
  48. // FileLabel returns the SELinux label for this path or returns an error.
  49. func FileLabel(fpath string) (string, error) {
  50. return fileLabel(fpath)
  51. }
  52. // SetFSCreateLabel tells kernel the label to create all file system objects
  53. // created by this task. Setting label="" to return to default.
  54. func SetFSCreateLabel(label string) error {
  55. return setFSCreateLabel(label)
  56. }
  57. // FSCreateLabel returns the default label the kernel which the kernel is using
  58. // for file system objects created by this task. "" indicates default.
  59. func FSCreateLabel() (string, error) {
  60. return fsCreateLabel()
  61. }
  62. // CurrentLabel returns the SELinux label of the current process thread, or an error.
  63. func CurrentLabel() (string, error) {
  64. return currentLabel()
  65. }
  66. // PidLabel returns the SELinux label of the given pid, or an error.
  67. func PidLabel(pid int) (string, error) {
  68. return pidLabel(pid)
  69. }
  70. // ExecLabel returns the SELinux label that the kernel will use for any programs
  71. // that are executed by the current process thread, or an error.
  72. func ExecLabel() (string, error) {
  73. return execLabel()
  74. }
  75. // CanonicalizeContext takes a context string and writes it to the kernel
  76. // the function then returns the context that the kernel will use. Use this
  77. // function to check if two contexts are equivalent
  78. func CanonicalizeContext(val string) (string, error) {
  79. return canonicalizeContext(val)
  80. }
  81. // ComputeCreateContext requests the type transition from source to target for
  82. // class from the kernel.
  83. func ComputeCreateContext(source string, target string, class string) (string, error) {
  84. return computeCreateContext(source, target, class)
  85. }
  86. // CalculateGlbLub computes the glb (greatest lower bound) and lub (least upper bound)
  87. // of a source and target range.
  88. // The glblub is calculated as the greater of the low sensitivities and
  89. // the lower of the high sensitivities and the and of each category bitset.
  90. func CalculateGlbLub(sourceRange, targetRange string) (string, error) {
  91. return calculateGlbLub(sourceRange, targetRange)
  92. }
  93. // SetExecLabel sets the SELinux label that the kernel will use for any programs
  94. // that are executed by the current process thread, or an error.
  95. func SetExecLabel(label string) error {
  96. return setExecLabel(label)
  97. }
  98. // SetTaskLabel sets the SELinux label for the current thread, or an error.
  99. // This requires the dyntransition permission.
  100. func SetTaskLabel(label string) error {
  101. return setTaskLabel(label)
  102. }
  103. // SetSocketLabel takes a process label and tells the kernel to assign the
  104. // label to the next socket that gets created
  105. func SetSocketLabel(label string) error {
  106. return setSocketLabel(label)
  107. }
  108. // SocketLabel retrieves the current socket label setting
  109. func SocketLabel() (string, error) {
  110. return socketLabel()
  111. }
  112. // PeerLabel retrieves the label of the client on the other side of a socket
  113. func PeerLabel(fd uintptr) (string, error) {
  114. return peerLabel(fd)
  115. }
  116. // SetKeyLabel takes a process label and tells the kernel to assign the
  117. // label to the next kernel keyring that gets created
  118. func SetKeyLabel(label string) error {
  119. return setKeyLabel(label)
  120. }
  121. // KeyLabel retrieves the current kernel keyring label setting
  122. func KeyLabel() (string, error) {
  123. return keyLabel()
  124. }
  125. // Get returns the Context as a string
  126. func (c Context) Get() string {
  127. return c.get()
  128. }
  129. // NewContext creates a new Context struct from the specified label
  130. func NewContext(label string) (Context, error) {
  131. return newContext(label)
  132. }
  133. // ClearLabels clears all reserved labels
  134. func ClearLabels() {
  135. clearLabels()
  136. }
  137. // ReserveLabel reserves the MLS/MCS level component of the specified label
  138. func ReserveLabel(label string) {
  139. reserveLabel(label)
  140. }
  141. // EnforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
  142. func EnforceMode() int {
  143. return enforceMode()
  144. }
  145. // SetEnforceMode sets the current SELinux mode Enforcing, Permissive.
  146. // Disabled is not valid, since this needs to be set at boot time.
  147. func SetEnforceMode(mode int) error {
  148. return setEnforceMode(mode)
  149. }
  150. // DefaultEnforceMode returns the systems default SELinux mode Enforcing,
  151. // Permissive or Disabled. Note this is is just the default at boot time.
  152. // EnforceMode tells you the systems current mode.
  153. func DefaultEnforceMode() int {
  154. return defaultEnforceMode()
  155. }
  156. // ReleaseLabel un-reserves the MLS/MCS Level field of the specified label,
  157. // allowing it to be used by another process.
  158. func ReleaseLabel(label string) {
  159. releaseLabel(label)
  160. }
  161. // ROFileLabel returns the specified SELinux readonly file label
  162. func ROFileLabel() string {
  163. return roFileLabel()
  164. }
  165. // KVMContainerLabels returns the default processLabel and mountLabel to be used
  166. // for kvm containers by the calling process.
  167. func KVMContainerLabels() (string, string) {
  168. return kvmContainerLabels()
  169. }
  170. // InitContainerLabels returns the default processLabel and file labels to be
  171. // used for containers running an init system like systemd by the calling process.
  172. func InitContainerLabels() (string, string) {
  173. return initContainerLabels()
  174. }
  175. // ContainerLabels returns an allocated processLabel and fileLabel to be used for
  176. // container labeling by the calling process.
  177. func ContainerLabels() (processLabel string, fileLabel string) {
  178. return containerLabels()
  179. }
  180. // SecurityCheckContext validates that the SELinux label is understood by the kernel
  181. func SecurityCheckContext(val string) error {
  182. return securityCheckContext(val)
  183. }
  184. // CopyLevel returns a label with the MLS/MCS level from src label replaced on
  185. // the dest label.
  186. func CopyLevel(src, dest string) (string, error) {
  187. return copyLevel(src, dest)
  188. }
  189. // Chcon changes the fpath file object to the SELinux label label.
  190. // If fpath is a directory and recurse is true, then Chcon walks the
  191. // directory tree setting the label.
  192. func Chcon(fpath string, label string, recurse bool) error {
  193. return chcon(fpath, label, recurse)
  194. }
  195. // DupSecOpt takes an SELinux process label and returns security options that
  196. // can be used to set the SELinux Type and Level for future container processes.
  197. func DupSecOpt(src string) ([]string, error) {
  198. return dupSecOpt(src)
  199. }
  200. // DisableSecOpt returns a security opt that can be used to disable SELinux
  201. // labeling support for future container processes.
  202. func DisableSecOpt() []string {
  203. return disableSecOpt()
  204. }