@@ -19,7 +19,6 @@ static LISTP_TYPE(libos_encrypted_files_key) g_keys = LISTP_INIT;
19
19
20
20
/* Protects the `g_keys` list, but also individual keys, since they can be updated */
21
21
static struct libos_lock g_keys_lock ;
22
-
23
22
static LISTP_TYPE (libos_encrypted_volume ) g_volumes = LISTP_INIT ;
24
23
25
24
/* Protects the `g_volumes` list. */
@@ -277,14 +276,19 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
277
276
ret = - EACCES ;
278
277
goto out ;
279
278
}
279
+ libos_encrypted_file_state_t new_state_in_map = PF_FILE_STATE_ACTIVE ;
280
280
pf_mac_t opening_root_mac ;
281
281
pf_status_t pfs = pf_open (pal_handle , norm_path , size , PF_FILE_MODE_READ | PF_FILE_MODE_WRITE ,
282
282
create , & enc -> volume -> key -> pf_key , & opening_root_mac , & pf );
283
283
unlock (& g_keys_lock );
284
284
if (PF_FAILURE (pfs )) {
285
- log_warning ("pf_open failed: %s" , pf_strerror (pfs ));
286
285
ret = - EACCES ;
287
- goto out ;
286
+ if (pfs != PF_STATUS_CORRUPTED ) {
287
+ log_warning ("pf_open failed: %s" , pf_strerror (pfs ));
288
+ goto out ;
289
+ }
290
+ log_error ("pf_open of file '%s' encountered corrupted state during open" , norm_path );
291
+ new_state_in_map = PF_FILE_STATE_ERROR ;
288
292
}
289
293
290
294
/* rollback protection */
@@ -294,53 +298,58 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
294
298
lock (& (enc -> volume -> files_state_map_lock ));
295
299
/* - get current state */
296
300
HASH_FIND_STR (enc -> volume -> files_state_map , norm_path , file_state );
297
- /* - check current state */
298
- if (create ) {
299
- if (file_state && (file_state -> state != PF_FILE_STATE_DELETED )) {
300
- log_error ("newly created file '%s' is in state %s" , norm_path ,
301
- file_state_to_string (file_state -> state ));
302
- if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
303
- pf_set_corrupted (pf );
304
- ret = - EEXIST ;
305
- goto out_unlock_map ;
306
- }
307
- }
308
- } else {
309
- if (file_state ) {
310
- if ((file_state -> state == PF_FILE_STATE_ERROR ) ||
311
- (file_state -> state == PF_FILE_STATE_DELETED )) {
312
- log_error ("file '%s' was seen before but in %s state" , norm_path ,
301
+ if (new_state_in_map != PF_FILE_STATE_ERROR ) {
302
+ /* - check current state */
303
+ if (create ) {
304
+ if (file_state && (file_state -> state != PF_FILE_STATE_DELETED )) {
305
+ // Note: with create=true we want to open without overwriting, so only valid state
306
+ // for an existing map entry is if the file was known to be deleted.
307
+ log_error ("newly created file '%s' is in state %s" , norm_path ,
313
308
file_state_to_string (file_state -> state ));
314
309
if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
315
310
pf_set_corrupted (pf );
316
- ret = - EACCES ;
317
- goto out_unlock_map ;
311
+ ret = - EEXIST ;
312
+ new_state_in_map = PF_FILE_STATE_ERROR ;
318
313
}
319
314
}
320
- if (memcmp (file_state -> last_seen_root_mac , opening_root_mac , sizeof (pf_mac_t )) != 0 ) {
321
- log_error (
322
- "file '%s' was seen before but in different inconsistent (rolled-back?) "
323
- "state, expected MAC=" MAC_PRINTF_PATTERN
324
- " but file had "
325
- "MAC=" MAC_PRINTF_PATTERN ,
326
- norm_path , MAC_PRINTF_ARGS (file_state -> last_seen_root_mac ),
327
- MAC_PRINTF_ARGS (opening_root_mac ));
328
- if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
315
+ } else {
316
+ if (file_state ) {
317
+ if ((file_state -> state == PF_FILE_STATE_ERROR ) ||
318
+ (file_state -> state == PF_FILE_STATE_DELETED )) {
319
+ log_error ("file '%s' was seen before but in %s state" , norm_path ,
320
+ file_state_to_string (file_state -> state ));
321
+ if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
322
+ pf_set_corrupted (pf );
323
+ ret = - EACCES ;
324
+ new_state_in_map = PF_FILE_STATE_ERROR ;
325
+ }
326
+ }
327
+ if (memcmp (file_state -> last_seen_root_mac , opening_root_mac , sizeof (pf_mac_t )) !=
328
+ 0 ) {
329
+ log_error (
330
+ "file '%s' was seen before but in different inconsistent (rolled-back?) "
331
+ "state, expected MAC=" MAC_PRINTF_PATTERN
332
+ " but file had "
333
+ "MAC=" MAC_PRINTF_PATTERN ,
334
+ norm_path , MAC_PRINTF_ARGS (file_state -> last_seen_root_mac ),
335
+ MAC_PRINTF_ARGS (opening_root_mac ));
336
+ if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
337
+ pf_set_corrupted (pf );
338
+ ret = - EACCES ;
339
+ new_state_in_map = PF_FILE_STATE_ERROR ;
340
+ }
341
+ }
342
+ } else {
343
+ if (enc -> volume -> protection_mode == PF_ENCLAVE_LIFE_RB_PROTECTION_STRICT ) {
344
+ log_error (
345
+ "file '%s' was not seen before which is not allowed with strict rollback "
346
+ "protection mode" ,
347
+ norm_path );
329
348
pf_set_corrupted (pf );
330
- ret = - EACCES ;
331
- goto out_unlock_map ;
349
+ ret = - EACCES ;
350
+ new_state_in_map = PF_FILE_STATE_ERROR ;
332
351
}
333
352
}
334
- } else {
335
- if (enc -> volume -> protection_mode == PF_ENCLAVE_LIFE_RB_PROTECTION_STRICT ) {
336
- log_error (
337
- "file '%s' was not seen before which is not allowed with strict rollback "
338
- "protection mode" ,
339
- norm_path );
340
- pf_set_corrupted (pf );
341
- ret = - EACCES ;
342
- goto out_unlock_map ;
343
- }
344
353
}
345
354
}
346
355
/* - uodate map with new state */
@@ -354,11 +363,15 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
354
363
norm_path = NULL ; /* to prevent freeing it */
355
364
HASH_ADD_KEYPTR (hh , enc -> volume -> files_state_map , file_state -> norm_path ,
356
365
strlen (file_state -> norm_path ), file_state );
366
+ log_debug (
367
+ "updated file protection map with file '%s', state '%s' and MAC=" MAC_PRINTF_PATTERN ,
368
+ norm_path , file_state_to_string (file_state -> state ),
369
+ MAC_PRINTF_ARGS (file_state -> last_seen_root_mac ));
357
370
}
358
371
/* we do below unconditionally as we might recreate a deleted file or overwrite an existing
359
372
* one */
360
373
memcpy (file_state -> last_seen_root_mac , opening_root_mac , sizeof (pf_mac_t ));
361
- file_state -> state = PF_FILE_STATE_ACTIVE ;
374
+ file_state -> state = new_state_in_map ;
362
375
363
376
enc -> pf = pf ;
364
377
enc -> pal_handle = pal_handle ;
0 commit comments