examples/fips_validation: fix buffer overflow
[dpdk.git] / examples / fips_validation / fips_validation.c
index ef24b72..13f763c 100644 (file)
@@ -144,6 +144,24 @@ fips_test_parse_header(void)
                                ret = parse_test_tdes_init();
                                if (ret < 0)
                                        return 0;
+                       } else if (strstr(info.vec[i], "PERMUTATION")) {
+                               algo_parsed = 1;
+                               info.algo = FIPS_TEST_ALGO_TDES;
+                               ret = parse_test_tdes_init();
+                               if (ret < 0)
+                                       return 0;
+                       } else if (strstr(info.vec[i], "VARIABLE")) {
+                               algo_parsed = 1;
+                               info.algo = FIPS_TEST_ALGO_TDES;
+                               ret = parse_test_tdes_init();
+                               if (ret < 0)
+                                       return 0;
+                       } else if (strstr(info.vec[i], "SUBSTITUTION")) {
+                               algo_parsed = 1;
+                               info.algo = FIPS_TEST_ALGO_TDES;
+                               ret = parse_test_tdes_init();
+                               if (ret < 0)
+                                       return 0;
                        } else if (strstr(info.vec[i], "SHA-")) {
                                algo_parsed = 1;
                                info.algo = FIPS_TEST_ALGO_SHA;
@@ -263,7 +281,11 @@ fips_test_init(const char *req_file_path, const char *rsp_file_path,
 
        fips_test_clear();
 
-       strcpy(info.file_name, req_file_path);
+       if (rte_strscpy(info.file_name, req_file_path,
+                               sizeof(info.file_name)) < 0) {
+               RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path);
+               return -EINVAL;
+       }
        info.algo = FIPS_TEST_ALGO_MAX;
        if (parse_file_type(req_file_path) < 0) {
                RTE_LOG(ERR, USER1, "File %s type not supported\n",
@@ -289,7 +311,11 @@ fips_test_init(const char *req_file_path, const char *rsp_file_path,
                return -ENOMEM;
        }
 
-       strlcpy(info.device_name, device_name, sizeof(info.device_name));
+       if (rte_strscpy(info.device_name, device_name,
+                               sizeof(info.device_name)) < 0) {
+               RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name);
+               return -EINVAL;
+       }
 
        if (fips_test_parse_header() < 0) {
                RTE_LOG(ERR, USER1, "Failed parsing header\n");
@@ -322,11 +348,13 @@ int
 fips_test_parse_one_case(void)
 {
        uint32_t i, j = 0;
-       uint32_t is_interim = 0;
+       uint32_t is_interim;
+       uint32_t interim_cnt = 0;
        int ret;
 
        if (info.interim_callbacks) {
                for (i = 0; i < info.nb_vec_lines; i++) {
+                       is_interim = 0;
                        for (j = 0; info.interim_callbacks[j].key != NULL; j++)
                                if (strstr(info.vec[i],
                                        info.interim_callbacks[j].key)) {
@@ -339,17 +367,24 @@ fips_test_parse_one_case(void)
                                        if (ret < 0)
                                                return ret;
                                }
+
+                       if (is_interim)
+                               interim_cnt += 1;
                }
        }
 
-       if (is_interim) {
-               for (i = 0; i < info.nb_vec_lines; i++)
+       info.vec_start_off = interim_cnt;
+
+       if (interim_cnt) {
+               for (i = 0; i < interim_cnt; i++)
                        fprintf(info.fp_wr, "%s\n", info.vec[i]);
                fprintf(info.fp_wr, "\n");
-               return 1;
+
+               if (info.nb_vec_lines == interim_cnt)
+                       return 1;
        }
 
-       for (i = 0; i < info.nb_vec_lines; i++) {
+       for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
                for (j = 0; info.callbacks[j].key != NULL; j++)
                        if (strstr(info.vec[i], info.callbacks[j].key)) {
                                ret = info.callbacks[j].cb(
@@ -369,7 +404,7 @@ fips_test_write_one_case(void)
 {
        uint32_t i;
 
-       for (i = 0; i < info.nb_vec_lines; i++)
+       for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
                fprintf(info.fp_wr, "%s\n", info.vec[i]);
 }
 
@@ -613,6 +648,14 @@ update_info_vec(uint32_t count)
 
        cb = &info.writeback_callbacks[0];
 
+       if (!(strstr(info.vec[0], cb->key))) {
+               fprintf(info.fp_wr, "%s%u\n", cb->key, count);
+               i = 0;
+       } else {
+               snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
+                               count);
+               i = 1;
+       }
        snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
 
        for (i = 1; i < info.nb_vec_lines; i++) {