-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmds: static variable used stack after return #1924
Conversation
1d534cc
to
4e913bf
Compare
static void cleanup_tempdir(void) | ||
{ | ||
if (!tmp_dirname) | ||
return; | ||
|
||
remove_directory(tmp_dirname); |
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 think wrong dirname
would be covered with opendir()
cheking.
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.
But let's check if tmp_dirname
has a valid string first.
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 think it's ok to check the first byte only.
if (tmp_dirname[0] == '\0')
return;
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 agree, Done!
4e913bf
to
f5f513c
Compare
cmds/live.c
Outdated
#define TMP_DIR_NAME_SIZE 32 | ||
|
||
static char tmp_dirname[TMP_DIR_NAME_SIZE] = { | ||
'\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.
I think you can initialize it with LIVE_NAME
below.
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.
Done @namhyung 👍
static void cleanup_tempdir(void) | ||
{ | ||
if (!tmp_dirname) | ||
return; | ||
|
||
remove_directory(tmp_dirname); |
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.
But let's check if tmp_dirname
has a valid string first.
cmds/live.c
Outdated
remove_directory(tmp_dirname); | ||
tmp_dirname = NULL; | ||
memset(tmp_dirname, 0, sizeof(char) * TMP_DIR_NAME_SIZE); |
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.
You can just use sizeof(tmp_dirname)
;
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.
Thank for the idea!
cmds/live.c
Outdated
@@ -416,15 +417,14 @@ static int forward_options(struct uftrace_opts *opts) | |||
int command_live(int argc, char *argv[], struct uftrace_opts *opts) | |||
{ | |||
#define LIVE_NAME "uftrace-live-XXXXXX" | |||
char template[32] = "/tmp/" LIVE_NAME; | |||
char template[TMP_DIR_NAME_SIZE] = "/tmp/" LIVE_NAME; |
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.
Now we can remove the template
.
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.
Done. 👍
cmds/live.c
Outdated
|
||
unlink(template); | ||
|
||
snprintf(tmp_dirname, strlen(template), "%s", template); |
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.
You should use sizeof(tmp_dirname)
instead of strlen(template)
, otherwise it might not set the last NUL byte.
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.
Thank you @namhyung. good advice! 👍
e3a4078
to
9afc188
Compare
cmds/live.c
Outdated
if (errno != EPERM && errno != ENOENT) | ||
pr_err("cannot access to /tmp"); | ||
pr_err("cannot access to " TMP_LIVE_NAME); |
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 think non-sense situation that someone makes directory or symbolic link named /tmp/uftrace-live-XXXXXX
.
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.
mkstemp
would replace "XXXXXX" to a random string. You need to print the actual string in tmp_dirname
.
#define LIVE_NAME "uftrace-live-XXXXXX" | ||
#define TMP_LIVE_NAME "/tmp/" LIVE_NAME |
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.
String comparison makes macro going to top of the code.
9afc188
to
d79deba
Compare
cmds/live.c
Outdated
static void cleanup_tempdir(void) | ||
{ | ||
if (!tmp_dirname) | ||
if (strncmp(TMP_LIVE_NAME, tmp_dirname, strlen(TMP_LIVE_NAME)) != 0 || | ||
strncmp(LIVE_NAME, tmp_dirname, strlen(LIVE_NAME)) != 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.
This condition is always true. You need to check if it has non-zero contents.
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.
Done @namhyung 👍
cmds/live.c
Outdated
if (errno != EPERM && errno != ENOENT) | ||
pr_err("cannot access to /tmp"); | ||
pr_err("cannot access to " TMP_LIVE_NAME); |
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.
mkstemp
would replace "XXXXXX" to a random string. You need to print the actual string in tmp_dirname
.
cmds/live.c
Outdated
|
||
if (fd < 0) | ||
pr_err("cannot create temp name"); | ||
tmp_dirname = template; | ||
pr_err("cannot create " LIVE_NAME); |
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.
Ditto.
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.
Thank you @namhyung, I understand the mkstemp would replace "XXXXXX"
. Done!
3532ea8
to
5218f60
Compare
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.
Also, this change is specific to live
command. So the commit subject should start with "live:" instead of generic "cmds:".
5218f60
to
91e10b0
Compare
ASAN detect stack after return on this scenario. $ uftrace record --agent --trace=off ./valkey-server $ uftrace --pid `pidof valkey-server` --trace=on $ uftrace --pid `pidof valkey-server` --trace=off This patch fix static tmp_dirname variable not pointed command_live()'s char template array stack variable. Signed-off-by: Yunseong Kim <yskelg@gmail.com>
91e10b0
to
ea31643
Compare
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.
LGTM
ASAN detect stack after return on this scenario.
This patch fix #1915
static tmp_dirname
variable not pointedcommand_live()
's char template array stack variable.