Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp684820pxj; Thu, 10 Jun 2021 10:10:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLhQGED7HYVUeU/hoACv8ZIClKCmpV4ZeM3o6iy96pFr9b45XeOvOAA3ytmoNp5vvk4+k7 X-Received: by 2002:a17:906:fa13:: with SMTP id lo19mr619803ejb.468.1623345010075; Thu, 10 Jun 2021 10:10:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623345010; cv=none; d=google.com; s=arc-20160816; b=xD2cTzs3Ic9yDjsLMDgwHuzc/1vIJGbmfKJe8/ieB+Ft0t2yNg2iPmcbtJ1VUuijiu LMNUnfVGtlpZwHkIjqEDuDtHwWzEnjGnGj1G4QbTTGcXGoqm6usVsgRugU2+vFqiYrex vRbouxavENq2isxS4ZgBAup3mW3/nyAoXUR1yJl1+9VGCqRVNHOwO1rzogci/VF5zuVr wU1zCSjvbO/HnsX67wJr1c26kniCvvqj0oN2jpQBNJwZKgVUU+9mU3gFrmxt/4f0CuJO gcxFcsofCGHv3Jk7w8Q8ZbfqGY0MXHppVt38Fi6Ko06UJFtyVZyEFJeHSC5a4VCIGX3X kXNg== 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=Tf6j48/EA7psoQT0Xa+Ykp/HBTN3iYdEAaBd6YZtNzA=; b=sPA3g4DLcQDbpYg8p4YN8OTjROfpSRCSj2y7Fx3RJRiLcg7T/jegIgX0QpItDVaXW2 ZQepFXkyZjDwrOQURRhMRmoG/pF/pNqtG06rIVv+WJC61LccQA8K52y5k0RPA6KfIpej uNwMJo06EoGSSeXof6pSOssTm1gk/qshvZBK83tfYGOS9FY800+i1oCGjaH7OVQCQ7hQ 3A+WGIQV2EQ5IyqrRoaLYTeo6//JfdMLj9l+zbI0M5w19OtkuRT5x4d+UqC3NKCuSIiR 2WIhBmbcwoGcplFDQMn2SX0hZGkPqSSw1kUhA83IDXNRBCeivIaC9mdMb6J1Hlh0Irkr TG8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BQ5qUY22; 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 gy21si2895053ejb.8.2021.06.10.10.09.45; Thu, 10 Jun 2021 10:10:10 -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=BQ5qUY22; 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 S230304AbhFJRI7 (ORCPT + 99 others); Thu, 10 Jun 2021 13:08:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229895AbhFJRI7 (ORCPT ); Thu, 10 Jun 2021 13:08:59 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99557C061574 for ; Thu, 10 Jun 2021 10:06:46 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 27so287126pgy.3 for ; Thu, 10 Jun 2021 10:06:46 -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=Tf6j48/EA7psoQT0Xa+Ykp/HBTN3iYdEAaBd6YZtNzA=; b=BQ5qUY2276upuseKeH0xi1cEhRtHiQIFJX1GijU6PYyl4Cthzv0BUWu+InvJdo0fyc nG8Pyy9LtiSpoNHDFjrgilIGZVKGi4qnfmsRJ1Ac9bjV3BFqIKua7PDRe5kS9WxvMPHF hGCQOxOSvU3j5mYsPkqMaz++wpX7b6l2jSRXE= 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=Tf6j48/EA7psoQT0Xa+Ykp/HBTN3iYdEAaBd6YZtNzA=; b=ZN2sqPonamzG2/wSBUz631sVm354mRC8Uma43Ewmqq8RkeRjUJODL3KduWpkZXB78B cXu1uiQkRpTKpNocZw05yLdNFXB5yo4MdxU6BJ+8/ZBVrjL4wb49PopvDEvzpW1yXjsA qSkwjhsXn+nX3Z0mIAr4D+wE6ujJ1Mmxfk/bAZ3aHSIapNEh20xx+76hliJLkRV6EcnQ y5hvsvkuDNvsIUDP7YTKaUknoVtr6W9dgaJ1infwC+uohKicQwHug5n7GuJ0ldMptyes NGrekFeLiSLRpcD496o8qAPWrnTxyf3t4Jgbzyb11R0fGdNq3K8Metr6XRdjketPYOxq o09g== X-Gm-Message-State: AOAM532yflBC1wpRoiQHI8XthiCDgnMlz2z9ul0PcNAisvv1Lk+5exzt 5MRM2jhse3QKmtL2u7Y5NOCAag== X-Received: by 2002:a63:5d66:: with SMTP id o38mr5923418pgm.444.1623344804696; Thu, 10 Jun 2021 10:06:44 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k25sm2852989pfk.33.2021.06.10.10.06.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 10:06:43 -0700 (PDT) Date: Thu, 10 Jun 2021 10:06:42 -0700 From: Kees Cook To: Yonghong Song Cc: Dmitry Vyukov , 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 Subject: Re: [PATCH v4] bpf: core: fix shift-out-of-bounds in ___bpf_prog_run Message-ID: <202106101002.DF8C7EF@keescook> References: <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> <1aaa2408-94b9-a1e6-beff-7523b66fe73d@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1aaa2408-94b9-a1e6-beff-7523b66fe73d@fb.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 09, 2021 at 11:06:31PM -0700, Yonghong Song wrote: > > > On 6/9/21 10:32 PM, Dmitry Vyukov wrote: > > 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. > > > > > > > [...] > > > > > > > > > > > > > > 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. Ah, gotcha: it's the BPF code itself that needs to catch it. > > > 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. Right. > > I guess the main question: what should happen if a bpf program writer > > does _not_ use compiler nor check_shl_overflow()? I think the BPF runtime needs to make such actions defined, instead of doing a blind shift. It needs to check the size of the shift explicitly when handling the shift instruction. > If kubsan is not enabled, everything should work as expected even with > shl overflow may cause undefined result. > > if kubsan is enabled, the reported shift-out-of-bounds warning > should be ignored. You could disasm the insn to ensure that > there indeed exists a potential shl overflow. Sure, but the point of UBSAN is to find and alert about undefined behavior, so we still need to fix this. -- Kees Cook