Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1107336imm; Fri, 27 Jul 2018 11:13:03 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdLSHmuEi7gF3k2EA7eC0u0EhQDECWOKQnpa5v6li5nYw5XEhtAbCNS8H9Hh7N5d6lDaJ/N X-Received: by 2002:a62:d98f:: with SMTP id b15-v6mr7668216pfl.1.1532715183164; Fri, 27 Jul 2018 11:13:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532715183; cv=none; d=google.com; s=arc-20160816; b=zSV4xGuwBHR67qAUMhglJvX6QvZ9+snvgtBde6/0F4t0zIMhPyEonZEkSdXdBzhFsX Kuchqj9UeOfes4O38c3FqMgcBF90OKXNJhLKK9q3mY+W8Kupzl8jO9VAHV2yyAJpebvR Wb+il08ltrU0vkazpmfs+Q0cbkvozSK0wzoMvI6xv7RlEZOB1gYSDLsv/pA8dNsOlkV8 idI2C8YL+lRztSi4qKMI58FDrinFoqxIS28q0u1aMJtebhBqoiWkzwctQkvFueEhL8ic UhsEBriRoAaus4mtEJu3WgMk/8Cjs3Qv+RKEgpnKtrXCgRlXSVFYdXg7+MKzYfXf7F/e e99Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=PLNBvjvDl8eeFdgzUSJprGo3IPgLAgnPSM5E6Ffxx2s=; b=UbIngSc9OssWAYvFBojy66Axr/NEYjMPPbs6zJtMlc/SwzHZSPoh6vBVn0MQkvlNYo d1rpPJ3Q1DAljUKbKn6HdnFA1d2Tu0S3Q5kYG6j07fXnRoBmhjNTs9HPIuNcacBdfiH3 DFIAARkUGxCpM5XHfuUsyQw+ZXinzMlFIUcV3B/WbfUUpA3jadD6t1i6TwU0JUj0COGu t6ipPZ+Mq00JZ+WsWFsHC9XKdIEPuyqASIndtX0MLljZ64Yuy8DefcXMAq3CFINrAoIv Uh/G51a3DEPd7kZ90xyHTQuVUyK22zh9r3460oLTp247TM0w2EBvltHr+sXBv2OqeUcM DRvA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d10-v6si4109487pfg.258.2018.07.27.11.12.24; Fri, 27 Jul 2018 11:13:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388925AbeG0Td1 (ORCPT + 99 others); Fri, 27 Jul 2018 15:33:27 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41604 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2388344AbeG0Td1 (ORCPT ); Fri, 27 Jul 2018 15:33:27 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AD2CC8197011; Fri, 27 Jul 2018 18:10:24 +0000 (UTC) Received: from treble (ovpn-120-105.rdu2.redhat.com [10.10.120.105]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 561901C5BA; Fri, 27 Jul 2018 18:10:24 +0000 (UTC) Date: Fri, 27 Jul 2018 13:10:22 -0500 From: Josh Poimboeuf To: Jeremy Cline Cc: Theodore Ts'o , Andreas Dilger , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 3/3] ext4: mballoc: Fix spectre gadget in ext4_mb_simple_scan_group Message-ID: <20180727181022.zjz3lwagugitwt5m@treble> References: <20180727162357.30801-1-jcline@redhat.com> <20180727162357.30801-4-jcline@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20180727162357.30801-4-jcline@redhat.com> User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 27 Jul 2018 18:10:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 27 Jul 2018 18:10:24 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jpoimboe@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 27, 2018 at 04:23:57PM +0000, Jeremy Cline wrote: > 'ac->ac_2order' is a user-controlled value used to index into > 'grp->bb_counters' and based on the value at that index, 'ac->ac_found' > is written to. Clamp the value right after the bounds check to avoid a > speculative out-of-bounds read of 'grp->bb_counters'. > > This also protects the access of the s_mb_offsets and s_mb_maxs arrays > inside mb_find_buddy(). > > These gadgets were discovered with the help of smatch: > > * fs/ext4/mballoc.c:1896 ext4_mb_simple_scan_group() warn: potential > spectre issue 'grp->bb_counters' [w] (local cap) > > * fs/ext4/mballoc.c:445 mb_find_buddy() warn: potential spectre issue > 'EXT4_SB(e4b->bd_sb)->s_mb_offsets' [r] (local cap) > > * fs/ext4/mballoc.c:446 mb_find_buddy() warn: potential spectre issue > 'EXT4_SB(e4b->bd_sb)->s_mb_maxs' [r] (local cap) > > Cc: Josh Poimboeuf > Cc: stable@vger.kernel.org > Signed-off-by: Jeremy Cline > --- > fs/ext4/mballoc.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c > index f7ab34088162..c0866007a949 100644 > --- a/fs/ext4/mballoc.c > +++ b/fs/ext4/mballoc.c > @@ -14,6 +14,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -1893,6 +1894,7 @@ void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, > > BUG_ON(ac->ac_2order <= 0); > for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) { > + i = array_index_nospec(i, sb->s_blocksize_bits + 2); > if (grp->bb_counters[i] == 0) > continue; Similar to my patch 1 comment, it's better to go up the call chain. ac_2order's user taint seems to come from ext4_mb_regular_allocator(), where it's derived from ac->ac_g_ex.fe_len, which has a user taint according to smatch. i = fls(ac->ac_g_ex.fe_len); ac->ac_2order = 0; /* * We search using buddy data only if the order of the request * is greater than equal to the sbi_s_mb_order2_reqs * You can tune it via /sys/fs/ext4//mb_order2_req * We also support searching for power-of-two requests only for * requests upto maximum buddy size we have constructed. */ if (i >= sbi->s_mb_order2_reqs && i <= sb->s_blocksize_bits + 2) { /* * This should tell if fe_len is exactly power of 2 */ if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0) ac->ac_2order = i - 1; } So here maybe you could change the assignment to: ac->ac_2order = array_index_nospec(i - 1, sb->s_blocksize_bits + 2); That makes it easier for a reader of the code to understand what speculation we're protecting against. And it also protects other consumers of this value down the call chain. -- Josh