Skip to content
Permalink
Browse files Browse the repository at this point in the history
opj_t2_encode_packet(): fix potential write heap buffer overflow (#992)
  • Loading branch information
rouault committed Aug 16, 2017
1 parent dcac91b commit c535531
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/openjp2/j2k.c
Expand Up @@ -4215,7 +4215,6 @@ static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
assert(p_stream != 00);

OPJ_UNUSED(p_stream);
OPJ_UNUSED(p_manager);

if (p_total_data_size < 12) {
opj_event_msg(p_manager, EVT_ERROR,
Expand Down Expand Up @@ -4617,6 +4616,12 @@ static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,

OPJ_UNUSED(p_stream);

if (p_total_data_size < 4) {
opj_event_msg(p_manager, EVT_ERROR,
"Not enough bytes in output buffer to write SOD marker\n");
return OPJ_FALSE;
}

opj_write_bytes(p_data, J2K_MS_SOD,
2); /* SOD */
p_data += 2;
Expand Down
18 changes: 18 additions & 0 deletions src/lib/openjp2/t2.c
Expand Up @@ -629,6 +629,15 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,

/* <SOP 0xff91> */
if (tcp->csty & J2K_CP_CSTY_SOP) {
if (length < 6) {
if (p_t2_mode == FINAL_PASS) {
opj_event_msg(p_manager, EVT_ERROR,
"opj_t2_encode_packet(): only %u bytes remaining in "
"output buffer. %u needed.\n",
length, 6);
}
return OPJ_FALSE;
}
c[0] = 255;
c[1] = 145;
c[2] = 0;
Expand Down Expand Up @@ -817,6 +826,15 @@ static OPJ_BOOL opj_t2_encode_packet(OPJ_UINT32 tileno,

/* <EPH 0xff92> */
if (tcp->csty & J2K_CP_CSTY_EPH) {
if (length < 2) {
if (p_t2_mode == FINAL_PASS) {
opj_event_msg(p_manager, EVT_ERROR,
"opj_t2_encode_packet(): only %u bytes remaining in "
"output buffer. %u needed.\n",
length, 2);
}
return OPJ_FALSE;
}
c[0] = 255;
c[1] = 146;
c += 2;
Expand Down

3 comments on commit c535531

@fcolista
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
Applying this patch to the stable openjpeg 2.2.0 release ends in a compile error.

/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c: In function 
'opj_t2_encode_packet':
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:622:17: error: 'p_t2_mode' undeclared (first use in this function)
             if (p_t2_mode == FINAL_PASS) {
                 ^~~~~~~~~
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:622:17: note: each undeclared identifier is reported only once for each function it appears in
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:623:31: error: 'p_manager' undeclared (first use in this function)
                 opj_event_msg(p_manager, EVT_ERROR,
                               ^~~~~~~~~
make[2]: *** [src/lib/openjp2/CMakeFiles/openjp2_static.dir/build.make:423: src/lib/openjp2/CMakeFiles/openjp2_static.dir/t2.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 44%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t1.c.o
[ 46%] Building C object src/lib/openjp2/CMakeFiles/openjp2.dir/t2.c.o
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c: In function 'opj_t2_encode_packet':
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:622:17: error: 'p_t2_mode' undeclared (first use in this function)
             if (p_t2_mode == FINAL_PASS) {
                 ^~~~~~~~~
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:622:17: note: each undeclared identifier is reported only once for each function it appears in
/home/fcolista/aports/main/openjpeg/src/openjpeg-2.2.0/src/lib/openjp2/t2.c:623:31: error: 'p_manager' undeclared (first use in this function)
                 opj_event_msg(p_manager, EVT_ERROR,
                               ^~~~~~~~~
make[2]: *** [src/lib/openjp2/CMakeFiles/openjp2.dir/build.make:423: src/lib/openjp2/CMakeFiles/openjp2.dir/t2.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:143: src/lib/openjp2/CMakeFiles/openjp2_static.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:106: src/lib/openjp2/CMakeFiles/openjp2.dir/all] Error 2
make: *** [Makefile:152: all] Error 2
>>> ERROR: openjpeg: all failed

Perhaps this is only valid for the master branch?
Can this patch be backported to stable release too, or can you release 2.2.1 which includes this fix?
Thankx.

@rouault
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this patch depends on previous commits, including 9624b2f and 0b4fef6 , but I'm not sure they are self sufficient by themselves and wouldn't introduce regressions.
A 2.2.1 release should happen soon

@rouault
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and even it that compiles, backporting "random" changesets is somewhat risky from a functionnality point of view. I strongly recommand you run the regression test suite with OPJ_NONCOMMERCIAL=YES ./tools/travis-ci/run.sh and compare the results before&after (there are some known failures depending on platforms, etc)

Please sign in to comment.