Fix: liblttng-ctl: unreported truncations when copying strings
[lttng-tools.git] / src / lib / lttng-ctl / rotate.c
index 5c5b1b70cc05b2eecee78809cce1309d716b9a14..bd048aa2e07ec1a568a6fb100b324e5e469e9fc6 100644 (file)
@@ -229,11 +229,14 @@ int lttng_rotate_session(const char *session_name,
 
        memset(&lsm, 0, sizeof(lsm));
        lsm.cmd_type = LTTNG_ROTATE_SESSION;
-       lttng_ctl_copy_string(lsm.session.name, session_name,
-                       sizeof(lsm.session.name));
+
+       ret = lttng_strncpy(lsm.session.name, session_name,
+                           sizeof(lsm.session.name));
+       /* Source length already validated. */
+       assert(ret == 0);
 
        ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return);
-       if (ret < 0) {
+       if (ret <= 0) {
                *rotation_handle = NULL;
                goto end;
        }
@@ -292,30 +295,42 @@ enum lttng_rotation_status lttng_rotation_update_schedule(
 
        memset(&lsm, 0, sizeof(lsm));
        lsm.cmd_type = LTTNG_ROTATION_SET_SCHEDULE;
-       lttng_ctl_copy_string(lsm.session.name, session_name,
+       ret = lttng_strncpy(lsm.session.name, session_name,
                        sizeof(lsm.session.name));
+       /* Source length already validated. */
+       assert(ret == 0);
 
        lsm.u.rotation_set_schedule.type = (uint32_t) schedule->type;
        switch (schedule->type) {
        case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
        {
+               uint64_t threshold;
+
                status = lttng_rotation_schedule_size_threshold_get_threshold(
-                               schedule, &lsm.u.rotation_set_schedule.value);
+                               schedule, &threshold);
                if (status != LTTNG_ROTATION_STATUS_OK) {
+                       if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
+                               status = LTTNG_ROTATION_STATUS_INVALID;
+                       }
                        goto end;
                }
-
+               lsm.u.rotation_set_schedule.value = threshold;
                lsm.u.rotation_set_schedule.set = !!add;
                break;
        }
        case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
        {
+               uint64_t period;
+
                status = lttng_rotation_schedule_periodic_get_period(
-                               schedule, &lsm.u.rotation_set_schedule.value);
+                               schedule, &period);
                if (status != LTTNG_ROTATION_STATUS_OK) {
+                       if (status == LTTNG_ROTATION_STATUS_UNAVAILABLE) {
+                               status = LTTNG_ROTATION_STATUS_INVALID;
+                       }
                        goto end;
                }
-
+               lsm.u.rotation_set_schedule.value = period;
                lsm.u.rotation_set_schedule.set = !!add;
                break;
        }
@@ -362,14 +377,23 @@ int get_schedules(const char *session_name,
 {
        int ret;
        struct lttcomm_session_msg lsm;
-       struct lttng_session_list_schedules_return *schedules_comm;
+       struct lttng_session_list_schedules_return *schedules_comm = NULL;
        struct lttng_rotation_schedules *schedules = NULL;
        struct lttng_rotation_schedule *periodic = NULL, *size = NULL;
 
+       if (!session_name) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
        memset(&lsm, 0, sizeof(lsm));
        lsm.cmd_type = LTTNG_SESSION_LIST_ROTATION_SCHEDULES;
-       lttng_ctl_copy_string(lsm.session.name, session_name,
+       ret = lttng_strncpy(lsm.session.name, session_name,
                        sizeof(lsm.session.name));
+       if (ret) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
 
        ret = lttng_ctl_ask_sessiond(&lsm, (void **) &schedules_comm);
        if (ret < 0) {
@@ -468,7 +492,8 @@ lttng_rotation_schedule_size_threshold_get_threshold(
        enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
        struct lttng_rotation_schedule_size_threshold *size_schedule;
 
-       if (!schedule || !size_threshold_bytes) {
+       if (!schedule || !size_threshold_bytes ||
+                       schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
                status = LTTNG_ROTATION_STATUS_INVALID;
                goto end;
        }
@@ -495,7 +520,8 @@ lttng_rotation_schedule_size_threshold_set_threshold(
        struct lttng_rotation_schedule_size_threshold *size_schedule;
 
        if (!schedule || size_threshold_bytes == 0 ||
-                       size_threshold_bytes == -1ULL) {
+                       size_threshold_bytes == -1ULL ||
+                       schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD) {
                status = LTTNG_ROTATION_STATUS_INVALID;
                goto end;
        }
@@ -532,7 +558,8 @@ lttng_rotation_schedule_periodic_get_period(
        enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
        struct lttng_rotation_schedule_periodic *periodic_schedule;
 
-       if (!schedule || !period_us) {
+       if (!schedule || !period_us ||
+                       schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
                status = LTTNG_ROTATION_STATUS_INVALID;
                goto end;
        }
@@ -558,7 +585,8 @@ lttng_rotation_schedule_periodic_set_period(
        enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK;
        struct lttng_rotation_schedule_periodic *periodic_schedule;
 
-       if (!schedule || period_us == 0 || period_us == -1ULL) {
+       if (!schedule || period_us == 0 || period_us == -1ULL ||
+                       schedule->type != LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC) {
                status = LTTNG_ROTATION_STATUS_INVALID;
                goto end;
        }
This page took 0.024626 seconds and 4 git commands to generate.