Blame SOURCES/openjpeg2-CVE-2021-29338.patch

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