From 2abee9c5b40977073ca02ce963f97c991f24708c Mon Sep 17 00:00:00 2001 From: "Lu, Han" Date: Tue, 20 Oct 2015 16:45:48 +0800 Subject: [PATCH] BAT: Use dynamic temp file Use dynamic temp file instead of fixed temp file to store recorded wav data, for better security. Signed-off-by: Lu, Han Signed-off-by: Takashi Iwai --- bat/bat.c | 23 ++++++++++++++++++++--- bat/common.h | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bat/bat.c b/bat/bat.c index de32a6d..56cfaf6 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -450,6 +450,7 @@ static int validate_options(struct bat *bat) static int bat_init(struct bat *bat) { int err = 0; + char name[] = TEMP_RECORD_FILE_NAME; /* Determine logging to a file or stdout and stderr */ if (bat->logarg) { @@ -472,10 +473,23 @@ static int bat_init(struct bat *bat) } /* Determine capture file */ - if (bat->local) + if (bat->local) { bat->capture.file = bat->playback.file; - else - bat->capture.file = TEMP_RECORD_FILE_NAME; + } else { + /* create temp file for sound record and analysis */ + err = mkstemp(name); + if (err == -1) { + fprintf(bat->err, _("Fail to create record file: %d\n"), + -errno); + return -errno; + } + /* store file name which is dynamically created */ + bat->capture.file = strdup(name); + if (bat->capture.file == NULL) + return -errno; + /* close temp file */ + close(err); + } /* Initial for playback */ if (bat->playback.file == NULL) { @@ -585,8 +599,11 @@ analyze: err = analyze_capture(&bat); out: fprintf(bat.log, _("\nReturn value is %d\n"), err); + if (bat.logarg) fclose(bat.log); + if (!bat.local) + free(bat.capture.file); return err; } diff --git a/bat/common.h b/bat/common.h index e6ff7f1..c04452d 100644 --- a/bat/common.h +++ b/bat/common.h @@ -15,7 +15,7 @@ #include -#define TEMP_RECORD_FILE_NAME "/tmp/bat.wav" +#define TEMP_RECORD_FILE_NAME "/tmp/bat.wav.XXXXXX" #define OPT_BASE 300 #define OPT_LOG (OPT_BASE + 1)