Skip to content

Commit 14a2276

Browse files
committed
media: s5p-jpeg: prevent buffer overflows
The current logic allows word to be less than 2. If this happens, there will be buffer overflows, as reported by smatch. Add extra checks to prevent it. While here, remove an unused word = 0 assignment. Fixes: 6c96dbb ("[media] s5p-jpeg: add support for 5433") Cc: stable@vger.kernel.org Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Reviewed-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>
1 parent 458ea1c commit 14a2276

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/media/platform/samsung/s5p-jpeg/jpeg-core.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,14 @@ static void exynos4_jpeg_parse_decode_h_tbl(struct s5p_jpeg_ctx *ctx)
775775
(unsigned long)vb2_plane_vaddr(&vb->vb2_buf, 0) + ctx->out_q.sos + 2;
776776
jpeg_buffer.curr = 0;
777777

778-
word = 0;
779-
780778
if (get_word_be(&jpeg_buffer, &word))
781779
return;
782-
jpeg_buffer.size = (long)word - 2;
780+
781+
if (word < 2)
782+
jpeg_buffer.size = 0;
783+
else
784+
jpeg_buffer.size = (long)word - 2;
785+
783786
jpeg_buffer.data += 2;
784787
jpeg_buffer.curr = 0;
785788

@@ -1058,6 +1061,7 @@ static int get_word_be(struct s5p_jpeg_buffer *buf, unsigned int *word)
10581061
if (byte == -1)
10591062
return -1;
10601063
*word = (unsigned int)byte | temp;
1064+
10611065
return 0;
10621066
}
10631067

@@ -1145,7 +1149,7 @@ static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
11451149
if (get_word_be(&jpeg_buffer, &word))
11461150
break;
11471151
length = (long)word - 2;
1148-
if (!length)
1152+
if (length <= 0)
11491153
return false;
11501154
sof = jpeg_buffer.curr; /* after 0xffc0 */
11511155
sof_len = length;
@@ -1176,7 +1180,7 @@ static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
11761180
if (get_word_be(&jpeg_buffer, &word))
11771181
break;
11781182
length = (long)word - 2;
1179-
if (!length)
1183+
if (length <= 0)
11801184
return false;
11811185
if (n_dqt >= S5P_JPEG_MAX_MARKER)
11821186
return false;
@@ -1189,7 +1193,7 @@ static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
11891193
if (get_word_be(&jpeg_buffer, &word))
11901194
break;
11911195
length = (long)word - 2;
1192-
if (!length)
1196+
if (length <= 0)
11931197
return false;
11941198
if (n_dht >= S5P_JPEG_MAX_MARKER)
11951199
return false;
@@ -1214,6 +1218,7 @@ static bool s5p_jpeg_parse_hdr(struct s5p_jpeg_q_data *result,
12141218
if (get_word_be(&jpeg_buffer, &word))
12151219
break;
12161220
length = (long)word - 2;
1221+
/* No need to check underflows as skip() does it */
12171222
skip(&jpeg_buffer, length);
12181223
break;
12191224
}

0 commit comments

Comments
 (0)