This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
[Pal/Linux-SGX] Fix renaming issue with protected files #2681
Closed
+197
−4
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* Protected file renaming. Renaming a file without closing its handle. */ | ||
|
||
#include <assert.h> | ||
#include <err.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#define PF_FOO_PATH "pftmp/foo.txt" | ||
#define PF_BAR_PATH "pftmp/bar.txt" | ||
#define BUF_SIZE 15 | ||
|
||
int main(void) { | ||
char buf[BUF_SIZE]; | ||
char input_text[] = "Hello, world!"; | ||
int fd = open(PF_FOO_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | ||
if (fd < 0) | ||
err(1, "File creation failed"); | ||
|
||
int size = strlen(input_text) + 1; | ||
char* str = input_text; | ||
while (size > 0) { | ||
ssize_t n = write(fd, str, size); | ||
if (n == -1) { | ||
close(fd); | ||
err(1, "Writing to file failed"); | ||
} | ||
assert(n <= size); | ||
size -= n; | ||
str += n; | ||
} | ||
|
||
int ret = close(fd); | ||
if (ret < 0) | ||
err(1, "Cannot close file"); | ||
|
||
ret = rename(PF_FOO_PATH, PF_BAR_PATH); | ||
if (ret < 0) { | ||
err(1, "Rename failed"); | ||
} | ||
|
||
/* Open the renamed file */ | ||
fd = open(PF_BAR_PATH, O_RDONLY); | ||
if (fd < 0) | ||
err(1, "Cannot open renamed file"); | ||
|
||
size_t pos = 0; | ||
do { | ||
ssize_t n = read(fd, &buf[pos], BUF_SIZE - pos); | ||
if (n == -1) | ||
err(1, "Reading from renamed file failed"); | ||
if (n == 0) { | ||
if (size > 0) { | ||
warnx("read less bytes than expected"); | ||
return -1; | ||
} | ||
break; | ||
} | ||
pos += n; | ||
} while (pos < BUF_SIZE); | ||
|
||
close(fd); | ||
if (ret < 0) | ||
err(1, "Cannot close file"); | ||
|
||
buf[sizeof(buf) - 1] = '\0'; | ||
|
||
/* Check if the renamed file's contents are same as original text */ | ||
if (strncmp(input_text, buf, sizeof(input_text))) | ||
err(1, "Renamed file content mismatching"); | ||
|
||
printf("TEST OK\n"); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need
free(tmp);
after renaming?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get your comment exactly. But, tmp needs to be freed up anyway before we return from this function. And, this rename is restoring the metadata back to what it was before. And, tmp is no more of use when we enter this if (ret < 0) condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@svenkata9 I mean heap variable
tmp
is not freed when renaming returns >= 0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@duanbing It is assigned in this line, so cannot be freed.
graphene/Pal/src/host/Linux-SGX/db_files.c
Line 806 in 33a68bc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Thanks.