Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 149B5C433F5 for ; Thu, 27 Jan 2022 15:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbiA0PfM (ORCPT ); Thu, 27 Jan 2022 10:35:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237917AbiA0PfL (ORCPT ); Thu, 27 Jan 2022 10:35:11 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B9D5C061714 for ; Thu, 27 Jan 2022 07:35:11 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6D78DB821C6 for ; Thu, 27 Jan 2022 15:35:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58EEAC340E4; Thu, 27 Jan 2022 15:35:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643297709; bh=KVzat2ekAP5s0/fy93UOT1Isd2nzRWT0I6x8yRwaxGo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MEpnOI3hQyEOCN0ttLy1hH3peQ7jMP13sFIP/83fO4L2gfLnpfj58EwPaQahEfhCT qXxV0o5n1zPaLn9SGWuWeFvyxjyRlMf3xWahlhoXpTkdPFHfcKsJq7zcnrsDBDSRJE IbmM+yVA8cbL830VDRD0GwkXpkIXGmHbz0J5n88k= Date: Thu, 27 Jan 2022 16:35:06 +0100 From: Greg KH To: Ben Hutchings Cc: stable@vger.kernel.org, Dan Carpenter , Luo Likang , Hans Verkuil , Mauro Carvalho Chehab Subject: Re: [PATCH 4.9] media: firewire: firedtv-avc: fix a buffer overflow in avc_ca_pmt() Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org On Mon, Jan 24, 2022 at 07:35:29PM +0100, Ben Hutchings wrote: > From: Dan Carpenter > > 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(). > > Cc: stable@vger.kernel.org > Reported-by: Luo Likang > Signed-off-by: Dan Carpenter > Signed-off-by: Hans Verkuil > Signed-off-by: Mauro Carvalho Chehab > [bwh: Backported to 4.9: adjust context] > Signed-off-by: Ben Hutchings > --- > drivers/media/firewire/firedtv-avc.c | 14 +++++++++++--- > drivers/media/firewire/firedtv-ci.c | 2 ++ > 2 files changed, 13 insertions(+), 3 deletions(-) > > diff --git a/drivers/media/firewire/firedtv-avc.c b/drivers/media/firewire/firedtv-avc.c > index 280b5ffea592..3a373711f5ad 100644 > --- a/drivers/media/firewire/firedtv-avc.c > +++ b/drivers/media/firewire/firedtv-avc.c > @@ -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; > } > diff --git a/drivers/media/firewire/firedtv-ci.c b/drivers/media/firewire/firedtv-ci.c > index edbb30fdd9d9..93fb4b7312af 100644 > --- a/drivers/media/firewire/firedtv-ci.c > +++ b/drivers/media/firewire/firedtv-ci.c > @@ -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