selinux.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package selinux
  2. import (
  3. "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. // maxCategory is the maximum number of categories used within containers
  13. maxCategory = 1024
  14. // DefaultCategoryRange is the upper bound on the category range
  15. DefaultCategoryRange = uint32(maxCategory)
  16. )
  17. var (
  18. // ErrMCSAlreadyExists is returned when trying to allocate a duplicate MCS.
  19. ErrMCSAlreadyExists = errors.New("MCS label already exists")
  20. // ErrEmptyPath is returned when an empty path has been specified.
  21. ErrEmptyPath = errors.New("empty path")
  22. // InvalidLabel is returned when an invalid label is specified.
  23. InvalidLabel = errors.New("Invalid Label")
  24. // ErrIncomparable is returned two levels are not comparable
  25. ErrIncomparable = errors.New("incomparable levels")
  26. // ErrLevelSyntax is returned when a sensitivity or category do not have correct syntax in a level
  27. ErrLevelSyntax = errors.New("invalid level syntax")
  28. // ErrContextMissing is returned if a requested context is not found in a file.
  29. ErrContextMissing = errors.New("context does not have a match")
  30. // ErrVerifierNil is returned when a context verifier function is nil.
  31. ErrVerifierNil = errors.New("verifier function is nil")
  32. // CategoryRange allows the upper bound on the category range to be adjusted
  33. CategoryRange = DefaultCategoryRange
  34. privContainerMountLabel string
  35. )
  36. // Context is a representation of the SELinux label broken into 4 parts
  37. type Context map[string]string
  38. // SetDisabled disables SELinux support for the package
  39. func SetDisabled() {
  40. setDisabled()
  41. }
  42. // GetEnabled returns whether SELinux is currently enabled.
  43. func GetEnabled() bool {
  44. return getEnabled()
  45. }
  46. // ClassIndex returns the int index for an object class in the loaded policy,
  47. // or -1 and an error
  48. func ClassIndex(class string) (int, error) {
  49. return classIndex(class)
  50. }
  51. // SetFileLabel sets the SELinux label for this path, following symlinks,
  52. // or returns an error.
  53. func SetFileLabel(fpath string, label string) error {
  54. return setFileLabel(fpath, label)
  55. }
  56. // LsetFileLabel sets the SELinux label for this path, not following symlinks,
  57. // or returns an error.
  58. func LsetFileLabel(fpath string, label string) error {
  59. return lSetFileLabel(fpath, label)
  60. }
  61. // FileLabel returns the SELinux label for this path, following symlinks,
  62. // or returns an error.
  63. func FileLabel(fpath string) (string, error) {
  64. return fileLabel(fpath)
  65. }
  66. // LfileLabel returns the SELinux label for this path, not following symlinks,
  67. // or returns an error.
  68. func LfileLabel(fpath string) (string, error) {
  69. return lFileLabel(fpath)
  70. }
  71. // SetFSCreateLabel tells the kernel what label to use for all file system objects
  72. // created by this task.
  73. // Set the label to an empty string to return to the default label. Calls to SetFSCreateLabel
  74. // should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until file system
  75. // objects created by this task are finished to guarantee another goroutine does not migrate
  76. // to the current thread before execution is complete.
  77. func SetFSCreateLabel(label string) error {
  78. return setFSCreateLabel(label)
  79. }
  80. // FSCreateLabel returns the default label the kernel which the kernel is using
  81. // for file system objects created by this task. "" indicates default.
  82. func FSCreateLabel() (string, error) {
  83. return fsCreateLabel()
  84. }
  85. // CurrentLabel returns the SELinux label of the current process thread, or an error.
  86. func CurrentLabel() (string, error) {
  87. return currentLabel()
  88. }
  89. // PidLabel returns the SELinux label of the given pid, or an error.
  90. func PidLabel(pid int) (string, error) {
  91. return pidLabel(pid)
  92. }
  93. // ExecLabel returns 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 ExecLabel() (string, error) {
  96. return execLabel()
  97. }
  98. // CanonicalizeContext takes a context string and writes it to the kernel
  99. // the function then returns the context that the kernel will use. Use this
  100. // function to check if two contexts are equivalent
  101. func CanonicalizeContext(val string) (string, error) {
  102. return canonicalizeContext(val)
  103. }
  104. // ComputeCreateContext requests the type transition from source to target for
  105. // class from the kernel.
  106. func ComputeCreateContext(source string, target string, class string) (string, error) {
  107. return computeCreateContext(source, target, class)
  108. }
  109. // CalculateGlbLub computes the glb (greatest lower bound) and lub (least upper bound)
  110. // of a source and target range.
  111. // The glblub is calculated as the greater of the low sensitivities and
  112. // the lower of the high sensitivities and the and of each category bitset.
  113. func CalculateGlbLub(sourceRange, targetRange string) (string, error) {
  114. return calculateGlbLub(sourceRange, targetRange)
  115. }
  116. // SetExecLabel sets the SELinux label that the kernel will use for any programs
  117. // that are executed by the current process thread, or an error. Calls to SetExecLabel
  118. // should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until execution
  119. // of the program is finished to guarantee another goroutine does not migrate to the current
  120. // thread before execution is complete.
  121. func SetExecLabel(label string) error {
  122. return setExecLabel(label)
  123. }
  124. // SetTaskLabel sets the SELinux label for the current thread, or an error.
  125. // This requires the dyntransition permission. Calls to SetTaskLabel should
  126. // be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() to guarantee
  127. // the current thread does not run in a new mislabeled thread.
  128. func SetTaskLabel(label string) error {
  129. return setTaskLabel(label)
  130. }
  131. // SetSocketLabel takes a process label and tells the kernel to assign the
  132. // label to the next socket that gets created. Calls to SetSocketLabel
  133. // should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until
  134. // the the socket is created to guarantee another goroutine does not migrate
  135. // to the current thread before execution is complete.
  136. func SetSocketLabel(label string) error {
  137. return setSocketLabel(label)
  138. }
  139. // SocketLabel retrieves the current socket label setting
  140. func SocketLabel() (string, error) {
  141. return socketLabel()
  142. }
  143. // PeerLabel retrieves the label of the client on the other side of a socket
  144. func PeerLabel(fd uintptr) (string, error) {
  145. return peerLabel(fd)
  146. }
  147. // SetKeyLabel takes a process label and tells the kernel to assign the
  148. // label to the next kernel keyring that gets created. Calls to SetKeyLabel
  149. // should be wrapped in runtime.LockOSThread()/runtime.UnlockOSThread() until
  150. // the kernel keyring is created to guarantee another goroutine does not migrate
  151. // to the current thread before execution is complete.
  152. func SetKeyLabel(label string) error {
  153. return setKeyLabel(label)
  154. }
  155. // KeyLabel retrieves the current kernel keyring label setting
  156. func KeyLabel() (string, error) {
  157. return keyLabel()
  158. }
  159. // Get returns the Context as a string
  160. func (c Context) Get() string {
  161. return c.get()
  162. }
  163. // NewContext creates a new Context struct from the specified label
  164. func NewContext(label string) (Context, error) {
  165. return newContext(label)
  166. }
  167. // ClearLabels clears all reserved labels
  168. func ClearLabels() {
  169. clearLabels()
  170. }
  171. // ReserveLabel reserves the MLS/MCS level component of the specified label
  172. func ReserveLabel(label string) {
  173. reserveLabel(label)
  174. }
  175. // EnforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
  176. func EnforceMode() int {
  177. return enforceMode()
  178. }
  179. // SetEnforceMode sets the current SELinux mode Enforcing, Permissive.
  180. // Disabled is not valid, since this needs to be set at boot time.
  181. func SetEnforceMode(mode int) error {
  182. return setEnforceMode(mode)
  183. }
  184. // DefaultEnforceMode returns the systems default SELinux mode Enforcing,
  185. // Permissive or Disabled. Note this is is just the default at boot time.
  186. // EnforceMode tells you the systems current mode.
  187. func DefaultEnforceMode() int {
  188. return defaultEnforceMode()
  189. }
  190. // ReleaseLabel un-reserves the MLS/MCS Level field of the specified label,
  191. // allowing it to be used by another process.
  192. func ReleaseLabel(label string) {
  193. releaseLabel(label)
  194. }
  195. // ROFileLabel returns the specified SELinux readonly file label
  196. func ROFileLabel() string {
  197. return roFileLabel()
  198. }
  199. // KVMContainerLabels returns the default processLabel and mountLabel to be used
  200. // for kvm containers by the calling process.
  201. func KVMContainerLabels() (string, string) {
  202. return kvmContainerLabels()
  203. }
  204. // InitContainerLabels returns the default processLabel and file labels to be
  205. // used for containers running an init system like systemd by the calling process.
  206. func InitContainerLabels() (string, string) {
  207. return initContainerLabels()
  208. }
  209. // ContainerLabels returns an allocated processLabel and fileLabel to be used for
  210. // container labeling by the calling process.
  211. func ContainerLabels() (processLabel string, fileLabel string) {
  212. return containerLabels()
  213. }
  214. // SecurityCheckContext validates that the SELinux label is understood by the kernel
  215. func SecurityCheckContext(val string) error {
  216. return securityCheckContext(val)
  217. }
  218. // CopyLevel returns a label with the MLS/MCS level from src label replaced on
  219. // the dest label.
  220. func CopyLevel(src, dest string) (string, error) {
  221. return copyLevel(src, dest)
  222. }
  223. // Chcon changes the fpath file object to the SELinux label label.
  224. // If fpath is a directory and recurse is true, then Chcon walks the
  225. // directory tree setting the label.
  226. //
  227. // The fpath itself is guaranteed to be relabeled last.
  228. func Chcon(fpath string, label string, recurse bool) error {
  229. return chcon(fpath, label, recurse)
  230. }
  231. // DupSecOpt takes an SELinux process label and returns security options that
  232. // can be used to set the SELinux Type and Level for future container processes.
  233. func DupSecOpt(src string) ([]string, error) {
  234. return dupSecOpt(src)
  235. }
  236. // DisableSecOpt returns a security opt that can be used to disable SELinux
  237. // labeling support for future container processes.
  238. func DisableSecOpt() []string {
  239. return disableSecOpt()
  240. }
  241. // GetDefaultContextWithLevel gets a single context for the specified SELinux user
  242. // identity that is reachable from the specified scon context. The context is based
  243. // on the per-user /etc/selinux/{SELINUXTYPE}/contexts/users/<username> if it exists,
  244. // and falls back to the global /etc/selinux/{SELINUXTYPE}/contexts/default_contexts
  245. // file.
  246. func GetDefaultContextWithLevel(user, level, scon string) (string, error) {
  247. return getDefaultContextWithLevel(user, level, scon)
  248. }
  249. // PrivContainerMountLabel returns mount label for privileged containers
  250. func PrivContainerMountLabel() string {
  251. // Make sure label is initialized.
  252. _ = label("")
  253. return privContainerMountLabel
  254. }