Skip to content

Commit

Permalink
fixup! Single-process-lifetime rollback protection for protected file…
Browse files Browse the repository at this point in the history
…s (WIP)

Signed-off-by: g2flyer <[email protected]>
  • Loading branch information
g2flyer committed May 1, 2024
1 parent 281b033 commit 6eca516
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions libos/include/libos_fs_encrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ int encrypted_file_read(struct libos_encrypted_file* enc, void* buf, size_t buf_
int encrypted_file_write(struct libos_encrypted_file* enc, const void* buf, size_t buf_size,
file_off_t offset, size_t* out_count);
int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri);
int encrypted_file_unlink(struct libos_encrypted_file* enc);

int encrypted_file_get_size(struct libos_encrypted_file* enc, file_off_t* out_size);
int encrypted_file_set_size(struct libos_encrypted_file* enc, file_off_t size);
Expand Down
7 changes: 7 additions & 0 deletions libos/src/fs/chroot/encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ static int chroot_encrypted_unlink(struct libos_dentry* dent) {
if (ret < 0)
return ret;

struct libos_encrypted_file* enc = dent->inode->data;
if (!enc)
return -EACCES;
ret = encrypted_file_unlink(enc);
if (ret < 0)
return ret;

PAL_HANDLE palhdl;
ret = PalStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
PAL_OPTION_PASSTHROUGH, &palhdl);
Expand Down
28 changes: 24 additions & 4 deletions libos/src/fs/libos_fs_encrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,13 @@ static void encrypted_file_internal_close(struct libos_encrypted_file* enc) {
file_state->state = PF_FILE_ERROR;
pf_set_corrupted(enc->pf);
} else {
memcpy(file_state->last_seen_root_gmac, closing_root_gmac, sizeof(pf_mac_t));
file_state->state = PF_FILE_CLOSED;
// TODO (MST): Below also has to rule out that our file is stale, i.e., somebody has renamed
// a file to our own original file name
if (file_state->state != PF_FILE_DELETED) {
// TODO (MST): omit below if read-only file?
memcpy(file_state->last_seen_root_gmac, closing_root_gmac, sizeof(pf_mac_t));
file_state->state = PF_FILE_CLOSED;
}
}
unlock(&(enc->volume->files_state_map_lock));

Expand Down Expand Up @@ -768,8 +773,8 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
HASH_ADD_KEYPTR(hh, enc->volume->files_state_map, new_file_state->norm_path,
strlen(new_file_state->norm_path), new_file_state);
} else {
free(new_file_state->norm_path); // should be same but free old one to simplify below
new_file_state->norm_path = new_norm_path;
free(new_norm_path); // should be same as old one used during HASH_ADD
new_norm_path = new_file_state->norm_path;
}
new_file_state->state = old_file_state->state;
memcpy(new_file_state->last_seen_root_gmac, new_root_gmac, sizeof(pf_mac_t));
Expand Down Expand Up @@ -803,6 +808,21 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
return ret;
}

int encrypted_file_unlink(struct libos_encrypted_file* enc) {
lock(&(enc->volume->files_state_map_lock));
struct libos_encrypted_volume_state_map* file_state = NULL;
HASH_FIND_STR(enc->volume->files_state_map, enc->norm_path, file_state);
assert(file_state != NULL);
pf_mac_t root_gmac_before_unlink;
memcpy(root_gmac_before_unlink, file_state->last_seen_root_gmac, sizeof(pf_mac_t));
file_state->state = PF_FILE_DELETED;
memset(file_state->last_seen_root_gmac, 0, sizeof(pf_mac_t));
unlock(&(enc->volume->files_state_map_lock));
log_debug("file '%s' unlinked, previously with MAC=" MAC_PRINTF_PATTERN, enc->norm_path,
MAC_PRINTF_ARGS(root_gmac_before_unlink)); // TODO (MST): remove me eventually?
return 0;
}

/* Checkpoint the `g_keys` list. */
BEGIN_CP_FUNC(all_encrypted_files_keys) {
__UNUSED(size);
Expand Down
1 change: 1 addition & 0 deletions libos/test/regression/rename_unlink

0 comments on commit 6eca516

Please sign in to comment.