@@ -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+
5765type 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
14241450func 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 )
0 commit comments