@@ -7,9 +7,12 @@ import (
7
7
"fmt"
8
8
"os"
9
9
"path/filepath"
10
+ "runtime"
10
11
"strconv"
11
12
"strings"
12
13
"testing"
14
+
15
+ "golang.org/x/sys/unix"
13
16
)
14
17
15
18
func TestSetFileLabel (t * testing.T ) {
@@ -187,6 +190,12 @@ func TestSocketLabel(t *testing.T) {
187
190
t .Skip ("SELinux not enabled, skipping." )
188
191
}
189
192
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
+
190
199
label := "system_u:object_r:container_t:s0:c1,c2"
191
200
if err := SetSocketLabel (label ); err != nil {
192
201
t .Fatal (err )
@@ -205,6 +214,16 @@ func TestKeyLabel(t *testing.T) {
205
214
t .Skip ("SELinux not enabled, skipping." )
206
215
}
207
216
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
+
208
227
label := "system_u:object_r:container_t:s0:c1,c2"
209
228
if err := SetKeyLabel (label ); err != nil {
210
229
t .Fatal (err )
@@ -235,6 +254,12 @@ func TestSELinux(t *testing.T) {
235
254
t .Skip ("SELinux not enabled, skipping." )
236
255
}
237
256
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
+
238
263
var (
239
264
err error
240
265
plabel , flabel string
@@ -259,21 +284,17 @@ func TestSELinux(t *testing.T) {
259
284
ReleaseLabel (plabel )
260
285
261
286
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 ))
263
288
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 )
269
291
}
292
+ t .Log (FSCreateLabel ())
270
293
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 )
276
296
}
297
+ t .Log (FSCreateLabel ())
277
298
t .Log (PidLabel (1 ))
278
299
}
279
300
0 commit comments