Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5197004pxj; Wed, 9 Jun 2021 11:24:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAyHT0gGWPnkPKCfekagMJ9jvhGGJ7+fjt0KPSiL1JGHsnq5HcJ8GB/Bli1IG0dOjSZAMp X-Received: by 2002:a17:906:6156:: with SMTP id p22mr1124167ejl.242.1623263090167; Wed, 09 Jun 2021 11:24:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623263090; cv=none; d=google.com; s=arc-20160816; b=HDLqLRdQ9LZLFc92mjnI+LY9gmNOq1o2nUiwSiVyHiTVWRQ6d93lEYuS4XvF4YO6cu 4Xp19OvY/oA07Pf+uTNwC26nbQ/DP8A/2I4c/Cq2vJKcbFxkGFKktQg4b5vIHppiSfeL GgrzBdb+7SYy6bDY0mt7yVYsrvg1GWQAZuDtmrtdkkB1UdaLBL+sGpi6VwQn2T6h/pQc eFvZm/NBcyGvRu4Y+S2qITnaeii6s/CdailhAjG/4BDP9LXEubU8hDCzEcTDTxo0fGkG 20qCKrNw7QHqGGdWW44p3jGZtKF3A1zzH+8mr/KRLOXza34gTLb8KUNg+9IV+02LY96s CjZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pMNtPdB4yeaOaJg4oKXM0D8sPt6m955IeGjoIXPrk70=; b=sjptleQFgn7j7PL7hdB3JIC4FMGqd80lNFw6/ys7QsG7TnCYDUpSO21UBXnAffFYrR q0BM5Y5fYEoqzuJRP2dpZR/dVQYaHEK9L/wOqVUrXirKKl0HrZCJXxzSE66p5ZLovi8C xYKIQETV0hQk/hg3WInycqs8+TCp6W/zt55XfeiqzIEgtxbqmSbgbrU6kMOIEFHaF5oJ 2wxMn2kygLQ6Iw2gmjv7ItkZtLXOaB4GYJUNhU4tGe16HOq1fxN5dA+J/zYTIWUImp1v J0kenv7XfryUfTS0h3eMLFUrlMNn7YOnfNtGOJH8MqCa2tl+aaGd+jFWc4JKPfPgqP96 +lRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PazdJpi7; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 19si345119ejx.529.2021.06.09.11.24.26; Wed, 09 Jun 2021 11:24:50 -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=@chromium.org header.s=google header.b=PazdJpi7; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbhFISXb (ORCPT + 99 others); Wed, 9 Jun 2021 14:23:31 -0400 Received: from mail-pf1-f180.google.com ([209.85.210.180]:41652 "EHLO mail-pf1-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbhFISXa (ORCPT ); Wed, 9 Jun 2021 14:23:30 -0400 Received: by mail-pf1-f180.google.com with SMTP id x73so19093544pfc.8 for ; Wed, 09 Jun 2021 11:21:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pMNtPdB4yeaOaJg4oKXM0D8sPt6m955IeGjoIXPrk70=; b=PazdJpi79UQEsNsJQD2CIaSYmwpf00Mw0MCZeW5GC2zdnsa0IB92sYjdiUDujBLmOv 3yiW/7qHaVhsPjbZomfBsLbYDeD+/ig/7taYFSIjA7Cs35U9xgJInZA2nA7XIhz+eX/f YAYlYYJX5H3ZqKlrBYtQLFY0ybk6rdc7RljwY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=pMNtPdB4yeaOaJg4oKXM0D8sPt6m955IeGjoIXPrk70=; b=c4DJ3eYdCmGg1PM9pkz76BkAWHTT2jtuxzwobf7Ayf5c/JIrjoEZI8lzzbpjqaRGYO xlSikKSFIkp42G7V/Dsa3TKEx8HaE3XeBQlEWUMnL5GhPzovJzpxgIyc33QyD+ASgRrG gU68t90C7/KVJYvZ+fWI8gbXZOiXdwG9cj0SWGWsG6KhxE5elMz4TJm1sHDF53P5h8UV sbfMeqVuKHcYV/596WlU5gv/SVM3ae/9zUTzxhP4NIOUd5Biz6iYY0M0Ln/eWCvm9sBp aAZDolCh1pb7D1ianYXBz3Y20sZ0vn8/wJwN81FKQsKIzi8SOqqC8iGsA4Zeh7F6dc45 42mQ== X-Gm-Message-State: AOAM533TmsLYmwva9YloZ/DBVWbcg++LuSynk93ZLyfTJw8bzxTXVRho 2KxJzQszX2dUYSr2zTZnHNSqEA== X-Received: by 2002:a62:3444:0:b029:2ec:9658:a755 with SMTP id b65-20020a6234440000b02902ec9658a755mr1010418pfa.71.1623262835683; Wed, 09 Jun 2021 11:20:35 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p14sm445214pgk.6.2021.06.09.11.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 11:20:34 -0700 (PDT) Date: Wed, 9 Jun 2021 11:20:33 -0700 From: Kees Cook To: Dmitry Vyukov Cc: Alexei Starovoitov , Yonghong Song , 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 Subject: Re: [PATCH v4] bpf: core: fix shift-out-of-bounds in ___bpf_prog_run Message-ID: <202106091119.84A88B6FE7@keescook> References: <000000000000c2987605be907e41@google.com> <20210602212726.7-1-fuzzybritches0@gmail.com> <87609-531187-curtm@phaethon> <6a392b66-6f26-4532-d25f-6b09770ce366@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. :) -- Kees Cook