Skip to content

Commit 8e01495

Browse files
committed
fixup! Single-process-lifetime rollback protection for protected files (WIP)
Signed-off-by: g2flyer <[email protected]>
1 parent 6193a19 commit 8e01495

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

libos/include/libos_fs_encrypted.h

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ int encrypted_file_read(struct libos_encrypted_file* enc, void* buf, size_t buf_
202202
int encrypted_file_write(struct libos_encrypted_file* enc, const void* buf, size_t buf_size,
203203
file_off_t offset, size_t* out_count);
204204
int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri);
205+
int encrypted_file_unlink(struct libos_encrypted_file* enc);
205206

206207
int encrypted_file_get_size(struct libos_encrypted_file* enc, file_off_t* out_size);
207208
int encrypted_file_set_size(struct libos_encrypted_file* enc, file_off_t size);

libos/src/fs/chroot/encrypted.c

+7
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ static int chroot_encrypted_unlink(struct libos_dentry* dent) {
338338
if (ret < 0)
339339
return ret;
340340

341+
struct libos_encrypted_file* enc = dent->inode->data;
342+
if (!enc)
343+
return -EACCES;
344+
ret = encrypted_file_unlink(enc);
345+
if (ret < 0)
346+
return ret;
347+
341348
PAL_HANDLE palhdl;
342349
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
343350
PAL_OPTION_PASSTHROUGH, &palhdl);

libos/src/fs/libos_fs_encrypted.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,13 @@ static void encrypted_file_internal_close(struct libos_encrypted_file* enc) {
300300
file_state->state = PF_FILE_ERROR;
301301
pf_set_corrupted(enc->pf);
302302
} else {
303-
memcpy(file_state->last_seen_root_gmac, closing_root_gmac, sizeof(pf_mac_t));
304-
file_state->state = PF_FILE_CLOSED;
303+
// TODO (MST): Below also has to rule out that our file is stale, i.e., somebody has renamed
304+
// a file to our own original file name
305+
if (file_state->state != PF_FILE_DELETED) {
306+
// TODO (MST): omit below if read-only file?
307+
memcpy(file_state->last_seen_root_gmac, closing_root_gmac, sizeof(pf_mac_t));
308+
file_state->state = PF_FILE_CLOSED;
309+
}
305310
}
306311
unlock(&(enc->volume->files_state_map_lock));
307312

@@ -768,8 +773,8 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
768773
HASH_ADD_KEYPTR(hh, enc->volume->files_state_map, new_file_state->norm_path,
769774
strlen(new_file_state->norm_path), new_file_state);
770775
} else {
771-
free(new_file_state->norm_path); // should be same but free old one to simplify below
772-
new_file_state->norm_path = new_norm_path;
776+
free(new_norm_path); // should be same as old one used during HASH_ADD
777+
new_norm_path = new_file_state->norm_path;
773778
}
774779
new_file_state->state = old_file_state->state;
775780
memcpy(new_file_state->last_seen_root_gmac, new_root_gmac, sizeof(pf_mac_t));
@@ -803,6 +808,21 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
803808
return ret;
804809
}
805810

811+
int encrypted_file_unlink(struct libos_encrypted_file* enc) {
812+
lock(&(enc->volume->files_state_map_lock));
813+
struct libos_encrypted_volume_state_map* file_state = NULL;
814+
HASH_FIND_STR(enc->volume->files_state_map, enc->norm_path, file_state);
815+
assert(file_state != NULL);
816+
pf_mac_t root_gmac_before_unlink;
817+
memcpy(root_gmac_before_unlink, file_state->last_seen_root_gmac, sizeof(pf_mac_t));
818+
file_state->state = PF_FILE_DELETED;
819+
memset(file_state->last_seen_root_gmac, 0, sizeof(pf_mac_t));
820+
unlock(&(enc->volume->files_state_map_lock));
821+
log_debug("file '%s' unlinked, previously with MAC=" MAC_PRINTF_PATTERN, enc->norm_path,
822+
MAC_PRINTF_ARGS(root_gmac_before_unlink)); // TODO (MST): remove me eventually?
823+
return 0;
824+
}
825+
806826
/* Checkpoint the `g_keys` list. */
807827
BEGIN_CP_FUNC(all_encrypted_files_keys) {
808828
__UNUSED(size);

libos/test/regression/rename_unlink

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../build/libos/test/regression/rename_unlink

0 commit comments

Comments
 (0)