From nobody Fri Dec 19 21:50:27 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6738C1F37D6 for ; Tue, 21 Jan 2025 14:25:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737469534; cv=none; b=f3Sng1tHR8+d2F27keemcAGw7SX+1GiNXu12z74EIDCL+c0GjH2nt6IjWtjAxGVTS+lH6q2JSjfApNHRJWW6Ur74BBlhCHVrGaXPkoylTXAHRnB9wT0NCHUPG7dzFcjkWYIFvNGjkHjbYk7spRs0Q35nfw5V8mliH+RYQ2MID4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737469534; c=relaxed/simple; bh=J7Vx3L310jAzxAh8q8+pNqO5NDwd9wWW5TXfltnQXI4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Ij4v2yODKEnR+1xcq7AKjz8jp0Xx8OeSMNlH5OrWL1WqOXjjELhzkoo6JWdwU0zW6OTIjQ7kty25xwUccxmDDS8Z2pTns4rpE/3WUtybGH8+DfJtK0G0dm4wsDlY75XFKnaSu9HLyqv/zIsovbmeV8hY58HmDERDeDyWf62lowI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=E8OaOaKq; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E8OaOaKq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737469531; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=vuGGo9UIaQnmFOwY4BK8JGBIoTzmm8Sq6jjvbqQ+D1Y=; b=E8OaOaKqqlWIPgt3AykRlWmCEUJDN+Z+Vl0lhdHmcrMVxA1izyt15rfMy30u7eDoBMOdEh bCtyQOSFB76cqYWRkwcoSA/z7eBXHI1vam/iOIKj1nZquH7fs8mlt8NnhzZYQj2Nnhye3u vfmTZLSpkaKZasu3w1rmgZezA45EO3s= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-249-S_l51klrP-OdixQpdD7avg-1; Tue, 21 Jan 2025 09:25:29 -0500 X-MC-Unique: S_l51klrP-OdixQpdD7avg-1 X-Mimecast-MFC-AGG-ID: S_l51klrP-OdixQpdD7avg Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-467be89d064so64168951cf.3 for ; Tue, 21 Jan 2025 06:25:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737469529; x=1738074329; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vuGGo9UIaQnmFOwY4BK8JGBIoTzmm8Sq6jjvbqQ+D1Y=; b=Tl6JcblSnUlPFJrHNN/tfAYAGfSKNonv5C9j7wvOD4b4xhA1rwu3HhKOEZqls4pjOz XBbvpy9pzQ0bggw/JqXQcwo0xX4Rs4D1LrzG5jCOXcam1o4vJoPnEUxT9ZyeTnucM88b Yv1JCRml5b3JF6u8vkwAw2/ZgBx/+13/mi5GVEbUEbkhTr1z6vgFVnTxPZlDNFQPGjKY rgM9xIOzEroIJ14lGUccZAfEl2yo/2Su6qnKcBxTN5LmdNst9wks2IGwWeL0jS+TT18e 7P7JsF9HSz659iCN8Nz1EYCE8PrKuOtQjmeCIoU3iM5ft2osV/dvaHFgu7pk9ax7mBMo aoZg== X-Forwarded-Encrypted: i=1; AJvYcCXTSzEw4S929taiVRYL4AVAU/iPXT+joyUNhIEEhnZjNFOCeL6ZJlNhWFa+ih5nwUsOFOmJD4XCMudndGM=@vger.kernel.org X-Gm-Message-State: AOJu0YwmyvJ+GXLIHKeDS9y5ceRou3Cgme508OOmdrgvtegWHIece+HI xcnx3+2V77G22gImBWqIxpt6myKw8Xe1aaG6t9TxGEMp8tQbB4x3/n1JWVHNZYI9nTI0vuDutlk 9cKGBHiHFIcLcPaMvMzrCxbqmCxSorFqxTvV3ugZos0pgtRcYraUC7LKwzBBzcQ== X-Gm-Gg: ASbGncvOa+2wIRnRYII/e/YGKwAL0NfuFPwft+A1Lp5crVYuEAC5bpzs4VqmPaHd1SF UHEfKkJZQDGJ4wN2aTKc0q2BOAAAcwZqzpiI687J2+J9G7Lx7rdJRn/d8Iw7wn2XNpsX+FwFECR IrcqsMlaOhARSbaz2GT4T0Qgkq/uctVpw+MeWcff6UvXn4gv/Jkn/iVWSbl2aNDUIx7ncbFvyEc 4vZCbm+YbYSee/eP3lIcNK6fz8o5EC6NLsAUQEyUUs9IcB//09XhZEJnJg5ZiykrpmXGrCxRIms uOMQoEr0VDXqIR+AKQCcwkv+/g== X-Received: by 2002:a05:622a:10b:b0:467:5016:57fa with SMTP id d75a77b69052e-46e12bb32cfmr270188841cf.44.1737469529267; Tue, 21 Jan 2025 06:25:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7FJAOEYs9FmZzN/e90EyVdvrbsiJ/wWpfja5T6g83n9uMLKvVIf9zTACdtczzOkqk6hC5Aw== X-Received: by 2002:a05:622a:10b:b0:467:5016:57fa with SMTP id d75a77b69052e-46e12bb32cfmr270188431cf.44.1737469528860; Tue, 21 Jan 2025 06:25:28 -0800 (PST) Received: from jkangas-thinkpadp1gen3.rmtuswa.csb ([2601:1c2:4301:5e20:98fe:4ecb:4f14:576b]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7be614ef1a5sm555999885a.102.2025.01.21.06.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 06:25:28 -0800 (PST) From: Jared Kangas To: bpf@vger.kernel.org Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, martin.lau@kernel.org, jkangas@redhat.com, ast@kernel.org, johannes.berg@intel.com, kafai@fb.com, songliubraving@fb.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Martin KaFai Lau Subject: [PATCH bpf v2] bpf: remove unnecessary BTF lookups in bpf_sk_storage_tracing_allowed Date: Tue, 21 Jan 2025 06:25:04 -0800 Message-ID: <20250121142504.1369436-1-jkangas@redhat.com> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When loading BPF programs, bpf_sk_storage_tracing_allowed() does a series of lookups to get a type name from the program's attach_btf_id, making the assumption that the type is present in the vmlinux BTF along the way. However, this results in btf_type_by_id() returning a null pointer if a non-vmlinux kernel BTF is attached to. Proof-of-concept on a kernel with CONFIG_IPV6=3Dm: $ cat bpfcrash.c #include #include #include static int bpf(enum bpf_cmd cmd, union bpf_attr *attr) { return syscall(__NR_bpf, cmd, attr, sizeof(*attr)); } int main(void) { const int btf_fd =3D bpf(BPF_BTF_GET_FD_BY_ID, &(union bpf_attr) { .btf_id =3D BTF_ID, }); if (btf_fd < 0) return 1; const int bpf_sk_storage_get =3D 107; const struct bpf_insn insns[] =3D { { .code =3D BPF_JMP | BPF_CALL, .imm =3D bpf_sk_storage_get}, { .code =3D BPF_JMP | BPF_EXIT }, }; return bpf(BPF_PROG_LOAD, &(union bpf_attr) { .prog_type =3D BPF_PROG_TYPE_TRACING, .expected_attach_type =3D BPF_TRACE_FENTRY, .license =3D (unsigned long)"GPL", .insns =3D (unsigned long)&insns, .insn_cnt =3D sizeof(insns) / sizeof(insns[0]), .attach_btf_obj_fd =3D btf_fd, .attach_btf_id =3D TYPE_ID, }); } $ sudo bpftool btf list | grep ipv6 2: name [ipv6] size 928200B $ sudo bpftool btf dump id 2 | awk '$3 ~ /inet6_sock_destruct/' [130689] FUNC 'inet6_sock_destruct' type_id=3D130677 linkage=3Dstatic $ gcc -D_DEFAULT_SOURCE -DBTF_ID=3D2 -DTYPE_ID=3D130689 \ bpfcrash.c -o bpfcrash $ sudo ./bpfcrash This causes a null pointer dereference: Unable to handle kernel NULL pointer dereference at virtual address 000= 0000000000000 Call trace: bpf_sk_storage_tracing_allowed+0x8c/0xb0 P check_helper_call.isra.0+0xa8/0x1730 do_check+0xa18/0xb40 do_check_common+0x140/0x640 bpf_check+0xb74/0xcb8 bpf_prog_load+0x598/0x9a8 __sys_bpf+0x580/0x980 __arm64_sys_bpf+0x28/0x40 invoke_syscall.constprop.0+0x54/0xe8 do_el0_svc+0xb4/0xd0 el0_svc+0x44/0x1f8 el0t_64_sync_handler+0x13c/0x160 el0t_64_sync+0x184/0x188 Resolve this by using prog->aux->attach_func_name and removing the lookups. Fixes: 8e4597c627fb ("bpf: Allow using bpf_sk_storage in FENTRY/FEXIT/RAW_T= P") Link: https://lore.kernel.org/bpf/20250116162356.1054047-1-jkangas@redhat.c= om Suggested-by: Martin KaFai Lau Signed-off-by: Jared Kangas --- V1 -> V2: Used Martin KaFai Lau's suggestion (see lore.kernel.org link), edited patch summary + description accordingly. net/core/bpf_sk_storage.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index 2f4ed83a75ae..72a0dc8b1646 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -352,11 +352,6 @@ const struct bpf_func_proto bpf_sk_storage_delete_prot= o =3D { =20 static bool bpf_sk_storage_tracing_allowed(const struct bpf_prog *prog) { - const struct btf *btf_vmlinux; - const struct btf_type *t; - const char *tname; - u32 btf_id; - if (prog->aux->dst_prog) return false; =20 @@ -371,13 +366,7 @@ static bool bpf_sk_storage_tracing_allowed(const struc= t bpf_prog *prog) return true; case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: - btf_vmlinux =3D bpf_get_btf_vmlinux(); - if (IS_ERR_OR_NULL(btf_vmlinux)) - return false; - btf_id =3D prog->aux->attach_btf_id; - t =3D btf_type_by_id(btf_vmlinux, btf_id); - tname =3D btf_name_by_offset(btf_vmlinux, t->name_off); - return !!strncmp(tname, "bpf_sk_storage", + return !!strncmp(prog->aux->attach_func_name, "bpf_sk_storage", strlen("bpf_sk_storage")); default: return false; --=20 2.47.1