From patchwork Mon Nov 7 09:20:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Jihong X-Patchwork-Id: 13034180 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C923C4332F for ; Mon, 7 Nov 2022 09:23:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231295AbiKGJXz (ORCPT ); Mon, 7 Nov 2022 04:23:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231709AbiKGJXw (ORCPT ); Mon, 7 Nov 2022 04:23:52 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFFA717068; Mon, 7 Nov 2022 01:23:51 -0800 (PST) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4N5QgH0zSyzpWDJ; Mon, 7 Nov 2022 17:20:11 +0800 (CST) Received: from kwepemm600003.china.huawei.com (7.193.23.202) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 7 Nov 2022 17:23:47 +0800 Received: from ubuntu1804.huawei.com (10.67.174.61) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 7 Nov 2022 17:23:46 +0800 From: Yang Jihong To: , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH bpf v2 2/5] bpf: Adjust sk size check for sk in bpf_skb_is_valid_access for CO_RE in 32-bit arch Date: Mon, 7 Nov 2022 17:20:29 +0800 Message-ID: <20221107092032.178235-3-yangjihong1@huawei.com> X-Mailer: git-send-email 2.30.GIT In-Reply-To: <20221107092032.178235-1-yangjihong1@huawei.com> References: <20221107092032.178235-1-yangjihong1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.61] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600003.china.huawei.com (7.193.23.202) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The error code -EACCES is returned when bpf prog is tested in 32-bit arch. This is because bpf_object__relocate modifies 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 arch, adjust check sk size. Signed-off-by: Yang Jihong --- net/core/filter.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/net/core/filter.c b/net/core/filter.c index bb0136e7a8e4..47cbad2e609f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -8269,7 +8269,13 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type return false; break; case offsetof(struct __sk_buff, sk): - if (type == BPF_WRITE || size != sizeof(__u64)) + /* CO_RE adjusts pointer accesses from 8-byte read to + * 4-byte reads in 32-bit host arch, so 32-bit can only + * read the 32-bit pointer or the full 64-bit value, + * and 64-bit can read write the 64-bit pointer. + */ + if (type == BPF_WRITE || + (size != sizeof(struct bpf_sock *) && size != sizeof(__u64))) return false; info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL; break;