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: 7923181 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id F206A9F32E for ; Sun, 27 Dec 2015 10:18:05 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 228CB2025B for ; Sun, 27 Dec 2015 10:18:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 342582025A for ; Sun, 27 Dec 2015 10:18:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751141AbbL0KRN (ORCPT ); Sun, 27 Dec 2015 05:17:13 -0500 Received: from mail-pa0-f41.google.com ([209.85.220.41]:33334 "EHLO mail-pa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750894AbbL0KRL (ORCPT ); Sun, 27 Dec 2015 05:17:11 -0500 Received: by mail-pa0-f41.google.com with SMTP id cy9so96551250pac.0 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=GMwjpNde4bcWVshppXF86GQHp1h2uaHZBPx42HjpoxAtuJjusLsC9uKJEMrjIMDoBr vxasAC7XY7H8BHnFV/s4MTD52lEDm+1kBRbrXlbriXrCacrIiwCEzFEcNQbuDDxa+/+9 G4+0a+/WgxXZQ8cTUej8o2JFptLiY+BOf20rnygOe3uDhPJ/zQO9Nf6GsdHvGWeAfSF7 kb+jZMVzf1E6AF5+EGJ/3gmlXV1mEQF6lJb8nhgUvX3WLq94xNQU3xRzrQlZC2dq5bJ6 1aPMPpd8YmSn6AphtxpRi6Ri089jZ990mK0dtGWPMka5bc6ccVSD8SdX1a0Ie00dmyDO +brQ== X-Gm-Message-State: ALoCoQlWbEgoibC/sBbXYmJEfymCN2BOYyJr/+agza7uFzPBeChZG+mN2LyHB0SJQrbf4M6rHn6/gXbiwYuymT6LbMiWfU9eJg== 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 Cc: Matthias Brugger , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, glider@chromium.org, Leilk Liu 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 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,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;