Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp50673pxy; Wed, 21 Apr 2021 18:11:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJybdscrbdyC+Vcv7jkKtBwR5h1X+iNgG3OdxB7kVAnbG9puJw7+ODyhMD0yr1yF9T9dDxxb X-Received: by 2002:a63:500a:: with SMTP id e10mr916959pgb.242.1619053882011; Wed, 21 Apr 2021 18:11:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619053882; cv=none; d=google.com; s=arc-20160816; b=zYjB3H7LqXpO9O95DVXwuk2br41w/XuWuZ4KVNmaTzarlU3fRp0SZaCenhAaZK9Mmm vFr+hj1KwkkHTjAs+EitInQCH5WQbP0T1O7UxSskb9vgKg8cCCd0lWAEtK95RrErmdo/ HR2AtSK/jU9LlYu18gHMvyFW4OdmLEhTUTwXrTCScGji8nwN/+eF9pB7chN6AlDTZk4o uQygIVxkUdqJdDranRCipC+2xYl+gJ3UUAVdlrKmzkVBvb57wmjPCvRCVFtXwOATtl4y uGviy+dg6Gwsdx22vQYOOeqYGhhafGpB00EFM7rMaA2CMpPNNSK3WpyIs5vtN7owi3HY MTEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=h/beBkqWPWtq3+yl17G4GLEbQfDSlnnJp5RXRR66KYE=; b=HT7gQ6aTEwpfD7ZeY0MUtOXhFTzNmzjYeIBZ+J9VyzgGURbtLKqucq6xPuXMPdPhsR Vl0Z9uYKStZc0Sw5qKUNd8zUpJJYn0vtlzNUS9v7LnOendNUhgoD6ESz2KV3wecVohxL 4v/lLR2zUNfeEGxcKtHUf7ngl0tWmOBdAxEm283CAFB2E3ScKry33dfkApQ0IIXe3UnA K6NhaCLDdL3t790bYb2nkSPD++5ZZb0eWrtZTMKXI6tQk8cwzHFE+zDnxLTV4CbbkhHp opcqLHgupBuE99H3J9FlC90/PVeoBjfMMz/6Pj7y7W4uQknaxBsjuyorp8VS0HirZbJ3 TM2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cE0ORofp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m189si1087838pfb.79.2021.04.21.18.11.09; Wed, 21 Apr 2021 18:11:21 -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=pass header.i=@chromium.org header.s=google header.b=cE0ORofp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242499AbhDURlF (ORCPT + 99 others); Wed, 21 Apr 2021 13:41:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242273AbhDURlB (ORCPT ); Wed, 21 Apr 2021 13:41:01 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F776C06174A for ; Wed, 21 Apr 2021 10:40:27 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id h11so11902755pfn.0 for ; Wed, 21 Apr 2021 10:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=h/beBkqWPWtq3+yl17G4GLEbQfDSlnnJp5RXRR66KYE=; b=cE0ORofprgoov158xmxjlX5mEid/m5laCX19wY+7OZffLWMAyu4RXrrYeIEa7iS9mW zSNDddWAs9KTix7ZcMvjMulSqHoNTB3r6cgSok/jUsVlVoYId3I8bcU4kfTnDPzWnXIU QbgdAPoqeRzIEGzsjsCiO8HMSyRXKLHi4ZBag= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=h/beBkqWPWtq3+yl17G4GLEbQfDSlnnJp5RXRR66KYE=; b=LC7BN/VyPMp6nawN7Q7OVDNw06M1INumKueYdX5ARWgnIVlMebgQjRcY5GB9C1S2Pc 8r3UhPIpJ3iFxMQ6eHRx9F/6+8tIJwJ1FNrgtt5lqfGf5zesb9hbDY++tVxTVxEdVi5W X/PqeMUghg5vl0yF6MzOZ1wE1AAYxpw0gP90BkkTKOys4c1t0hzc/pOFagflk7imwkHa j/Lo23Z/qL4oy48KNmRjEugh9Cu4ipt1LEy4IwckK0BQHdkkPow/9cOEwyu30J5gJ9Aj unIUdSlFlF+cxjLYhwuEOfLFZJcLaMeqEqyhLQbA3MWUCu8Eb8FUTgFUDjT+QUO3Y8uW N8bw== X-Gm-Message-State: AOAM531yusIcPHCtaB/UWsbj1vSLiWXrHHlyW9v/Q9HGmfAVxRggpaaI 4SJoxte4+skqPZBwcQLntKdWBQ== X-Received: by 2002:a63:5b5c:: with SMTP id l28mr22480973pgm.363.1619026826588; Wed, 21 Apr 2021 10:40:26 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id m9sm58735pgt.65.2021.04.21.10.40.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Apr 2021 10:40:25 -0700 (PDT) Date: Wed, 21 Apr 2021 10:40:24 -0700 From: Kees Cook To: "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 Subject: Re: [PATCH] media: ngene: Fix out-of-bounds bug in ngene_command_config_free_buf() Message-ID: <202104211039.31E9785@keescook> References: <20210420001631.GA45456@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210420001631.GA45456@embeddedor> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > -- Kees Cook