@@ -20,18 +20,31 @@ const SELINUXFS: &str = "/sys/fs/selinux";
20
20
/// The SELinux xattr
21
21
#[ cfg( feature = "install" ) ]
22
22
const SELINUX_XATTR : & [ u8 ] = b"security.selinux\0 " ;
23
+ const SELF_CURRENT : & str = "/proc/self/attr/current" ;
23
24
24
25
#[ context( "Querying selinux availability" ) ]
25
26
pub ( crate ) fn selinux_enabled ( ) -> Result < bool > {
26
27
let filesystems = std:: fs:: read_to_string ( "/proc/filesystems" ) ?;
27
28
Ok ( filesystems. contains ( "selinuxfs\n " ) )
28
29
}
29
30
31
+ /// Get the current process SELinux security context
32
+ fn get_current_security_context ( ) -> Result < String > {
33
+ std:: fs:: read_to_string ( SELF_CURRENT ) . with_context ( || format ! ( "Reading {SELF_CURRENT}" ) )
34
+ }
35
+
36
+ /// Determine if a security context is the "install_t" type which can
37
+ /// write arbitrary labels.
38
+ fn context_is_install_t ( context : & str ) -> bool {
39
+ // TODO: we shouldn't actually hardcode this...it's just ugly though
40
+ // to figure out whether we really can gain CAP_MAC_ADMIN.
41
+ context. contains ( ":install_t:" )
42
+ }
43
+
30
44
#[ context( "Ensuring selinux install_t type" ) ]
31
45
pub ( crate ) fn selinux_ensure_install ( ) -> Result < ( ) > {
32
46
let guardenv = "_bootc_selinuxfs_mounted" ;
33
- let current = std:: fs:: read_to_string ( "/proc/self/attr/current" )
34
- . context ( "Reading /proc/self/attr/current" ) ?;
47
+ let current = get_current_security_context ( ) ?;
35
48
tracing:: debug!( "Current security context is {current}" ) ;
36
49
if let Some ( p) = std:: env:: var_os ( guardenv) {
37
50
let p = Path :: new ( & p) ;
@@ -85,13 +98,13 @@ impl Drop for SetEnforceGuard {
85
98
#[ cfg( feature = "install" ) ]
86
99
pub ( crate ) fn selinux_ensure_install_or_setenforce ( ) -> Result < Option < SetEnforceGuard > > {
87
100
// If the process already has install_t, exit early
88
- if self_has_install_t ( ) ? {
101
+ let current = get_current_security_context ( ) ?;
102
+ if context_is_install_t ( & current) {
89
103
return Ok ( None ) ;
90
104
}
105
+ // Note that this will re-exec the entire process
91
106
selinux_ensure_install ( ) ?;
92
- let current = std:: fs:: read_to_string ( "/proc/self/attr/current" )
93
- . context ( "Reading /proc/self/attr/current" ) ?;
94
- let g = if !current. contains ( "install_t" ) {
107
+ let g = if !context_is_install_t ( & current) {
95
108
tracing:: warn!( "Failed to enter install_t; temporarily setting permissive mode" ) ;
96
109
selinux_set_permissive ( true ) ?;
97
110
Some ( SetEnforceGuard )
@@ -174,10 +187,3 @@ pub(crate) fn xattrs_have_selinux(xattrs: &ostree::glib::Variant) -> bool {
174
187
}
175
188
false
176
189
}
177
-
178
- fn self_has_install_t ( ) -> Result < bool > {
179
- let current = std:: fs:: read_to_string ( "/proc/self/attr/current" )
180
- . context ( "Reading /proc/self/attr/current" ) ?;
181
-
182
- Ok ( current. contains ( "install_t" ) )
183
- }
0 commit comments