2014-06-10 06:52:12 +08:00
|
|
|
// +build selinux,linux
|
|
|
|
|
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/selinux"
|
2014-06-10 06:52:12 +08:00
|
|
|
)
|
|
|
|
|
2015-10-06 20:51:54 +08:00
|
|
|
// Valid Label Options
|
|
|
|
var validOptions = map[string]bool{
|
|
|
|
"disable": true,
|
|
|
|
"type": true,
|
|
|
|
"user": true,
|
|
|
|
"role": true,
|
|
|
|
"level": true,
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:36:35 +08:00
|
|
|
var ErrIncompatibleLabel = fmt.Errorf("Bad SELinux option z and Z can not be used together")
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// InitLabels returns the process label and file labels to be used within
|
|
|
|
// the container. A list of options can be passed into this function to alter
|
|
|
|
// the labels. The labels returned will include a random MCS String, that is
|
|
|
|
// guaranteed to be unique.
|
|
|
|
func InitLabels(options []string) (string, string, error) {
|
2014-06-10 06:52:12 +08:00
|
|
|
if !selinux.SelinuxEnabled() {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
processLabel, mountLabel := selinux.GetLxcContexts()
|
|
|
|
if processLabel != "" {
|
2014-07-18 02:57:57 +08:00
|
|
|
pcon := selinux.NewContext(processLabel)
|
|
|
|
mcon := selinux.NewContext(mountLabel)
|
|
|
|
for _, opt := range options {
|
2017-01-30 02:18:17 +08:00
|
|
|
if opt == "disable" {
|
2016-10-12 20:39:12 +08:00
|
|
|
return "", "", nil
|
2014-07-18 02:57:57 +08:00
|
|
|
}
|
2017-01-30 02:18:17 +08:00
|
|
|
if i := strings.Index(opt, ":"); i == -1 {
|
|
|
|
return "", "", fmt.Errorf("Bad label option %q, valid options 'disable' or \n'user, role, level, type' followed by ':' and a value", opt)
|
|
|
|
}
|
|
|
|
con := strings.SplitN(opt, ":", 2)
|
|
|
|
if !validOptions[con[0]] {
|
|
|
|
return "", "", fmt.Errorf("Bad label option %q, valid options 'disable, user, role, level, type'", con[0])
|
2015-10-06 20:51:54 +08:00
|
|
|
|
|
|
|
}
|
2014-07-18 02:57:57 +08:00
|
|
|
pcon[con[0]] = con[1]
|
|
|
|
if con[0] == "level" || con[0] == "user" {
|
|
|
|
mcon[con[0]] = con[1]
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 02:57:57 +08:00
|
|
|
processLabel = pcon.Get()
|
|
|
|
mountLabel = mcon.Get()
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
2014-10-29 08:49:57 +08:00
|
|
|
return processLabel, mountLabel, nil
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
|
|
|
|
2016-10-18 04:56:42 +08:00
|
|
|
func GetROMountLabel() string {
|
|
|
|
return selinux.GetROFileLabel()
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// DEPRECATED: The GenLabels function is only to be used during the transition to the official API.
|
|
|
|
func GenLabels(options string) (string, string, error) {
|
|
|
|
return InitLabels(strings.Fields(options))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatMountLabel returns a string to be used by the mount command.
|
|
|
|
// The format of this string will be used to alter the labeling of the mountpoint.
|
|
|
|
// The string returned is suitable to be used as the options field of the mount command.
|
|
|
|
// If you need to have additional mount point options, you can pass them in as
|
|
|
|
// the first parameter. Second parameter is the label that you wish to apply
|
|
|
|
// to all content in the mount point.
|
2014-06-10 06:52:12 +08:00
|
|
|
func FormatMountLabel(src, mountLabel string) string {
|
|
|
|
if mountLabel != "" {
|
|
|
|
switch src {
|
|
|
|
case "":
|
|
|
|
src = fmt.Sprintf("context=%q", mountLabel)
|
|
|
|
default:
|
|
|
|
src = fmt.Sprintf("%s,context=%q", src, mountLabel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// SetProcessLabel takes a process label and tells the kernel to assign the
|
|
|
|
// label to the next program executed by the current process.
|
2014-06-10 06:52:12 +08:00
|
|
|
func SetProcessLabel(processLabel string) error {
|
2014-10-11 04:16:00 +08:00
|
|
|
if processLabel == "" {
|
|
|
|
return nil
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
2014-10-11 04:16:00 +08:00
|
|
|
return selinux.Setexeccon(processLabel)
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// GetProcessLabel returns the process label that the kernel will assign
|
|
|
|
// to the next program executed by the current process. If "" is returned
|
|
|
|
// this indicates that the default labeling will happen for the process.
|
2014-06-10 06:52:12 +08:00
|
|
|
func GetProcessLabel() (string, error) {
|
2014-10-11 04:16:00 +08:00
|
|
|
return selinux.Getexeccon()
|
2014-06-10 06:52:12 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 04:28:38 +08:00
|
|
|
// GetFileLabel returns the label for specified path
|
|
|
|
func GetFileLabel(path string) (string, error) {
|
|
|
|
return selinux.Getfilecon(path)
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// SetFileLabel modifies the "path" label to the specified file label
|
2014-06-10 06:52:12 +08:00
|
|
|
func SetFileLabel(path string, fileLabel string) error {
|
|
|
|
if selinux.SelinuxEnabled() && fileLabel != "" {
|
|
|
|
return selinux.Setfilecon(path, fileLabel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// SetFileCreateLabel tells the kernel the label for all files to be created
|
2014-11-21 06:26:43 +08:00
|
|
|
func SetFileCreateLabel(fileLabel string) error {
|
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
return selinux.Setfscreatecon(fileLabel)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 16:12:23 +08:00
|
|
|
// Relabel changes the label of path to the filelabel string.
|
2015-07-31 04:36:35 +08:00
|
|
|
// It changes the MCS label to s0 if shared is true.
|
|
|
|
// This will allow all containers to share the content.
|
|
|
|
func Relabel(path string, fileLabel string, shared bool) error {
|
|
|
|
if !selinux.SelinuxEnabled() {
|
2014-06-24 23:30:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
|
|
|
|
if fileLabel == "" {
|
2015-06-10 04:35:16 +08:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
|
|
|
|
exclude_paths := map[string]bool{"/": true, "/usr": true, "/etc": true}
|
|
|
|
if exclude_paths[path] {
|
2016-09-14 22:39:53 +08:00
|
|
|
return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
|
2015-04-06 22:07:08 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
|
|
|
|
if shared {
|
2014-06-24 23:30:17 +08:00
|
|
|
c := selinux.NewContext(fileLabel)
|
|
|
|
c["level"] = "s0"
|
|
|
|
fileLabel = c.Get()
|
|
|
|
}
|
2016-09-14 22:39:53 +08:00
|
|
|
if err := selinux.Chcon(path, fileLabel, true); err != nil {
|
2017-02-22 22:21:33 +08:00
|
|
|
return err
|
2016-09-14 22:39:53 +08:00
|
|
|
}
|
|
|
|
return nil
|
2014-06-24 23:30:17 +08:00
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// GetPidLabel will return the label of the process running with the specified pid
|
|
|
|
func GetPidLabel(pid int) (string, error) {
|
2014-06-10 06:52:12 +08:00
|
|
|
return selinux.Getpidcon(pid)
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// Init initialises the labeling system
|
2014-06-10 06:52:12 +08:00
|
|
|
func Init() {
|
|
|
|
selinux.SelinuxEnabled()
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:57:57 +08:00
|
|
|
// ReserveLabel will record the fact that the MCS label has already been used.
|
|
|
|
// This will prevent InitLabels from using the MCS label in a newly created
|
|
|
|
// container
|
2014-06-10 06:52:12 +08:00
|
|
|
func ReserveLabel(label string) error {
|
|
|
|
selinux.ReserveLabel(label)
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-21 04:43:15 +08:00
|
|
|
|
|
|
|
// UnreserveLabel will remove the reservation of the MCS label.
|
|
|
|
// This will allow InitLabels to use the MCS label in a newly created
|
|
|
|
// containers
|
|
|
|
func UnreserveLabel(label string) error {
|
|
|
|
selinux.FreeLxcContexts(label)
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-29 08:49:57 +08:00
|
|
|
|
2016-11-30 13:27:50 +08:00
|
|
|
// DupSecOpt takes a process label and returns security options that
|
2014-10-29 08:49:57 +08:00
|
|
|
// can be used to set duplicate labels on future container processes
|
|
|
|
func DupSecOpt(src string) []string {
|
|
|
|
return selinux.DupSecOpt(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableSecOpt returns a security opt that can disable labeling
|
|
|
|
// support for future container processes
|
|
|
|
func DisableSecOpt() []string {
|
|
|
|
return selinux.DisableSecOpt()
|
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
|
|
|
|
// Validate checks that the label does not include unexpected options
|
|
|
|
func Validate(label string) error {
|
|
|
|
if strings.Contains(label, "z") && strings.Contains(label, "Z") {
|
|
|
|
return ErrIncompatibleLabel
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-29 03:44:38 +08:00
|
|
|
// RelabelNeeded checks whether the user requested a relabel
|
|
|
|
func RelabelNeeded(label string) bool {
|
|
|
|
return strings.Contains(label, "z") || strings.Contains(label, "Z")
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:36:35 +08:00
|
|
|
// IsShared checks that the label includes a "shared" mark
|
|
|
|
func IsShared(label string) bool {
|
|
|
|
return strings.Contains(label, "z")
|
|
|
|
}
|