@@ -10,9 +10,10 @@ use core::{
1010use core:: cell:: Cell ;
1111
1212use super :: {
13- CanaryCorruptedError , ForkPolicy , ForkProtectionRequest , ProtectedSecretFillError ,
14- ProtectionControl , ProtectionError , ProtectionFailure , ProtectionReport , ProtectionRequest ,
15- ProtectionState , Requirement , RollbackReport , RollbackState , SecretIntegrityError ,
13+ protection:: replacement_request_preserving_established, CanaryCorruptedError , ForkPolicy ,
14+ ForkProtectionRequest , ProtectedSecretFillError , ProtectionControl , ProtectionError ,
15+ ProtectionFailure , ProtectionReport , ProtectionRequest , ProtectionState , Requirement ,
16+ RollbackReport , RollbackState , SecretIntegrityError ,
1617} ;
1718
1819#[ cfg( all(
@@ -1067,6 +1068,11 @@ impl GuardedSecretVec {
10671068 self . request
10681069 }
10691070
1071+ #[ cfg( all( test, target_os = "linux" , not( feature = "memory-lock" ) ) ) ]
1072+ pub ( crate ) fn mark_memory_lock_established_for_replacement_test ( & mut self ) {
1073+ self . report . memory_lock = ProtectionState :: Established ;
1074+ }
1075+
10701076 /// Run a closure with read-only access to initialized secret bytes.
10711077 #[ inline]
10721078 pub fn try_with_secret < R > (
@@ -1122,17 +1128,18 @@ impl GuardedSecretVec {
11221128 /// If the current guarded mapping is large enough, the old writable
11231129 /// region is cleared before the new bytes are copied in. If the new
11241130 /// value requires a larger mapping, a replacement mapping is allocated
1125- /// with the same lock state, populated with the new bytes, and then the
1126- /// old mapping is cleared before it is unmapped.
1131+ /// without permitting an established preferred control to downgrade,
1132+ /// populated with the new bytes, and then the old mapping is cleared
1133+ /// before it is unmapped.
11271134 pub fn try_replace_from_slice (
11281135 & mut self ,
11291136 bytes : & [ u8 ] ,
11301137 ) -> Result < ( ) , SecretIntegrityError < GuardPageError > > {
11311138 self . verify_integrity ( ) ?;
11321139
11331140 if bytes. len ( ) > self . data_capacity {
1134- let mut replacement = Self :: with_capacity_with_protection ( bytes . len ( ) , self . request )
1135- . map_err ( protection_error_as_guard_page )
1141+ let mut replacement = self
1142+ . replacement_with_capacity ( bytes . len ( ) )
11361143 . map_err ( SecretIntegrityError :: Operation ) ?;
11371144 replacement. as_mut_capacity_slice ( ) [ ..bytes. len ( ) ] . copy_from_slice ( bytes) ;
11381145 replacement. finish_initialization ( bytes. len ( ) ) ;
@@ -1150,16 +1157,17 @@ impl GuardedSecretVec {
11501157
11511158 /// Replace all initialized secret bytes with generated bytes.
11521159 ///
1153- /// A replacement mapping is allocated with the same lock state and
1154- /// populated before the old mapping is cleared and swapped out.
1160+ /// A replacement mapping is allocated without permitting an established
1161+ /// preferred control to downgrade, then populated before the old mapping
1162+ /// is cleared and swapped out.
11551163 pub fn try_replace_from_fn (
11561164 & mut self ,
11571165 len : usize ,
11581166 mut make_byte : impl FnMut ( usize ) -> u8 ,
11591167 ) -> Result < ( ) , SecretIntegrityError < GuardPageError > > {
11601168 self . verify_integrity ( ) ?;
1161- let mut replacement = Self :: with_capacity_with_protection ( len , self . request )
1162- . map_err ( protection_error_as_guard_page )
1169+ let mut replacement = self
1170+ . replacement_with_capacity ( len )
11631171 . map_err ( SecretIntegrityError :: Operation ) ?;
11641172 replacement. fill_from_fn ( len, & mut make_byte) ;
11651173
@@ -1179,8 +1187,8 @@ impl GuardedSecretVec {
11791187 mut make_byte : impl FnMut ( usize ) -> Result < u8 , E > ,
11801188 ) -> Result < ( ) , SecretIntegrityError < GuardedSecretVecGenerateError < E > > > {
11811189 self . verify_integrity ( ) ?;
1182- let mut replacement = Self :: with_capacity_with_protection ( len , self . request )
1183- . map_err ( protection_error_as_guard_page )
1190+ let mut replacement = self
1191+ . replacement_with_capacity ( len )
11841192 . map_err ( GuardedSecretVecGenerateError :: Guard )
11851193 . map_err ( SecretIntegrityError :: Operation ) ?;
11861194 replacement
@@ -1264,8 +1272,8 @@ impl GuardedSecretVec {
12641272 . saturating_mul ( 2 )
12651273 . max ( required)
12661274 . max ( page_granule) ;
1267- let mut replacement = Self :: with_capacity_with_protection ( next_capacity , self . request )
1268- . map_err ( protection_error_as_guard_page )
1275+ let mut replacement = self
1276+ . replacement_with_capacity ( next_capacity )
12691277 . map_err ( SecretIntegrityError :: Operation ) ?;
12701278 replacement. as_mut_capacity_slice ( ) [ ..self . len ] . copy_from_slice ( self . as_slice ( ) ) ;
12711279 replacement. finish_initialization ( self . len ) ;
@@ -1275,6 +1283,16 @@ impl GuardedSecretVec {
12751283 Ok ( ( ) )
12761284 }
12771285
1286+ fn replacement_with_capacity ( & self , capacity : usize ) -> Result < Self , GuardPageError > {
1287+ let request = self . request ;
1288+ let replacement_request =
1289+ replacement_request_preserving_established ( request, & self . report , capacity) ;
1290+ let mut replacement = Self :: with_capacity_with_protection ( capacity, replacement_request)
1291+ . map_err ( protection_error_as_guard_page) ?;
1292+ replacement. request = request;
1293+ Ok ( replacement)
1294+ }
1295+
12781296 /// Run a closure with shared access, panicking on canary corruption.
12791297 #[ inline]
12801298 pub fn with_secret_or_panic < R > ( & self , inspect : impl FnOnce ( & [ u8 ] ) -> R ) -> R {
0 commit comments