Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4132491pxv; Mon, 19 Jul 2021 17:59:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOpXLbikP8cqj9BjqTBbijhQCcmZu3l4LQ4Y2HIHCY2zrQUL+EKlNqMKV1oNKTiByJpHkP X-Received: by 2002:a17:907:2125:: with SMTP id qo5mr29862474ejb.252.1626742774993; Mon, 19 Jul 2021 17:59:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626742774; cv=none; d=google.com; s=arc-20160816; b=nYImYyaJzAogbGpM77qD/JnU/8pLHuR/o5oZRXNVSJLwWEH+SCBCx2wer7SJI3LXgd RcuRArMS44tqm7aZbchqdkjNJIaEzrLrk0j8xl+cfiwqQo/P8XWz03LdB9f7Kt56QGjj TKVKAGFLj6KpVnCz1jBxDGdM0HY8l7GBGJNtzCQbJslSMXNo4RWgind1ZDzDv7sHU3dl KjBtM7BtmgA8ZboZVr94xGNFHOTn9SY7UeZmzyJ5c9+KHVvYB/6//QbzOiIfcHJOU3Vz ecDEnWVd+KlSYSUM4SEHijRMXy9MiZDCjK32iMx68K7Vla/mum0PmOKVO2GgK1yIaFbB jrPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=nW0eV1Ts9Idn1vqEBQVZi/l1xO20XSwEQFJm3xKZUSk=; b=e24dVxnV5ycCKLip+idDrk3bGKGeWBSFwmIvP+ifZkyfMfPBcMG4r84d4a1tQuFopR FUcTmdIDpVpC+E0hkzM9SL/w58ri/vaO1fLpVunlsiMWPI45g4GMdMG3qJK7d4mVKUwm skOhGQbF/3wc4p4Qt4yml/m879ShjTW8Fa11haz5ncUTlQyeTmT/1SrRyXr3NwX2wLUE qlVkX/c7goJt+vJIIrdIGOF1IWAwQ2HRA/xyIzyKHp5y/lPboorpGhTJeTDal/NzKmji t3F5RZn5MWtYV51HSGE+al6xfhqTBKeHI4paAYhlbMwnRRCABCwdjvBB2dr2tJ/Q87l2 wEnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@embeddedor.com header.s=default header.b=OWST1RtC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l4si23296523edc.31.2021.07.19.17.59.05; Mon, 19 Jul 2021 17:59:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@embeddedor.com header.s=default header.b=OWST1RtC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349892AbhGTAL2 (ORCPT + 99 others); Mon, 19 Jul 2021 20:11:28 -0400 Received: from gateway30.websitewelcome.com ([192.185.193.11]:12540 "EHLO gateway30.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237024AbhGSXyI (ORCPT ); Mon, 19 Jul 2021 19:54:08 -0400 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway30.websitewelcome.com (Postfix) with ESMTP id AD358C08A for ; Mon, 19 Jul 2021 19:34:29 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id 5diPmwlSUuMjb5diPmhOhy; Mon, 19 Jul 2021 19:34:29 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=nW0eV1Ts9Idn1vqEBQVZi/l1xO20XSwEQFJm3xKZUSk=; b=OWST1RtCmqW2E+qNpLEcXgKHPn 2nQ3Jj6ZkOiJ0pnImB633q+ECV+aJGoEnYPUpIPP3z9U4AvLwEvMparH+Ei2blHjythXiK3hLcXHj 8u+QA5EkvY/K7rRDCZL3sDzVkvXQUS4S03TjBe3sJwZqVi2DzFl1ATHvJh2l/RCaJKjYcTHIPqqMv Lkz2Au0fNxfWLOlotGgbnBSzSGk3NoOieRquLnaU3W6aEUSykoEmmPR0Ye9V/VmHi2dd+1KlYGgfI k4QNOl1X16vHYwPyNMjODjAw+9A8JrS/BLv8o/IlTAgMI7b5sY7Xoj7b9L6MzQj090kKMbKJlaUru BQdFllBA==; Received: from 187-162-31-110.static.axtel.net ([187.162.31.110]:50338 helo=[192.168.15.8]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1m5diM-0017RG-68; Mon, 19 Jul 2021 19:34:26 -0500 Subject: Re: [PATCH] media: ngene: Fix out-of-bounds bug in ngene_command_config_free_buf() To: Kees Cook , "Gustavo A. R. Silva" Cc: Mauro Carvalho Chehab , Ralph Metzler , Matthias Benesch , Oliver Endriss , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20210420001631.GA45456@embeddedor> <202104211039.31E9785@keescook> From: "Gustavo A. R. Silva" Message-ID: <70836714-f419-8d85-a490-190bc91ba5ae@embeddedor.com> Date: Mon, 19 Jul 2021 19:36:38 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <202104211039.31E9785@keescook> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.162.31.110 X-Source-L: No X-Exim-ID: 1m5diM-0017RG-68 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187-162-31-110.static.axtel.net ([192.168.15.8]) [187.162.31.110]:50338 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 8 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi all, I'm taking this in my tree[1] for 5.14-rc3. Thanks -- Gustavo [1] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/log/?h=for-next/array-bounds On 4/21/21 12:40, Kees Cook wrote: > On Mon, Apr 19, 2021 at 07:16:31PM -0500, Gustavo A. R. Silva wrote: >> Fix an 11-year old bug in ngene_command_config_free_buf() while >> addressing the following warnings caught with -Warray-bounds: >> >> arch/alpha/include/asm/string.h:22:16: warning: '__builtin_memcpy' offset [12, 16] from the object at 'com' is out of the bounds of referenced subobject 'config' with type 'unsigned char' at offset 10 [-Warray-bounds] >> arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [12, 16] from the object at 'com' is out of the bounds of referenced subobject 'config' with type 'unsigned char' at offset 10 [-Warray-bounds] >> >> The problem is that the original code is trying to copy 6 bytes of >> data into a one-byte size member _config_ of the wrong structue >> FW_CONFIGURE_BUFFERS, in a single call to memcpy(). This causes a >> legitimate compiler warning because memcpy() overruns the length >> of &com.cmd.ConfigureBuffers.config. It seems that the right >> structure is FW_CONFIGURE_FREE_BUFFERS, instead, because it contains >> 6 more members apart from the header _hdr_. Also, the name of >> the function ngene_command_config_free_buf() suggests that the actual >> intention is to ConfigureFreeBuffers, instead of ConfigureBuffers >> (which configuration takes place in the function ngene_command_config_buf(), >> above). >> >> Fix this by enclosing those 6 members of struct FW_CONFIGURE_FREE_BUFFERS >> into new struct config, and use &com.cmd.ConfigureFreeBuffers.config as >> the destination address, instead of &com.cmd.ConfigureBuffers.config, >> when calling memcpy(). >> >> This also helps with the ongoing efforts to globally enable >> -Warray-bounds and get us closer to being able to tighten the >> FORTIFY_SOURCE routines on memcpy(). >> >> Link: https://github.com/KSPP/linux/issues/109 >> Fixes: dae52d009fc9 ("V4L/DVB: ngene: Initial check-in") >> Cc: stable@vger.kernel.org >> Reported-by: kernel test robot >> Signed-off-by: Gustavo A. R. Silva > > Nice find! Yeah, this looks like a copy/paste bug but it went unnoticed > because it's occupying the same memory via the union. Heh. > > Reviewed-by: Kees Cook > > -Kees > >> --- >> drivers/media/pci/ngene/ngene-core.c | 2 +- >> drivers/media/pci/ngene/ngene.h | 14 ++++++++------ >> 2 files changed, 9 insertions(+), 7 deletions(-) >> >> diff --git a/drivers/media/pci/ngene/ngene-core.c b/drivers/media/pci/ngene/ngene-core.c >> index 07f342db6701..7481f553f959 100644 >> --- a/drivers/media/pci/ngene/ngene-core.c >> +++ b/drivers/media/pci/ngene/ngene-core.c >> @@ -385,7 +385,7 @@ static int ngene_command_config_free_buf(struct ngene *dev, u8 *config) >> >> com.cmd.hdr.Opcode = CMD_CONFIGURE_FREE_BUFFER; >> com.cmd.hdr.Length = 6; >> - memcpy(&com.cmd.ConfigureBuffers.config, config, 6); >> + memcpy(&com.cmd.ConfigureFreeBuffers.config, config, 6); >> com.in_len = 6; >> com.out_len = 0; >> >> diff --git a/drivers/media/pci/ngene/ngene.h b/drivers/media/pci/ngene/ngene.h >> index 84f04e0e0cb9..3d296f1998a1 100644 >> --- a/drivers/media/pci/ngene/ngene.h >> +++ b/drivers/media/pci/ngene/ngene.h >> @@ -407,12 +407,14 @@ enum _BUFFER_CONFIGS { >> >> struct FW_CONFIGURE_FREE_BUFFERS { >> struct FW_HEADER hdr; >> - u8 UVI1_BufferLength; >> - u8 UVI2_BufferLength; >> - u8 TVO_BufferLength; >> - u8 AUD1_BufferLength; >> - u8 AUD2_BufferLength; >> - u8 TVA_BufferLength; >> + struct { >> + u8 UVI1_BufferLength; >> + u8 UVI2_BufferLength; >> + u8 TVO_BufferLength; >> + u8 AUD1_BufferLength; >> + u8 AUD2_BufferLength; >> + u8 TVA_BufferLength; >> + } __packed config; >> } __attribute__ ((__packed__)); >> >> struct FW_CONFIGURE_UART { >> -- >> 2.27.0 >> >