2015-11-26 22:14:51 +08:00
|
|
|
// +build linux,selinux
|
2014-07-17 07:24:28 +08:00
|
|
|
|
2014-06-10 06:52:12 +08:00
|
|
|
package selinux_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-07-17 07:24:28 +08:00
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/selinux"
|
2014-06-10 06:52:12 +08:00
|
|
|
)
|
|
|
|
|
2015-11-28 16:21:46 +08:00
|
|
|
func TestSetfilecon(t *testing.T) {
|
2014-06-10 06:52:12 +08:00
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
tmp := "selinux_test"
|
2016-04-08 04:28:38 +08:00
|
|
|
con := "system_u:object_r:bin_t:s0"
|
2015-11-28 16:21:46 +08:00
|
|
|
out, _ := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE, 0)
|
2014-06-10 06:52:12 +08:00
|
|
|
out.Close()
|
2016-04-08 04:28:38 +08:00
|
|
|
err := selinux.Setfilecon(tmp, con)
|
2014-06-10 06:52:12 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Log("Setfilecon failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-08 04:28:38 +08:00
|
|
|
filecon, err := selinux.Getfilecon(tmp)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Getfilecon failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if con != filecon {
|
|
|
|
t.Fatal("Getfilecon failed, returned %s expected %s", filecon, con)
|
|
|
|
}
|
|
|
|
|
2014-06-10 06:52:12 +08:00
|
|
|
os.Remove(tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSELinux(t *testing.T) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
plabel, flabel string
|
|
|
|
)
|
|
|
|
|
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
t.Log("Enabled")
|
|
|
|
plabel, flabel = selinux.GetLxcContexts()
|
|
|
|
t.Log(plabel)
|
|
|
|
t.Log(flabel)
|
|
|
|
selinux.FreeLxcContexts(plabel)
|
|
|
|
plabel, flabel = selinux.GetLxcContexts()
|
|
|
|
t.Log(plabel)
|
|
|
|
t.Log(flabel)
|
|
|
|
selinux.FreeLxcContexts(plabel)
|
|
|
|
t.Log("getenforce ", selinux.SelinuxGetEnforce())
|
2016-01-08 07:44:01 +08:00
|
|
|
mode := selinux.SelinuxGetEnforceMode()
|
|
|
|
t.Log("getenforcemode ", mode)
|
|
|
|
|
|
|
|
defer selinux.SelinuxSetEnforce(mode)
|
|
|
|
if err := selinux.SelinuxSetEnforce(selinux.Enforcing); err != nil {
|
|
|
|
t.Fatalf("enforcing selinux failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := selinux.SelinuxSetEnforce(selinux.Permissive); err != nil {
|
|
|
|
t.Fatalf("setting selinux mode to permissive failed: %v", err)
|
|
|
|
}
|
|
|
|
selinux.SelinuxSetEnforce(mode)
|
|
|
|
|
2014-06-10 06:52:12 +08:00
|
|
|
pid := os.Getpid()
|
2014-11-06 00:56:47 +08:00
|
|
|
t.Logf("PID:%d MCS:%s\n", pid, selinux.IntToMcs(pid, 1023))
|
2014-06-10 06:52:12 +08:00
|
|
|
err = selinux.Setfscreatecon("unconfined_u:unconfined_r:unconfined_t:s0")
|
|
|
|
if err == nil {
|
|
|
|
t.Log(selinux.Getfscreatecon())
|
|
|
|
} else {
|
|
|
|
t.Log("setfscreatecon failed", err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = selinux.Setfscreatecon("")
|
|
|
|
if err == nil {
|
|
|
|
t.Log(selinux.Getfscreatecon())
|
|
|
|
} else {
|
|
|
|
t.Log("setfscreatecon failed", err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(selinux.Getpidcon(1))
|
|
|
|
} else {
|
|
|
|
t.Log("Disabled")
|
|
|
|
}
|
|
|
|
}
|