From patchwork Sun Dec 27 10:17:06 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Boichat X-Patchwork-Id: 7923161 Return-Path: X-Original-To: patchwork-linux-mediatek@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 54C0EBEEE5 for ; Sun, 27 Dec 2015 10:17:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4C89820263 for ; Sun, 27 Dec 2015 10:17:45 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 696AA2025A for ; Sun, 27 Dec 2015 10:17:44 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aD8OU-0004Yg-TJ; Sun, 27 Dec 2015 10:17:42 +0000 Received: from mail-pa0-x22f.google.com ([2607:f8b0:400e:c03::22f]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aD8OK-0004Vw-Lo for linux-mediatek@lists.infradead.org; Sun, 27 Dec 2015 10:17:34 +0000 Received: by mail-pa0-x22f.google.com with SMTP id yy13so4177229pab.3 for ; Sun, 27 Dec 2015 02:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=r2QusB3Y392kpSox0+zT4Nq+YgBeuvfmVWbRxSQUAv8=; b=YP2BiJQKzW37mVSlP+SEdyIVAiUDu6Up3CMmfPSSwlLriZnDCPto35P2tF2E9h9T0g iKeyMEU7PmVGLoQRTwkjxyqxA0/mWG4Wce7lQFB9ZZWYMSXo3HMpjOhePXZrcmp2Nwz0 sUH6cXDjNVYlbuKy9hH3mMTEtYWtSzpUayHio= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=r2QusB3Y392kpSox0+zT4Nq+YgBeuvfmVWbRxSQUAv8=; b=AkJVOmrwdHWfNUJ40Wn8tT/VfjUH7OoLDMUay4Zd9a0XTuHmX/jjJ3Rt6RzSwZS18Z Q/bWwfmjmUMOulOpA4G2oh2pSd/+11cj3hA084IxthUgKzZqYNJYHNoiGBHG7ycLpR5Z NZCnjKRecByx+Uz0SfAiWutRwGFNicFfF3styaE0U6ZgLla+h1bFmK8BR5xSGSQAKx7S htsSUfX37dHw0765OgN0+r7tJDtk+IS1gc6hkMtoqrMVbkrnUQJCRDlHtoQ5dVGQ5kR9 zYHmzEwh7KgcEbQ2XXQ73yf8k2z1qQAkhvVwbZ+r3wUnbFPRzB4RRdwzKW2DI0LfEEhi Qcyw== X-Gm-Message-State: ALoCoQmZ6J6ohhdhTLKczuzPzJtk13DjTDQP3tN7N8e89icQ4Q8L2lE/EQVqVx9tsSEZliTcxUvHcD576T6elRdw5ogSUJl17w== X-Received: by 10.66.164.196 with SMTP id ys4mr70566442pab.119.1451211431353; Sun, 27 Dec 2015 02:17:11 -0800 (PST) Received: from drinkcat.tpe.corp.google.com ([172.30.210.53]) by smtp.gmail.com with ESMTPSA id q82sm21767852pfq.1.2015.12.27.02.17.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 27 Dec 2015 02:17:10 -0800 (PST) From: Nicolas Boichat To: Mark Brown Subject: [PATCH] spi: mediatek: Prevent overflows in FIFO transfers Date: Sun, 27 Dec 2015 18:17:06 +0800 Message-Id: <1451211426-20436-1-git-send-email-drinkcat@chromium.org> X-Mailer: git-send-email 2.6.0.rc2.230.g3dd15c0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151227_021732_809840_8A43CC35 X-CRM114-Status: GOOD ( 12.99 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: glider@chromium.org, Leilk Liu , linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org, linux-mediatek@lists.infradead.org, Matthias Brugger , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In the case where transfer length is not a multiple of 4, KASAN reports 2 out-of-bounds memory accesses: - mtk_spi_interrupt: ioread32_rep writes past the end of trans->rx_buf. - mtk_spi_fifo_transfer: iowrite32_rep reads past the end of xfer->tx_buf. Fix this by using memcpy on the remainder of the bytes. Signed-off-by: Nicolas Boichat --- drivers/spi/spi-mt65xx.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/drivers/spi/spi-mt65xx.c b/drivers/spi/spi-mt65xx.c index 00a36da..8763eff 100644 --- a/drivers/spi/spi-mt65xx.c +++ b/drivers/spi/spi-mt65xx.c @@ -320,7 +320,8 @@ static int mtk_spi_fifo_transfer(struct spi_master *master, struct spi_device *spi, struct spi_transfer *xfer) { - int cnt; + int cnt, remainder; + u32 reg_val; struct mtk_spi *mdata = spi_master_get_devdata(master); mdata->cur_transfer = xfer; @@ -328,12 +329,16 @@ static int mtk_spi_fifo_transfer(struct spi_master *master, mtk_spi_prepare_transfer(master, xfer); mtk_spi_setup_packet(master); - if (xfer->len % 4) - cnt = xfer->len / 4 + 1; - else - cnt = xfer->len / 4; + cnt = xfer->len / 4; iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt); + remainder = xfer->len % 4; + if (remainder > 0) { + reg_val = 0; + memcpy(®_val, xfer->tx_buf + (cnt * 4), remainder); + writel(reg_val, mdata->base + SPI_TX_DATA_REG); + } + mtk_spi_enable_transfer(master); return 1; @@ -415,7 +420,7 @@ static int mtk_spi_setup(struct spi_device *spi) static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id) { - u32 cmd, reg_val, cnt; + u32 cmd, reg_val, cnt, remainder; struct spi_master *master = dev_id; struct mtk_spi *mdata = spi_master_get_devdata(master); struct spi_transfer *trans = mdata->cur_transfer; @@ -428,12 +433,15 @@ static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id) if (!master->can_dma(master, master->cur_msg->spi, trans)) { if (trans->rx_buf) { - if (mdata->xfer_len % 4) - cnt = mdata->xfer_len / 4 + 1; - else - cnt = mdata->xfer_len / 4; + cnt = mdata->xfer_len / 4; ioread32_rep(mdata->base + SPI_RX_DATA_REG, trans->rx_buf, cnt); + remainder = mdata->xfer_len % 4; + if (remainder > 0) { + reg_val = readl(mdata->base + SPI_RX_DATA_REG); + memcpy(trans->rx_buf + (cnt * 4), + ®_val, remainder); + } } spi_finalize_current_transfer(master); return IRQ_HANDLED;