From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 34970C433FE for ; Fri, 4 Nov 2022 22:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xJT1sgTZGieVSGYj6phM81WZlhOrEqoZH94+C/37x1s=; b=DW9XAqV2e23V/D ZepRxeEzR+HMONDOslkmrzUChxAfmgNVZyVhkp0oK09TT7+3Me+KjZwOQp26ncdRaTbFWwvRDkZJt O1K9qkkB9HgcMZ+RrlRlarKXw+m3rwVRSQ9WEMBwneza/o8RWnLOH3RAn1cJ0h0umQkt7xTVwYf3E sTxxfdp1SNuaID5WWDEF+c90pnSDS0qCa+GaU8baOdXgcrNhQIYm3pI3d247/ToROkMRi2FNypbFr t4wE98AXFbed1kQXpaNpwulLt5/kqextBEOxsYZ7fwUD6CxTRq7k8wQFuhRpCsXHTOWUFCFtA+v43 tw7JCp94gekIfBsrZGYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1or5Pu-005Ooq-DG; Fri, 04 Nov 2022 22:44:02 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1or5Pr-005OoF-Fx for linux-arm-kernel@lists.infradead.org; Fri, 04 Nov 2022 22:44:00 +0000 Received: by mail-ej1-x636.google.com with SMTP id d26so16798556eje.10 for ; 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=lfYYydqbYqvnF9Seio8/o6E77Fn58/e8MwxGqYcXNvoTma/e7S4WcyY5y1y2gKVxts NRBNIGRKS2PrFLYC/c4IOUvRqIsENw7bG9rht/ueX3gZ3JrMYjPV8wrE4fQYmspcLvxd Mj4sxYH9rUUuKBp/KwPAXgHFo56qHxSeRNB3o8wPAo5lNkBno1hwi/DzuJTzBxRttqnQ eEd7ggaUurthaNbqMOAK48TOS8ue7CQocBqlqEf6L2iqOQnBWaB/CowqAXfAxhuZpxho jxOxmY1okOr7fKLjxc5WrCqTo9KHzTj0cRkbLl1MNelH591rXWI+ktGsWkju1BW/Ws05 Jb8Q== X-Gm-Message-State: ACrzQf1Ew2n8Ty3K5D5Gi/kiXfNDYsE8C3G6bMmJuQHh1IMwvxGOWwVy YQjjfyfqe/b3j4+XFvKQ7rLoqihQ4rw64V7Vzb4= X-Google-Smtp-Source: AMsMyM6m1JxGLOo6osuiEcTeO/CWp+a/VY4LBfYEMkKC6S8wslZ+0LSSDVoSUCw6MkSfeqGk5g8o1QthY79rE/UmA4k= 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" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221104_154359_553388_854DE521 X-CRM114-Status: GOOD ( 24.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel