From patchwork Thu Mar 15 02:31:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobin Harding X-Patchwork-Id: 10283841 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CA9A16061F for ; Thu, 15 Mar 2018 02:32:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD8442879C for ; Thu, 15 Mar 2018 02:32:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B1F70287AD; Thu, 15 Mar 2018 02:32:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 548C02879C for ; Thu, 15 Mar 2018 02:32:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751618AbeCOCbr (ORCPT ); Wed, 14 Mar 2018 22:31:47 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:40781 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751495AbeCOCbp (ORCPT ); Wed, 14 Mar 2018 22:31:45 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id AF51B20B77; Wed, 14 Mar 2018 22:31:44 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute5.internal (MEProxy); Wed, 14 Mar 2018 22:31:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tobin.cc; h=cc :date:from:message-id:subject:to:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=DSgqCwRLQt+PDfEuSGauGX9xbD6DIcA8kGGyZgFiu iI=; b=t7179Oil5OwANOpiv08FgwxJHcNofYUXlWnbkwUGNb0JtyiD3mxFS38kl /0LFYxXFkqDi8OflWwMWegZfSdYQIe4qXFowmYtXGckq6yydul84TmM0G/DxlKFd U5wYcjW/A70SlNX+idA4IIzukOP5jgmcNV6ljVWWwXKNkqk50TpFrYU+NbFuBP7a iFC1//kmV5Ovx905IFK8QckJ4YNU6ZJoZCC5yE4e09lgzSXqPgp/jjXTxq3sFz2I yHTYEw7w/Am6zbPiqbENq2gCcxe7t9/Q4vDnBrHf2wy9Y/uFVIuM37ryg9XQLQNi oqwQ5oA3xfjFDBDLrWYHgDGLW1P3Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=DSgqCwRLQt+PDfEuS GauGX9xbD6DIcA8kGGyZgFiuiI=; b=BnM0LZWbiDtfNskxpvFPy4jHGmw896RP3 WaIAQNOZxHscL0K6j2MmLyECXcZkNsxttM3BBijF/864wuV08/JPvB2ELP4Ha4f+ G6VNKHN2u8aabfMCinylKgHL1g4MaPd/Brf2Vn88R0392C0DzjnJ3DqcT5ZpQRdL xm1/sZaUvkF8kgw9VarBk5K28hFF9RbIRAHBPx7AUXg0HpmoIqyXy8RYrwfF3cLz dOmTtjx48VZejpvwHTVGV+VDEQP4eqKy3sodI3OFPkFZ+JXX9Z+QLAcvqBZmPlVb KebOLY62vcrZiMLTo/dp1pOxiP406ExsTTs6VP8MkNRvE/lzuxq+g== X-ME-Sender: Received: from localhost (124-170-217-156.dyn.iinet.net.au [124.170.217.156]) by mail.messagingengine.com (Postfix) with ESMTPA id EFBF17E12E; Wed, 14 Mar 2018 22:31:43 -0400 (EDT) From: "Tobin C. Harding" To: Kalle Valo Cc: "Tobin C. Harding" , kernel-hardening@lists.openwall.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-wireless@vger.kernel.org, Tycho Andersen , Kees Cook , Larry Finger Subject: [PATCH v2] rsi: Remove stack VLA usage Date: Thu, 15 Mar 2018 13:31:25 +1100 Message-Id: <1521081085-16404-1-git-send-email-me@tobin.cc> X-Mailer: git-send-email 2.7.4 Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The use of stack Variable Length Arrays needs to be avoided, as they can be a vector for stack exhaustion, which can be both a runtime bug (kernel Oops) or a security flaw (overwriting memory beyond the stack). Also, in general, as code evolves it is easy to lose track of how big a VLA can get. Thus, we can end up having runtime failures that are hard to debug. As part of the directive[1] to remove all VLAs from the kernel, and build with -Wvla. Currently rsi code uses a VLA based on a function argument to `rsi_sdio_load_data_master_write()`. The function call chain is Both these functions rsi_sdio_reinit_device() rsi_probe() start the call chain: rsi_hal_device_init() rsi_load_fw() auto_fw_upgrade() ping_pong_write() rsi_sdio_load_data_master_write() [Without familiarity with the code] it appears that none of the 4 locks mutex rx_mutex tx_mutex tx_bus_mutex are held when `rsi_sdio_load_data_master_write()` is called. It is therefore safe to use kmalloc with GFP_KERNEL. We can avoid using the VLA by using `kmalloc()` and free'ing the memory on all exit paths. Change buffer from 'u8 array' to 'u8 *'. Call `kmalloc()` to allocate memory for the buffer. Using goto statement to call `kfree()` on all return paths. It can be expected that this patch will result in a small increase in overhead due to the use of `kmalloc()` however this code is only called on initialization (and re-initialization) so this overhead should not degrade performance. [1] https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Tobin C. Harding --- This applies onto tip of wireless-drivers next, commit (28bf8312a983 mwifiex: get_channel from firmware v2: - Use kmalloc instead of #define (suggested by Larry) - (Apply on top of wireless-drivers-next tree) - (Fix up user name on patchwork.kernel.org) drivers/net/wireless/rsi/rsi_91x_sdio.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/net/wireless/rsi/rsi_91x_sdio.c b/drivers/net/wireless/rsi/rsi_91x_sdio.c index 98c7d1dae18e..13705fca59dd 100644 --- a/drivers/net/wireless/rsi/rsi_91x_sdio.c +++ b/drivers/net/wireless/rsi/rsi_91x_sdio.c @@ -576,7 +576,7 @@ static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter, { u32 num_blocks, offset, i; u16 msb_address, lsb_address; - u8 temp_buf[block_size]; + u8 *temp_buf; int status; num_blocks = instructions_sz / block_size; @@ -585,11 +585,15 @@ static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter, rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n", instructions_sz, num_blocks); + temp_buf = kmalloc(block_size, GFP_KERNEL); + if (!temp_buf) + return -ENOMEM; + /* Loading DM ms word in the sdio slave */ status = rsi_sdio_master_access_msword(adapter, msb_address); if (status < 0) { rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__); - return status; + goto out_free; } for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) { @@ -601,7 +605,7 @@ static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter, temp_buf, block_size); if (status < 0) { rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__); - return status; + goto out_free; } rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i); base_address += block_size; @@ -616,7 +620,7 @@ static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter, rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__); - return status; + goto out_free; } } } @@ -632,12 +636,16 @@ static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter, temp_buf, instructions_sz % block_size); if (status < 0) - return status; + goto out_free; rsi_dbg(INFO_ZONE, "Written Last Block in Address 0x%x Successfully\n", offset | RSI_SD_REQUEST_MASTER); } - return 0; + + status = 0; +out_free: + kfree(temp_buf); + return status; } #define FLASH_SIZE_ADDR 0x04000016