@@ -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,69 +276,78 @@ 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 */
291
- struct libos_encrypted_volume_state_map * file_state = NULL ;
292
295
log_debug ("file '%s' opened with MAC=" MAC_PRINTF_PATTERN , norm_path ,
293
296
MAC_PRINTF_ARGS (opening_root_mac )); // TODO (MST): remove me eventually?
297
+ struct libos_encrypted_volume_state_map * file_state = NULL ;
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
- pf_set_corrupted (pf );
316
- ret = - EACCES ;
317
- goto out_unlock_map ;
318
- }
319
- }
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 ) {
329
- pf_set_corrupted (pf );
330
- ret = - EACCES ;
331
- goto out_unlock_map ;
310
+ pf_close (pf , NULL );
311
+ ret = - EEXIST ;
312
+ new_state_in_map = PF_FILE_STATE_ERROR ;
332
313
}
333
314
}
334
315
} 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 ;
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_close (pf , NULL );
323
+ ret = - EACCES ;
324
+ new_state_in_map = PF_FILE_STATE_ERROR ;
325
+ }
326
+ } else if (memcmp (file_state -> last_seen_root_mac , opening_root_mac ,
327
+ sizeof (pf_mac_t )) != 0 ) {
328
+ log_error (
329
+ "file '%s' was seen before but in different inconsistent (rolled-back?) "
330
+ "state, expected MAC=" MAC_PRINTF_PATTERN
331
+ " but file had "
332
+ "MAC=" MAC_PRINTF_PATTERN ,
333
+ norm_path , MAC_PRINTF_ARGS (file_state -> last_seen_root_mac ),
334
+ MAC_PRINTF_ARGS (opening_root_mac ));
335
+ if (enc -> volume -> protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE ) {
336
+ pf_close (pf , NULL );
337
+ ret = - EACCES ;
338
+ new_state_in_map = PF_FILE_STATE_ERROR ;
339
+ }
340
+ }
341
+ } else {
342
+ if (enc -> volume -> protection_mode == PF_ENCLAVE_LIFE_RB_PROTECTION_STRICT ) {
343
+ log_error (
344
+ "file '%s' was not seen before which is not allowed with strict rollback "
345
+ "protection mode" ,
346
+ norm_path );
347
+ pf_close (pf , NULL );
348
+ ret = - EACCES ;
349
+ new_state_in_map = PF_FILE_STATE_ERROR ;
350
+ }
343
351
}
344
352
}
345
353
}
@@ -354,15 +362,20 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
354
362
norm_path = NULL ; /* to prevent freeing it */
355
363
HASH_ADD_KEYPTR (hh , enc -> volume -> files_state_map , file_state -> norm_path ,
356
364
strlen (file_state -> norm_path ), file_state );
365
+ log_debug (
366
+ "updated file protection map with file '%s', state '%s' and MAC=" MAC_PRINTF_PATTERN ,
367
+ file_state -> norm_path , file_state_to_string (file_state -> state ),
368
+ MAC_PRINTF_ARGS (file_state -> last_seen_root_mac ));
357
369
}
358
370
/* we do below unconditionally as we might recreate a deleted file or overwrite an existing
359
371
* one */
360
372
memcpy (file_state -> last_seen_root_mac , opening_root_mac , sizeof (pf_mac_t ));
361
- file_state -> state = PF_FILE_STATE_ACTIVE ;
373
+ file_state -> state = new_state_in_map ;
362
374
363
- enc -> pf = pf ;
364
- enc -> pal_handle = pal_handle ;
365
- ret = 0 ;
375
+ if (ret == 0 ) {
376
+ enc -> pf = pf ;
377
+ enc -> pal_handle = pal_handle ;
378
+ }
366
379
367
380
out_unlock_map :
368
381
unlock (& (enc -> volume -> files_state_map_lock ));
0 commit comments