Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp238667pxj; Wed, 9 Jun 2021 22:38:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVJD173kpMcP24ernd4UJpBjssog0D9skOGZnn0now45S5LdIME7YwHHD5gBdXpk3ai/D3 X-Received: by 2002:a05:6402:1771:: with SMTP id da17mr2919770edb.31.1623303513681; Wed, 09 Jun 2021 22:38:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623303513; cv=none; d=google.com; s=arc-20160816; b=i1iJKa5TenUFmQGOYhCKVhpDU4J06e0P5Z0dc8mPjfs0yHxlbt/oqvfAlqGgd/i/7B T+OwFXEtr5ZaDtr2B+aNzyJDIOGXwPAnvACApqwFY6vy+Lo7PG1DrtYTPdB7sXPXwMr0 Xs0pOWJ5OUzEzpoS2DXz4c3tCgkWW9fC/ojftrLCF8+Y5KH5ACoa7yiPURsZaNgbcaF2 dJbQIjj+wGtoQeuwH9vdgmdIUvqLf8OX2JnNUeosIXVoJEIZu0Ym7+qSfU22unXykK3g TN8Vo5SQbgChqypkUfAvKbv1vHJecydvFwsx7+QClOCsbtAc8c7Evb3O6LuTvM/mS4pg I3QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=OaVwnGtK9jh2uHt5Ipx2vrWPn+KDUOXrS2y79saIzrk=; b=jXNyLk5viyHoUeDZ6LgG11NS4LgEPkGRybuAnuUp05zbcqvD08o5aeUP0QSW74aXnD ybBbXJF6MkfOi0pDuAoZ/twzBhwcSuCV1NR9E1bw3TdIje5qPcMNwAvJySYAupd1OxWJ oW14squaoZp7XT9dexOA4e3T9KUCaS/zbl19An3EWsGnHoAJsp148YfpqsFa7HIkLZ+u xx3HM4b5n0gtJdiiyBBOPkhfmx1lVhN/tyBpf9dOXqwD3w94TRcOmPNvnp6JPHRFGFAL KtErUoK74pjM3IUUWSO6IJWGsEvXXnkXu62BehSoy0MomNcM5SoowCNh9vZNcQCOdrN1 VCjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LVuckSqO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j20si1632438edp.274.2021.06.09.22.38.08; Wed, 09 Jun 2021 22:38:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LVuckSqO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229911AbhFJFfs (ORCPT + 99 others); Thu, 10 Jun 2021 01:35:48 -0400 Received: from mail-qv1-f42.google.com ([209.85.219.42]:39769 "EHLO mail-qv1-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229692AbhFJFfs (ORCPT ); Thu, 10 Jun 2021 01:35:48 -0400 Received: by mail-qv1-f42.google.com with SMTP id u14so11205909qvq.6 for ; Wed, 09 Jun 2021 22:33:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OaVwnGtK9jh2uHt5Ipx2vrWPn+KDUOXrS2y79saIzrk=; b=LVuckSqOTYzxTsUyz/Dn8mquXY7TuARv2fApO1OjzaIq/krnrR5d5ShV+lUflmO6Fz sSGKHXNWk/tgIIIHDNg+pGfzKn7Ij6Zvcuw47GW8BrKJG1Pwv/sW2vav62DwI37IgXoh 6CDEJwoa3NhF5YygebCPdY/XS9lc9Qax0HWp5bhaOBYgSmfN9pehgzm+Niu7t8fS7ioW HNf0bjXiJcDigUzABktmGUNYqmhaPmJt91XrVmsJJan64o6ItFsbHqRGu/2doopvFRuN 4pghcbCmmzTM30tSHsmXGlf293hIE0belEUweoRZkaLT9l7XbckAnVFMsZTl56EwNiCK 9LoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=OaVwnGtK9jh2uHt5Ipx2vrWPn+KDUOXrS2y79saIzrk=; b=gkLQcmzHoXSWai7ymopmk6FA0P3N7ZSy0SXwDbV4DNzZUIRt2rxe5NBhWXyfnD/Abq A0KIJb5WcFHzuHGEcy0jGY3JmGj2mfkQs197x8tSYHJ3yfTJA7raRiJbOd+OEtsq1IyX Q4Vmakhyg6Qz5pBUk46gbGEiwTLGVYJSdTG3zEo5HRS0MIKTNBf5/QHQ57c7v2Db9QDe 8hM+clT53yju+iSQ1pxnIvktTgobsCUXmF3sNKtMyWG0OaxHd4SFnPyP+iqWRZ4Ci9/N X1k2jq7c6V88F9a7m2tfJ6i0iwNYcfnfUMTt3KDJX+uuPmqQrCbskFrCGJp83h8vC1eD I+mQ== X-Gm-Message-State: AOAM532+JhIhYylzSWg8TBrp1kRcpz/BmlpS/k91pC+8XjpQnH0n3w3t PFujbiJuVmw7dL+GEqSSIaDT9O87rT7Gy87xymzBpQ== X-Received: by 2002:a0c:d610:: with SMTP id c16mr3488166qvj.13.1623303156474; Wed, 09 Jun 2021 22:32:36 -0700 (PDT) MIME-Version: 1.0 References: <000000000000c2987605be907e41@google.com> <20210602212726.7-1-fuzzybritches0@gmail.com> <87609-531187-curtm@phaethon> <6a392b66-6f26-4532-d25f-6b09770ce366@fb.com> <202106091119.84A88B6FE7@keescook> <752cb1ad-a0b1-92b7-4c49-bbb42fdecdbe@fb.com> In-Reply-To: <752cb1ad-a0b1-92b7-4c49-bbb42fdecdbe@fb.com> From: Dmitry Vyukov Date: Thu, 10 Jun 2021 07:32:24 +0200 Message-ID: Subject: Re: [PATCH v4] bpf: core: fix shift-out-of-bounds in ___bpf_prog_run To: Yonghong Song Cc: Kees Cook , Alexei Starovoitov , Kurt Manucredo , syzbot+bed360704c521841c85d@syzkaller.appspotmail.com, Andrii Nakryiko , Alexei Starovoitov , bpf , Daniel Borkmann , "David S. Miller" , Jesper Dangaard Brouer , John Fastabend , Martin KaFai Lau , KP Singh , Jakub Kicinski , LKML , Network Development , Song Liu , syzkaller-bugs , nathan@kernel.org, Nick Desaulniers , Clang-Built-Linux ML , linux-kernel-mentees@lists.linuxfoundation.org, Shuah Khan , Greg Kroah-Hartman , Kernel Hardening , kasan-dev Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 10, 2021 at 1:40 AM Yonghong Song wrote: > On 6/9/21 11:20 AM, Kees Cook wrote: > > On Mon, Jun 07, 2021 at 09:38:43AM +0200, 'Dmitry Vyukov' via Clang Built Linux wrote: > >> On Sat, Jun 5, 2021 at 9:10 PM Alexei Starovoitov > >> wrote: > >>> On Sat, Jun 5, 2021 at 10:55 AM Yonghong Song wrote: > >>>> On 6/5/21 8:01 AM, Kurt Manucredo wrote: > >>>>> Syzbot detects a shift-out-of-bounds in ___bpf_prog_run() > >>>>> kernel/bpf/core.c:1414:2. > >>>> > >>>> This is not enough. We need more information on why this happens > >>>> so we can judge whether the patch indeed fixed the issue. > >>>> > >>>>> > >>>>> I propose: In adjust_scalar_min_max_vals() move boundary check up to avoid > >>>>> missing them and return with error when detected. > >>>>> > >>>>> Reported-and-tested-by: syzbot+bed360704c521841c85d@syzkaller.appspotmail.com > >>>>> Signed-off-by: Kurt Manucredo > >>>>> --- > >>>>> > >>>>> https://syzkaller.appspot.com/bug?id=edb51be4c9a320186328893287bb30d5eed09231 > >>>>> > >>>>> Changelog: > >>>>> ---------- > >>>>> v4 - Fix shift-out-of-bounds in adjust_scalar_min_max_vals. > >>>>> Fix commit message. > >>>>> v3 - Make it clearer what the fix is for. > >>>>> v2 - Fix shift-out-of-bounds in ___bpf_prog_run() by adding boundary > >>>>> check in check_alu_op() in verifier.c. > >>>>> v1 - Fix shift-out-of-bounds in ___bpf_prog_run() by adding boundary > >>>>> check in ___bpf_prog_run(). > >>>>> > >>>>> thanks > >>>>> > >>>>> kind regards > >>>>> > >>>>> Kurt > >>>>> > >>>>> kernel/bpf/verifier.c | 30 +++++++++--------------------- > >>>>> 1 file changed, 9 insertions(+), 21 deletions(-) > >>>>> > >>>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > >>>>> index 94ba5163d4c5..ed0eecf20de5 100644 > >>>>> --- a/kernel/bpf/verifier.c > >>>>> +++ b/kernel/bpf/verifier.c > >>>>> @@ -7510,6 +7510,15 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, > >>>>> u32_min_val = src_reg.u32_min_value; > >>>>> u32_max_val = src_reg.u32_max_value; > >>>>> > >>>>> + if ((opcode == BPF_LSH || opcode == BPF_RSH || opcode == BPF_ARSH) && > >>>>> + umax_val >= insn_bitness) { > >>>>> + /* Shifts greater than 31 or 63 are undefined. > >>>>> + * This includes shifts by a negative number. > >>>>> + */ > >>>>> + verbose(env, "invalid shift %lld\n", umax_val); > >>>>> + return -EINVAL; > >>>>> + } > >>>> > >>>> I think your fix is good. I would like to move after > >>> > >>> I suspect such change will break valid programs that do shift by register. > >>> > >>>> the following code though: > >>>> > >>>> if (!src_known && > >>>> opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { > >>>> __mark_reg_unknown(env, dst_reg); > >>>> return 0; > >>>> } > >>>> > >>>>> + > >>>>> if (alu32) { > >>>>> src_known = tnum_subreg_is_const(src_reg.var_off); > >>>>> if ((src_known && > >>>>> @@ -7592,39 +7601,18 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, > >>>>> scalar_min_max_xor(dst_reg, &src_reg); > >>>>> break; > >>>>> case BPF_LSH: > >>>>> - if (umax_val >= insn_bitness) { > >>>>> - /* Shifts greater than 31 or 63 are undefined. > >>>>> - * This includes shifts by a negative number. > >>>>> - */ > >>>>> - mark_reg_unknown(env, regs, insn->dst_reg); > >>>>> - break; > >>>>> - } > >>>> > >>>> I think this is what happens. For the above case, we simply > >>>> marks the dst reg as unknown and didn't fail verification. > >>>> So later on at runtime, the shift optimization will have wrong > >>>> shift value (> 31/64). Please correct me if this is not right > >>>> analysis. As I mentioned in the early please write detailed > >>>> analysis in commit log. > >>> > >>> The large shift is not wrong. It's just undefined. > >>> syzbot has to ignore such cases. > >> > >> Hi Alexei, > >> > >> The report is produced by KUBSAN. I thought there was an agreement on > >> cleaning up KUBSAN reports from the kernel (the subset enabled on > >> syzbot at least). > >> What exactly cases should KUBSAN ignore? > >> +linux-hardening/kasan-dev for KUBSAN false positive > > > > Can check_shl_overflow() be used at all? Best to just make things > > readable and compiler-happy, whatever the implementation. :) > > This is not a compile issue. If the shift amount is a constant, > compiler should have warned and user should fix the warning. > > This is because user code has > something like > a << s; > where s is a unknown variable and > verifier just marked the result of a << s as unknown value. > Verifier may not reject the code depending on how a << s result > is used. > > If bpf program writer uses check_shl_overflow() or some kind > of checking for shift value and won't do shifting if the > shifting may cause an undefined result, there should not > be any kubsan warning. I guess the main question: what should happen if a bpf program writer does _not_ use compiler nor check_shl_overflow()?