0591ff
From efbfbbb723e100cfbcea287a30958bf678e83458 Mon Sep 17 00:00:00 2001
0591ff
From: Ariadne Conill <ariadne@dereferenced.org>
0591ff
Date: Tue, 27 Apr 2021 09:37:40 -0600
0591ff
Subject: [PATCH] opj_{compress,decompress,dump}: fix possible buffer overflows
0591ff
 in path manipulation functions
0591ff
0591ff
---
0591ff
 src/bin/jp2/opj_compress.c   | 12 ++++++------
0591ff
 src/bin/jp2/opj_decompress.c | 13 ++++++-------
0591ff
 src/bin/jp2/opj_dump.c       | 14 +++++++-------
0591ff
 3 files changed, 19 insertions(+), 20 deletions(-)
0591ff
0591ff
diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c
0591ff
index 6827484..d8f894c 100644
0591ff
--- a/src/bin/jp2/opj_compress.c
0591ff
+++ b/src/bin/jp2/opj_compress.c
0591ff
@@ -543,8 +543,8 @@ static char * get_file_name(char *name)
0591ff
 static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
                           opj_cparameters_t *parameters)
0591ff
 {
0591ff
-    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
0591ff
-         outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
0591ff
+    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN * 2],
0591ff
+         outfilename[OPJ_PATH_LEN * 2], temp_ofname[OPJ_PATH_LEN];
0591ff
     char *temp_p, temp1[OPJ_PATH_LEN] = "";
0591ff
 
0591ff
     strcpy(image_filename, dirptr->filename[imageno]);
0591ff
@@ -553,7 +553,7 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
     if (parameters->decod_format == -1) {
0591ff
         return 1;
0591ff
     }
0591ff
-    sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
0591ff
+    snprintf(infilename, OPJ_PATH_LEN * 2, "%s/%s", img_fol->imgdirpath, image_filename);
0591ff
     if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
0591ff
                      infilename) != 0) {
0591ff
         return 1;
0591ff
@@ -566,7 +566,7 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
         sprintf(temp1, ".%s", temp_p);
0591ff
     }
0591ff
     if (img_fol->set_out_format == 1) {
0591ff
-        sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
+        snprintf(outfilename, OPJ_PATH_LEN * 2, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
                 img_fol->out_format);
0591ff
         if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
0591ff
                          outfilename) != 0) {
0591ff
@@ -1910,9 +1910,9 @@ int main(int argc, char **argv)
0591ff
         num_images = get_num_images(img_fol.imgdirpath);
0591ff
         dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
0591ff
         if (dirptr) {
0591ff
-            dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
0591ff
+            dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
0591ff
                     char)); /* Stores at max 10 image file names*/
0591ff
-            dirptr->filename = (char**) malloc(num_images * sizeof(char*));
0591ff
+            dirptr->filename = (char**) calloc(num_images, sizeof(char*));
0591ff
             if (!dirptr->filename_buf) {
0591ff
                 ret = 0;
0591ff
                 goto fin;
0591ff
diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c
0591ff
index 2634907..e54e54f 100644
0591ff
--- a/src/bin/jp2/opj_decompress.c
0591ff
+++ b/src/bin/jp2/opj_decompress.c
0591ff
@@ -455,13 +455,13 @@ const char* path_separator = "/";
0591ff
 char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
                    opj_decompress_parameters *parameters)
0591ff
 {
0591ff
-    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
0591ff
-         outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
0591ff
+    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN * 2],
0591ff
+         outfilename[OPJ_PATH_LEN * 2], temp_ofname[OPJ_PATH_LEN];
0591ff
     char *temp_p, temp1[OPJ_PATH_LEN] = "";
0591ff
 
0591ff
     strcpy(image_filename, dirptr->filename[imageno]);
0591ff
     fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
0591ff
-    sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator,
0591ff
+    snprintf(infilename, OPJ_PATH_LEN * 2, "%s%s%s", img_fol->imgdirpath, path_separator,
0591ff
             image_filename);
0591ff
     parameters->decod_format = infile_format(infilename);
0591ff
     if (parameters->decod_format == -1) {
0591ff
@@ -479,7 +479,7 @@ char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
         sprintf(temp1, ".%s", temp_p);
0591ff
     }
