Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp1563895rwl; Fri, 4 Nov 2022 16:00:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7wLh0wyhcQEOiOYeZp0Kq/afkAm4omJht/Rm81ZaCw/dyle0pJV2tWl50iPLwXHCeY+cBp X-Received: by 2002:aa7:c9c7:0:b0:463:4eff:a07 with SMTP id i7-20020aa7c9c7000000b004634eff0a07mr29821820edt.88.1667602846706; Fri, 04 Nov 2022 16:00:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667602846; cv=none; d=google.com; s=arc-20160816; b=I2QHFkAaMAegR6cxBhGEXQa11hfYDQwhq/wMZYyiiyTjWgZ7QVwROF2MV0EvzOb3FW FhVDu4j+V376+Uf2F3uDLqaRoBtJRWau1gtc6FwucqmL04xsQ+eD+09PqXAKmwdchY1v g793k6/LByADEI1sIDSHDSQt0iCSDdjSmBJe3Vm4p9Oc7tIZp0kB2mEY2SjkIKEiO9Pj nnbPXSVP60w9t7qiSAFrPcltlmK1n3Qrl6eniS8Zqe7DW7ccB6NAFeEzfSKWrTrf55EM 4FlHb1OSe7Hp9hC3NIWgL2fyCHS85kjFsoXETqgxKOEkZq/3Ekdegy9OO4P+/qfDMJVb zS9A== 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=Rz+uJhq6MzU/GpngjVCSms293gxGW6YlyI6hslqJJXY=; b=JkVkmeN26crVYrl6jvSdBNZnVCVPzmpnS6WSmU0rDNUkl5ArJ6t3Zgvii543e2tWwO YdMG6TF9XCccY3ZyOs1tOt+HpjT2xwbq2dpBdjnLqhfpXmXpt5hVtHjCJ4KOGK8E2LHW ZszRaOZtniAE2Q0pxbkwghLMjD3q7BsEsgcAmM8S/dWjNwnQE3xBHxZAm5unMXxlJYu6 Rm1vee2RycYcAkFmYDk0INVwLBvvvJWuUJhTu1chQTFFHJ4FKuC1K7hDl6cckXkHQ7RC 9vBX5qm2SlFj+B9YfErw29hETH9cgcx0bxXlcZU55o63q2eciMnYHrR4sWzmo1BAcf20 ggIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TTSC1PYs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rn5-20020a170906d92500b0078d0f88a6e9si284671ejb.284.2022.11.04.16.00.23; Fri, 04 Nov 2022 16:00:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TTSC1PYs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230127AbiKDWtw (ORCPT + 98 others); Fri, 4 Nov 2022 18:49:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230387AbiKDWt2 (ORCPT ); Fri, 4 Nov 2022 18:49:28 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5B1A862FC; Fri, 4 Nov 2022 15:43:57 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id k2so16881409ejr.2; Fri, 04 Nov 2022 15:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Rz+uJhq6MzU/GpngjVCSms293gxGW6YlyI6hslqJJXY=; b=TTSC1PYsIl4wNygBOjEUlZux3jycvLQZX0WHn6DVKJn/Bn+ZesGrvgIXyj+0/ouW/k h+wn0Cygd7/4L02ScnxFa3Jb67ww5ZmXvVAMflPFlv7nTf5Whhb66Ykyk7IWxFQSVQmO GROHmfOl0tHjh3wYbOE0M8nW/ZaDmzNzm3sDGSvq/8y0aBb5LT8XGwUHsBJwX3fKoLKF y/f+vkNDtMPsLJDISf0UkF9MSxgt3+Fffs7FX0xox4CNCxREk9/WX5bT1RMDmQifKkIe ckFoAW0z2iUajI+YynPu9iCasmv6pxkObEVy1j/jGQBUeN1ECw3xa0SosZZBwDnp3poT ia/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Rz+uJhq6MzU/GpngjVCSms293gxGW6YlyI6hslqJJXY=; b=lnGH4UG//S7a6y73TYU7TViqjf+L51CLjFcKfqZnW9YaTo0SqQOpwy+QmEb/dEvhLm x7Ul0flXIbnWy2Woxqr3CUsCo+BPhJWPNVYh+G2Kjz5qorfChDVBq1zujXdjoCTIt5UO nxcTarTjiCKOlc38xskTgPpmFQxi9n8WkJyjkJTGuPiU4njPcqhZ813HRtI+gRa5pIji KWCxkxuN04iBxcJF8btzekHbTomdYanhqh5ZLzdh45BswUEKad2N5e0O8m8BK8VmjP0z OcbaLSCfl1sxFoJqNVf+DJ9YGqcKBV4kI2a3jFDtmdC0W1qq8u4Qj9Yp+KVdJKcNhF28 rO+Q== X-Gm-Message-State: ACrzQf0nqN21VDQXXBT04ztsH+hha1XxKI1o3IOF0arZvumvQCHWdGY3 GY82H3vBNyMdLNxCNH+W6zlo7Y8Y6DINMYzb3EU= X-Received: by 2002:a17:907:8a24:b0:795:bb7d:643b with SMTP id sc36-20020a1709078a2400b00795bb7d643bmr37412552ejc.115.1667601836086; Fri, 04 Nov 2022 15:43:56 -0700 (PDT) MIME-Version: 1.0 References: <20221103092118.248600-1-yangjihong1@huawei.com> <20221103092118.248600-3-yangjihong1@huawei.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 4 Nov 2022 15:43:44 -0700 Message-ID: Subject: Re: [PATCH bpf RESEND 2/4] bpf: Remove size check for sk in bpf_skb_is_valid_access for 32-bit architecture To: Alexei Starovoitov Cc: "Russell King (Oracle)" , Yang Jihong , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shubham Bansal , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mykola Lysenko , Shuah Khan , Benjamin Tissoires , Kumar Kartikeya Dwivedi , Delyan Kratunov , Artem Savkov , bpf , linux-arm-kernel , LKML , Network Development , "open list:KERNEL SELFTEST FRAMEWORK" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 3, 2022 at 11:15 AM Alexei Starovoitov wrote: > > On Thu, Nov 3, 2022 at 4:23 AM Russell King (Oracle) > wrote: > > > > On Thu, Nov 03, 2022 at 05:21:16PM +0800, Yang Jihong wrote: > > > The error code -EACCES is returned when bpf prog is tested in 32-bit environment, > > > This is because bpf_object__relocate modifies the instruction to change memory > > > size to 4 bytes, as shown in the following messages: > > > > > > libbpf: prog 'kfunc_call_test1': relo #2: matching candidate #0 [18342] struct __sk_buff.sk (0:30:0 @ offset 168) > > > libbpf: prog 'kfunc_call_test1': relo #2: patched insn #1 (LDX/ST/STX) off 168 -> 168 > > > libbpf: prog 'kfunc_call_test1': relo #2: patched insn #1 (LDX/ST/STX) mem_sz 8 -> 4 > > > > > > As a result, the bpf_skb_is_valid_access check fails. For 32-bit architecture, > > > unnecessary checks need to be deleted. > > > > Isn't the purpose of this check to ensure that the entire pointer is > > written, and BPF can't write half of it? > > > > > > > case offsetof(struct __sk_buff, sk): > > > - if (type == BPF_WRITE || size != sizeof(__u64)) > > > - return false; > > > > Wouldn't "(size != sizeof(struct bpf_sock *) && size != sizeof(__u64))" > > be more appropriate here, so 32-bit can only write the 32-bit pointer > > or the full 64-bit value, and 64-bit can only write the 64-bit pointer? > > Or is there a reason not to? bpf folk? > > You're correct. The patch is completely wrong. > The bug is elsewhere. So I looked at this a bit (and replied to the old version of this patch). What happens in the kernel is that we expect 64-bit load but rewrite it to 32-bit load on 32-bit architectures (because we just use sizeof(struct sk_buff, sk) which is 4 bytes on 32-bit arch. The problem here is that libbpf adjusts such pointer accesses from 8-byte read to 4-byte reads for preserve_access_index (because libbpf sees that pointer is really 4 byte long), which is what we actually want in the general case. Here the assumption was made before CO-RE that __sk_buff is a stable (and fake) UAPI and the correct BPF program will access sk as a 64-bit pointer because BPF-side pointers always appear as 64-bit. But from a correctness standpoint I think it should be fine to enable both 32- and 64-bit loads for such pointers in __sk_buff for 32-bit host arch. This will work well with CO-RE and will be correctly rewritten to 32-bit or 64-bit accesses, depending on host architecture. We should still reject 32-bit load on 64-bit host arch, though.