From patchwork Mon Mar 21 13:26:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8633241 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@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 322439F372 for ; Mon, 21 Mar 2016 13:32:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4023A2026F for ; Mon, 21 Mar 2016 13:32:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 49AF520204 for ; Mon, 21 Mar 2016 13:32:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756037AbcCUNaG (ORCPT ); Mon, 21 Mar 2016 09:30:06 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36304 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754583AbcCUN0k (ORCPT ); Mon, 21 Mar 2016 09:26:40 -0400 Received: by mail-wm0-f65.google.com with SMTP id l68so21876552wml.3; Mon, 21 Mar 2016 06:26:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JiuP/jGzbX4SfrOcIqf9TBhmheml8xfgzGfmzZBH9ac=; b=hS+fajXuaq7c+JHnt4l8mGS9MSiR26NbRowlGhZ4DXrLmEDZnfJzpPqpOi5vh3VpQz TKPpzYPU2+kwgiJz5OsR99l7Sl/sJm4QwL+rzjaN60Z1HeIDUQRk1Y7UOPCcjaGm+Izl D14xTEeD6dEv9xg2nym/4YFlLUvUzpKuc+mEAdbypWpkY0d2bSEHktbSKGPYaCwwWnTm PX4Iccm1RvXJV8AVQYozbEmVFhuVMFnGwO9X4kX48KUSogQiFNsFSRhPzL4231VDXe39 KbsKCkft+9TNmHeFF0zrjYtSgy0oeBEqYfZMz8SjN+iRgtyIgyvkVDioVTc1AUujuHc6 ZWcw== 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:in-reply-to :references; bh=JiuP/jGzbX4SfrOcIqf9TBhmheml8xfgzGfmzZBH9ac=; b=XrpoDTqx0TioyJp5WkUNdWH0/NRziKr753RirdB7oYgXn1Cuio4DKycW3H6Jl/6i0u v9C9/blRJYlm99wHvS4vQCV292sAmD1IRyIHzwMydRIoOZAHUc61wNsD3KdlhjvXazJ0 2jxuSOgVEqknaevUVStkQnOXORQyZ/RSLBtlD4dHfIt0n/Hbk9uLIHeDq2mXEjkqFdWD BKli2cTIimu0h+edHmwbjEbXfTQloS6onmp3zZEfYC2+iAgsYRd+yZdz094Ba1xCvCws lobsTG2iK8fk7KSXS85+8n9DVWWWHOmsySeEkVhpUczsIrINLeRKH7AgJRfRAIsNclCT 5nvw== X-Gm-Message-State: AD7BkJJ51MfRqJBhOUsVjSq3fvtx6WlfoJd67C34F/Q4a653bAKhh5kmQUqXJcDcPfNIXA== X-Received: by 10.194.123.35 with SMTP id lx3mr29841912wjb.132.1458566799434; Mon, 21 Mar 2016 06:26:39 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id ks5sm25430566wjb.13.2016.03.21.06.26.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:39 -0700 (PDT) From: Nicolai Stange To: Herbert Xu , "David S. Miller" Cc: Tadeusz Struk , Michal Marek , Andrzej Zaborowski , Stephan Mueller , Arnd Bergmann , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH RESEND v2 04/14] lib/mpi: mpi_write_sgl(): fix out-of-bounds stack access Date: Mon, 21 Mar 2016 14:26:05 +0100 Message-Id: <1458566775-5239-5-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458566775-5239-1-git-send-email-nicstange@gmail.com> References: <1458566775-5239-1-git-send-email-nicstange@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, 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 Within the copying loop in mpi_write_sgl(), we have if (lzeros) { mpi_limb_t *limb1 = (void *)p - sizeof(alimb); mpi_limb_t *limb2 = (void *)p - sizeof(alimb) + lzeros; *limb1 = *limb2; ... } where p points past the end of alimb2 which lives on the stack and contains the current limb in BE order. The purpose of the above is to shift the non-zero bytes of alimb2 to its beginning in memory, i.e. to skip its leading zero bytes. However, limb2 points somewhere into the middle of alimb2 and thus, reading *limb2 pulls in lzero bytes from somewhere. Indeed, KASAN splats: BUG: KASAN: stack-out-of-bounds in mpi_write_to_sgl+0x4e3/0x6f0 at addr ffff8800cb04f601 Read of size 8 by task systemd-udevd/391 page:ffffea00032c13c0 count:0 mapcount:0 mapping: (null) index:0x0 flags: 0x3fff8000000000() page dumped because: kasan: bad access detected CPU: 3 PID: 391 Comm: systemd-udevd Tainted: G B L 4.5.0-next-20160316+ #12 [...] Call Trace: [] dump_stack+0xdc/0x15e [] ? _atomic_dec_and_lock+0xa2/0xa2 [] ? __dump_page+0x185/0x330 [] kasan_report_error+0x5e6/0x8b0 [] ? kzfree+0x2d/0x40 [] ? mpi_free_limb_space+0xe/0x20 [] ? mpi_powm+0x37e/0x16f0 [] kasan_report+0x71/0xa0 [] ? mpi_write_to_sgl+0x4e3/0x6f0 [] __asan_load8+0x64/0x70 [] mpi_write_to_sgl+0x4e3/0x6f0 [] ? mpi_set_buffer+0x620/0x620 [] ? mpi_cmp+0xbf/0x180 [] rsa_verify+0x202/0x260 What's more, since lzeros can be anything from 1 to sizeof(mpi_limb_t)-1, the above will cause unaligned accesses which is bad on non-x86 archs. Fix the issue, by preparing the starting point p for the upcoming copy operation instead of shifting the source memory, i.e. alimb2. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 78ec4e1..b05d390 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -403,15 +403,11 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, #error please implement for this limb size. #endif if (lzeros) { - mpi_limb_t *limb1 = (void *)p - sizeof(alimb); - mpi_limb_t *limb2 = (void *)p - sizeof(alimb) - + lzeros; - *limb1 = *limb2; y = lzeros; lzeros = 0; } - p = p - sizeof(alimb); + p = p - sizeof(alimb) + y; for (x = 0; x < sizeof(alimb) - y; x++) { if (!buf_len) {