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
Closed
Changes from 1 commit
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,68 @@ | ||
/* Protected file renaming. Renaming a file without closing its handle. */ | ||
|
||
#include <err.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
int main(void) { | ||
char buf[15]; | ||
char input_text[] = "Hello, world!"; | ||
int fd = open("pftmp/foo.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | ||
if (fd < 0) | ||
err(1, "Cannot create file pftmp/foo.txt."); | ||
|
||
int num_bytes = write(fd, input_text, strlen(input_text) + 1); | ||
if (num_bytes < 0) { | ||
close(fd); | ||
err(1, "Writing to file failed."); | ||
} | ||
|
||
close(fd); | ||
|
||
int ret = rename("pftmp/foo.txt", "pftmp/bar.txt"); | ||
if (ret < 0) { | ||
fd = open("pftmp/foo.txt", O_RDONLY); | ||
if (fd < 0) | ||
err(1, "Rename failed, Original file corrupted & unusable."); | ||
|
||
num_bytes = read(fd, buf, sizeof(buf)); | ||
|
||
close(fd); | ||
|
||
if (num_bytes < 0) | ||
err(1, "Rename failed, Original file corrupted & unusable."); | ||
|
||
buf[sizeof(buf) - 1] = '\0'; | ||
|
||
if (strncmp(input_text, buf, sizeof(input_text))) | ||
err(1, "Rename failed, Original file corrupted & unusable."); | ||
|
||
err(1, "Rename failed, Original file intact."); | ||
} | ||
|
||
/* Open the renamed file */ | ||
fd = open("pftmp/bar.txt", O_RDONLY); | ||
if (fd < 0) | ||
err(1, "Cannot open renamed file!"); | ||
|
||
num_bytes = read(fd, buf, sizeof(buf)); | ||
|
||
close(fd); | ||
|
||
if (num_bytes < 0) | ||
err(1, "Reading from renamed file failed."); | ||
|
||
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.