Fix EnforceMode, SetEnforceMode, and SecurityCheckContext#64
Fix EnforceMode, SetEnforceMode, and SecurityCheckContext#64rhatdan merged 4 commits intoopencontainers:masterfrom
Conversation
Use the value from x/sys/unix directly. While at it, remove buf.Type typecast, since we're comparing it with an (untyped) const, so it should not bring any warnings on e.g. 32-bit ARM. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
The part of TestSELinux test case fails: > selinux_linux_test.go:72: enforcing selinux failed: open /sys/fs/selinux/enforce: permission denied The problem is, one needs to be root to open this file rw. Let's move this code to a separate test case, adding a Skip() if run under non-root. That way, we will: * not fail tests if run under non-root; * see that this test case was NOT run. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
|
What's missing is a test case for |
|
LGTM |
|
NB I'm not familiar with this code so take this with a grain of salt. |
While working on minor improvements to tests, I found out that SetEnforceMode() is not working even for the root user: > --- FAIL: TestSetEnforceMode (0.00s) > selinux_linux_test.go:94: Enforcing Mode: -1 > selinux_linux_test.go:96: Default Enforce Mode: 1 > selinux_linux_test.go:100: setting selinux mode to enforcing failed: /sys/fs/selinux/enforce not on procfs If you look closer, you'll see that EnforceMode() is not working either, since it returns -1. The problem is, these functions, as well as SecurityCheckContext(), are using readCon() and writeCon() methods, which require the files being read/written to be on `procfs`. In these cases, though, the files are on `selinuxfs`, and the filesystem check is not really required since we already checked that during selinuxfs mount point search. So, just use ioutil.ReadFile/WriteFile here. While at it - convert code to use path.Join() instead of fmt.Sprintf() - add a test case for SecurityCheckContext Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Simplify isProcHandle() to only return an error, making it easier to use. Improve errors from isProcHandle (and so from readCon/writeCon) to provide the file name in case of fstatfs() error. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
You're quite right, in fact there's no need to check for selinuxfs magic since it was checked already when we searched for selinuxfs mount point. I've updated this PR to do just that.
Thanks, I've added a test case.
I guess this is out of scope for this PR, so I'll create a separate one for that |
Here: #66 |
|
@stephensmalley We wanted a pure Go library for handling SELinux functions. Using libselinux with this would have pulled in C Bindings which can cause issues. So years ago it was decided to write the functions required by selinux in native go. |
|
Just found out this was a recent breakage, caused by #59 |
|
@kolyshkin this change is causing a problem in kubernetes : https://prow.k8s.io/view/gcs/kubernetes-jenkins/pr-logs/pull/86975/pull-kubernetes-typecheck/1232997816310173697 |
|
@dims looks like 86a2c3f#diff-dce803d0d70ed84c964812cea9579bc4 removed the Probably good to add a comment when fixing; I can open a quick PR |
|
oh I see it's fixed on master through #71 |
|
yep. thanks for checking :) |
While working on minor improvements to tests, I found out
that
SetEnforceMode()is not working even for the root user:If you look closer, you'll see that
EnforceMode()is not working either,since it returns
-1.The problem is, these functions, as well as
SecurityCheckContext(),are using
readCon()andwriteCon(), which require the filesbeing read/written to be on
procfs.In these cases, though, the files are on
selinuxfs, and the filesystemcheck is not really required since we already checked that during
selinuxfs mount point search.
So, just use ioutil.ReadFile/WriteFile here.
While at it
path.Join()instead offmt.Sprintf()SecurityCheckContextOther improvements in this PR:
TestSetEnforceMode: separate and fix for non-rootisProcHandle()to only return an error (and therefore simplify its usage)isProcHandle(and thusreadCon()/writeCon())to provide file name in case of
fstatfs()errorRemove SelinuxfsMagicconstant, use one fromx/sys/unix.@rhatdan @mrunalp @stephensmalley PTAL