Received: by 2002:ab2:1c04:0:b0:1f7:53ba:1ebe with SMTP id f4csp98939lqg; Fri, 26 Apr 2024 09:45:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUw5KNf6c6uoNS8nvIqWTy28cndKjzB/oGOj2a1cFD7sZx68fNSHhNkAhd3Ql4ienI0ikxaxpc44jPl9hapKcPRmgojdokuxQugEVkc4g== X-Google-Smtp-Source: AGHT+IHj1Ct8jDfIWbErfwybf9yI2WLSKnDsnLrzl4P/rTO+WBrDi1NFjT/zVsDCmUaLPCBGI2Gi X-Received: by 2002:a17:90a:b8f:b0:2a2:e56a:ca8f with SMTP id 15-20020a17090a0b8f00b002a2e56aca8fmr3618792pjr.42.1714149930600; Fri, 26 Apr 2024 09:45:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714149930; cv=pass; d=google.com; s=arc-20160816; b=gO+Gj2t8vJfKo3PV1St0TlAO1a3t7sbfkO9LV/bfUBNcnZLZfVki3243uiwidO5vRl RBUOveczZ1nP7g1eidXkUE+QzsvIlujAPAqSKhOEzx2TuvIJg/yplYjyGjGyVztUSJd8 1eAr1zrde8+26mfYn3RTh8YZRltup/hH9jDbnAaA4T/0fHs8Gla4bVYDIBvXIw+zxKju ci5pFh+0ursNEUnJK2ZgzZJrBjQl0pdgV0pAsdBZQiEuU1LggTjosZ60Yfm6AtzTfmXj Iw0mTM7qu+as7MvHBYYaRSE+rn/1kqosypI0coN3U1XeIEFwjDtF5WYfeDJ/zRA3/u9R g/uw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=kw9QLqD6nB7hprQJkCu7PMWTPHcWaojrDOA/ksIoMTA=; fh=X6kffvUcJ/nhnwYrA/XNypDu+0IBsQcSzNR2Bu+p+zc=; b=ZklTHzjrRYJ0LlAAh2rPZYZ2M25HDpq0EkF7h6KqEiCotLovydWUZ3uJeiYf3/8VcB EsICNzV4CRLGUzeRSZaxWGEFya25iDRWADLcbh0aEOskj/cE/I+JaBkiY2Ie3OWxfN8G IyPA6oO/VyZwI68RoJ/p1AjdeY4jH9+0F5LBBs/6v/zFmtE/HSvho/Nwo4tKdmV/W5ds oCMSiiQUK9aLBWM0t3LPJI1QJnL/Ze6f9bv6AccKRoFQh7+glTHLQ723Ai8pccQ0cwc3 ICwslHERkXSKz+4clSvDYelB1PoEmaEBGU4SyDzz71zZJdPhgQLtn+MHJyrVP31n5i/k 50Iw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FT0QMFye; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-bluetooth+bounces-4122-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4122-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id o21-20020a17090ab89500b002a52e9db5bdsi15448454pjr.38.2024.04.26.09.45.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 09:45:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-4122-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FT0QMFye; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-bluetooth+bounces-4122-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4122-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 3C3F7286DB9 for ; Fri, 26 Apr 2024 16:45:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0ADC31581FE; Fri, 26 Apr 2024 16:45:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FT0QMFye" X-Original-To: linux-bluetooth@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 58C0C2B9AF; Fri, 26 Apr 2024 16:45:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714149921; cv=none; b=C4k2j9xUPAOr4NhhyCaQA+HfyPp8GM8sS3KaR9LLV7TXs5FshKMNm2+VDB1Iu+PTA6fSIhnXf87dlt/jmYB3kcKG5ibow1pyoJRcHdTojwSHJ1RTp872Ly+XatD2jsoa99uWiS9ENEcr8qZMSsrvtCBsrwPsx6raCl2Onqylhqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714149921; c=relaxed/simple; bh=gPXudTVXPzAYk/IBuGVv5BUulrSGwKA4k/IvdqMV+jI=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=XcIns9jGwnwj++uSlzgq1tb3Cb7ZreTylcm/7c3a395M8KJOpOAdzNEhxUl4LIyOXBMwik5xsafh/pJvQ+DzHHhmfMJDmLe+x1FPZTvPPFrxaE0BJlRwYkt4XY5TvfZ5TfzZDY5piUsh5HO2vFotsTKuy7hvknUbqlJAvp/bMs4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FT0QMFye; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D726AC113CD; Fri, 26 Apr 2024 16:45:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714149921; bh=gPXudTVXPzAYk/IBuGVv5BUulrSGwKA4k/IvdqMV+jI=; h=Date:From:To:Cc:Subject:From; b=FT0QMFyeSVc2Lx2CmRk7crSVNfJaB0pkKTvk5aoG+2MGabFQBQ5HqlIp6LG8pX/pV NmgQiygFd+jDSleYF6/O1OL+bfbUBxu3DsNubI1LEN5RaN/5u/KtTxMhosLM/VzxJP nxsk+KBjNop6Zenc6VqkwdEeZ3Wt8C80RH1l/ycne0OHmUMxy74B0MEJwzxPKMTfhk bdfZvWNdzoseDHsOAtrYU6v2VFZLTXV4ag9Z+Xgtol26uVtpTUI5IteVk5z3uhntaj 7s6C4QJ8HedVi03aEmUPc9oNTUGeGx8OU1HnsionPFDfnJKqPiUF5bdRCje2+yfm77 KtuxRii81zOtQ== Date: Fri, 26 Apr 2024 10:45:17 -0600 From: "Gustavo A. R. Silva" To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org Subject: [PATCH v2][next] Bluetooth: hci_conn: Use __counted_by() in struct hci_cp_le_big_create_sync and avoid -Wfamnae warning Message-ID: Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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). Also, -Wflex-array-member-not-at-end is coming in GCC-14, and we are getting ready to enable it globally. So, use the `DEFINE_FLEX()` helper for an on-stack definition of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly. With these changes, fix the following warning: net/bluetooth/hci_conn.c:2116:50: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] Link: https://github.com/KSPP/linux/issues/202 Signed-off-by: Gustavo A. R. Silva --- Changes in v2: - Use `sizeof(*pdu) + num_bis` instead of `__struct_size(pdu)` v1: - Link: https://lore.kernel.org/linux-hardening/ZivYcpzZsc7aIFZ5@neat/ include/net/bluetooth/hci.h | 2 +- net/bluetooth/hci_conn.c | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index 5c12761cbc0e..fe23e862921d 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -2216,7 +2216,7 @@ struct hci_cp_le_big_create_sync { __u8 mse; __le16 timeout; __u8 num_bis; - __u8 bis[]; + __u8 bis[] __counted_by(num_bis); } __packed; #define HCI_OP_LE_BIG_TERM_SYNC 0x206c diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 81166a5bc034..d6daf55f0307 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -2112,13 +2112,10 @@ int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon, struct bt_iso_qos *qos, __u16 sync_handle, __u8 num_bis, __u8 bis[]) { - struct _packed { - struct hci_cp_le_big_create_sync cp; - __u8 bis[0x11]; - } pdu; + DEFINE_FLEX(struct hci_cp_le_big_create_sync, pdu, bis, num_bis, 0x11); int err; - if (num_bis < 0x01 || num_bis > sizeof(pdu.bis)) + if (num_bis < 0x01 || num_bis > pdu->num_bis) return -EINVAL; err = qos_set_big(hdev, qos); @@ -2128,18 +2125,17 @@ int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon, if (hcon) hcon->iso_qos.bcast.big = qos->bcast.big; - memset(&pdu, 0, sizeof(pdu)); - pdu.cp.handle = qos->bcast.big; - pdu.cp.sync_handle = cpu_to_le16(sync_handle); - pdu.cp.encryption = qos->bcast.encryption; - memcpy(pdu.cp.bcode, qos->bcast.bcode, sizeof(pdu.cp.bcode)); - pdu.cp.mse = qos->bcast.mse; - pdu.cp.timeout = cpu_to_le16(qos->bcast.timeout); - pdu.cp.num_bis = num_bis; - memcpy(pdu.bis, bis, num_bis); + pdu->handle = qos->bcast.big; + pdu->sync_handle = cpu_to_le16(sync_handle); + pdu->encryption = qos->bcast.encryption; + memcpy(pdu->bcode, qos->bcast.bcode, sizeof(pdu->bcode)); + pdu->mse = qos->bcast.mse; + pdu->timeout = cpu_to_le16(qos->bcast.timeout); + pdu->num_bis = num_bis; + memcpy(pdu->bis, bis, num_bis); return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC, - sizeof(pdu.cp) + num_bis, &pdu); + sizeof(*pdu) + num_bis, pdu); } static void create_big_complete(struct hci_dev *hdev, void *data, int err) -- 2.34.1