0591ff
     if (img_fol->set_out_format == 1) {
0591ff
-        sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
+        snprintf(outfilename, OPJ_PATH_LEN * 2, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
                 img_fol->out_format);
0591ff
         if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
0591ff
                          outfilename) != 0) {
0591ff
@@ -1357,14 +1357,13 @@ int main(int argc, char **argv)
0591ff
             return EXIT_FAILURE;
0591ff
         }
0591ff
         /* Stores at max 10 image file names */
0591ff
-        dirptr->filename_buf = (char*)malloc(sizeof(char) *
0591ff
-                                             (size_t)num_images * OPJ_PATH_LEN);
0591ff
+        dirptr->filename_buf = calloc((size_t) num_images, sizeof(char) * OPJ_PATH_LEN);
0591ff
         if (!dirptr->filename_buf) {
0591ff
             failed = 1;
0591ff
             goto fin;
0591ff
         }
0591ff
 
0591ff
-        dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
0591ff
+        dirptr->filename = (char**) calloc((size_t) num_images, sizeof(char*));
0591ff
 
0591ff
         if (!dirptr->filename) {
0591ff
             failed = 1;
0591ff
diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c
0591ff
index 6e15fee..4e19c61 100644
0591ff
--- a/src/bin/jp2/opj_dump.c
0591ff
+++ b/src/bin/jp2/opj_dump.c
0591ff
@@ -201,8 +201,8 @@ static int get_file_format(const char *filename)
0591ff
 static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
                           opj_dparameters_t *parameters)
0591ff
 {
0591ff
-    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
0591ff
-         outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
0591ff
+    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN * 2],
0591ff
+         outfilename[OPJ_PATH_LEN * 2], temp_ofname[OPJ_PATH_LEN];
0591ff
     char *temp_p, temp1[OPJ_PATH_LEN] = "";
0591ff
 
0591ff
     strcpy(image_filename, dirptr->filename[imageno]);
0591ff
@@ -211,7 +211,7 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
     if (parameters->decod_format == -1) {
0591ff
         return 1;
0591ff
     }
0591ff
-    sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
0591ff
+    snprintf(infilename, OPJ_PATH_LEN * 2, "%s/%s", img_fol->imgdirpath, image_filename);
0591ff
     if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
0591ff
                      infilename) != 0) {
0591ff
         return 1;
0591ff
@@ -224,7 +224,7 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
0591ff
         sprintf(temp1, ".%s", temp_p);
0591ff
     }
0591ff
     if (img_fol->set_out_format == 1) {
0591ff
-        sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
+        snprintf(outfilename, OPJ_PATH_LEN * 2, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
0591ff
                 img_fol->out_format);
0591ff
         if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
0591ff
                          outfilename) != 0) {
0591ff
@@ -457,7 +457,7 @@ int main(int argc, char *argv[])
0591ff
     opj_codestream_info_v2_t* cstr_info = NULL;
0591ff
     opj_codestream_index_t* cstr_index = NULL;
0591ff
 
0591ff
-    OPJ_INT32 num_images, imageno;
0591ff
+    int num_images, imageno;
0591ff
     img_fol_t img_fol;
0591ff
     dircnt_t *dirptr = NULL;
0591ff
 
0591ff
@@ -486,13 +486,13 @@ int main(int argc, char *argv[])
0591ff
         if (!dirptr) {
0591ff
             return EXIT_FAILURE;
0591ff
         }
0591ff
-        dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
0591ff
+        dirptr->filename_buf = (char*) calloc((size_t) num_images, OPJ_PATH_LEN * sizeof(
0591ff
                 char)); /* Stores at max 10 image file names*/
0591ff
         if (!dirptr->filename_buf) {
0591ff
             free(dirptr);
0591ff
             return EXIT_FAILURE;
0591ff
         }
0591ff
-        dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
0591ff
+        dirptr->filename = (char**) calloc((size_t) num_images, sizeof(char*));
0591ff
 
0591ff
         if (!dirptr->filename) {
0591ff
             goto fails;
0591ff
-- 
0591ff
2.31.1
0591ff