X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fsave.c;h=89b45ba832e1a047429dfa905b4b071e0bd66c66;hb=aa3514e96f12c13f681a81ea275dc51dd63473c8;hp=8599d2d18a6796bc3c6ba90017f4ea55420c0d6d;hpb=fb198a1138d32ac7218695c564909d96018eb1b7;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/save.c b/src/bin/lttng-sessiond/save.c index 8599d2d18..89b45ba83 100644 --- a/src/bin/lttng-sessiond/save.c +++ b/src/bin/lttng-sessiond/save.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -1239,6 +1238,7 @@ end_net_output: ret = !output->dst.net.control_isset ? LTTNG_ERR_URL_CTRL_MISS : LTTNG_ERR_URL_DATA_MISS; + free(uri); goto end; } @@ -1397,6 +1397,7 @@ int save_session(struct ltt_session *session, struct lttng_save_session_attr *attr, lttng_sock_cred *creds) { int ret, fd; + unsigned int file_opened = 0; /* Indicate if the file has been opened */ char config_file_path[PATH_MAX]; size_t len; struct config_writer *writer = NULL; @@ -1408,6 +1409,7 @@ int save_session(struct ltt_session *session, assert(creds); session_name_len = strlen(session->name); + memset(config_file_path, 0, sizeof(config_file_path)); if (!session_access_ok(session, LTTNG_SOCK_GET_UID_CRED(creds), @@ -1418,13 +1420,15 @@ int save_session(struct ltt_session *session, provided_path = lttng_save_session_attr_get_output_url(attr); if (provided_path) { + DBG3("Save session in provided path %s", provided_path); len = strlen(provided_path); - if (len > PATH_MAX) { + if (len >= sizeof(config_file_path)) { ret = LTTNG_ERR_SET_URL; goto end; } strncpy(config_file_path, provided_path, len); } else { + ssize_t ret_len; char *home_dir = utils_get_user_home_dir( LTTNG_SOCK_GET_UID_CRED(creds)); if (!home_dir) { @@ -1432,22 +1436,24 @@ int save_session(struct ltt_session *session, goto end; } - len = snprintf(config_file_path, PATH_MAX, + ret_len = snprintf(config_file_path, sizeof(config_file_path), DEFAULT_SESSION_HOME_CONFIGPATH, home_dir); free(home_dir); - if (len < 0) { + if (ret_len < 0) { PERROR("snprintf save session"); ret = LTTNG_ERR_SET_URL; goto end; } + len = ret_len; } /* - * Check the path fits in PATH_MAX, including the / followed by trailing - * .lttng extension and the NULL terminated string. + * Check the path fits in the config file path dst including the '/' + * followed by trailing .lttng extension and the NULL terminated string. */ - if (len + session_name_len + 2 + - sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION) > PATH_MAX) { + if ((len + session_name_len + 2 + + sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) + > sizeof(config_file_path)) { ret = LTTNG_ERR_SET_URL; goto end; } @@ -1459,13 +1465,20 @@ int save_session(struct ltt_session *session, goto end; } + /* + * At this point, we know that everything fits in the buffer. Validation + * was done just above. + */ config_file_path[len++] = '/'; strncpy(config_file_path + len, session->name, session_name_len); len += session_name_len; strcpy(config_file_path + len, DEFAULT_SESSION_CONFIG_FILE_EXTENSION); + len += sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION); + config_file_path[len] = '\0'; if (!access(config_file_path, F_OK) && !attr->overwrite) { - /* A file with the same name already exists, skip */ + /* File exists, notify the user since the overwrite flag is off. */ + ret = LTTNG_ERR_SAVE_FILE_EXIST; goto end; } @@ -1477,6 +1490,7 @@ int save_session(struct ltt_session *session, ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; } + file_opened = 1; writer = config_writer_create(fd); if (!writer) { @@ -1509,7 +1523,7 @@ int save_session(struct ltt_session *session, } ret = config_writer_write_element_bool(writer, config_element_started, - session->enabled); + session->active); if (ret) { ret = LTTNG_ERR_SAVE_IO_FAIL; goto end; @@ -1571,7 +1585,7 @@ end: } if (ret) { /* Delete file in case of error */ - if (unlink(config_file_path)) { + if (file_opened && unlink(config_file_path)) { PERROR("Unlinking XML session configuration."); } }