media: firewire: firedtv-avc: fix a buffer overflow in avc_ca_pmt()
PATCH 1/1 media: firewire: firedtv-avc: fix a buffer overflow in avc_ca_pmt()
From: Dan Carpenter <[email protected]>
commit 35d2969ea3c7d32aee78066b1f3cf61a0d935a4e upstream.
The bounds checking in avc_ca_pmt() is not strict enough. It should be checking "read_pos + 4" because it's reading 5 bytes. If the "es_info_length" is non-zero then it reads a 6th byte so there needs to be an additional check for that.
I also added checks for the "write_pos". I don't think these are required because "read_pos" and "write_pos" are tied together so checking one ought to be enough. But they make the code easier to understand for me. The check on write_pos is:
if (write_pos + 4 >= sizeof(c->operand) - 4) {The first "+ 4" is because we're writing 5 bytes and the last " - 4" is to leave space for the CRC.
The other problem is that "length" can be invalid. It comes from "data_length" in fdtv_ca_pmt().
--- drivers/media/firewire/firedtv-avc.c | 14 +++++++++++--- drivers/media/firewire/firedtv-ci.c | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-)
drivers/media/firewire/firedtv-avc.c +11 -3
@@ -1169,7 +1169,11 @@ int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length) read_pos += program_info_length; write_pos += program_info_length; } -while (read_pos < length) { +while (read_pos +4 < length) { +if (write_pos +4 >=sizeof (c->operand) -4 ) { + ret = -EINVAL ; + goto out; + } c->operand[write_pos++] = msg[read_pos++]; c->operand[write_pos++] = msg[read_pos++]; c->operand[write_pos++] = msg[read_pos++];
@@ -1181,13 +1185,17 @@ int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length) c->operand[write_pos++] = es_info_length >>8 ; c->operand[write_pos++] = es_info_length &0xff ;if (es_info_length >0 ) { +if (read_pos >= length) { + ret = -EINVAL ; + goto out; + } pmt_cmd_id = msg[read_pos++];if (pmt_cmd_id !=1 && pmt_cmd_id !=4 )dev_err (fdtv->device,"invalid pmt_cmd_id %d " "at stream level\n" , pmt_cmd_id); -if (es_info_length >sizeof (c->operand) -4 - - write_pos) { +if (es_info_length >sizeof (c->operand) -4 - write_pos || + es_info_length > length - read_pos) { ret = -EINVAL ; goto out; }
drivers/media/firewire/firedtv-ci.c +2 -0
@@ -138,6 +138,8 @@ static int fdtv_ca_pmt(struct firedtv *fdtv, void *arg) }else { data_length = msg->msg[3 ]; } +if (data_length >sizeof (msg->msg) - data_pos) +return -EINVAL ;return avc_ca_pmt (fdtv, &msg->msg[data_pos], data_length); }
Greg KH
maintainerQuoted 76 lines · Ben Hutchings
From: Dan Carpenter <[email protected]>
commit
35d2969ea3c7d32aee78066b1f3cf61a0d935a4eupstream.The bounds checking in
avc_ca_pmt()is not strict enough. It should be checking "read_pos+ 4" because it's reading 5 bytes. If the "es_info_length" is non-zero then it reads a 6th byte so there needs to be an additional check for that.I also added checks for the "
write_pos". I don't think these are required because "read_pos" and "write_pos" are tied together so checking one ought to be enough. But they make the code easier to understand for me. The check onwrite_posis:if (write_pos + 4 >= sizeof(c->operand) - 4) {The first "+ 4" is because we're writing 5 bytes and the last " - 4" is to leave space for the CRC.
The other problem is that "length" can be invalid. It comes from "
data_length" infdtv_ca_pmt().Reported-by Luo Likang <[email protected]>Signed-off-by Dan Carpenter <[email protected]>Signed-off-by Hans Verkuil <[email protected]>Signed-off-by Mauro Carvalho Chehab <[email protected]>[bwh: Backported to 4.9: adjust context]Signed-off-by Ben Hutchings <[email protected]>--- drivers/media/firewire/firedtv-avc.c | 14 +++++++++++--- drivers/media/firewire/firedtv-ci.c | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-)drivers/media/firewire/firedtv-avc.c +11
-3@@ -1169,7 +1169,11 @@ int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length) read_pos += program_info_length; write_pos += program_info_length; } -while (read_pos < length) { +while (read_pos +4 < length) { +if (write_pos +4 >=sizeof (c->operand) -4 ) { + ret = -EINVAL ; + goto out; + } c->operand[write_pos++] = msg[read_pos++]; c->operand[write_pos++] = msg[read_pos++]; c->operand[write_pos++] = msg[read_pos++];@@ -1181,13 +1185,17 @@ int avc_ca_pmt(struct firedtv *fdtv, char *msg, int length) c->operand[write_pos++] = es_info_length >>8 ; c->operand[write_pos++] = es_info_length &0xff ;if (es_info_length >0 ) { +if (read_pos >= length) { + ret = -EINVAL ; + goto out; + } pmt_cmd_id = msg[read_pos++];if (pmt_cmd_id !=1 && pmt_cmd_id !=4 )dev_err (fdtv->device,"invalid pmt_cmd_id %d " "at stream level\n" , pmt_cmd_id); -if (es_info_length >sizeof (c->operand) -4 - - write_pos) { +if (es_info_length >sizeof (c->operand) -4 - write_pos || + es_info_length > length - read_pos) { ret = -EINVAL ; goto out; }drivers/media/firewire/firedtv-ci.c +0
-0
drivers/media/firewire/firedtv-ci.c static int fdtv_ca_pmt(struct firedtv *fdtv, void *arg) · line 145
Quoted 8 lines
drivers/media/firewire/firedtv-ci.c +2
-0@@ -138,6 +138,8 @@ static int fdtv_ca_pmt(struct firedtv *fdtv, void *arg) }else { data_length = msg->msg[3 ]; } +if (data_length >sizeof (msg->msg) - data_pos) +return -EINVAL ;return avc_ca_pmt (fdtv, &msg->msg[data_pos], data_length); }
All of the 4.9 backports now queued up,t hanks.
greg k-h