@@ -7,9 +7,12 @@ import (
77 "fmt"
88 "os"
99 "path/filepath"
10+ "runtime"
1011 "strconv"
1112 "strings"
1213 "testing"
14+
15+ "golang.org/x/sys/unix"
1316)
1417
1518func TestSetFileLabel (t * testing.T ) {
@@ -187,6 +190,12 @@ func TestSocketLabel(t *testing.T) {
187190 t .Skip ("SELinux not enabled, skipping." )
188191 }
189192
193+ // Ensure the thread stays the same for duration of the test.
194+ // Otherwise Go runtime can switch this to a different thread,
195+ // which results in EACCES in call to SetSocketLabel.
196+ runtime .LockOSThread ()
197+ defer runtime .UnlockOSThread ()
198+
190199 label := "system_u:object_r:container_t:s0:c1,c2"
191200 if err := SetSocketLabel (label ); err != nil {
192201 t .Fatal (err )
@@ -205,6 +214,16 @@ func TestKeyLabel(t *testing.T) {
205214 t .Skip ("SELinux not enabled, skipping." )
206215 }
207216
217+ // Ensure the thread stays the same for duration of the test.
218+ // Otherwise Go runtime can switch this to a different thread,
219+ // which results in EACCES in call to SetKeyLabel.
220+ runtime .LockOSThread ()
221+ defer runtime .UnlockOSThread ()
222+
223+ if unix .Getpid () != unix .Gettid () {
224+ t .Skip (ErrNotTGLeader )
225+ }
226+
208227 label := "system_u:object_r:container_t:s0:c1,c2"
209228 if err := SetKeyLabel (label ); err != nil {
210229 t .Fatal (err )
@@ -235,6 +254,12 @@ func TestSELinux(t *testing.T) {
235254 t .Skip ("SELinux not enabled, skipping." )
236255 }
237256
257+ // Ensure the thread stays the same for duration of the test.
258+ // Otherwise Go runtime can switch this to a different thread,
259+ // which results in EACCES in call to SetFSCreateLabel.
260+ runtime .LockOSThread ()
261+ defer runtime .UnlockOSThread ()
262+
238263 var (
239264 err error
240265 plabel , flabel string
@@ -259,21 +284,17 @@ func TestSELinux(t *testing.T) {
259284 ReleaseLabel (plabel )
260285
261286 pid := os .Getpid ()
262- t .Logf ("PID:%d MCS:%s\n " , pid , intToMcs (pid , 1023 ))
287+ t .Logf ("PID:%d MCS:%s" , pid , intToMcs (pid , 1023 ))
263288 err = SetFSCreateLabel ("unconfined_u:unconfined_r:unconfined_t:s0" )
264- if err == nil {
265- t .Log (FSCreateLabel ())
266- } else {
267- t .Log ("SetFSCreateLabel failed" , err )
268- t .Fatal (err )
289+ if err != nil {
290+ t .Fatal ("SetFSCreateLabel failed:" , err )
269291 }
292+ t .Log (FSCreateLabel ())
270293 err = SetFSCreateLabel ("" )
271- if err == nil {
272- t .Log (FSCreateLabel ())
273- } else {
274- t .Log ("SetFSCreateLabel failed" , err )
275- t .Fatal (err )
294+ if err != nil {
295+ t .Fatal ("SetFSCreateLabel failed:" , err )
276296 }
297+ t .Log (FSCreateLabel ())
277298 t .Log (PidLabel (1 ))
278299}
279300
0 commit comments