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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF952C433F5 for ; Sun, 1 May 2022 19:00:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351422AbiEATDv (ORCPT ); Sun, 1 May 2022 15:03:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346687AbiEATDs (ORCPT ); Sun, 1 May 2022 15:03:48 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CA5CCC6 for ; Sun, 1 May 2022 12:00:22 -0700 (PDT) Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2416Tp3T009199 for ; Sun, 1 May 2022 12:00:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=DB/V8EV9IL0BwHpkchgZAowHLHCDycXLHpKtsINkUjI=; b=CEI5Rs3g5mVdJEE5uNFc2e9V12PtmqA/YVtY8bHszJhn2lp8cFLx9wiUr/P44CQkCagf tE92vWBDqJH5XzFNJ7bFb0lf3jh20mjvSn087esF5lba6mbq3L2cEY2eYobQw11U1K/d UUbBFqY0pHRojT3Pli2wO10BeAR5Vzlwf3Y= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3fs2tmwmhq-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 01 May 2022 12:00:22 -0700 Received: from twshared8053.07.ash9.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 1 May 2022 12:00:20 -0700 Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id 00F479C01F49; Sun, 1 May 2022 12:00:17 -0700 (PDT) From: Yonghong Song To: CC: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Subject: [PATCH bpf-next 03/12] libbpf: Fix an error in 64bit relocation value computation Date: Sun, 1 May 2022 12:00:17 -0700 Message-ID: <20220501190017.2577688-1-yhs@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220501190002.2576452-1-yhs@fb.com> References: <20220501190002.2576452-1-yhs@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: rdowVeKlp0aSqW8oQxirNFD5076Z-4Iq X-Proofpoint-ORIG-GUID: rdowVeKlp0aSqW8oQxirNFD5076Z-4Iq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-01_07,2022-04-28_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Currently, the 64bit relocation value in the instruction is computed as follows: __u64 imm =3D insn[0].imm + ((__u64)insn[1].imm << 32) Suppose insn[0].imm =3D -1 (0xffffffff) and insn[1].imm =3D 1. With the above computation, insn[0].imm will first sign-extend to 64bit -1 (0xffffffffFFFFFFFF) and then add 0x1FFFFFFFF, producing incorrect value 0xFFFFFFFF. The correct value should be 0x1FFFFFFFF. Changing insn[0].imm to __u32 first will prevent 64bit sign extension and fix the issue. Signed-off-by: Yonghong Song --- tools/lib/bpf/relo_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c index 2ed94daabbe5..f25ffd03c3b1 100644 --- a/tools/lib/bpf/relo_core.c +++ b/tools/lib/bpf/relo_core.c @@ -1024,7 +1024,7 @@ int bpf_core_patch_insn(const char *prog_name, stru= ct bpf_insn *insn, return -EINVAL; } =20 - imm =3D insn[0].imm + ((__u64)insn[1].imm << 32); + imm =3D (__u32)insn[0].imm + ((__u64)insn[1].imm << 32); if (res->validate && imm !=3D orig_val) { pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: go= t %llu, exp %llu -> %llu\n", prog_name, relo_idx, --=20 2.30.2