123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- // +build selinux,linux
- package selinux
- import (
- "bufio"
- "bytes"
- "crypto/rand"
- "encoding/binary"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "regexp"
- "strconv"
- "strings"
- "sync"
- "syscall"
- )
- const (
- // Enforcing constant indicate SELinux is in enforcing mode
- Enforcing = 1
- // Permissive constant to indicate SELinux is in permissive mode
- Permissive = 0
- // Disabled constant to indicate SELinux is disabled
- Disabled = -1
- selinuxDir = "/etc/selinux/"
- selinuxConfig = selinuxDir + "config"
- selinuxfsMount = "/sys/fs/selinux"
- selinuxTypeTag = "SELINUXTYPE"
- selinuxTag = "SELINUX"
- xattrNameSelinux = "security.selinux"
- stRdOnly = 0x01
- selinuxfsMagic = 0xf97cff8c
- )
- type selinuxState struct {
- enabledSet bool
- enabled bool
- selinuxfsSet bool
- selinuxfs string
- mcsList map[string]bool
- sync.Mutex
- }
- var (
- // ErrMCSAlreadyExists is returned when trying to allocate a duplicate MCS.
- ErrMCSAlreadyExists = errors.New("MCS label already exists")
- // ErrEmptyPath is returned when an empty path has been specified.
- ErrEmptyPath = errors.New("empty path")
- assignRegex = regexp.MustCompile(`^([^=]+)=(.*)$`)
- roFileLabel string
- state = selinuxState{
- mcsList: make(map[string]bool),
- }
- )
- // Context is a representation of the SELinux label broken into 4 parts
- type Context map[string]string
- func (s *selinuxState) setEnable(enabled bool) bool {
- s.Lock()
- defer s.Unlock()
- s.enabledSet = true
- s.enabled = enabled
- return s.enabled
- }
- func (s *selinuxState) getEnabled() bool {
- s.Lock()
- enabled := s.enabled
- enabledSet := s.enabledSet
- s.Unlock()
- if enabledSet {
- return enabled
- }
- enabled = false
- if fs := getSelinuxMountPoint(); fs != "" {
- if con, _ := CurrentLabel(); con != "kernel" {
- enabled = true
- }
- }
- return s.setEnable(enabled)
- }
- // SetDisabled disables selinux support for the package
- func SetDisabled() {
- state.setEnable(false)
- }
- func (s *selinuxState) setSELinuxfs(selinuxfs string) string {
- s.Lock()
- defer s.Unlock()
- s.selinuxfsSet = true
- s.selinuxfs = selinuxfs
- return s.selinuxfs
- }
- func verifySELinuxfsMount(mnt string) bool {
- var buf syscall.Statfs_t
- for {
- err := syscall.Statfs(mnt, &buf)
- if err == nil {
- break
- }
- if err == syscall.EAGAIN {
- continue
- }
- return false
- }
- if uint32(buf.Type) != uint32(selinuxfsMagic) {
- return false
- }
- if (buf.Flags & stRdOnly) != 0 {
- return false
- }
- return true
- }
- func findSELinuxfs() string {
- // fast path: check the default mount first
- if verifySELinuxfsMount(selinuxfsMount) {
- return selinuxfsMount
- }
- // check if selinuxfs is available before going the slow path
- fs, err := ioutil.ReadFile("/proc/filesystems")
- if err != nil {
- return ""
- }
- if !bytes.Contains(fs, []byte("\tselinuxfs\n")) {
- return ""
- }
- // slow path: try to find among the mounts
- f, err := os.Open("/proc/self/mountinfo")
- if err != nil {
- return ""
- }
- defer f.Close()
- scanner := bufio.NewScanner(f)
- for {
- mnt := findSELinuxfsMount(scanner)
- if mnt == "" { // error or not found
- return ""
- }
- if verifySELinuxfsMount(mnt) {
- return mnt
- }
- }
- }
- // findSELinuxfsMount returns a next selinuxfs mount point found,
- // if there is one, or an empty string in case of EOF or error.
- func findSELinuxfsMount(s *bufio.Scanner) string {
- for s.Scan() {
- txt := s.Text()
- // The first field after - is fs type.
- // Safe as spaces in mountpoints are encoded as \040
- if !strings.Contains(txt, " - selinuxfs ") {
- continue
- }
- const mPos = 5 // mount point is 5th field
- fields := strings.SplitN(txt, " ", mPos+1)
- if len(fields) < mPos+1 {
- continue
- }
- return fields[mPos-1]
- }
- return ""
- }
- func (s *selinuxState) getSELinuxfs() string {
- s.Lock()
- selinuxfs := s.selinuxfs
- selinuxfsSet := s.selinuxfsSet
- s.Unlock()
- if selinuxfsSet {
- return selinuxfs
- }
- return s.setSELinuxfs(findSELinuxfs())
- }
- // getSelinuxMountPoint returns the path to the mountpoint of an selinuxfs
- // filesystem or an empty string if no mountpoint is found. Selinuxfs is
- // a proc-like pseudo-filesystem that exposes the selinux policy API to
- // processes. The existence of an selinuxfs mount is used to determine
- // whether selinux is currently enabled or not.
- func getSelinuxMountPoint() string {
- return state.getSELinuxfs()
- }
- // GetEnabled returns whether selinux is currently enabled.
- func GetEnabled() bool {
- return state.getEnabled()
- }
- func readConfig(target string) string {
- var (
- val, key string
- bufin *bufio.Reader
- )
- in, err := os.Open(selinuxConfig)
- if err != nil {
- return ""
- }
- defer in.Close()
- bufin = bufio.NewReader(in)
- for done := false; !done; {
- var line string
- if line, err = bufin.ReadString('\n'); err != nil {
- if err != io.EOF {
- return ""
- }
- done = true
- }
- line = strings.TrimSpace(line)
- if len(line) == 0 {
- // Skip blank lines
- continue
- }
- if line[0] == ';' || line[0] == '#' {
- // Skip comments
- continue
- }
- if groups := assignRegex.FindStringSubmatch(line); groups != nil {
- key, val = strings.TrimSpace(groups[1]), strings.TrimSpace(groups[2])
- if key == target {
- return strings.Trim(val, "\"")
- }
- }
- }
- return ""
- }
- func getSELinuxPolicyRoot() string {
- return filepath.Join(selinuxDir, readConfig(selinuxTypeTag))
- }
- func readCon(fpath string) (string, error) {
- if fpath == "" {
- return "", ErrEmptyPath
- }
- in, err := os.Open(fpath)
- if err != nil {
- return "", err
- }
- defer in.Close()
- var retval string
- if _, err := fmt.Fscanf(in, "%s", &retval); err != nil {
- return "", err
- }
- return strings.Trim(retval, "\x00"), nil
- }
- // SetFileLabel sets the SELinux label for this path or returns an error.
- func SetFileLabel(fpath string, label string) error {
- if fpath == "" {
- return ErrEmptyPath
- }
- return lsetxattr(fpath, xattrNameSelinux, []byte(label), 0)
- }
- // FileLabel returns the SELinux label for this path or returns an error.
- func FileLabel(fpath string) (string, error) {
- if fpath == "" {
- return "", ErrEmptyPath
- }
- label, err := lgetxattr(fpath, xattrNameSelinux)
- if err != nil {
- return "", err
- }
- // Trim the NUL byte at the end of the byte buffer, if present.
- if len(label) > 0 && label[len(label)-1] == '\x00' {
- label = label[:len(label)-1]
- }
- return string(label), nil
- }
- /*
- SetFSCreateLabel tells kernel the label to create all file system objects
- created by this task. Setting label="" to return to default.
- */
- func SetFSCreateLabel(label string) error {
- return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid()), label)
- }
- /*
- FSCreateLabel returns the default label the kernel which the kernel is using
- for file system objects created by this task. "" indicates default.
- */
- func FSCreateLabel() (string, error) {
- return readCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid()))
- }
- // CurrentLabel returns the SELinux label of the current process thread, or an error.
- func CurrentLabel() (string, error) {
- return readCon(fmt.Sprintf("/proc/self/task/%d/attr/current", syscall.Gettid()))
- }
- // PidLabel returns the SELinux label of the given pid, or an error.
- func PidLabel(pid int) (string, error) {
- return readCon(fmt.Sprintf("/proc/%d/attr/current", pid))
- }
- /*
- ExecLabel returns the SELinux label that the kernel will use for any programs
- that are executed by the current process thread, or an error.
- */
- func ExecLabel() (string, error) {
- return readCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()))
- }
- func writeCon(fpath string, val string) error {
- if fpath == "" {
- return ErrEmptyPath
- }
- out, err := os.OpenFile(fpath, os.O_WRONLY, 0)
- if err != nil {
- return err
- }
- defer out.Close()
- if val != "" {
- _, err = out.Write([]byte(val))
- } else {
- _, err = out.Write(nil)
- }
- return err
- }
- /*
- CanonicalizeContext takes a context string and writes it to the kernel
- the function then returns the context that the kernel will use. This function
- can be used to see if two contexts are equivalent
- */
- func CanonicalizeContext(val string) (string, error) {
- return readWriteCon(filepath.Join(getSelinuxMountPoint(), "context"), val)
- }
- func readWriteCon(fpath string, val string) (string, error) {
- if fpath == "" {
- return "", ErrEmptyPath
- }
- f, err := os.OpenFile(fpath, os.O_RDWR, 0)
- if err != nil {
- return "", err
- }
- defer f.Close()
- _, err = f.Write([]byte(val))
- if err != nil {
- return "", err
- }
- var retval string
- if _, err := fmt.Fscanf(f, "%s", &retval); err != nil {
- return "", err
- }
- return strings.Trim(retval, "\x00"), nil
- }
- /*
- SetExecLabel sets the SELinux label that the kernel will use for any programs
- that are executed by the current process thread, or an error.
- */
- func SetExecLabel(label string) error {
- return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()), label)
- }
- // Get returns the Context as a string
- func (c Context) Get() string {
- if c["level"] != "" {
- return fmt.Sprintf("%s:%s:%s:%s", c["user"], c["role"], c["type"], c["level"])
- }
- return fmt.Sprintf("%s:%s:%s", c["user"], c["role"], c["type"])
- }
- // NewContext creates a new Context struct from the specified label
- func NewContext(label string) Context {
- c := make(Context)
- if len(label) != 0 {
- con := strings.SplitN(label, ":", 4)
- c["user"] = con[0]
- c["role"] = con[1]
- c["type"] = con[2]
- if len(con) > 3 {
- c["level"] = con[3]
- }
- }
- return c
- }
- // ReserveLabel reserves the MLS/MCS level component of the specified label
- func ReserveLabel(label string) {
- if len(label) != 0 {
- con := strings.SplitN(label, ":", 4)
- if len(con) > 3 {
- mcsAdd(con[3])
- }
- }
- }
- func selinuxEnforcePath() string {
- return fmt.Sprintf("%s/enforce", getSelinuxMountPoint())
- }
- // EnforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
- func EnforceMode() int {
- var enforce int
- enforceS, err := readCon(selinuxEnforcePath())
- if err != nil {
- return -1
- }
- enforce, err = strconv.Atoi(string(enforceS))
- if err != nil {
- return -1
- }
- return enforce
- }
- /*
- SetEnforceMode sets the current SELinux mode Enforcing, Permissive.
- Disabled is not valid, since this needs to be set at boot time.
- */
- func SetEnforceMode(mode int) error {
- return writeCon(selinuxEnforcePath(), fmt.Sprintf("%d", mode))
- }
- /*
- DefaultEnforceMode returns the systems default SELinux mode Enforcing,
- Permissive or Disabled. Note this is is just the default at boot time.
- EnforceMode tells you the systems current mode.
- */
- func DefaultEnforceMode() int {
- switch readConfig(selinuxTag) {
- case "enforcing":
- return Enforcing
- case "permissive":
- return Permissive
- }
- return Disabled
- }
- func mcsAdd(mcs string) error {
- if mcs == "" {
- return nil
- }
- state.Lock()
- defer state.Unlock()
- if state.mcsList[mcs] {
- return ErrMCSAlreadyExists
- }
- state.mcsList[mcs] = true
- return nil
- }
- func mcsDelete(mcs string) {
- if mcs == "" {
- return
- }
- state.Lock()
- defer state.Unlock()
- state.mcsList[mcs] = false
- }
- func intToMcs(id int, catRange uint32) string {
- var (
- SETSIZE = int(catRange)
- TIER = SETSIZE
- ORD = id
- )
- if id < 1 || id > 523776 {
- return ""
- }
- for ORD > TIER {
- ORD = ORD - TIER
- TIER--
- }
- TIER = SETSIZE - TIER
- ORD = ORD + TIER
- return fmt.Sprintf("s0:c%d,c%d", TIER, ORD)
- }
- func uniqMcs(catRange uint32) string {
- var (
- n uint32
- c1, c2 uint32
- mcs string
- )
- for {
- binary.Read(rand.Reader, binary.LittleEndian, &n)
- c1 = n % catRange
- binary.Read(rand.Reader, binary.LittleEndian, &n)
- c2 = n % catRange
- if c1 == c2 {
- continue
- } else {
- if c1 > c2 {
- c1, c2 = c2, c1
- }
- }
- mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
- if err := mcsAdd(mcs); err != nil {
- continue
- }
- break
- }
- return mcs
- }
- /*
- ReleaseLabel will unreserve the MLS/MCS Level field of the specified label.
- Allowing it to be used by another process.
- */
- func ReleaseLabel(label string) {
- if len(label) != 0 {
- con := strings.SplitN(label, ":", 4)
- if len(con) > 3 {
- mcsDelete(con[3])
- }
- }
- }
- // ROFileLabel returns the specified SELinux readonly file label
- func ROFileLabel() string {
- return roFileLabel
- }
- /*
- ContainerLabels returns an allocated processLabel and fileLabel to be used for
- container labeling by the calling process.
- */
- func ContainerLabels() (processLabel string, fileLabel string) {
- var (
- val, key string
- bufin *bufio.Reader
- )
- if !GetEnabled() {
- return "", ""
- }
- lxcPath := fmt.Sprintf("%s/contexts/lxc_contexts", getSELinuxPolicyRoot())
- in, err := os.Open(lxcPath)
- if err != nil {
- return "", ""
- }
- defer in.Close()
- bufin = bufio.NewReader(in)
- for done := false; !done; {
- var line string
- if line, err = bufin.ReadString('\n'); err != nil {
- if err == io.EOF {
- done = true
- } else {
- goto exit
- }
- }
- line = strings.TrimSpace(line)
- if len(line) == 0 {
- // Skip blank lines
- continue
- }
- if line[0] == ';' || line[0] == '#' {
- // Skip comments
- continue
- }
- if groups := assignRegex.FindStringSubmatch(line); groups != nil {
- key, val = strings.TrimSpace(groups[1]), strings.TrimSpace(groups[2])
- if key == "process" {
- processLabel = strings.Trim(val, "\"")
- }
- if key == "file" {
- fileLabel = strings.Trim(val, "\"")
- }
- if key == "ro_file" {
- roFileLabel = strings.Trim(val, "\"")
- }
- }
- }
- if processLabel == "" || fileLabel == "" {
- return "", ""
- }
- if roFileLabel == "" {
- roFileLabel = fileLabel
- }
- exit:
- scon := NewContext(processLabel)
- if scon["level"] != "" {
- mcs := uniqMcs(1024)
- scon["level"] = mcs
- processLabel = scon.Get()
- scon = NewContext(fileLabel)
- scon["level"] = mcs
- fileLabel = scon.Get()
- }
- return processLabel, fileLabel
- }
- // SecurityCheckContext validates that the SELinux label is understood by the kernel
- func SecurityCheckContext(val string) error {
- return writeCon(fmt.Sprintf("%s/context", getSelinuxMountPoint()), val)
- }
- /*
- CopyLevel returns a label with the MLS/MCS level from src label replaced on
- the dest label.
- */
- func CopyLevel(src, dest string) (string, error) {
- if src == "" {
- return "", nil
- }
- if err := SecurityCheckContext(src); err != nil {
- return "", err
- }
- if err := SecurityCheckContext(dest); err != nil {
- return "", err
- }
- scon := NewContext(src)
- tcon := NewContext(dest)
- mcsDelete(tcon["level"])
- mcsAdd(scon["level"])
- tcon["level"] = scon["level"]
- return tcon.Get(), nil
- }
- // Prevent users from relabing system files
- func badPrefix(fpath string) error {
- if fpath == "" {
- return ErrEmptyPath
- }
- badPrefixes := []string{"/usr"}
- for _, prefix := range badPrefixes {
- if strings.HasPrefix(fpath, prefix) {
- return fmt.Errorf("relabeling content in %s is not allowed", prefix)
- }
- }
- return nil
- }
- // Chcon changes the `fpath` file object to the SELinux label `label`.
- // If `fpath` is a directory and `recurse`` is true, Chcon will walk the
- // directory tree setting the label.
- func Chcon(fpath string, label string, recurse bool) error {
- if fpath == "" {
- return ErrEmptyPath
- }
- if label == "" {
- return nil
- }
- if err := badPrefix(fpath); err != nil {
- return err
- }
- callback := func(p string, info os.FileInfo, err error) error {
- return SetFileLabel(p, label)
- }
- if recurse {
- return filepath.Walk(fpath, callback)
- }
- return SetFileLabel(fpath, label)
- }
- // DupSecOpt takes an SELinux process label and returns security options that
- // can be used to set the SELinux Type and Level for future container processes.
- func DupSecOpt(src string) []string {
- if src == "" {
- return nil
- }
- con := NewContext(src)
- if con["user"] == "" ||
- con["role"] == "" ||
- con["type"] == "" {
- return nil
- }
- dup := []string{"user:" + con["user"],
- "role:" + con["role"],
- "type:" + con["type"],
- }
- if con["level"] != "" {
- dup = append(dup, "level:"+con["level"])
- }
- return dup
- }
- // DisableSecOpt returns a security opt that can be used to disable SELinux
- // labeling support for future container processes.
- func DisableSecOpt() []string {
- return []string{"disable"}
- }
|