[next] xen: privcmd: Replace zero-length array with flex-array member and use __counted_by
Message ID | ZVZlg3tPMPCRdteh@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp44954vqn; Thu, 16 Nov 2023 10:56:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IESfzTzByO1A90aiFIvFNfe2p0zEXDAabk9kASk1Go9Ri1iwfCoiFM4VYgG/T7bRBIwPdjJ X-Received: by 2002:a05:6a21:9992:b0:186:bd68:fa74 with SMTP id ve18-20020a056a21999200b00186bd68fa74mr12582379pzb.22.1700160997843; Thu, 16 Nov 2023 10:56:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700160997; cv=none; d=google.com; s=arc-20160816; b=0YDS8bw6E1n7hliC+baEWgaogDJ5DXgZDUV+kyWKf4CWa4q5yHRHkfX/L57Hxni62+ 3Osu/eNZEzp2S+H6ZAVtpTxi7z+dAyASj7D6ffTWZhDAXPa02b2lJVr/jrnHfYeJsAJV ezJND7DLiKVlERySkqEcEfFtDHqI9Brtmc9aJyv4YKoH0qNiu/5Zb5s3JlbERz81koAX N6ngNZaYsp5JYqJKLEOHFDyhOYyVae6GbndYuFpKNrwH/PPHRyOHBI97bKeSrvocDVgm 3DbAZMdqSZFUh94HMJ16EQ3QrmoLQrQ/Th9I/K7ZNl39SEx6npuR93/wmU2kljyZoYco QC2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=OCPJFEqpeLxujmn8gJLKB3zbLNlfUGdZvBYYuSiWDJ8=; fh=grvoduVzbCjvQHNuY8TNqZyALqbAi7v/eKkAflEVkYI=; b=WW2zHrVB6bjXlbrkU4cG9iiwyH/2Wt2lUKKGJQT2cM/Aju0/Jn+TjND5Qx9Um5Yfsl 9VK7NlMi8d8zNBiKdLj+J3tXnQXwtxMZF/hAQeZyUbtXVVbf5Oz3LRSX5PVIOUMBpe4a T2I96aoEYzTsSity1gPdUvcRxhVVOKGRiMsJUddzvQHNPXsPwCm9AHtBQOIcerAH0Ak1 VVWo910eSus2eqWwB7vst1JsycxWqPDMQWHDYbpCyOLPe+oBqk6+279YsOXREMIsMBMe NDRGfmLJEwMciXjLep1qFCp5aILPUi9WFyr7q2/N4zQAt0KveR2tmeWIfDZ/PpxBtXcd Pfag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q2KeOstZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id bn6-20020a056a02030600b00578b8d202b0si60548pgb.536.2023.11.16.10.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 10:56:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q2KeOstZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id DBDF781DD7C1; Thu, 16 Nov 2023 10:56:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229534AbjKPSzH (ORCPT <rfc822;jaysivo@gmail.com> + 30 others); Thu, 16 Nov 2023 13:55:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjKPSzG (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 16 Nov 2023 13:55:06 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A7FC1A8 for <linux-kernel@vger.kernel.org>; Thu, 16 Nov 2023 10:55:03 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BBB8C433C8; Thu, 16 Nov 2023 18:55:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700160902; bh=JwQgKXdyVFHDeYy2JqIrcrbfOB3OycZsBTQgBU7qjPM=; h=Date:From:To:Cc:Subject:From; b=Q2KeOstZqpSdN4JhKTSNmJecY3BmKrmxV0TKxlDYtF5gv7UBoA9ki8l+3oU7WT0Hx SGmSc3VfEncIDsy7g1gUdu/dUurdmxWoNKqVqXpNG0JIQn/9CRKtLj4EZby3gR0ebM W29DzH+9f5rXc4ky/j82PuHjBZxanaXp2OXpLgfdOeYwEyc8H6InT7BIuTjYwI2twZ K2Q59QirBuKsjjD1uHuCUq6Vt0CT0Ut1+Mc3R70pFEkwWmeLddoRJdAqfYUx+Un6j7 onTVWGPMm6W16RlorLGNo7T/80qIpz+vuwThsSHkscF6lBwIA6x+UPnUOlCQ+Gw8al 9yTiFS6TTIGSg== Date: Thu, 16 Nov 2023 12:54:59 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Juergen Gross <jgross@suse.com>, Stefano Stabellini <sstabellini@kernel.org>, Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> Cc: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] xen: privcmd: Replace zero-length array with flex-array member and use __counted_by Message-ID: <ZVZlg3tPMPCRdteh@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 16 Nov 2023 10:56:36 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782748018210975005 X-GMAIL-MSGID: 1782748018210975005 |
Series |
[next] xen: privcmd: Replace zero-length array with flex-array member and use __counted_by
|
|
Commit Message
Gustavo A. R. Silva
Nov. 16, 2023, 6:54 p.m. UTC
Fake flexible arrays (zero-length and one-element arrays) are deprecated,
and should be replaced by flexible-array members. So, replace
zero-length array with a flexible-array member in `struct
privcmd_kernel_ioreq`.
Also annotate array `ports` with `__counted_by()` to prepare for the
coming implementation by GCC and Clang of the `__counted_by` attribute.
Flexible array members annotated with `__counted_by` can have their
accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array
indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions).
This fixes multiple -Warray-bounds warnings:
drivers/xen/privcmd.c:1239:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
drivers/xen/privcmd.c:1240:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
drivers/xen/privcmd.c:1241:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
drivers/xen/privcmd.c:1245:33: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
drivers/xen/privcmd.c:1258:67: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=]
This results in no differences in binary output.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
drivers/xen/privcmd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Thu, Nov 16, 2023 at 12:54:59PM -0600, Gustavo A. R. Silva wrote: > Fake flexible arrays (zero-length and one-element arrays) are deprecated, > and should be replaced by flexible-array members. So, replace > zero-length array with a flexible-array member in `struct > privcmd_kernel_ioreq`. > > Also annotate array `ports` with `__counted_by()` to prepare for the > coming implementation by GCC and Clang of the `__counted_by` attribute. > Flexible array members annotated with `__counted_by` can have their > accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array > indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions). > > This fixes multiple -Warray-bounds warnings: > drivers/xen/privcmd.c:1239:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1240:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1241:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1245:33: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1258:67: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > > This results in no differences in binary output. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Looks right to me. I can see the allocation: size = struct_size(kioreq, ports, ioeventfd->vcpus); kioreq = kzalloc(size, GFP_KERNEL); if (!kioreq) return ERR_PTR(-ENOMEM); kioreq->dom = ioeventfd->dom; kioreq->vcpus = ioeventfd->vcpus; Reviewed-by: Kees Cook <keescook@chromium.org>
On 11/16/23 15:08, Kees Cook wrote: > On Thu, Nov 16, 2023 at 12:54:59PM -0600, Gustavo A. R. Silva wrote: >> Fake flexible arrays (zero-length and one-element arrays) are deprecated, >> and should be replaced by flexible-array members. So, replace >> zero-length array with a flexible-array member in `struct >> privcmd_kernel_ioreq`. >> >> Also annotate array `ports` with `__counted_by()` to prepare for the >> coming implementation by GCC and Clang of the `__counted_by` attribute. >> Flexible array members annotated with `__counted_by` can have their >> accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array >> indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions). >> >> This fixes multiple -Warray-bounds warnings: >> drivers/xen/privcmd.c:1239:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] >> drivers/xen/privcmd.c:1240:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] >> drivers/xen/privcmd.c:1241:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] >> drivers/xen/privcmd.c:1245:33: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] >> drivers/xen/privcmd.c:1258:67: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] >> >> This results in no differences in binary output. >> >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > > Looks right to me. I can see the allocation: Yep, I always check for that; in particular, the 'counter' assignment. :) Do you want me to mention it in the changelog text? > > size = struct_size(kioreq, ports, ioeventfd->vcpus); > kioreq = kzalloc(size, GFP_KERNEL); > if (!kioreq) > return ERR_PTR(-ENOMEM); > > kioreq->dom = ioeventfd->dom; > kioreq->vcpus = ioeventfd->vcpus; > > > Reviewed-by: Kees Cook <keescook@chromium.org> Thanks! -- Gustavo
On 16.11.23 19:54, Gustavo A. R. Silva wrote: > Fake flexible arrays (zero-length and one-element arrays) are deprecated, > and should be replaced by flexible-array members. So, replace > zero-length array with a flexible-array member in `struct > privcmd_kernel_ioreq`. > > Also annotate array `ports` with `__counted_by()` to prepare for the > coming implementation by GCC and Clang of the `__counted_by` attribute. > Flexible array members annotated with `__counted_by` can have their > accesses bounds-checked at run-time via `CONFIG_UBSAN_BOUNDS` (for array > indexing) and `CONFIG_FORTIFY_SOURCE` (for strcpy/memcpy-family functions). > > This fixes multiple -Warray-bounds warnings: > drivers/xen/privcmd.c:1239:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1240:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1241:30: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1245:33: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > drivers/xen/privcmd.c:1258:67: warning: array subscript i is outside array bounds of 'struct ioreq_port[0]' [-Warray-bounds=] > > This results in no differences in binary output. > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Reviewed-by: Juergen Gross <jgross@suse.com> Juergen
diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c index 1ce7f3c7a950..0eb337a8ec0f 100644 --- a/drivers/xen/privcmd.c +++ b/drivers/xen/privcmd.c @@ -1115,7 +1115,7 @@ struct privcmd_kernel_ioreq { spinlock_t lock; /* Protects ioeventfds list */ struct list_head ioeventfds; struct list_head list; - struct ioreq_port ports[0]; + struct ioreq_port ports[] __counted_by(vcpus); }; static irqreturn_t ioeventfd_interrupt(int irq, void *dev_id)