Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4241485rdb; Thu, 14 Sep 2023 16:57:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH+KpschvG70VyqEDRlH/TPEkEK5nQjuDA8xIIfUYiaeU9B75Yox9frZY1YZ+cFaSrmPT/S X-Received: by 2002:a17:90a:e50a:b0:274:8951:b5e4 with SMTP id t10-20020a17090ae50a00b002748951b5e4mr19262pjy.48.1694735870887; Thu, 14 Sep 2023 16:57:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694735870; cv=none; d=google.com; s=arc-20160816; b=oQwJhg80x1CL8tQao+KOT7+08Bz59zya4ZegAdhLFqrNY1fDhj+PALLBGs2HQQaDFt lKhtXHAYle8hw2hMWwbR3ogST9Gpz+Qv7f5MOgqg6egxnu1M2RqJVsniFe25t8ZG7dHf EiOu+fJ/nHVcQdz5N3nmN5ZulXuYeCOAgTez6njaryGrIpYVbXdxhYaFej5Hz5WSE/lv D21zhHF15D5+FxFwyIpOTaKvO7jigrzE3iGxR/Y+EfQ0k/0AcZR9WGLFQkD7tWq++tTh l1ejdJZTom7gejDEw6H7Ece8qn/LqFxtwGYiF+4p19kI27QxbnWcCH1K0948tlTm/Puu AQyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=+rQkSX/sTt9mCfEyiFHgFHaHzrtXbCcdvkzQAAhcaCs=; fh=YlsaD7UefnpjnVsuL5JyOuu3MDyyU2wgK9c2Pqas0bI=; b=m3+g/yAqoYzvbUjSqbWUvJZ1bAi7nxQFvxyCCjBcI0/S7Mt39mY6TfZzIXuLbyDHgO J3EKdtJzEekls+L4upkOvsuCU1ulkjTAgbIu90Gl5HfQrgAdlRwajxAIZDLpUai/XAR8 Nea0H4PJBvpSxAMu8zQAW72GKTVE42jbSdKbGfRLOmjmbG9m3wwFKd95wIBUh/iTpxoI /xgTT5whB+hjyQ9hWtuO/kT7DwkSLrq430yTdpRR+eXXldcy35q+kOaNIKxNmkisRc3Y FC/WouW5+EC42zZHDSEOsqtaw7vxtX6oQ6BB5aMyOsHbp2PouQ8sLTCGCv8ZP+gkYih/ pAVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@iogearbox.net header.s=default2302 header.b=jJYYKQiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id pf6-20020a17090b1d8600b00263cfc9753csi4977422pjb.5.2023.09.14.16.57.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 16:57:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@iogearbox.net header.s=default2302 header.b=jJYYKQiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id B5AF6836E1BB; Thu, 14 Sep 2023 10:26:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240159AbjINRZ7 (ORCPT + 99 others); Thu, 14 Sep 2023 13:25:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240003AbjINRZp (ORCPT ); Thu, 14 Sep 2023 13:25:45 -0400 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24101359C; Thu, 14 Sep 2023 10:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iogearbox.net; s=default2302; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=+rQkSX/sTt9mCfEyiFHgFHaHzrtXbCcdvkzQAAhcaCs=; b=jJYYKQiLbpdOVDjsdPtWUT6TFj gIUSL0E1I0K9FlPVK7YspQJTHRg8rpubRjsPu6LnmF2U95xpHfr3wlI3gvjQinqqbGOBohWR2umHr /oLG2eG2yInnO/eUSA7/xml20eBP76E7sgvVjIu+3H6NnDye2dsuxDMkYmsftqg9eYsFXFkl9TkUn +O/XGVj0+MTrUU5BhYwV6I24vjTVr7m32a9Dhz1fdd2S4nYmWEa+uCzGdeiikHwbziwI2M3GfUYa1 gSq4dY0QkoWQC11WwBan3CsrqqUDr/s/o4pTND1h2ZEfxN9FqRqWTsOCuqSeiL5dMwQq3Wds8K39S eWDU0MpA==; Received: from sslproxy05.your-server.de ([78.46.172.2]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qgq4Z-000J4w-I0; Thu, 14 Sep 2023 19:24:11 +0200 Received: from [64.61.70.24] (helo=localhost.localdomain) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qgq4Y-0009Z8-JG; Thu, 14 Sep 2023 19:24:10 +0200 Subject: Re: [PATCH 2/3] Revert "bpf: Fix issue in verifying allow_ptr_leaks" To: Alexei Starovoitov , Luis Gerhorst Cc: Andrii Nakryiko , Alexei Starovoitov , bpf , Hao Luo , John Fastabend , Jiri Olsa , KP Singh , Yafang Shao , Martin KaFai Lau , Stanislav Fomichev , Song Liu , Yonghong Song , Mykola Lysenko , Shuah Khan , gerhorst@amazon.de, Ilya Leoshkevich , "open list:KERNEL SELFTEST FRAMEWORK" , LKML , Hagar Gamal Halim Hemdan , Puranjay Mohan References: <20230913122827.91591-1-gerhorst@amazon.de> From: Daniel Borkmann Message-ID: <723a49b4-c4ed-3b0b-2a9d-915b49725411@iogearbox.net> Date: Thu, 14 Sep 2023 19:24:06 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.8/27031/Thu Sep 14 09:39:27 2023) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 14 Sep 2023 10:26:26 -0700 (PDT) X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email On 9/14/23 6:20 PM, Alexei Starovoitov wrote: > On Wed, Sep 13, 2023 at 5:30 AM Luis Gerhorst wrote: >> >> This reverts commit d75e30dddf73449bc2d10bb8e2f1a2c446bc67a2. >> >> To mitigate Spectre v1, the verifier relies on static analysis to deduct >> constant pointer bounds, which can then be enforced by rewriting pointer >> arithmetic [1] or index masking [2]. This relies on the fact that every >> memory region to be accessed has a static upper bound and every date >> below that bound is accessible. The verifier can only rewrite pointer >> arithmetic or insert masking instructions to mitigate Spectre v1 if a >> static upper bound, below of which every access is valid, can be given. >> >> When allowing packet pointer comparisons, this introduces a way for the >> program to effectively construct an accessible pointer for which no >> static upper bound is known. Intuitively, this is obvious as a packet >> might be of any size and therefore 0 is the only statically known upper >> bound below of which every date is always accessible (i.e., none). >> >> To clarify, the problem is not that comparing two pointers can be used >> for pointer leaks in the same way in that comparing a pointer to a known >> scalar can be used for pointer leaks. That is because the "secret" >> components of the addresses cancel each other out if the pointers are >> into the same region. >> >> With [3] applied, the following malicious BPF program can be loaded into >> the kernel without CAP_PERFMON: >> >> r2 = *(u32 *)(r1 + 76) // data >> r3 = *(u32 *)(r1 + 80) // data_end >> r4 = r2 >> r4 += 1 >> if r4 > r3 goto exit >> r5 = *(u8 *)(r2 + 0) // speculatively read secret >> r5 &= 1 // choose bit to leak >> // ... side channel to leak secret bit >> exit: >> // ... >> >> This is jited to the following amd64 code which still contains the >> gadget: >> >> 0: endbr64 >> 4: nopl 0x0(%rax,%rax,1) >> 9: xchg %ax,%ax >> b: push %rbp >> c: mov %rsp,%rbp >> f: endbr64 >> 13: push %rbx >> 14: mov 0xc8(%rdi),%rsi // data >> 1b: mov 0x50(%rdi),%rdx // data_end >> 1f: mov %rsi,%rcx >> 22: add $0x1,%rcx >> 26: cmp %rdx,%rcx >> 29: ja 0x000000000000003f // branch to mispredict >> 2b: movzbq 0x0(%rsi),%r8 // speculative load of secret >> 30: and $0x1,%r8 // choose bit to leak >> 34: xor %ebx,%ebx >> 36: cmp %rbx,%r8 >> 39: je 0x000000000000003f // branch based on secret >> 3b: imul $0x61,%r8,%r8 // leak using port contention side channel >> 3f: xor %eax,%eax >> 41: pop %rbx >> 42: leaveq >> 43: retq >> >> Here I'm using a port contention side channel because storing the secret >> to the stack causes the verifier to insert an lfence for unrelated >> reasons (SSB mitigation) which would terminate the speculation. >> >> As Daniel already pointed out to me, data_end is even attacker >> controlled as one could send many packets of sufficient length to train >> the branch prediction into assuming data_end >= data will never be true. >> When the attacker then sends a packet with insufficient data, the >> Spectre v1 gadget leaks the chosen bit of some value that lies behind >> data_end. > > The above analysis is correct, but unlike traditional spec_v1 > the attacker doesn't control data/data_end. > The attack can send many large packets to train that data + X < data_end > and then send a small packet where CPU will mispredict that branch > and data + X will speculatively read past data_end, > so the attacker can extract a bit past data_end, > but data/data_end themselves cannot be controlled. > So whether this bit 0 or 1 has no bearing. > The attack cannot be repeated for the same location. > The attacker can read one bit 8 times in a row and all of them > will be from different locations in the memory. > Same as reading 8 random bits from 8 random locations. > Hence I don't think this revert is necessary. > I don't believe you can craft an actual exploit. > > Your patch 3 says: > /* Speculative access to be prevented. */ > + char secret = *((char *) iph); > + > + /* Leak the first bit of the secret value that lies behind data_end to a > + * SMP silbling thread that also executes imul instructions. If the bit > + * is 1, the silbling will experience a slowdown. */ > + long long x = secret; > + if (secret & 1) { > + x *= 97; > + } > > the comment is correct, but speculative access alone is not enough > to leak data. What you write makes sense, it will probably be hard to craft an exploit. Where it's a bit more of an unknown to me is whether struct skb_shared_info could have e.g. destructor_arg rather static (at last the upper addr bits) so that you would leak out kernel addresses.