From patchwork Sun Jan 29 18:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 50032 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1848301wrn; Sun, 29 Jan 2023 10:44:20 -0800 (PST) X-Google-Smtp-Source: AK7set9Rbohga+G7b+7FQi0BQ+YHHJz1ylAFthrVMtz0U3fYuCLiiH8QowkYvperjnd02uy3vXfC X-Received: by 2002:a17:902:e546:b0:196:4814:2a2d with SMTP id n6-20020a170902e54600b0019648142a2dmr5717601plf.39.1675017859747; Sun, 29 Jan 2023 10:44:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675017859; cv=none; d=google.com; s=arc-20160816; b=VVGyEkMNzUTDTNYIPLhYd2UXZuafz+J736BPyzunosAhfvrqc3/vgnQlu47UQn7D3I MvP57afxND546y/Az3+tz5eAIpYP9sbAhUFrMVyOOpiqW0ixcsjgVESdMcpiRyep/EPW suYhVVBMcULTmCeyZPf3Ui60cSLN2KHoZX8T9wlpQBOg1cx5KGWRouxsQCGMBp9enOAQ xUTDpAYGJjdo6gbCwTp3l5xzaVOZUcQMcWJshHIwxzYYk39dvPUezNvjmf799tbT4Epn ZakHZrYrevJtu6Vl9sZx/xazUfSdhKFjjYUlnG8bwv+5r2hQwCqIK7fElXaaEFCfJVwU LISg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6YbFBpcBq30kgPaFjgWen5xBakteS5ohPQw3PDSNSpA=; b=Z/CS5mbhC64OUlSzeH/kB6mSURcQVD4ZParRMppka56wRsI5yG1BdpHXK+wR4P6uL6 DYaU5sH48aG6nzTvHTVdr0Og9ouHwqcElMoZQeqg4GggMrbDXdnyQkNeiPaqpnunIJG/ U7D+lS1gnoHrYss68Czgcl/b+FsQ3OTv0XRvj8IJ7YXW78zsexNlQtJuUg6tXia0FUuu Pbt80LLexSNE+Csm1YuBRjzXyg8ul/tCr/hL9NGNtrT6ooYLF7bFzbAupNbtUlqZJjJn GE52gyPElQBZ0rr56Mt4WfawFivzTmz69lEy2ttO02q94KPxmW0gBBwWJmlogTHvSKVr yZww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c1-20020a170902f30100b00193f8c6a020si10126264ple.111.2023.01.29.10.44.07; Sun, 29 Jan 2023 10:44:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235277AbjA2SXa (ORCPT + 99 others); Sun, 29 Jan 2023 13:23:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235154AbjA2SX2 (ORCPT ); Sun, 29 Jan 2023 13:23:28 -0500 Received: from smtp.smtpout.orange.fr (smtp-12.smtpout.orange.fr [80.12.242.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2141F1BAEC for ; Sun, 29 Jan 2023 10:23:27 -0800 (PST) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id MCKlpGwDQMaRbMCKmpnji1; Sun, 29 Jan 2023 19:23:25 +0100 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 29 Jan 2023 19:23:25 +0100 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: gregkh@linuxfoundation.org, peterz@infradead.org, pmladek@suse.com, john.ogness@linutronix.de, baolu.lu@linux.intel.com, tglx@linutronix.de, mingo@kernel.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET Subject: [PATCH 1/3] usb: early: xhci-dbc: Fix a potential out-of-bound memory access Date: Sun, 29 Jan 2023 19:23:08 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756383527595496413?= X-GMAIL-MSGID: =?utf-8?q?1756383527595496413?= If xdbc_bulk_write() fails, the values in 'buf' can be anything. So the string is not guaranteed to be NULL terminated when xdbc_trace() is called. Reserve an extra byte, which will be zeroed automatically because 'buf' is a static variable, in order to avoid troubles, should it happen. Fixes: aeb9dd1de98c ("usb/early: Add driver for xhci debug capability") Signed-off-by: Christophe JAILLET --- drivers/usb/early/xhci-dbc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c index 797047154820..f3e23be227d4 100644 --- a/drivers/usb/early/xhci-dbc.c +++ b/drivers/usb/early/xhci-dbc.c @@ -874,7 +874,8 @@ static int xdbc_bulk_write(const char *bytes, int size) static void early_xdbc_write(struct console *con, const char *str, u32 n) { - static char buf[XDBC_MAX_PACKET]; + /* static variables are zeroed, so buf is always NULL terminated */ + static char buf[XDBC_MAX_PACKET + 1]; int chunk, ret; int use_cr = 0; From patchwork Sun Jan 29 18:23:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 50030 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1845533wrn; Sun, 29 Jan 2023 10:34:14 -0800 (PST) X-Google-Smtp-Source: AK7set89i2wF0FueLY+hJKxZmrqdggW+ShJ0iaNf2icDTcsM8pfYP6F4Mg0SUgO/Dom+lqFPtqBV X-Received: by 2002:a17:907:a42a:b0:84d:4be4:aa2b with SMTP id sg42-20020a170907a42a00b0084d4be4aa2bmr8934103ejc.68.1675017254855; Sun, 29 Jan 2023 10:34:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675017254; cv=none; d=google.com; s=arc-20160816; b=0GWSlD7cqz0gKUXm2JmC/ke8sgqszsD57aHvmi6Puvo08jqIdNuhZ9qisI4nkG4XTl Qev5+S9BN5O16+fQFL61FXdDNrqZvHFiUsq0W9gPRvD1ZO80cYqLuWcV2y3JVGaX2CpQ 7Wzv6rXZ5Y9+TWUc0Cc5CviLYNu+eYCRjN3daWwLYdV5cunfr8ZmR9FspHVMcFF1yxAV B+Iu1A2ol3GLpvizlF2nG8xHunCi7BiJ3LIscb+XsMh8vb8oujKacCjjxE642p2gMlQ+ Brkpy9pGcSwAD2Zsr5zK5d4Ru5cr6TgKzcPY7hiPpUQo6/5Or6qvDWPO4waBxydx4TnU dEpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1ot/5SY2NBRh52AhPTRcrRcwQg7n9kYBJBx6IGGBT38=; b=Ki/mhm6VLyhvsqeXLvOX30Z+7GV1wvpxGEMzDlC3mcGfqPd5KJIach1y4M/rxvHRjj t6YoUK4ShV9T+jhU/QFzoJ6CW9uJxuRKMPAWk1PmTSsTFL3OoPds20wsD5s7xBlHtyc0 pPex2GPgcawBV6FFsJtX8xuFBMlum1P0ZPxW20CHvFUrxkx4PuP6y+Wmay6aphwb5a0A ltcq9fMVNSRZXE82R9MpG0Xy9Fd+Q3ei8eqb0Au0dFkEfPtK3CIL+Y2Ce9hlXGfnOVYn z25rjvVqUcSMYlFcwaokTU2HImQpHCaDrbxdJvCinqHCzt6OcUb0yeIU2yAl1CcC9J6P aTcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 26-20020a170906101a00b008818fe220c1si7671611ejm.80.2023.01.29.10.33.50; Sun, 29 Jan 2023 10:34:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235286AbjA2SXn (ORCPT + 99 others); Sun, 29 Jan 2023 13:23:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235287AbjA2SXk (ORCPT ); Sun, 29 Jan 2023 13:23:40 -0500 Received: from smtp.smtpout.orange.fr (smtp-12.smtpout.orange.fr [80.12.242.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7021B1C311 for ; Sun, 29 Jan 2023 10:23:37 -0800 (PST) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id MCKlpGwDQMaRbMCL1pnjjv; Sun, 29 Jan 2023 19:23:35 +0100 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 29 Jan 2023 19:23:35 +0100 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: gregkh@linuxfoundation.org, peterz@infradead.org, pmladek@suse.com, john.ogness@linutronix.de, baolu.lu@linux.intel.com, tglx@linutronix.de, mingo@kernel.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET Subject: [PATCH 2/3] usb: early: xhci-dbc: Optimize early_xdbc_write() Date: Sun, 29 Jan 2023 19:23:09 +0100 Message-Id: <687bbcd940c59fbddd0e3a8b578fd3422962e50f.1675016180.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756382892654211681?= X-GMAIL-MSGID: =?utf-8?q?1756382892654211681?= There is no point in zeroing 'buf'. It would be cleared only once, and if the 'while' loop is executed several times, all but the first run would have a 'dirty' buffer. Moreover, the size of the chunk is computed in the loop and this size is passed to xdbc_bulk_write(). So remove this useless memset(). Signed-off-by: Christophe JAILLET --- Untested. --- drivers/usb/early/xhci-dbc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c index f3e23be227d4..965a24e47c0f 100644 --- a/drivers/usb/early/xhci-dbc.c +++ b/drivers/usb/early/xhci-dbc.c @@ -881,7 +881,7 @@ static void early_xdbc_write(struct console *con, const char *str, u32 n) if (!xdbc.xdbc_reg) return; - memset(buf, 0, XDBC_MAX_PACKET); + while (n > 0) { for (chunk = 0; chunk < XDBC_MAX_PACKET && n > 0; str++, chunk++, n--) { From patchwork Sun Jan 29 18:23:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 50033 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1848993wrn; Sun, 29 Jan 2023 10:47:13 -0800 (PST) X-Google-Smtp-Source: AK7set8hyDr2qZy4HH3g7qxPtcmFvrQfB4+u8FKpFQU4Lif5EXb3+9tsitSCKMzgSoUkC6Bkc672 X-Received: by 2002:a05:6a21:9993:b0:bc:cd2c:dfce with SMTP id ve19-20020a056a21999300b000bccd2cdfcemr4053168pzb.31.1675018033446; Sun, 29 Jan 2023 10:47:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675018033; cv=none; d=google.com; s=arc-20160816; b=zDhe/0WHIituZNzSWjB+7m7ctnoIWGBgEZCJECX8dkDhCYpSu7pIEGzapHOuIsPvDJ uaW9I4Q7UKexAFW8x+jCWnjRllxx89p5hIG9ZJqXJiP2bfcCnhY/BX+gANnBofdK+KrU 2wEuwyBOGydF8p8FfrFJ/3jqGi/Vlx6NI0PweCdAsSKnTR0+47OI4fEPap8pqb4p3TSx ikVdLvkGttrVlj1pOqE0qoZtFtj7iIONeb97iLa1p5UdOgprulWhNa67qS9cMghWacQs lPc702ai+u9Xb2aEvzuj6UK2yfZP3VuvxJZA5mXrsZEBRUCQ921kcYBnxr3CVw+V8Dp/ nlgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=sqe2krDouTgNp0WG3ZUem2JqbK1Ia+qR60WkfRXDAdU=; b=JnhMvFygYUuR2bKMXRgUooN5p+ToKCQbm785MNWOGRF4vAkzjSOzof90OLj+tCnyqX A53ZPKZlCARBaG80bMpK2TymlKJ/o2vSkCm2CDr8M2NV4YzQMneqLMEP/TUHKYfWYp0q cQUQBovRU7FDykpAOLYQdRgiWldC2RXYhk+VscYUBNtdjulo+5gxv0BbvCRuExHYCN6/ oSr2SNYrZJ08Yx01BI0P83h1WFXtbelnk7kjnLfAvgFBeYSDpen8NbEH6j1VKVvyQRJC qbEy/HghuAU8ixGSCsQ8glfnLh7yxHZXPTyyHmFMWX/7AEAP7QCuVsP2Ea+vyJUAEog3 zHow== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k12-20020a633d0c000000b004ac9348e706si10650061pga.113.2023.01.29.10.47.01; Sun, 29 Jan 2023 10:47:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235305AbjA2SXq (ORCPT + 99 others); Sun, 29 Jan 2023 13:23:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235290AbjA2SXo (ORCPT ); Sun, 29 Jan 2023 13:23:44 -0500 Received: from smtp.smtpout.orange.fr (smtp-11.smtpout.orange.fr [80.12.242.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2319F1BAF7 for ; Sun, 29 Jan 2023 10:23:40 -0800 (PST) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id MCKlpGwDQMaRbMCL4pnjkG; Sun, 29 Jan 2023 19:23:38 +0100 X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 29 Jan 2023 19:23:38 +0100 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: gregkh@linuxfoundation.org, peterz@infradead.org, pmladek@suse.com, john.ogness@linutronix.de, baolu.lu@linux.intel.com, tglx@linutronix.de, mingo@kernel.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET Subject: [PATCH 3/3] usb: early: xhci-dbc: Use memcpy_and_pad() Date: Sun, 29 Jan 2023 19:23:10 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756383709172256391?= X-GMAIL-MSGID: =?utf-8?q?1756383709172256391?= Instead of zeroing some memory and then copying data in part or all of it, use memcpy_and_pad(). This avoids writing some memory twice and should save a few cycles. Signed-off-by: Christophe JAILLET --- drivers/usb/early/xhci-dbc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/usb/early/xhci-dbc.c b/drivers/usb/early/xhci-dbc.c index 965a24e47c0f..341408410ed9 100644 --- a/drivers/usb/early/xhci-dbc.c +++ b/drivers/usb/early/xhci-dbc.c @@ -499,8 +499,7 @@ static int xdbc_bulk_transfer(void *data, int size, bool read) addr = xdbc.in_dma; xdbc.flags |= XDBC_FLAGS_IN_PROCESS; } else { - memset(xdbc.out_buf, 0, XDBC_MAX_PACKET); - memcpy(xdbc.out_buf, data, size); + memcpy_and_pad(xdbc.out_buf, XDBC_MAX_PACKET, data, size, 0); addr = xdbc.out_dma; xdbc.flags |= XDBC_FLAGS_OUT_PROCESS; }