Skip to content

Commit 2c7435e

Browse files
committed
fixup! [LibOS] Single-process-lifetime rollback protection for protected files (SPLRB)
Signed-off-by: g2flyer <[email protected]>
1 parent 41556b5 commit 2c7435e

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

common/src/protected_files/protected_files.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ static void ipf_delete_cache(pf_context_t* pf) {
10761076
}
10771077
}
10781078

1079-
static bool ipf_close(pf_context_t* pf, pf_mac_t* closing_root_gmac) {
1079+
static bool ipf_close(pf_context_t* pf, pf_mac_t* closing_root_mac) {
10801080
bool retval = true;
10811081

10821082
if (pf->file_status != PF_STATUS_SUCCESS) {
@@ -1089,8 +1089,8 @@ static bool ipf_close(pf_context_t* pf, pf_mac_t* closing_root_gmac) {
10891089
}
10901090
}
10911091

1092-
if (closing_root_gmac != NULL) {
1093-
memcpy(*closing_root_gmac, pf->file_metadata.plain_part.metadata_gmac, sizeof(pf_mac_t));
1092+
if (closing_root_mac != NULL) {
1093+
memcpy(*closing_root_mac, pf->metadata_node.plaintext_part.metadata_mac, sizeof(pf_mac_t));
10941094
}
10951095

10961096
// omeg: fs close is done by Gramine handler
@@ -1131,24 +1131,24 @@ void pf_set_callbacks(pf_read_f read_f, pf_write_f write_f, pf_fsync_f fsync_f,
11311131

11321132
pf_status_t pf_open(pf_handle_t handle, const char* path, uint64_t underlying_size,
11331133
pf_file_mode_t mode, bool create, const pf_key_t* key,
1134-
pf_mac_t* opening_root_gmac, pf_context_t** context) {
1134+
pf_mac_t* opening_root_mac, pf_context_t** context) {
11351135
if (!g_initialized)
11361136
return PF_STATUS_UNINITIALIZED;
11371137

11381138
pf_status_t status;
11391139
*context = ipf_open(path, mode, create, handle, underlying_size, key, &status);
1140-
if ((*context != NULL) && (opening_root_gmac != NULL)) {
1141-
memcpy(*opening_root_gmac, (*context)->file_metadata.plain_part.metadata_gmac,
1140+
if ((*context != NULL) && (opening_root_mac != NULL)) {
1141+
memcpy(*opening_root_mac, (*context)->metadata_node.plaintext_part.metadata_mac,
11421142
sizeof(pf_mac_t));
11431143
}
11441144
return status;
11451145
}
11461146

1147-
pf_status_t pf_close(pf_context_t* pf, pf_mac_t* closing_root_gmac) {
1147+
pf_status_t pf_close(pf_context_t* pf, pf_mac_t* closing_root_mac) {
11481148
if (!g_initialized)
11491149
return PF_STATUS_UNINITIALIZED;
11501150

1151-
if (ipf_close(pf, closing_root_gmac)) {
1151+
if (ipf_close(pf, closing_root_mac)) {
11521152
free(pf);
11531153
return PF_STATUS_SUCCESS;
11541154
}
@@ -1217,7 +1217,7 @@ pf_status_t pf_set_size(pf_context_t* pf, uint64_t size) {
12171217
return PF_STATUS_SUCCESS;
12181218
}
12191219

1220-
pf_status_t pf_rename(pf_context_t* pf, const char* new_path, pf_mac_t* new_root_gmac) {
1220+
pf_status_t pf_rename(pf_context_t* pf, const char* new_path, pf_mac_t* new_root_mac) {
12211221
if (!g_initialized)
12221222
return PF_STATUS_UNINITIALIZED;
12231223

@@ -1233,8 +1233,8 @@ pf_status_t pf_rename(pf_context_t* pf, const char* new_path, pf_mac_t* new_root
12331233
pf->need_writing = true;
12341234
if (!ipf_internal_flush(pf))
12351235
return pf->last_error;
1236-
if (new_root_gmac != NULL) {
1237-
memcpy(*new_root_gmac, pf->file_metadata.plain_part.metadata_gmac, sizeof(pf_mac_t));
1236+
if (new_root_mac != NULL) {
1237+
memcpy(*new_root_mac, pf->metadata_node.plaintext_part.metadata_mac, sizeof(pf_mac_t));
12381238
}
12391239

12401240
return PF_STATUS_SUCCESS;

common/src/protected_files/protected_files.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,24 @@ const char* pf_strerror(int err);
224224
* \param mode Access mode.
225225
* \param create Overwrite file contents if true.
226226
* \param key Wrap key.
227-
* \param opening_root_gmac If non-NULL, !create & successfull open, returns root-hash of file
227+
* \param opening_root_mac If non-NULL, !create & successfull open, returns root-hash of file
228228
* \param[out] context PF context for later calls.
229229
*
230230
* \returns PF status.
231231
*/
232232
pf_status_t pf_open(pf_handle_t handle, const char* path, uint64_t underlying_size,
233233
pf_file_mode_t mode, bool create, const pf_key_t* key,
234-
pf_mac_t* opening_root_gmac, pf_context_t** context);
234+
pf_mac_t* opening_root_mac, pf_context_t** context);
235235

236236
/*!
237237
* \brief Close a protected file and commit all changes to disk.
238238
*
239239
* \param pf PF context.
240-
* \param closing_root_gmac If non-NULL, returns root-hash of file at closing time
240+
* \param closing_root_mac If non-NULL, returns root-hash of file at closing time
241241
*
242242
* \returns PF status.
243243
*/
244-
pf_status_t pf_close(pf_context_t* pf, pf_mac_t* closing_root_gmac);
244+
pf_status_t pf_close(pf_context_t* pf, pf_mac_t* closing_root_mac);
245245

246246
/*!
247247
* \brief Read from a protected file.
@@ -296,12 +296,12 @@ pf_status_t pf_set_size(pf_context_t* pf, uint64_t size);
296296
*
297297
* \param pf PF context.
298298
* \param new_path New file path.
299-
* \param new_root_gmac if non-NULL, returns new root-hash of file
299+
* \param new_root_mac if non-NULL, returns new root-hash of file
300300
*
301301
* Updates the path inside protected file header, and flushes all changes. The caller is responsible
302302
* for renaming the underlying file.
303303
*/
304-
pf_status_t pf_rename(pf_context_t* pf, const char* new_path, pf_mac_t* new_root_gmac);
304+
pf_status_t pf_rename(pf_context_t* pf, const char* new_path, pf_mac_t* new_root_mac);
305305

306306
/*!
307307
* \brief Flush any pending data of a protected file to disk.

libos/include/libos_fs_encrypted.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ typedef enum {
4848
struct libos_encrypted_volume_state_map {
4949
char* norm_path; // assumptions: all paths canonicalized, symlinks are resolved & no hard links
5050
libos_encrypted_file_state_t state;
51-
pf_mac_t last_seen_root_gmac;
51+
pf_mac_t last_seen_root_mac;
5252
UT_hash_handle hh;
5353
};
5454

libos/src/fs/libos_fs_encrypted.c

+21-21
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
221221
ret = -EACCES;
222222
goto out;
223223
}
224-
pf_mac_t opening_root_gmac;
224+
pf_mac_t opening_root_mac;
225225
pf_status_t pfs = pf_open(pal_handle, norm_path, size, PF_FILE_MODE_READ | PF_FILE_MODE_WRITE,
226-
create, &enc->volume->key->pf_key, &opening_root_gmac, &pf);
226+
create, &enc->volume->key->pf_key, &opening_root_mac, &pf);
227227
unlock(&g_keys_lock);
228228
if (PF_FAILURE(pfs)) {
229229
log_warning("pf_open failed: %s", pf_strerror(pfs));
@@ -233,7 +233,7 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
233233
/* rollback protection */
234234
struct libos_encrypted_volume_state_map* file_state = NULL;
235235
log_debug("file '%s' opened with MAC=" MAC_PRINTF_PATTERN, norm_path,
236-
MAC_PRINTF_ARGS(opening_root_gmac)); // TODO (MST): remove me eventually?
236+
MAC_PRINTF_ARGS(opening_root_mac)); // TODO (MST): remove me eventually?
237237
lock(&(enc->volume->files_state_map_lock));
238238
/* - get current state */
239239
HASH_FIND_STR(enc->volume->files_state_map, norm_path, file_state);
@@ -259,14 +259,14 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
259259
goto out_unlock_map;
260260
}
261261
}
262-
if (memcmp(file_state->last_seen_root_gmac, opening_root_gmac, sizeof(pf_mac_t)) != 0) {
262+
if (memcmp(file_state->last_seen_root_mac, opening_root_mac, sizeof(pf_mac_t)) != 0) {
263263
log_error(
264264
"file '%s' was seen before but in different inconsistent (rolled-back?) "
265265
"state, expected MAC=" MAC_PRINTF_PATTERN
266266
" but file had "
267267
"MAC=" MAC_PRINTF_PATTERN,
268-
norm_path, MAC_PRINTF_ARGS(file_state->last_seen_root_gmac),
269-
MAC_PRINTF_ARGS(opening_root_gmac));
268+
norm_path, MAC_PRINTF_ARGS(file_state->last_seen_root_mac),
269+
MAC_PRINTF_ARGS(opening_root_mac));
270270
if (enc->volume->protection_mode != PF_ENCLAVE_LIFE_RB_PROTECTION_NONE) {
271271
pf_set_corrupted(pf);
272272
ret = -EACCES;
@@ -299,7 +299,7 @@ static int encrypted_file_internal_open(struct libos_encrypted_file* enc, PAL_HA
299299
}
300300
/* we do below unconditionally as we might recreate a deleted file or overwrite an existing
301301
* one */
302-
memcpy(file_state->last_seen_root_gmac, opening_root_gmac, sizeof(pf_mac_t));
302+
memcpy(file_state->last_seen_root_mac, opening_root_mac, sizeof(pf_mac_t));
303303
file_state->state = PF_FILE_STATE_ACTIVE;
304304

305305
enc->pf = pf;
@@ -335,16 +335,16 @@ int parse_pf_key(const char* key_str, pf_key_t* pf_key) {
335335

336336
static void encrypted_file_internal_close(struct libos_encrypted_file* enc, bool fs_reachable) {
337337
assert(enc->pf);
338-
pf_mac_t closing_root_gmac;
339-
pf_status_t pfs = pf_close(enc->pf, &closing_root_gmac);
338+
pf_mac_t closing_root_mac;
339+
pf_status_t pfs = pf_close(enc->pf, &closing_root_mac);
340340
char* norm_path = NULL;
341341
int ret = uri_to_normalized_path(enc->uri, &norm_path);
342342
if (ret < 0) {
343343
log_error("Could not normalize uri %s while closing file (ret=%d)", enc->uri, ret);
344344
} else {
345345
log_debug("%sreachable file '%s' closed with MAC=" MAC_PRINTF_PATTERN,
346346
(fs_reachable ? "" : "un"), norm_path,
347-
MAC_PRINTF_ARGS(closing_root_gmac)); // TODO (MST): remove me eventually?
347+
MAC_PRINTF_ARGS(closing_root_mac)); // TODO (MST): remove me eventually?
348348
lock(&(enc->volume->files_state_map_lock));
349349
struct libos_encrypted_volume_state_map* file_state = NULL;
350350

@@ -359,7 +359,7 @@ static void encrypted_file_internal_close(struct libos_encrypted_file* enc, bool
359359
/* note: we only update if reachable in fileystem to prevent file-handles made
360360
* unreachable via unlink or rename to modify state. We also do not touch it if
361361
* earlier we determined this file is in inconsistent error state. */
362-
memcpy(file_state->last_seen_root_gmac, closing_root_gmac, sizeof(pf_mac_t));
362+
memcpy(file_state->last_seen_root_mac, closing_root_mac, sizeof(pf_mac_t));
363363
}
364364
}
365365
unlock(&(enc->volume->files_state_map_lock));
@@ -818,8 +818,8 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
818818
if (ret < 0)
819819
goto out;
820820

821-
pf_mac_t new_root_gmac;
822-
pf_status_t pfs = pf_rename(enc->pf, new_norm_path, &new_root_gmac);
821+
pf_mac_t new_root_mac;
822+
pf_status_t pfs = pf_rename(enc->pf, new_norm_path, &new_root_mac);
823823
if (PF_FAILURE(pfs)) {
824824
log_warning("pf_rename failed: %s", pf_strerror(pfs));
825825
ret = -EACCES;
@@ -831,7 +831,7 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
831831
log_warning("PalStreamChangeName failed: %s", pal_strerror(ret));
832832

833833
/* We failed to rename the file. Try to restore the name in header. */
834-
pfs = pf_rename(enc->pf, old_norm_path, &new_root_gmac);
834+
pfs = pf_rename(enc->pf, old_norm_path, &new_root_mac);
835835
if (PF_FAILURE(pfs)) {
836836
log_warning("pf_rename (during cleanup) failed, the file might be unusable: %s",
837837
pf_strerror(pfs));
@@ -843,7 +843,7 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
843843
/* update file state map */
844844
log_debug("file '%s' renamed to '%s' with MAC=" MAC_PRINTF_PATTERN, old_norm_path,
845845
new_norm_path,
846-
MAC_PRINTF_ARGS(new_root_gmac)); // TODO (MST): remove me eventually?
846+
MAC_PRINTF_ARGS(new_root_mac)); // TODO (MST): remove me eventually?
847847
lock(&(enc->volume->files_state_map_lock));
848848
struct libos_encrypted_volume_state_map* old_file_state = NULL;
849849
HASH_FIND_STR(enc->volume->files_state_map, old_norm_path, old_file_state);
@@ -864,10 +864,10 @@ int encrypted_file_rename(struct libos_encrypted_file* enc, const char* new_uri)
864864
new_norm_path = new_file_state->norm_path;
865865
}
866866
new_file_state->state = old_file_state->state;
867-
memcpy(new_file_state->last_seen_root_gmac, new_root_gmac, sizeof(pf_mac_t));
867+
memcpy(new_file_state->last_seen_root_mac, new_root_mac, sizeof(pf_mac_t));
868868
old_file_state->state = PF_FILE_STATE_DELETED; /* note: this might remove error state from that
869869
file but that is fine as it is deleted now. */
870-
memset(old_file_state->last_seen_root_gmac, 0, sizeof(pf_mac_t));
870+
memset(old_file_state->last_seen_root_mac, 0, sizeof(pf_mac_t));
871871
unlock(&(enc->volume->files_state_map_lock));
872872

873873
free(enc->uri);
@@ -905,13 +905,13 @@ int encrypted_file_unlink(struct libos_encrypted_file* enc) {
905905
struct libos_encrypted_volume_state_map* file_state = NULL;
906906
HASH_FIND_STR(enc->volume->files_state_map, norm_path, file_state);
907907
assert(file_state != NULL);
908-
pf_mac_t root_gmac_before_unlink;
909-
memcpy(root_gmac_before_unlink, file_state->last_seen_root_gmac, sizeof(pf_mac_t));
908+
pf_mac_t root_mac_before_unlink;
909+
memcpy(root_mac_before_unlink, file_state->last_seen_root_mac, sizeof(pf_mac_t));
910910
file_state->state = PF_FILE_STATE_DELETED;
911-
memset(file_state->last_seen_root_gmac, 0, sizeof(pf_mac_t));
911+
memset(file_state->last_seen_root_mac, 0, sizeof(pf_mac_t));
912912
unlock(&(enc->volume->files_state_map_lock));
913913
log_debug("file '%s' unlinked, previously with MAC=" MAC_PRINTF_PATTERN, norm_path,
914-
MAC_PRINTF_ARGS(root_gmac_before_unlink)); // TODO (MST): remove me eventually?
914+
MAC_PRINTF_ARGS(root_mac_before_unlink)); // TODO (MST): remove me eventually?
915915
return 0;
916916
}
917917

0 commit comments

Comments
 (0)