Skip to content

Commit 6d93352

Browse files
committed
open context files lazily
1 parent fb43b05 commit 6d93352

File tree

2 files changed

+62
-42
lines changed

2 files changed

+62
-42
lines changed

go-selinux/selinux_linux.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ type mlsRange struct {
5454
high *level
5555
}
5656

57+
type openReaderCloser func() (io.ReadCloser, error)
58+
59+
func createOpener(path string) openReaderCloser {
60+
return func() (io.ReadCloser, error) {
61+
return os.Open(path)
62+
}
63+
}
64+
5765
type defaultSECtx struct {
58-
userRdr io.Reader
66+
openUserRdr openReaderCloser
5967
verifier func(string) error
60-
defaultRdr io.Reader
61-
failsafeRdr io.Reader
68+
openDefaultRdr openReaderCloser
69+
openFailsafeRdr openReaderCloser
6270
user, level, scon string
6371
}
6472

@@ -1391,7 +1399,13 @@ func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
13911399
context["user"] = c.user
13921400
context["level"] = c.level
13931401

1394-
conn, err := findUserInContext(context, c.userRdr, c.verifier)
1402+
userRdr, err := c.openUserRdr()
1403+
if err != nil {
1404+
return "", fmt.Errorf("failed to open user context file: %w", err)
1405+
}
1406+
defer userRdr.Close()
1407+
1408+
conn, err := findUserInContext(context, userRdr, c.verifier)
13951409
if err != nil {
13961410
return "", fmt.Errorf("failed to read %q's user context file: %w", c.user, err)
13971411
}
@@ -1400,7 +1414,13 @@ func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
14001414
return conn, nil
14011415
}
14021416

1403-
conn, err = findUserInContext(context, c.defaultRdr, c.verifier)
1417+
defaultRdr, err := c.openDefaultRdr()
1418+
if err != nil {
1419+
return "", fmt.Errorf("failed to open default context file: %w", err)
1420+
}
1421+
defer defaultRdr.Close()
1422+
1423+
conn, err = findUserInContext(context, defaultRdr, c.verifier)
14041424
if err != nil {
14051425
return "", fmt.Errorf("failed to read default user context file: %w", err)
14061426
}
@@ -1409,7 +1429,13 @@ func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
14091429
return conn, nil
14101430
}
14111431

1412-
conn, err = getFailsafeContext(context, c.failsafeRdr, c.verifier)
1432+
failsafeRdr, err := c.openFailsafeRdr()
1433+
if err != nil {
1434+
return "", fmt.Errorf("failed to open failsafe context file: %w", err)
1435+
}
1436+
defer failsafeRdr.Close()
1437+
1438+
conn, err = getFailsafeContext(context, failsafeRdr, c.verifier)
14131439
if err != nil {
14141440
return "", fmt.Errorf("failed to read failsafe_context: %w", err)
14151441
}
@@ -1423,34 +1449,17 @@ func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
14231449

14241450
func getDefaultContextWithLevel(user, level, scon string) (string, error) {
14251451
userPath := filepath.Join(policyRoot(), selinuxUsersDir, user)
1426-
fu, err := os.Open(userPath)
1427-
if err != nil {
1428-
return "", fmt.Errorf("failed to open %q's user context file: %w", user, err)
1429-
}
1430-
defer fu.Close()
1431-
14321452
defaultPath := filepath.Join(policyRoot(), defaultContexts)
1433-
fd, err := os.Open(defaultPath)
1434-
if err != nil {
1435-
return "", fmt.Errorf("failed to open default user context file: %w", err)
1436-
}
1437-
defer fd.Close()
1438-
14391453
failsafePath := filepath.Join(policyRoot(), failsafeContext)
1440-
fs, err := os.Open(failsafePath)
1441-
if err != nil {
1442-
return "", fmt.Errorf("failed to open failsafe user context file: %w", err)
1443-
}
1444-
defer fs.Close()
14451454

14461455
c := defaultSECtx{
1447-
user: user,
1448-
level: level,
1449-
scon: scon,
1450-
userRdr: fu,
1451-
defaultRdr: fd,
1452-
failsafeRdr: fs,
1453-
verifier: securityCheckContext,
1456+
user: user,
1457+
level: level,
1458+
scon: scon,
1459+
openUserRdr: createOpener(userPath),
1460+
openDefaultRdr: createOpener(defaultPath),
1461+
openFailsafeRdr: createOpener(failsafePath),
1462+
verifier: securityCheckContext,
14541463
}
14551464

14561465
return getDefaultContextFromReaders(&c)

go-selinux/selinux_linux_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"os/user"
1011
"path/filepath"
@@ -782,12 +783,16 @@ fake_r:fake_t:s0 baz_r:baz_t:s0 sysadm_r:sysadm_t:s0
782783
for _, tt := range tests {
783784
t.Run(tt.name, func(t *testing.T) {
784785
c := defaultSECtx{
785-
user: "bob",
786-
level: "SystemLow-SystemHigh",
787-
scon: "system_u:staff_r:staff_t:s0",
788-
userRdr: bytes.NewBufferString(tt.userBuff),
789-
defaultRdr: bytes.NewBufferString(tt.defaultBuff),
790-
verifier: verifier,
786+
user: "bob",
787+
level: "SystemLow-SystemHigh",
788+
scon: "system_u:staff_r:staff_t:s0",
789+
openUserRdr: func() (io.ReadCloser, error) {
790+
return io.NopCloser(bytes.NewBufferString(tt.userBuff)), nil
791+
},
792+
openDefaultRdr: func() (io.ReadCloser, error) {
793+
return io.NopCloser(bytes.NewBufferString(tt.defaultBuff)), nil
794+
},
795+
verifier: verifier,
791796
}
792797

793798
got, err := getDefaultContextFromReaders(&c)
@@ -809,12 +814,18 @@ fake_r:fake_t:s0 baz_r:baz_t:s0 sysadm_r:sysadm_t:s0
809814
dne_r:dne_t:s0 baz_r:baz_t:s0 sysadm_r:sysadm_t:s0
810815
`
811816
c := defaultSECtx{
812-
user: "bob",
813-
level: "SystemLow-SystemHigh",
814-
scon: "system_u:staff_r:staff_t:s0",
815-
userRdr: bytes.NewBufferString(badUserBuff),
816-
defaultRdr: bytes.NewBufferString(badDefaultBuff),
817-
failsafeRdr: bytes.NewBufferString(goodFailsafeBuff),
817+
user: "bob",
818+
level: "SystemLow-SystemHigh",
819+
scon: "system_u:staff_r:staff_t:s0",
820+
openUserRdr: func() (io.ReadCloser, error) {
821+
return io.NopCloser(bytes.NewBufferString(badUserBuff)), nil
822+
},
823+
openDefaultRdr: func() (io.ReadCloser, error) {
824+
return io.NopCloser(bytes.NewBufferString(badDefaultBuff)), nil
825+
},
826+
openFailsafeRdr: func() (io.ReadCloser, error) {
827+
return io.NopCloser(bytes.NewBufferString(goodFailsafeBuff)), nil
828+
},
818829
verifier: func(s string) error {
819830
return nil
820831
},

0 commit comments

Comments
 (0)