Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp53944pxb; Thu, 21 Apr 2022 16:51:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXvzrpMEWmh2qbxK44zoFUxI+BwIrSXQEG8iuXoSra1A1GXW7bBVMgyHfyJf23OhuHmi4x X-Received: by 2002:a17:90a:8b8b:b0:1ca:6007:36e5 with SMTP id z11-20020a17090a8b8b00b001ca600736e5mr2259819pjn.128.1650585069462; Thu, 21 Apr 2022 16:51:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650585069; cv=none; d=google.com; s=arc-20160816; b=wBjeRehlOSY2A2dLxA71CT6uEgEn28p55wflyUR+9WIbtunUZvrhGPV2JMKyCtcTjI jxalI8K6BBe1tZT2KAfXXzFZLGHtPmFpR1j+qfUMcrGANikM6Q53Y6V3rH/R76174fhs vHvVDGLlz7rrbIp2YiPBs/0CdTEeCHK75q+YY6tDjDbl70KJmCNJ/3zXBLthtoilFPvp XIhQUToNlSeAigDTH9L/gDTTK94ukbdl1k1tGdBt91vEPCFTqnxcIQ04RWTb7uldGYXz liUTmK40344LnTlU5S57GgzeTQD/rqriAnM0RBo9VhI5txgukhyF2Y7srDjoLRdSpFGr e/Yg== 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=GIRfho/UEmgLKksz3Hef8/60q1Jdf6mHBM1iOrwH5Jc=; b=qdZ6w+QMN1zXcrCzJQPQ/tGM+rpacq99ELu0KmH2VxzpnRkHPKY4Y4OHu81/3BIpRs FkXeJxL5VwVjobWF2ECVbp8qh45nkq9ea74qMF6T3OIwSmZmgiAIyP4rXE6DhnFJpAa6 BjnQT6pasXNQQpk95q0tAbN1k8yW8vhR19ogFQjvGy/BlzcAJkPzFujS0O88hp7GHOPf ib7JC7xvrc79A9cF115NYMbTFKtB6s1ciK4IOmN8WsaQD0Kuy0OCbudOcZch1lZjFDJV Jm8RHBI/EMpMxzYGfj/EaJ8MSQtFwkPnqSjJfdb+O+y8LUdzYqyXj5mM5C3bdUwm5U/d DQKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="H7/G2cA+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ng5-20020a17090b1a8500b001d529ef7481si5851440pjb.168.2022.04.21.16.50.53; Thu, 21 Apr 2022 16:51:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="H7/G2cA+"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1381429AbiDTSOT (ORCPT + 99 others); Wed, 20 Apr 2022 14:14:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379742AbiDTSOP (ORCPT ); Wed, 20 Apr 2022 14:14:15 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D66147061 for ; Wed, 20 Apr 2022 11:11:28 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id q3so2534917plg.3 for ; Wed, 20 Apr 2022 11:11:28 -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=GIRfho/UEmgLKksz3Hef8/60q1Jdf6mHBM1iOrwH5Jc=; b=H7/G2cA+tMU6dhaS4+bs+4ByL91KwkZGMQRvZqTZCWUqUKvbULyIT5oOWTFBeD+/qY CMOoaahlyITn1LWInAQNMlc0b1Zf1piHoFjlFn+sJHnvK9u0oS6Mq5poA3EkpZpa5pR8 BJTymtJhTU9IAKHqcSLOujdSNa+6oggXyVj9s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=GIRfho/UEmgLKksz3Hef8/60q1Jdf6mHBM1iOrwH5Jc=; b=5Pcmhn0K2C8qqGexHfd9UV6CyXQwbiA4SWyR1jO7ZO6G2xkPdHxJal00PyPkLzhjQQ m65HR4EOx0L98eu5BceNvFBIUDrTeB0Z40pny6wFGf4YAWNBDlOsIIe//cKHmJRgmO52 VFBv94RuUumbH9ITra4eT6zA+fwNRBI7ENGGl5iSjMJV8jDM9T4CdmwyzU9iS4Hqwsmf rO+MQCvoa2rEZ3Nem1+HDg6yOktgFML6tWJNNNF/jqWJEeTIQpUGt12W5Vt584H3/uxn AsHpwVsJrRdlVLv34npVESM9aSdVzrDAapsmFzLlK0MsSrNBpx5s/9xwVvAKZrsAkEsz qcbA== X-Gm-Message-State: AOAM5337ku9TemxqBwUlLSJYi9dgV6xMDpO1AsHLlyPz4AnqlzUIlXE/ sQMi0z6O832fcFMt4CEqAPOKZw== X-Received: by 2002:a17:903:41c4:b0:158:f3af:f5df with SMTP id u4-20020a17090341c400b00158f3aff5dfmr18794348ple.102.1650478288055; Wed, 20 Apr 2022 11:11:28 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q15-20020a056a00084f00b0050ab60bf37fsm5166814pfk.22.2022.04.20.11.11.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 11:11:27 -0700 (PDT) Date: Wed, 20 Apr 2022 11:11:26 -0700 From: Kees Cook To: Jann Horn Cc: Johan Hovold , kernel test robot , Greg Kroah-Hartman , linux-usb@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] USB: serial: Fix heap overflow in WHITEHEAT_GET_DTR_RTS Message-ID: <202204201056.5A1A6BAE04@keescook> References: <20220419041742.4117026-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 20, 2022 at 02:33:06PM +0200, Jann Horn wrote: > On Wed, Apr 20, 2022 at 10:14 AM Johan Hovold wrote: > > On Mon, Apr 18, 2022 at 09:17:42PM -0700, Kees Cook wrote: > > > This looks like it's harmless, as both the source and the destinations are > > > currently the same allocation size (4 bytes) and don't use their padding, > > > but if anything were to ever be added after the "mcr" member in "struct > > > whiteheat_private", it would be overwritten. The structs both have a > > > single u8 "mcr" member, but are 4 bytes in padded size. The memcpy() > > > destination was explicitly targeting the u8 member (size 1) with the > > > length of the whole structure (size 4), triggering the memcpy buffer > > > overflow warning: > > > > Ehh... No. The size of a structure with a single u8 is 1, not 4. There's > > nothing wrong with the current code even if the use of memcpy for this > > is a bit odd. I thought that was surprising too, and suspected it was something specific to the build (as Jann also suggested). I tracked it down[1] to "-mabi=apcs-gnu", which is from CONFIG_AEABI=n. whiteheat_private { __u8 mcr; /* 0 1 */ /* size: 4, cachelines: 1, members: 1 */ /* padding: 3 */ /* last cacheline: 4 bytes */ }; > > > > > In file included from include/linux/string.h:253, > > > from include/linux/bitmap.h:11, > > > from include/linux/cpumask.h:12, > > > from include/linux/smp.h:13, > > > from include/linux/lockdep.h:14, > > > from include/linux/spinlock.h:62, > > > from include/linux/mmzone.h:8, > > > from include/linux/gfp.h:6, > > > from include/linux/slab.h:15, > > > from drivers/usb/serial/whiteheat.c:17: > > > In function 'fortify_memcpy_chk', > > > inlined from 'firm_send_command' at drivers/usb/serial/whiteheat.c:587:4: > > > include/linux/fortify-string.h:328:25: warning: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning] > > > 328 | __write_overflow_field(p_size_field, size); > > > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > So something is confused here. > > So something's going wrong in fortify_memcpy_chk()? It looks like it > is called with constant "size" equal to 1, and the condition sizeof(info->mcr) is 1. sizeof(struct whiteheat_dr_info) (with CONFIG_AEABI=n) is 4. Given nothing actually uses "struct whiteheat_dr_info", except for the sizing (har har), I suspect the better solution is just to do: info->mcr = command_info->result_buffer[0]; -Kees [1] https://godbolt.org/z/3YvM1MYWW -- Kees Cook