Skip to content

Commit

Permalink
cmds: static variable used stack after return
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
yskelg committed May 15, 2024
1 parent d79deba commit 9afc188
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions cmds/live.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
#include "utils/socket.h"
#include "utils/utils.h"

static char *tmp_dirname;
#define LIVE_NAME "uftrace-live-XXXXXX"
#define TMP_LIVE_NAME "/tmp/" LIVE_NAME

#define TMP_DIR_NAME_SIZE 32

static char tmp_dirname[TMP_DIR_NAME_SIZE];
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) {
return;
}

remove_directory(tmp_dirname);
tmp_dirname = NULL;

memset(tmp_dirname, '\0', sizeof(tmp_dirname));
}

/* trigger actions that need to be done in replay */
Expand Down Expand Up @@ -415,30 +423,26 @@ 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;
int fd;
struct sigaction sa = {
.sa_flags = SA_RESETHAND,
};
int ret;

if (!opts->record) {
tmp_dirname = template;
snprintf(tmp_dirname, sizeof(tmp_dirname), "%s", TMP_LIVE_NAME);
umask(022);
fd = mkstemp(template);
fd = mkstemp(tmp_dirname);
if (fd < 0) {
/* can't reuse first template because it was trashed by mkstemp */
strcpy(template, LIVE_NAME);

if (errno != EPERM && errno != ENOENT)
pr_err("cannot access to /tmp");
pr_err("cannot access to " TMP_LIVE_NAME);

fd = mkstemp(template);
/* can't reuse first template because it was trashed by mkstemp */
snprintf(tmp_dirname, sizeof(tmp_dirname), "%s", LIVE_NAME);
fd = mkstemp(tmp_dirname);

if (fd < 0)
pr_err("cannot create temp name");
tmp_dirname = template;
pr_err("cannot create " LIVE_NAME);
}

close(fd);
Expand Down

0 comments on commit 9afc188

Please sign in to comment.