@@ -41,8 +41,18 @@ fn context_is_install_t(context: &str) -> bool {
41
41
context. contains ( ":install_t:" )
42
42
}
43
43
44
+ #[ context( "Testing install_t" ) ]
45
+ fn test_install_t ( ) -> Result < bool > {
46
+ let tmpf = tempfile:: NamedTempFile :: new ( ) ?;
47
+ let st = Command :: new ( "chcon" )
48
+ . args ( [ "-t" , "invalid_bootcinstall_testlabel_t" ] )
49
+ . arg ( tmpf. path ( ) )
50
+ . status ( ) ?;
51
+ Ok ( st. success ( ) )
52
+ }
53
+
44
54
#[ context( "Ensuring selinux install_t type" ) ]
45
- pub ( crate ) fn selinux_ensure_install ( ) -> Result < ( ) > {
55
+ pub ( crate ) fn selinux_ensure_install ( ) -> Result < bool > {
46
56
let guardenv = "_bootc_selinuxfs_mounted" ;
47
57
let current = get_current_security_context ( ) ?;
48
58
tracing:: debug!( "Current security context is {current}" ) ;
@@ -54,9 +64,13 @@ pub(crate) fn selinux_ensure_install() -> Result<()> {
54
64
} else {
55
65
tracing:: debug!( "Assuming we now have a privileged (e.g. install_t) label" ) ;
56
66
}
57
- return Ok ( ( ) ) ;
67
+ return test_install_t ( ) ;
68
+ }
69
+ if test_install_t ( ) ? {
70
+ tracing:: debug!( "We have install_t" ) ;
71
+ return Ok ( true ) ;
58
72
}
59
- tracing:: debug!( "Copying self to temporary file for re-exec" ) ;
73
+ tracing:: debug!( "Lacking install_t capabilities; copying self to temporary file for re-exec" ) ;
60
74
// OK now, we always copy our binary to a tempfile, set its security context
61
75
// to match that of /usr/bin/ostree, and then re-exec. This is really a gross
62
76
// hack; we can't always rely on https://github.com/fedora-selinux/selinux-policy/pull/1500/commits/67eb283c46d35a722636d749e5b339615fe5e7f5
@@ -102,18 +116,16 @@ pub(crate) fn selinux_ensure_install_or_setenforce() -> Result<Option<SetEnforce
102
116
if context_is_install_t ( & current) {
103
117
return Ok ( None ) ;
104
118
}
105
- // Note that this will re-exec the entire process
106
- selinux_ensure_install ( ) ?;
107
- let g = if !context_is_install_t ( & current) {
108
- if std:: env:: var_os ( "BOOTC_SETENFORCE0_FALLBACK" ) . is_some ( ) {
109
- tracing:: warn!( "Failed to enter install_t; temporarily setting permissive mode" ) ;
110
- selinux_set_permissive ( true ) ?;
111
- Some ( SetEnforceGuard )
112
- } else {
113
- anyhow:: bail!( "Failed to enter install_t (running as {current}) - use BOOTC_SETENFORCE0_FALLBACK=1 to override" ) ;
114
- }
119
+ // Note that this may re-exec the entire process
120
+ if selinux_ensure_install ( ) ? {
121
+ return Ok ( None ) ;
122
+ }
123
+ let g = if std:: env:: var_os ( "BOOTC_SETENFORCE0_FALLBACK" ) . is_some ( ) {
124
+ tracing:: warn!( "Failed to enter install_t; temporarily setting permissive mode" ) ;
125
+ selinux_set_permissive ( true ) ?;
126
+ Some ( SetEnforceGuard )
115
127
} else {
116
- None
128
+ anyhow :: bail! ( "Failed to enter install_t (running as {current}) - use BOOTC_SETENFORCE0_FALLBACK=1 to override" ) ;
117
129
} ;
118
130
Ok ( g)
119
131
}
0 commit comments