From patchwork Sat May 18 08:30:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13667522 Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2057.outbound.protection.outlook.com [40.92.90.57]) (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 D98B79460; Sat, 18 May 2024 08:31:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021074; cv=fail; b=p4doMpGVneDwUeS1s6x+HJwHBfBZQLeN2pjFhR0Aci+kM+tCPaR/JiVcXrk+5Y3f85La9+kpjWfm16yF+c2nvq5bTJZUZWLIcXTaMU5ztGCOwztp5aPYD7CSoImIeHqz4/9DwLdDdOVmP3Iv4XxNpTqAdEOCLsvJ3PdCsO9/LnU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021074; c=relaxed/simple; bh=gbdPQpnZG0u34Q+Lu6u7oT0mVZR9ulv3pg3+qshARC0=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=Hghs2LgyBNPKjMxpSXa8JH5/+vwq5KPRPJ64KC/JLwcMSNyF75gTq4U3ywzmXrjeQnSk5bdylc9Ik7bNiS3zTYfwqIjkuTXB0OapBk0nHE6sOsyujQQQLZY+JsZfjV7/N3OdEShNdqI///pf45awnC+FNG5Ixp4LOsQhO24ASfU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=cJq2ASOM; arc=fail smtp.client-ip=40.92.90.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="cJq2ASOM" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IcgjSjwXlIWsjWAfN28F+HbedfXBJrFq/yQg+GNaIjiwxe3NF78BSBR8s9KOe1r7ugE+NQcDUVE7SxMF3TuB88xHUiMXul2wvWECBr0ZgvQfT/zkyDVCUDWL24RAmQUqArdgT2WG8WsRPl3SZFqjrTAkiGa6z3ahpD1vK68tXqdRGnJ4cf0XIgWbquzvFDC7xV8kdHdD3DdolVRXBnkOyAKAfuJMsJskANdg5mleJEX+V8+qhFDBaRFU6nD23KbA4Y/OEojaljYKS6UusiwW/gC4qxfgMQMX4isJwSGKDJ9pWx75AqO4zMWXGuCd4ilOgEMAzLnkplO/2QyliJZ2HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=AGQT2HI96Df6uYB6sJ24nvpYiLiwEkBs8CwOI9JeLVU=; b=O4saKOnmY/dpH+ScKuM4R7X/zc9glHfz/L92lyhqCdMNpw75oeTM/yR8pMx85p/m3wAq96PwSn1nxaupr/bcCMoN5XMhuIeiR3xbXICW7M6KHjDg3r8s9pweEyxQer6bU7dwjZq6AwnaqLANRIwYDEK2qHi9nY0mCfhQhNehpxB11abD5Z3r9dUjp+JjbwMG+uhHfsEnuFRezZKKFmF7BXId1ZV118ZtdwZCXIUXNgFmkwBr/WXWFGMhFDzj/7D+6DJNas77AWBP8LGalxSjr25OlJBWMdlZ+m/qr5cetuXnaCrmZifhUoSAaJtJJu9blo3CYdH6aGq16od1oOtyag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AGQT2HI96Df6uYB6sJ24nvpYiLiwEkBs8CwOI9JeLVU=; b=cJq2ASOMVzKh1AZf45EuyjI3MpBRtHLi8TfL1deNjK/3KWoq6Dxr3Q1P+dMB3OTOWN6oKNKJSeHElLdpRgdaUAeKh4wi9n82Y9kAA7m0lsDm5DoiZoFfMoNkEznh1wXXIyBWVkb35fhFq65BG2GdIDw56npt1oQUyn98/wumg2YphOYfVi8AKEp+h7ciPC7aE8bKIPr0hZqRbrmuJoDe+oHJ8NtwhcgEQMRgfWfUrjZH8fgx+dae1G1qKlsnGpffxtxZK269rWhdD2X+ccn28D5NGGJ4R2aYACwBYcSdlCLaSuRjJ97CL00FLfa8xpULElWsanBFNBnm6kbL/GciJA== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB8723.eurprd02.prod.outlook.com (2603:10a6:150:7c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.31; Sat, 18 May 2024 08:31:07 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7587.028; Sat, 18 May 2024 08:31:07 +0000 From: Erick Archer To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: Erick Archer , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 0/2] Bluetooth: hci_core: Refactor hci_get_dev_list() function Date: Sat, 18 May 2024 10:30:37 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 X-TMN: [nbTI7sV68c0uztVbCXbEzvnTt4GRqg0i] X-ClientProxiedBy: MA2P292CA0026.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::12) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240518083039.7202-1-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8PR02MB7237:EE_|GV2PR02MB8723:EE_ X-MS-Office365-Filtering-Correlation-Id: 0a04f747-5296-4efb-ecc8-08dc7714dce5 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1602099003|1710799017; X-Microsoft-Antispam-Message-Info: uw1ZdkKe9aLU8le5R+KuUW8zSL0V8MuvUYVe00LYQ3qKOz08bYaBzN8mYXc1F+maHOVZBu6+lWPaiZRdb8pf8O2+nth9JFAuFuUZkRO40JyBguTjeK1EIu+r7xx/cnITEtqBLSaPku/cCoNtGGoboFwabseNxOKiQL7RUSB0TOz6sd0eo+j6sqIrOApfwVB1sq7SqkH5TWWl+5fYRdVq1zSbppLVwp/YEEXr1bMf55yRyzQwthkEauL1AdnCsMHoNHNC7NzUnSAVUgvKnZYWynwrLNGzqe70f6SRS9sBNTWR7FZehnyVER33bR1Nw7Mj11u122chDRT0/TZlhiZDg0FehEE0khiNC4zloPr0IqEiHLzu0TtfBOsDP+0eBfkw8Ow3jHU8weWuR4Z9k+vqaot62zbh9R45KZXhoCaL44rgn+gA9z6dl5sP73SiRST6CNpi+k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E4uAZ5nV2+BjkkEI8Aw/aM2G+8ZCuv6ri8x5zqMU04XspyxLIjUzCHcnokm49mz+xpmE0YpbxFbnMPjUwhYLH5+iSe6Ux+c/JP1yqswwPLFU2AVd4f2oSeFo5BZzx405t15OOS/i5tgnLHKdrDpdr/XVYcmel1uVh9+C4HXIQ6mI5WkU+dfkKKwx8yyuKH3lUTOsjk9R4MCLZIdIbTJvmMAMAOTGq++nSj4Uscq8TiMlkDmWN5ugC83lmsPijwbTyvj70lq3/4/nttk4yS/T0m9zgZ5wIXpOVYc6EwwqMDF/t/PgE1euC5EguW7e3WiQg1a1Kl4dRvXlwHbcuG7DxnJijevDfi508UemRm0FyIeerfiEkIDn9WcSkjGzbuG8vta0vo2Of8wocXtJ6520P5YGSYEr8LvnA98vU2c3LWa+Ah1Gamg3nXSmx5ClEU9HHvONZJMaX+KuE2qoBGSvU4yfSTwF4M6W1tOxp8jbek7Idr52yaxJcYOHemb4yy96XCeOIzgSLNc7vfsomi X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0a04f747-5296-4efb-ecc8-08dc7714dce5 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 May 2024 08:31:07.1402 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR02MB8723 This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "dl" variable is a pointer to "struct hci_dev_list_req" and this structure ends in a flexible array: struct hci_dev_list_req { [...] struct hci_dev_req dev_req[]; /* hci_dev_req structures */ }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc() and copy_to_user() functions. At the same time, 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). In this case, it is important to note that the logic needs a little refactoring to ensure that the "dev_num" member is initialized before the first access to the flex array. Specifically, add the assignment before the list_for_each_entry() loop. Also remove the "size" variable as it is no longer needed and refactor the list_for_each_entry() loop to use dr[n] instead of (dr + n). This way, the code is more readable, idiomatic and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Specifically, the first patch is related to the struct_size() helper and the second patch refactors the list_for_each_entry() loop to use array indexing instead of pointer arithmetic. Regards, Erick Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] --- Changes in v2: - Add the "Reviewed-by:" tags. - Split the changes in two commits (Luiz Augusto von Dentz). Previous versions: v1 -> https://lore.kernel.org/linux-hardening/AS8PR02MB7237ECD397BDB7F529ADC7468BE12@AS8PR02MB7237.eurprd02.prod.outlook.com/ --- Erick Archer (2): Bluetooth: hci_core: Prefer struct_size over open coded arithmetic Bluetooth: hci_core: Prefer array indexing over pointer arithmetic include/net/bluetooth/hci_sock.h | 2 +- net/bluetooth/hci_core.c | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-)