Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2289958lqo; Mon, 20 May 2024 00:32:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWymlSsYJzUcwLAQO5RGTcg5BKH346jy5zZk68l4vQqiYZiMqqNo37STWbUoAR5FYQtOp5OoLGaD7FP9CkWwHMGIuCBdOfTbh+19/2aMg== X-Google-Smtp-Source: AGHT+IEElf+QGL8vnjYEGJ9Trzy7JzhOE6Vi8kz2UnU4xWlppqCzw0FvqllaFTMBOLcSrprfMgu7 X-Received: by 2002:a05:6830:1e84:b0:6f0:e1b8:4490 with SMTP id 46e09a7af769-6f0e92c8f6dmr29855890a34.23.1716190346391; Mon, 20 May 2024 00:32:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716190346; cv=pass; d=google.com; s=arc-20160816; b=MrnIuyWOYGxOznbiz0veoxDXsxVAYmK9AtHhOErGE7cfgrrCuqhSNK1gzvmWjs/XcI XGSA8pyv7Y7lkOni+RlZH5hXRZl0bsBKP9PFSi/iBm4JmoKzQ7WpPm4U5tYZ6l+tie3F weu/VaqayIrcdzWp5dx6HdmVhgy6+guYuLcI5wH/LAo+Uv+n1oaRnAX/GvTmalWoFFca DIOcl+nvqcDBf9ehgO5MWfkJhhH814D7VhKcIg+ichmGYjMwUeM5CTBRTaY8vDiqMu7V Gk2WCoFtEx2SzrtTurOTl8BVI0NMkrdvVI7H72nnzM36pZzOM76wAoEHXwe3X/2fwoBj +icA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=E/kjzu1Wex4qcsfYIAvHw87WxHOGH99XN8pJZ/+CH0Q=; fh=unwcCOvioABILO7S336WOPprLIGW+4hu8nC5qaE+I40=; b=n/AB19/ps7DBsSDnW0BcGOf3kYcPuFsbwnVKbnsmCD+CFox9ohjFgyq/Mb4Qalytdc ZwOFH9jecd8VayvbcM/ZsMyEQoFBYiJ7SnczHeIgYcMRP8o1gVaC7r1Qqwwa58bJ8D3m QB3aInkhq8Ah8Vaz5YNa3mg5MNPhw6w4vGDqHfB8oPnw40eOfbkFzOhgnw5vIVnO3CwH t7dves5sMqL9KTwKGY5nHm/63Vwh13ZwxPI1EOin8j2SOEpBt7j6+DoaRvPAoVZObfPn MRbtIt9o381Puq+yOTe3+gC1nESMOCgZioRLGpJpsQeHJgliG3WqeuUYfuGFVEzyh1DV qSYA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass (test mode) header.i=@wp.pl header.s=1024a header.b=EfuHjjdH; arc=pass (i=1 spf=pass spfdomain=wp.pl dkim=pass dkdomain=wp.pl dmarc=pass fromdomain=wp.pl); spf=pass (google.com: domain of linux-wireless+bounces-7838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-7838-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wp.pl Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-63bde1f76fesi1008228a12.656.2024.05.20.00.32.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 00:32:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless+bounces-7838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@wp.pl header.s=1024a header.b=EfuHjjdH; arc=pass (i=1 spf=pass spfdomain=wp.pl dkim=pass dkdomain=wp.pl dmarc=pass fromdomain=wp.pl); spf=pass (google.com: domain of linux-wireless+bounces-7838-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-7838-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wp.pl Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 720E32812C9 for ; Mon, 20 May 2024 07:32:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 368F718EA1; Mon, 20 May 2024 07:32:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=wp.pl header.i=@wp.pl header.b="EfuHjjdH" X-Original-To: linux-wireless@vger.kernel.org Received: from mx4.wp.pl (mx4.wp.pl [212.77.101.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B839F17BA3 for ; Mon, 20 May 2024 07:32:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.77.101.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716190343; cv=none; b=dMHvml0vrp+sa/oJJu6o9zWHBMsHz1MV5WgYVePYs/n3jRgEgOlEErRxtbvP/23lZLNdl8RDWKYIVwwuUGnDMeUpdjEsKtmKeXVVv7seDlIJYCxipP0vc5i2gTLlqEPlo8sh5r+q8y052iHNm+UrpdiRSi3uOz8hs5ZG0QEmxVM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716190343; c=relaxed/simple; bh=SXWLRqg/cx1GwPNZn/xC44bifhSGMJ1Ot6OtCQCtJ/8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WrH7EB/cLsflp39gZp9e9a5FRjFwZRHPoDGG939cRedP83dPRhgZUMiJ2pu/j9H3GjSspfRke/GZ5pumqWp8fWFkAr9V9lTcltLh5AckKXkVzE8SrHzGVpDZBnWQ3ST+HSl1vZC6C2BKYR6Tgjtl/+q/caKciedP3AuijypjTwg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wp.pl; spf=pass smtp.mailfrom=wp.pl; dkim=pass (1024-bit key) header.d=wp.pl header.i=@wp.pl header.b=EfuHjjdH; arc=none smtp.client-ip=212.77.101.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wp.pl Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wp.pl Received: (wp-smtpd smtp.wp.pl 43762 invoked from network); 20 May 2024 09:32:10 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wp.pl; s=1024a; t=1716190330; bh=E/kjzu1Wex4qcsfYIAvHw87WxHOGH99XN8pJZ/+CH0Q=; h=From:To:Cc:Subject; b=EfuHjjdHSt0YksyHxNYWhj5RZIg5INw1WjZZ2lJ5z/oG5CIeaDjzbMsN4TSwCk90f fk3kXqSpGMc2dbMXoYO4FhjnZfZI0bmyb4Uwa6OveFzcDP/oxZJPsKhlSYcx+EgSgd s2wF54eg6X86ZHz4OKLQ+yXSF8N+o4Hmn8/pbeS8= Received: from 89-64-9-76.dynamic.chello.pl (HELO localhost) (stf_xl@wp.pl@[89.64.9.76]) (envelope-sender ) by smtp.wp.pl (WP-SMTPD) with ECDHE-RSA-AES256-GCM-SHA384 encrypted SMTP for ; 20 May 2024 09:32:10 +0200 Date: Mon, 20 May 2024 09:32:10 +0200 From: Stanislaw Gruszka To: Kees Cook Cc: "Gustavo A. R. Silva" , Xose Vazquez Perez , linux-wireless@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [WARNING] memcpy: detected field-spanning write (size 1005) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3173 (size 320) Message-ID: <20240520073210.GA693073@wp.pl> References: <2c534d01-449a-43f4-9216-eacdb3b35577@gmail.com> <20240518092939.GA643846@wp.pl> <202405181033.6399B7E416@keescook> Precedence: bulk X-Mailing-List: linux-wireless@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202405181033.6399B7E416@keescook> X-WP-MailID: 39b0581376fbfbf62c4d4b81b3d927d8 X-WP-AV: skaner antywirusowy Poczty Wirtualnej Polski X-WP-SPAM: NO 0000002 [ARGB] On Sat, May 18, 2024 at 10:48:08AM -0700, Kees Cook wrote: > On Sat, May 18, 2024 at 11:29:39AM +0200, Stanislaw Gruszka wrote: > > Hi > > > > On Fri, Apr 12, 2024 at 07:48:39PM +0200, Xose Vazquez Perez wrote: > > > Hi, > > > > > > In Fedora kernel 6.8.5-301.fc40.x86_64, dmesg shows: > > > > > > [ device: 03:00.0 Network controller [0280]: Intel Corporation PRO/Wireless 4965 AG or AGN [Kedron] Network Connection [8086:4230] (rev 61) ] > > > > > > Thanks. > > > > > > [ 53.407607] ------------[ cut here ]------------ > > > [ 53.407622] memcpy: detected field-spanning write (size 1005) of single field "&out_cmd->cmd.payload" at drivers/net/wireless/intel/iwlegacy/common.c:3173 (size 320) > > > [ 53.407721] WARNING: CPU: 1 PID: 1632 at drivers/net/wireless/intel/iwlegacy/common.c:3173 il_enqueue_hcmd+0x477/0x560 [iwlegacy] > > > > For CMD_SIZE_HUGE we have allocated 4k, so we do not do anything wrong. > > Except maybe code is convoluted, since we use same structure for > > huge and small il_device_cmd allocations. > > > > But I'm thinking how to fix this fortify warning without refactoring and > > some extra runtime cost ... > > > > Xose, could you test below patch? I did not tested it, but I think > > it should make this particular warning gone and does not break > > anything. But maybe it will trigger some others fortify warnings. > > tl;dr: the proposed patch should work. Refactoring will still be needed > in the future. :) > > Long version: > > struct il_device_cmd { > struct il_cmd_header hdr; /* uCode API */ > union { > u32 flags; > u8 val8; > u16 val16; > u32 val32; > struct il_tx_cmd tx; > u8 payload[DEF_CMD_PAYLOAD_SIZE]; > } __packed cmd; > } __packed; > > struct il_cmd_header { > ... > /* command or response/notification data follows immediately */ > u8 data[]; > } __packed; > > Yes, the proposed fix will silence the warning, but this struct is > certainly on Gustavo's list to fix for "flexible arrays not-at-end" > warnings[1]. > > This memcpy() is the perfect example of why we need to refactor these > kinds of structs: the object size is ambiguous for the compiler. It > could be as little as sizeof(struct il_device_cmd), but it could larger, > because of the "hdr" member. Right now, it depends on how we happen to > address it (as your change is doing). I think we can just remove "data" from il_cmd_heder (I relized it's not used anyway) and put it into command union. So below patch should fix current warning and potential future warnings as well. Xose, could you give it a try ? Thanks Stanislaw diff --git a/drivers/net/wireless/intel/iwlegacy/commands.h b/drivers/net/wireless/intel/iwlegacy/commands.h index 28cf4e832152..dca8ecf89d1e 100644 --- a/drivers/net/wireless/intel/iwlegacy/commands.h +++ b/drivers/net/wireless/intel/iwlegacy/commands.h @@ -201,9 +201,6 @@ struct il_cmd_header { * 15 unsolicited RX or uCode-originated notification */ __le16 sequence; - - /* command or response/notification data follows immediately */ - u8 data[]; } __packed; /** diff --git a/drivers/net/wireless/intel/iwlegacy/common.c b/drivers/net/wireless/intel/iwlegacy/common.c index 9d33a66a49b5..8f02f252b577 100644 --- a/drivers/net/wireless/intel/iwlegacy/common.c +++ b/drivers/net/wireless/intel/iwlegacy/common.c @@ -3170,7 +3170,7 @@ il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd) out_meta->callback = cmd->callback; out_cmd->hdr.cmd = cmd->id; - memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len); + memcpy(&out_cmd->cmd.raw, cmd->data, cmd->len); /* At this point, the out_cmd now has all of the incoming cmd * information */ diff --git a/drivers/net/wireless/intel/iwlegacy/common.h b/drivers/net/wireless/intel/iwlegacy/common.h index 69687fcf963f..5682cccfa394 100644 --- a/drivers/net/wireless/intel/iwlegacy/common.h +++ b/drivers/net/wireless/intel/iwlegacy/common.h @@ -555,6 +555,7 @@ struct il_device_cmd { u32 val32; struct il_tx_cmd tx; u8 payload[DEF_CMD_PAYLOAD_SIZE]; + DECLARE_FLEX_ARRAY(u8, raw); } __packed cmd; } __packed;