Fix sdlog2/logger path/file name overflows. (#5138)

* logger: prevent logpath buffer overflows

The handling of the log path had the potential to cause buffer
overflows, especially on POSIX platforms where the paths are often much
longer than just 64 chars.

* sdlog2: prevent logpath buffer overflows

When the log folder path was created, this was done with the unsafe
sprintf function instead of snprintf. This caused buffer overflows in
SITL but the overflow was usually not detected until recent testing of
some work in progress.
This commit is contained in:
Julian Oes
2016-07-27 00:55:29 -07:00
committed by Lorenz Meier
parent 4656b34244
commit c4cb916afa
3 changed files with 35 additions and 4 deletions
+14 -1
View File
@@ -170,7 +170,11 @@ struct logbuffer_s lb;
static pthread_mutex_t logbuffer_mutex;
static pthread_cond_t logbuffer_cond;
#ifdef __PX4_NUTTX
#define LOG_BASE_PATH_LEN 64
#else
#define LOG_BASE_PATH_LEN 256
#endif
static char log_dir[LOG_BASE_PATH_LEN];
@@ -452,6 +456,10 @@ int create_log_dir()
if (log_name_timestamp && time_ok) {
int n = snprintf(log_dir, sizeof(log_dir), "%s/", log_root);
if (n >= sizeof(log_dir)) {
PX4_ERR("log path too long");
return -1;
}
strftime(log_dir + n, sizeof(log_dir) - n, "%Y-%m-%d", &tt);
mkdir_ret = mkdir(log_dir, S_IRWXU | S_IRWXG | S_IRWXO);
@@ -466,7 +474,12 @@ int create_log_dir()
* let's re-use it. */
while (dir_number <= MAX_NO_LOGFOLDER && !sess_folder_created) {
/* format log dir: e.g. /fs/microsd/sess001 */
sprintf(log_dir, "%s/sess%03u", log_root, dir_number);
int n = snprintf(log_dir, sizeof(log_dir), "%s/sess%03u", log_root, dir_number);
if (n >= sizeof(log_dir)) {
PX4_ERR("log path too long");
return -1;
}
mkdir_ret = mkdir(log_dir, S_IRWXU | S_IRWXG | S_IRWXO);
if (mkdir_ret == 0) {