selinux_linux.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. package selinux
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/rand"
  6. "encoding/binary"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "math/big"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "golang.org/x/sys/unix"
  19. )
  20. const (
  21. minSensLen = 2
  22. contextFile = "/usr/share/containers/selinux/contexts"
  23. selinuxDir = "/etc/selinux/"
  24. selinuxUsersDir = "contexts/users"
  25. defaultContexts = "contexts/default_contexts"
  26. selinuxConfig = selinuxDir + "config"
  27. selinuxfsMount = "/sys/fs/selinux"
  28. selinuxTypeTag = "SELINUXTYPE"
  29. selinuxTag = "SELINUX"
  30. xattrNameSelinux = "security.selinux"
  31. )
  32. type selinuxState struct {
  33. enabledSet bool
  34. enabled bool
  35. selinuxfsOnce sync.Once
  36. selinuxfs string
  37. mcsList map[string]bool
  38. sync.Mutex
  39. }
  40. type level struct {
  41. sens uint
  42. cats *big.Int
  43. }
  44. type mlsRange struct {
  45. low *level
  46. high *level
  47. }
  48. type defaultSECtx struct {
  49. user, level, scon string
  50. userRdr, defaultRdr io.Reader
  51. verifier func(string) error
  52. }
  53. type levelItem byte
  54. const (
  55. sensitivity levelItem = 's'
  56. category levelItem = 'c'
  57. )
  58. var (
  59. readOnlyFileLabel string
  60. state = selinuxState{
  61. mcsList: make(map[string]bool),
  62. }
  63. // for attrPath()
  64. attrPathOnce sync.Once
  65. haveThreadSelf bool
  66. // for policyRoot()
  67. policyRootOnce sync.Once
  68. policyRootVal string
  69. // for label()
  70. loadLabelsOnce sync.Once
  71. labels map[string]string
  72. )
  73. func policyRoot() string {
  74. policyRootOnce.Do(func() {
  75. policyRootVal = filepath.Join(selinuxDir, readConfig(selinuxTypeTag))
  76. })
  77. return policyRootVal
  78. }
  79. func (s *selinuxState) setEnable(enabled bool) bool {
  80. s.Lock()
  81. defer s.Unlock()
  82. s.enabledSet = true
  83. s.enabled = enabled
  84. return s.enabled
  85. }
  86. func (s *selinuxState) getEnabled() bool {
  87. s.Lock()
  88. enabled := s.enabled
  89. enabledSet := s.enabledSet
  90. s.Unlock()
  91. if enabledSet {
  92. return enabled
  93. }
  94. enabled = false
  95. if fs := getSelinuxMountPoint(); fs != "" {
  96. if con, _ := CurrentLabel(); con != "kernel" {
  97. enabled = true
  98. }
  99. }
  100. return s.setEnable(enabled)
  101. }
  102. // setDisabled disables SELinux support for the package
  103. func setDisabled() {
  104. state.setEnable(false)
  105. }
  106. func verifySELinuxfsMount(mnt string) bool {
  107. var buf unix.Statfs_t
  108. for {
  109. err := unix.Statfs(mnt, &buf)
  110. if err == nil {
  111. break
  112. }
  113. if err == unix.EAGAIN || err == unix.EINTR { //nolint:errorlint // unix errors are bare
  114. continue
  115. }
  116. return false
  117. }
  118. if uint32(buf.Type) != uint32(unix.SELINUX_MAGIC) {
  119. return false
  120. }
  121. if (buf.Flags & unix.ST_RDONLY) != 0 {
  122. return false
  123. }
  124. return true
  125. }
  126. func findSELinuxfs() string {
  127. // fast path: check the default mount first
  128. if verifySELinuxfsMount(selinuxfsMount) {
  129. return selinuxfsMount
  130. }
  131. // check if selinuxfs is available before going the slow path
  132. fs, err := ioutil.ReadFile("/proc/filesystems")
  133. if err != nil {
  134. return ""
  135. }
  136. if !bytes.Contains(fs, []byte("\tselinuxfs\n")) {
  137. return ""
  138. }
  139. // slow path: try to find among the mounts
  140. f, err := os.Open("/proc/self/mountinfo")
  141. if err != nil {
  142. return ""
  143. }
  144. defer f.Close()
  145. scanner := bufio.NewScanner(f)
  146. for {
  147. mnt := findSELinuxfsMount(scanner)
  148. if mnt == "" { // error or not found
  149. return ""
  150. }
  151. if verifySELinuxfsMount(mnt) {
  152. return mnt
  153. }
  154. }
  155. }
  156. // findSELinuxfsMount returns a next selinuxfs mount point found,
  157. // if there is one, or an empty string in case of EOF or error.
  158. func findSELinuxfsMount(s *bufio.Scanner) string {
  159. for s.Scan() {
  160. txt := s.Bytes()
  161. // The first field after - is fs type.
  162. // Safe as spaces in mountpoints are encoded as \040
  163. if !bytes.Contains(txt, []byte(" - selinuxfs ")) {
  164. continue
  165. }
  166. const mPos = 5 // mount point is 5th field
  167. fields := bytes.SplitN(txt, []byte(" "), mPos+1)
  168. if len(fields) < mPos+1 {
  169. continue
  170. }
  171. return string(fields[mPos-1])
  172. }
  173. return ""
  174. }
  175. func (s *selinuxState) getSELinuxfs() string {
  176. s.selinuxfsOnce.Do(func() {
  177. s.selinuxfs = findSELinuxfs()
  178. })
  179. return s.selinuxfs
  180. }
  181. // getSelinuxMountPoint returns the path to the mountpoint of an selinuxfs
  182. // filesystem or an empty string if no mountpoint is found. Selinuxfs is
  183. // a proc-like pseudo-filesystem that exposes the SELinux policy API to
  184. // processes. The existence of an selinuxfs mount is used to determine
  185. // whether SELinux is currently enabled or not.
  186. func getSelinuxMountPoint() string {
  187. return state.getSELinuxfs()
  188. }
  189. // getEnabled returns whether SELinux is currently enabled.
  190. func getEnabled() bool {
  191. return state.getEnabled()
  192. }
  193. func readConfig(target string) string {
  194. in, err := os.Open(selinuxConfig)
  195. if err != nil {
  196. return ""
  197. }
  198. defer in.Close()
  199. scanner := bufio.NewScanner(in)
  200. for scanner.Scan() {
  201. line := bytes.TrimSpace(scanner.Bytes())
  202. if len(line) == 0 {
  203. // Skip blank lines
  204. continue
  205. }
  206. if line[0] == ';' || line[0] == '#' {
  207. // Skip comments
  208. continue
  209. }
  210. fields := bytes.SplitN(line, []byte{'='}, 2)
  211. if len(fields) != 2 {
  212. continue
  213. }
  214. if bytes.Equal(fields[0], []byte(target)) {
  215. return string(bytes.Trim(fields[1], `"`))
  216. }
  217. }
  218. return ""
  219. }
  220. func isProcHandle(fh *os.File) error {
  221. var buf unix.Statfs_t
  222. for {
  223. err := unix.Fstatfs(int(fh.Fd()), &buf)
  224. if err == nil {
  225. break
  226. }
  227. if err != unix.EINTR { //nolint:errorlint // unix errors are bare
  228. return &os.PathError{Op: "fstatfs", Path: fh.Name(), Err: err}
  229. }
  230. }
  231. if buf.Type != unix.PROC_SUPER_MAGIC {
  232. return fmt.Errorf("file %q is not on procfs", fh.Name())
  233. }
  234. return nil
  235. }
  236. func readCon(fpath string) (string, error) {
  237. if fpath == "" {
  238. return "", ErrEmptyPath
  239. }
  240. in, err := os.Open(fpath)
  241. if err != nil {
  242. return "", err
  243. }
  244. defer in.Close()
  245. if err := isProcHandle(in); err != nil {
  246. return "", err
  247. }
  248. return readConFd(in)
  249. }
  250. func readConFd(in *os.File) (string, error) {
  251. data, err := ioutil.ReadAll(in)
  252. if err != nil {
  253. return "", err
  254. }
  255. return string(bytes.TrimSuffix(data, []byte{0})), nil
  256. }
  257. // classIndex returns the int index for an object class in the loaded policy,
  258. // or -1 and an error
  259. func classIndex(class string) (int, error) {
  260. permpath := fmt.Sprintf("class/%s/index", class)
  261. indexpath := filepath.Join(getSelinuxMountPoint(), permpath)
  262. indexB, err := ioutil.ReadFile(indexpath)
  263. if err != nil {
  264. return -1, err
  265. }
  266. index, err := strconv.Atoi(string(indexB))
  267. if err != nil {
  268. return -1, err
  269. }
  270. return index, nil
  271. }
  272. // setFileLabel sets the SELinux label for this path or returns an error.
  273. func setFileLabel(fpath string, label string) error {
  274. if fpath == "" {
  275. return ErrEmptyPath
  276. }
  277. for {
  278. err := unix.Lsetxattr(fpath, xattrNameSelinux, []byte(label), 0)
  279. if err == nil {
  280. break
  281. }
  282. if err != unix.EINTR { //nolint:errorlint // unix errors are bare
  283. return &os.PathError{Op: "lsetxattr", Path: fpath, Err: err}
  284. }
  285. }
  286. return nil
  287. }
  288. // fileLabel returns the SELinux label for this path or returns an error.
  289. func fileLabel(fpath string) (string, error) {
  290. if fpath == "" {
  291. return "", ErrEmptyPath
  292. }
  293. label, err := lgetxattr(fpath, xattrNameSelinux)
  294. if err != nil {
  295. return "", &os.PathError{Op: "lgetxattr", Path: fpath, Err: err}
  296. }
  297. // Trim the NUL byte at the end of the byte buffer, if present.
  298. if len(label) > 0 && label[len(label)-1] == '\x00' {
  299. label = label[:len(label)-1]
  300. }
  301. return string(label), nil
  302. }
  303. // setFSCreateLabel tells kernel the label to create all file system objects
  304. // created by this task. Setting label="" to return to default.
  305. func setFSCreateLabel(label string) error {
  306. return writeAttr("fscreate", label)
  307. }
  308. // fsCreateLabel returns the default label the kernel which the kernel is using
  309. // for file system objects created by this task. "" indicates default.
  310. func fsCreateLabel() (string, error) {
  311. return readAttr("fscreate")
  312. }
  313. // currentLabel returns the SELinux label of the current process thread, or an error.
  314. func currentLabel() (string, error) {
  315. return readAttr("current")
  316. }
  317. // pidLabel returns the SELinux label of the given pid, or an error.
  318. func pidLabel(pid int) (string, error) {
  319. return readCon(fmt.Sprintf("/proc/%d/attr/current", pid))
  320. }
  321. // ExecLabel returns the SELinux label that the kernel will use for any programs
  322. // that are executed by the current process thread, or an error.
  323. func execLabel() (string, error) {
  324. return readAttr("exec")
  325. }
  326. func writeCon(fpath, val string) error {
  327. if fpath == "" {
  328. return ErrEmptyPath
  329. }
  330. if val == "" {
  331. if !getEnabled() {
  332. return nil
  333. }
  334. }
  335. out, err := os.OpenFile(fpath, os.O_WRONLY, 0)
  336. if err != nil {
  337. return err
  338. }
  339. defer out.Close()
  340. if err := isProcHandle(out); err != nil {
  341. return err
  342. }
  343. if val != "" {
  344. _, err = out.Write([]byte(val))
  345. } else {
  346. _, err = out.Write(nil)
  347. }
  348. if err != nil {
  349. return err
  350. }
  351. return nil
  352. }
  353. func attrPath(attr string) string {
  354. // Linux >= 3.17 provides this
  355. const threadSelfPrefix = "/proc/thread-self/attr"
  356. attrPathOnce.Do(func() {
  357. st, err := os.Stat(threadSelfPrefix)
  358. if err == nil && st.Mode().IsDir() {
  359. haveThreadSelf = true
  360. }
  361. })
  362. if haveThreadSelf {
  363. return path.Join(threadSelfPrefix, attr)
  364. }
  365. return path.Join("/proc/self/task/", strconv.Itoa(unix.Gettid()), "/attr/", attr)
  366. }
  367. func readAttr(attr string) (string, error) {
  368. return readCon(attrPath(attr))
  369. }
  370. func writeAttr(attr, val string) error {
  371. return writeCon(attrPath(attr), val)
  372. }
  373. // canonicalizeContext takes a context string and writes it to the kernel
  374. // the function then returns the context that the kernel will use. Use this
  375. // function to check if two contexts are equivalent
  376. func canonicalizeContext(val string) (string, error) {
  377. return readWriteCon(filepath.Join(getSelinuxMountPoint(), "context"), val)
  378. }
  379. // computeCreateContext requests the type transition from source to target for
  380. // class from the kernel.
  381. func computeCreateContext(source string, target string, class string) (string, error) {
  382. classidx, err := classIndex(class)
  383. if err != nil {
  384. return "", err
  385. }
  386. return readWriteCon(filepath.Join(getSelinuxMountPoint(), "create"), fmt.Sprintf("%s %s %d", source, target, classidx))
  387. }
  388. // catsToBitset stores categories in a bitset.
  389. func catsToBitset(cats string) (*big.Int, error) {
  390. bitset := new(big.Int)
  391. catlist := strings.Split(cats, ",")
  392. for _, r := range catlist {
  393. ranges := strings.SplitN(r, ".", 2)
  394. if len(ranges) > 1 {
  395. catstart, err := parseLevelItem(ranges[0], category)
  396. if err != nil {
  397. return nil, err
  398. }
  399. catend, err := parseLevelItem(ranges[1], category)
  400. if err != nil {
  401. return nil, err
  402. }
  403. for i := catstart; i <= catend; i++ {
  404. bitset.SetBit(bitset, int(i), 1)
  405. }
  406. } else {
  407. cat, err := parseLevelItem(ranges[0], category)
  408. if err != nil {
  409. return nil, err
  410. }
  411. bitset.SetBit(bitset, int(cat), 1)
  412. }
  413. }
  414. return bitset, nil
  415. }
  416. // parseLevelItem parses and verifies that a sensitivity or category are valid
  417. func parseLevelItem(s string, sep levelItem) (uint, error) {
  418. if len(s) < minSensLen || levelItem(s[0]) != sep {
  419. return 0, ErrLevelSyntax
  420. }
  421. val, err := strconv.ParseUint(s[1:], 10, 32)
  422. if err != nil {
  423. return 0, err
  424. }
  425. return uint(val), nil
  426. }
  427. // parseLevel fills a level from a string that contains
  428. // a sensitivity and categories
  429. func (l *level) parseLevel(levelStr string) error {
  430. lvl := strings.SplitN(levelStr, ":", 2)
  431. sens, err := parseLevelItem(lvl[0], sensitivity)
  432. if err != nil {
  433. return fmt.Errorf("failed to parse sensitivity: %w", err)
  434. }
  435. l.sens = sens
  436. if len(lvl) > 1 {
  437. cats, err := catsToBitset(lvl[1])
  438. if err != nil {
  439. return fmt.Errorf("failed to parse categories: %w", err)
  440. }
  441. l.cats = cats
  442. }
  443. return nil
  444. }
  445. // rangeStrToMLSRange marshals a string representation of a range.
  446. func rangeStrToMLSRange(rangeStr string) (*mlsRange, error) {
  447. mlsRange := &mlsRange{}
  448. levelSlice := strings.SplitN(rangeStr, "-", 2)
  449. switch len(levelSlice) {
  450. // rangeStr that has a low and a high level, e.g. s4:c0.c1023-s6:c0.c1023
  451. case 2:
  452. mlsRange.high = &level{}
  453. if err := mlsRange.high.parseLevel(levelSlice[1]); err != nil {
  454. return nil, fmt.Errorf("failed to parse high level %q: %w", levelSlice[1], err)
  455. }
  456. fallthrough
  457. // rangeStr that is single level, e.g. s6:c0,c3,c5,c30.c1023
  458. case 1:
  459. mlsRange.low = &level{}
  460. if err := mlsRange.low.parseLevel(levelSlice[0]); err != nil {
  461. return nil, fmt.Errorf("failed to parse low level %q: %w", levelSlice[0], err)
  462. }
  463. }
  464. if mlsRange.high == nil {
  465. mlsRange.high = mlsRange.low
  466. }
  467. return mlsRange, nil
  468. }
  469. // bitsetToStr takes a category bitset and returns it in the
  470. // canonical selinux syntax
  471. func bitsetToStr(c *big.Int) string {
  472. var str string
  473. length := 0
  474. for i := int(c.TrailingZeroBits()); i < c.BitLen(); i++ {
  475. if c.Bit(i) == 0 {
  476. continue
  477. }
  478. if length == 0 {
  479. if str != "" {
  480. str += ","
  481. }
  482. str += "c" + strconv.Itoa(i)
  483. }
  484. if c.Bit(i+1) == 1 {
  485. length++
  486. continue
  487. }
  488. if length == 1 {
  489. str += ",c" + strconv.Itoa(i)
  490. } else if length > 1 {
  491. str += ".c" + strconv.Itoa(i)
  492. }
  493. length = 0
  494. }
  495. return str
  496. }
  497. func (l1 *level) equal(l2 *level) bool {
  498. if l2 == nil || l1 == nil {
  499. return l1 == l2
  500. }
  501. if l1.sens != l2.sens {
  502. return false
  503. }
  504. if l2.cats == nil || l1.cats == nil {
  505. return l2.cats == l1.cats
  506. }
  507. return l1.cats.Cmp(l2.cats) == 0
  508. }
  509. // String returns an mlsRange as a string.
  510. func (m mlsRange) String() string {
  511. low := "s" + strconv.Itoa(int(m.low.sens))
  512. if m.low.cats != nil && m.low.cats.BitLen() > 0 {
  513. low += ":" + bitsetToStr(m.low.cats)
  514. }
  515. if m.low.equal(m.high) {
  516. return low
  517. }
  518. high := "s" + strconv.Itoa(int(m.high.sens))
  519. if m.high.cats != nil && m.high.cats.BitLen() > 0 {
  520. high += ":" + bitsetToStr(m.high.cats)
  521. }
  522. return low + "-" + high
  523. }
  524. func max(a, b uint) uint {
  525. if a > b {
  526. return a
  527. }
  528. return b
  529. }
  530. func min(a, b uint) uint {
  531. if a < b {
  532. return a
  533. }
  534. return b
  535. }
  536. // calculateGlbLub computes the glb (greatest lower bound) and lub (least upper bound)
  537. // of a source and target range.
  538. // The glblub is calculated as the greater of the low sensitivities and
  539. // the lower of the high sensitivities and the and of each category bitset.
  540. func calculateGlbLub(sourceRange, targetRange string) (string, error) {
  541. s, err := rangeStrToMLSRange(sourceRange)
  542. if err != nil {
  543. return "", err
  544. }
  545. t, err := rangeStrToMLSRange(targetRange)
  546. if err != nil {
  547. return "", err
  548. }
  549. if s.high.sens < t.low.sens || t.high.sens < s.low.sens {
  550. /* these ranges have no common sensitivities */
  551. return "", ErrIncomparable
  552. }
  553. outrange := &mlsRange{low: &level{}, high: &level{}}
  554. /* take the greatest of the low */
  555. outrange.low.sens = max(s.low.sens, t.low.sens)
  556. /* take the least of the high */
  557. outrange.high.sens = min(s.high.sens, t.high.sens)
  558. /* find the intersecting categories */
  559. if s.low.cats != nil && t.low.cats != nil {
  560. outrange.low.cats = new(big.Int)
  561. outrange.low.cats.And(s.low.cats, t.low.cats)
  562. }
  563. if s.high.cats != nil && t.high.cats != nil {
  564. outrange.high.cats = new(big.Int)
  565. outrange.high.cats.And(s.high.cats, t.high.cats)
  566. }
  567. return outrange.String(), nil
  568. }
  569. func readWriteCon(fpath string, val string) (string, error) {
  570. if fpath == "" {
  571. return "", ErrEmptyPath
  572. }
  573. f, err := os.OpenFile(fpath, os.O_RDWR, 0)
  574. if err != nil {
  575. return "", err
  576. }
  577. defer f.Close()
  578. _, err = f.Write([]byte(val))
  579. if err != nil {
  580. return "", err
  581. }
  582. return readConFd(f)
  583. }
  584. // setExecLabel sets the SELinux label that the kernel will use for any programs
  585. // that are executed by the current process thread, or an error.
  586. func setExecLabel(label string) error {
  587. return writeAttr("exec", label)
  588. }
  589. // setTaskLabel sets the SELinux label for the current thread, or an error.
  590. // This requires the dyntransition permission.
  591. func setTaskLabel(label string) error {
  592. return writeAttr("current", label)
  593. }
  594. // setSocketLabel takes a process label and tells the kernel to assign the
  595. // label to the next socket that gets created
  596. func setSocketLabel(label string) error {
  597. return writeAttr("sockcreate", label)
  598. }
  599. // socketLabel retrieves the current socket label setting
  600. func socketLabel() (string, error) {
  601. return readAttr("sockcreate")
  602. }
  603. // peerLabel retrieves the label of the client on the other side of a socket
  604. func peerLabel(fd uintptr) (string, error) {
  605. label, err := unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_PEERSEC)
  606. if err != nil {
  607. return "", &os.PathError{Op: "getsockopt", Path: "fd " + strconv.Itoa(int(fd)), Err: err}
  608. }
  609. return label, nil
  610. }
  611. // setKeyLabel takes a process label and tells the kernel to assign the
  612. // label to the next kernel keyring that gets created
  613. func setKeyLabel(label string) error {
  614. err := writeCon("/proc/self/attr/keycreate", label)
  615. if errors.Is(err, os.ErrNotExist) {
  616. return nil
  617. }
  618. if label == "" && errors.Is(err, os.ErrPermission) {
  619. return nil
  620. }
  621. return err
  622. }
  623. // keyLabel retrieves the current kernel keyring label setting
  624. func keyLabel() (string, error) {
  625. return readCon("/proc/self/attr/keycreate")
  626. }
  627. // get returns the Context as a string
  628. func (c Context) get() string {
  629. if level := c["level"]; level != "" {
  630. return c["user"] + ":" + c["role"] + ":" + c["type"] + ":" + level
  631. }
  632. return c["user"] + ":" + c["role"] + ":" + c["type"]
  633. }
  634. // newContext creates a new Context struct from the specified label
  635. func newContext(label string) (Context, error) {
  636. c := make(Context)
  637. if len(label) != 0 {
  638. con := strings.SplitN(label, ":", 4)
  639. if len(con) < 3 {
  640. return c, InvalidLabel
  641. }
  642. c["user"] = con[0]
  643. c["role"] = con[1]
  644. c["type"] = con[2]
  645. if len(con) > 3 {
  646. c["level"] = con[3]
  647. }
  648. }
  649. return c, nil
  650. }
  651. // clearLabels clears all reserved labels
  652. func clearLabels() {
  653. state.Lock()
  654. state.mcsList = make(map[string]bool)
  655. state.Unlock()
  656. }
  657. // reserveLabel reserves the MLS/MCS level component of the specified label
  658. func reserveLabel(label string) {
  659. if len(label) != 0 {
  660. con := strings.SplitN(label, ":", 4)
  661. if len(con) > 3 {
  662. _ = mcsAdd(con[3])
  663. }
  664. }
  665. }
  666. func selinuxEnforcePath() string {
  667. return path.Join(getSelinuxMountPoint(), "enforce")
  668. }
  669. // enforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
  670. func enforceMode() int {
  671. var enforce int
  672. enforceB, err := ioutil.ReadFile(selinuxEnforcePath())
  673. if err != nil {
  674. return -1
  675. }
  676. enforce, err = strconv.Atoi(string(enforceB))
  677. if err != nil {
  678. return -1
  679. }
  680. return enforce
  681. }
  682. // setEnforceMode sets the current SELinux mode Enforcing, Permissive.
  683. // Disabled is not valid, since this needs to be set at boot time.
  684. func setEnforceMode(mode int) error {
  685. return ioutil.WriteFile(selinuxEnforcePath(), []byte(strconv.Itoa(mode)), 0o644)
  686. }
  687. // defaultEnforceMode returns the systems default SELinux mode Enforcing,
  688. // Permissive or Disabled. Note this is is just the default at boot time.
  689. // EnforceMode tells you the systems current mode.
  690. func defaultEnforceMode() int {
  691. switch readConfig(selinuxTag) {
  692. case "enforcing":
  693. return Enforcing
  694. case "permissive":
  695. return Permissive
  696. }
  697. return Disabled
  698. }
  699. func mcsAdd(mcs string) error {
  700. if mcs == "" {
  701. return nil
  702. }
  703. state.Lock()
  704. defer state.Unlock()
  705. if state.mcsList[mcs] {
  706. return ErrMCSAlreadyExists
  707. }
  708. state.mcsList[mcs] = true
  709. return nil
  710. }
  711. func mcsDelete(mcs string) {
  712. if mcs == "" {
  713. return
  714. }
  715. state.Lock()
  716. defer state.Unlock()
  717. state.mcsList[mcs] = false
  718. }
  719. func intToMcs(id int, catRange uint32) string {
  720. var (
  721. SETSIZE = int(catRange)
  722. TIER = SETSIZE
  723. ORD = id
  724. )
  725. if id < 1 || id > 523776 {
  726. return ""
  727. }
  728. for ORD > TIER {
  729. ORD -= TIER
  730. TIER--
  731. }
  732. TIER = SETSIZE - TIER
  733. ORD += TIER
  734. return fmt.Sprintf("s0:c%d,c%d", TIER, ORD)
  735. }
  736. func uniqMcs(catRange uint32) string {
  737. var (
  738. n uint32
  739. c1, c2 uint32
  740. mcs string
  741. )
  742. for {
  743. _ = binary.Read(rand.Reader, binary.LittleEndian, &n)
  744. c1 = n % catRange
  745. _ = binary.Read(rand.Reader, binary.LittleEndian, &n)
  746. c2 = n % catRange
  747. if c1 == c2 {
  748. continue
  749. } else if c1 > c2 {
  750. c1, c2 = c2, c1
  751. }
  752. mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
  753. if err := mcsAdd(mcs); err != nil {
  754. continue
  755. }
  756. break
  757. }
  758. return mcs
  759. }
  760. // releaseLabel un-reserves the MLS/MCS Level field of the specified label,
  761. // allowing it to be used by another process.
  762. func releaseLabel(label string) {
  763. if len(label) != 0 {
  764. con := strings.SplitN(label, ":", 4)
  765. if len(con) > 3 {
  766. mcsDelete(con[3])
  767. }
  768. }
  769. }
  770. // roFileLabel returns the specified SELinux readonly file label
  771. func roFileLabel() string {
  772. return readOnlyFileLabel
  773. }
  774. func openContextFile() (*os.File, error) {
  775. if f, err := os.Open(contextFile); err == nil {
  776. return f, nil
  777. }
  778. return os.Open(filepath.Join(policyRoot(), "/contexts/lxc_contexts"))
  779. }
  780. func loadLabels() {
  781. labels = make(map[string]string)
  782. in, err := openContextFile()
  783. if err != nil {
  784. return
  785. }
  786. defer in.Close()
  787. scanner := bufio.NewScanner(in)
  788. for scanner.Scan() {
  789. line := bytes.TrimSpace(scanner.Bytes())
  790. if len(line) == 0 {
  791. // Skip blank lines
  792. continue
  793. }
  794. if line[0] == ';' || line[0] == '#' {
  795. // Skip comments
  796. continue
  797. }
  798. fields := bytes.SplitN(line, []byte{'='}, 2)
  799. if len(fields) != 2 {
  800. continue
  801. }
  802. key, val := bytes.TrimSpace(fields[0]), bytes.TrimSpace(fields[1])
  803. labels[string(key)] = string(bytes.Trim(val, `"`))
  804. }
  805. con, _ := NewContext(labels["file"])
  806. con["level"] = fmt.Sprintf("s0:c%d,c%d", maxCategory-2, maxCategory-1)
  807. privContainerMountLabel = con.get()
  808. reserveLabel(privContainerMountLabel)
  809. }
  810. func label(key string) string {
  811. loadLabelsOnce.Do(func() {
  812. loadLabels()
  813. })
  814. return labels[key]
  815. }
  816. // kvmContainerLabels returns the default processLabel and mountLabel to be used
  817. // for kvm containers by the calling process.
  818. func kvmContainerLabels() (string, string) {
  819. processLabel := label("kvm_process")
  820. if processLabel == "" {
  821. processLabel = label("process")
  822. }
  823. return addMcs(processLabel, label("file"))
  824. }
  825. // initContainerLabels returns the default processLabel and file labels to be
  826. // used for containers running an init system like systemd by the calling process.
  827. func initContainerLabels() (string, string) {
  828. processLabel := label("init_process")
  829. if processLabel == "" {
  830. processLabel = label("process")
  831. }
  832. return addMcs(processLabel, label("file"))
  833. }
  834. // containerLabels returns an allocated processLabel and fileLabel to be used for
  835. // container labeling by the calling process.
  836. func containerLabels() (processLabel string, fileLabel string) {
  837. if !getEnabled() {
  838. return "", ""
  839. }
  840. processLabel = label("process")
  841. fileLabel = label("file")
  842. readOnlyFileLabel = label("ro_file")
  843. if processLabel == "" || fileLabel == "" {
  844. return "", fileLabel
  845. }
  846. if readOnlyFileLabel == "" {
  847. readOnlyFileLabel = fileLabel
  848. }
  849. return addMcs(processLabel, fileLabel)
  850. }
  851. func addMcs(processLabel, fileLabel string) (string, string) {
  852. scon, _ := NewContext(processLabel)
  853. if scon["level"] != "" {
  854. mcs := uniqMcs(CategoryRange)
  855. scon["level"] = mcs
  856. processLabel = scon.Get()
  857. scon, _ = NewContext(fileLabel)
  858. scon["level"] = mcs
  859. fileLabel = scon.Get()
  860. }
  861. return processLabel, fileLabel
  862. }
  863. // securityCheckContext validates that the SELinux label is understood by the kernel
  864. func securityCheckContext(val string) error {
  865. return ioutil.WriteFile(path.Join(getSelinuxMountPoint(), "context"), []byte(val), 0o644)
  866. }
  867. // copyLevel returns a label with the MLS/MCS level from src label replaced on
  868. // the dest label.
  869. func copyLevel(src, dest string) (string, error) {
  870. if src == "" {
  871. return "", nil
  872. }
  873. if err := SecurityCheckContext(src); err != nil {
  874. return "", err
  875. }
  876. if err := SecurityCheckContext(dest); err != nil {
  877. return "", err
  878. }
  879. scon, err := NewContext(src)
  880. if err != nil {
  881. return "", err
  882. }
  883. tcon, err := NewContext(dest)
  884. if err != nil {
  885. return "", err
  886. }
  887. mcsDelete(tcon["level"])
  888. _ = mcsAdd(scon["level"])
  889. tcon["level"] = scon["level"]
  890. return tcon.Get(), nil
  891. }
  892. // Prevent users from relabeling system files
  893. func badPrefix(fpath string) error {
  894. if fpath == "" {
  895. return ErrEmptyPath
  896. }
  897. badPrefixes := []string{"/usr"}
  898. for _, prefix := range badPrefixes {
  899. if strings.HasPrefix(fpath, prefix) {
  900. return fmt.Errorf("relabeling content in %s is not allowed", prefix)
  901. }
  902. }
  903. return nil
  904. }
  905. // chcon changes the fpath file object to the SELinux label label.
  906. // If fpath is a directory and recurse is true, then chcon walks the
  907. // directory tree setting the label.
  908. func chcon(fpath string, label string, recurse bool) error {
  909. if fpath == "" {
  910. return ErrEmptyPath
  911. }
  912. if label == "" {
  913. return nil
  914. }
  915. if err := badPrefix(fpath); err != nil {
  916. return err
  917. }
  918. if !recurse {
  919. return setFileLabel(fpath, label)
  920. }
  921. return rchcon(fpath, label)
  922. }
  923. // dupSecOpt takes an SELinux process label and returns security options that
  924. // can be used to set the SELinux Type and Level for future container processes.
  925. func dupSecOpt(src string) ([]string, error) {
  926. if src == "" {
  927. return nil, nil
  928. }
  929. con, err := NewContext(src)
  930. if err != nil {
  931. return nil, err
  932. }
  933. if con["user"] == "" ||
  934. con["role"] == "" ||
  935. con["type"] == "" {
  936. return nil, nil
  937. }
  938. dup := []string{
  939. "user:" + con["user"],
  940. "role:" + con["role"],
  941. "type:" + con["type"],
  942. }
  943. if con["level"] != "" {
  944. dup = append(dup, "level:"+con["level"])
  945. }
  946. return dup, nil
  947. }
  948. // disableSecOpt returns a security opt that can be used to disable SELinux
  949. // labeling support for future container processes.
  950. func disableSecOpt() []string {
  951. return []string{"disable"}
  952. }
  953. // findUserInContext scans the reader for a valid SELinux context
  954. // match that is verified with the verifier. Invalid contexts are
  955. // skipped. It returns a matched context or an empty string if no
  956. // match is found. If a scanner error occurs, it is returned.
  957. func findUserInContext(context Context, r io.Reader, verifier func(string) error) (string, error) {
  958. fromRole := context["role"]
  959. fromType := context["type"]
  960. scanner := bufio.NewScanner(r)
  961. for scanner.Scan() {
  962. fromConns := strings.Fields(scanner.Text())
  963. if len(fromConns) == 0 {
  964. // Skip blank lines
  965. continue
  966. }
  967. line := fromConns[0]
  968. if line[0] == ';' || line[0] == '#' {
  969. // Skip comments
  970. continue
  971. }
  972. // user context files contexts are formatted as
  973. // role_r:type_t:s0 where the user is missing.
  974. lineArr := strings.SplitN(line, ":", 4)
  975. // skip context with typo, or role and type do not match
  976. if len(lineArr) != 3 ||
  977. lineArr[0] != fromRole ||
  978. lineArr[1] != fromType {
  979. continue
  980. }
  981. for _, cc := range fromConns[1:] {
  982. toConns := strings.SplitN(cc, ":", 4)
  983. if len(toConns) != 3 {
  984. continue
  985. }
  986. context["role"] = toConns[0]
  987. context["type"] = toConns[1]
  988. outConn := context.get()
  989. if err := verifier(outConn); err != nil {
  990. continue
  991. }
  992. return outConn, nil
  993. }
  994. }
  995. if err := scanner.Err(); err != nil {
  996. return "", fmt.Errorf("failed to scan for context: %w", err)
  997. }
  998. return "", nil
  999. }
  1000. func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
  1001. if c.verifier == nil {
  1002. return "", ErrVerifierNil
  1003. }
  1004. context, err := newContext(c.scon)
  1005. if err != nil {
  1006. return "", fmt.Errorf("failed to create label for %s: %w", c.scon, err)
  1007. }
  1008. // set so the verifier validates the matched context with the provided user and level.
  1009. context["user"] = c.user
  1010. context["level"] = c.level
  1011. conn, err := findUserInContext(context, c.userRdr, c.verifier)
  1012. if err != nil {
  1013. return "", err
  1014. }
  1015. if conn != "" {
  1016. return conn, nil
  1017. }
  1018. conn, err = findUserInContext(context, c.defaultRdr, c.verifier)
  1019. if err != nil {
  1020. return "", err
  1021. }
  1022. if conn != "" {
  1023. return conn, nil
  1024. }
  1025. return "", fmt.Errorf("context %q not found: %w", c.scon, ErrContextMissing)
  1026. }
  1027. func getDefaultContextWithLevel(user, level, scon string) (string, error) {
  1028. userPath := filepath.Join(policyRoot(), selinuxUsersDir, user)
  1029. fu, err := os.Open(userPath)
  1030. if err != nil {
  1031. return "", err
  1032. }
  1033. defer fu.Close()
  1034. defaultPath := filepath.Join(policyRoot(), defaultContexts)
  1035. fd, err := os.Open(defaultPath)
  1036. if err != nil {
  1037. return "", err
  1038. }
  1039. defer fd.Close()
  1040. c := defaultSECtx{
  1041. user: user,
  1042. level: level,
  1043. scon: scon,
  1044. userRdr: fu,
  1045. defaultRdr: fd,
  1046. verifier: securityCheckContext,
  1047. }
  1048. return getDefaultContextFromReaders(&c)
  1049. }