Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398532; b=jYzD41Up2wF1al/gnxhSVmq03ir7fH1soRyoqQJ6p5/E+WrvhBewOcy28Gqz2IDT7F49 A/7kNY9m0v6YBC6vXZZokvNlPQLmX3Uqcx4i4sM7tNlhyjkVCKMJUgj4wD31J5OZpaOIp 6qXcx8sXGId0r7/e/qm+LDcIjdFubDiYjTasXobGDrQTDTRWp3VxemvMnXJB+FNT+1RZy E/v5BxlCymwxQnTSPQR49hUORLUGCXrRZEkbxBp0+x1qP4amJ0+b9BpQ3ooKjCUzP1MLW W/ERVc1OJ7nEXDwUTea5e9EZDhoUnHhKi8oK6qaXEgk6C0rpLnTcnSUarYFGRTsS7Bw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398532; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=YTH7DT+aKrkjlBpIBPcKk9l9IoNp1XSc9OWwVpzYMzM=; b=w6tqFGzOwyyo0GruN1b+/wy3F0jG++KkQe97JabYzDAnoSVpt3xJmUqjAudG+NS8PRzq OSutbp90L0/jw+3J1xe8O9pro3FF/BTTtlgGgkiJbUeu8/6HfTnFl47Yqe7n8PKCEhs3v Th1H7mo0TVcM3vV4GFZ54x3xijhllUH3ScCc1GGwM3k32o8M7B+xfvudGwPlicKnbMmXi lBq7dLSPm43mGao8ztmYS579ZwWANNiv0Yvw19L3tXu54LKQMK7FmNCDRTM8U9+T1rq3o PK5S/LXrFXm8BkCiOURAhfT9s0L+CLczJ5YrfkI5l89UZ6xFsFMWtnK190EyGF4zEYw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uC034cFJ; arc=none smtp.remote-ip=209.85.221.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EAFD606A330 for ; Fri, 4 Dec 2026 15:35:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9EAFD606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uC034cFJ Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-490d59c3edaso736508f8f.1 for ; Fri, 04 Dec 2026 07:35:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796398530; x=1797003330; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=YTH7DT+aKrkjlBpIBPcKk9l9IoNp1XSc9OWwVpzYMzM=; b=uC034cFJEyEg1ASj84WEkPuvMl4Pcip/nCF+3012CvkN4uGIVWNJtUUcyNFIAlHCwQ UFdUHdhp9FNZ0Ij7j6YE3LKbBawW5jUqWhlfyh/IzO0Vw+MCXryfsXS+kkTtWyQ5h5GV /OMil+ffWp7WXO3R6vZZw+dmRfKDTAoSncvS6045Zt8lLmAooFxJZBFmd2N5SDy3Y+3m dNdwoJ3WEh5P4pbCo/RHEXE/thbmlFIFUZUPrtkdGU0ujarFRWFTctMha2UrXswLXWg/ U8Tyc9giBbHcYF8HUr4NmaTfaZG9xnwE6gTYt151FoqdsXi4o3YBb4skDUTXS9O4zao2 zFwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796398530; x=1797003330; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=YTH7DT+aKrkjlBpIBPcKk9l9IoNp1XSc9OWwVpzYMzM=; b=k87NprW4TtzqmA/pva/n9QQGF4SF3idTG7+l1cPkmOhLkSNxwgC9vnfhMF0PMI9uoj g+TIhINiUsA8HlahzxR49xJQ1kkj9uGIwj8rcnue45uRa2WWTLmdNVN+JDj0IghPG3dg znYKA8Tkx8z5ILnBEAWE31qgmqUOpx9kKxbE2JMk9P+2Y5Y5kevEaePm2yLwnkIHk9q6 0jiCgsr6b13sj/Suaex1ClBrDTSSkEILQECOLT8ZOSrV0drWPB9ANBBrjzlkE1odQX72 QWJwzItYcuneOz4QAHr2jQQixdJtNBXUvoNZmBLBmPbhaIuLfU91o905u58wYNClbSDw WhuQ== X-Gm-Message-State: AB4q+uFJOSHOyi7bb5uUMV2Ug7gUEMeXJIrGppHttNtfx13dAn5/Gj8P PsfzAXegDpZc4Wtx7mxcyTsfUqaFNPML4fqOM5ZJi2eKLmIPzO4prtlkT4YkQzCa X-Gm-Gg: AZFdTZcQPpeh8SrVOJ6dNkSGlSreCKh5Nkn+bkxqNQhJBIvgaose6ULwdLo1+lq615c ubFGZpqbQOHjVFIclVgp3BWAbFkwYHyu/cQ1mjQ0kIL6i4gBGqyXqD0YbSYKiyvb20OY2TLkMzk Tk4JYraDaDi0suYjjrx27KciHQ1H4VTPqWoUhez33acfKboVns4F4qktFTpsaFr8phJagHa1V2b MyMbSaKIj3/L0J1Q4wXXI3ExwBGS0XpIDW67OmXJ/4/L66q+eNrTgu9LB+UrlEnuGVKpofF9EdX WY3wa32KmW0LU4cjMWLVLIHwFS+yV2Vfsql1K+rke2tlqukJGtfR3tnWIYjp3Kv7UUq34+fPTrJ a9RO5n+WRkf87w/I6I1X7yJETlbhz9OVzjnuFtunLqaZ7U1HKYr5WC8PX6xe0fZw/WnExmtukPT zFVsWkPTLzBfOAawcDWkdTC0pS9x2SzsAlUUrHiJRe27ofUwSHtDd8NkHjGGR7j82HDsVejTLOX VS6UsSQmpJlWRBLJk0I4Pe71QragXKZf0h/0CgXE4ttkXpCVQ+1nTNCOThScqQN+PL+37M2PjC/ nWR//YSYBVjVnDeLUJFmktdmCsn/Y5AUksAIYA3xcvcg5B0= X-Received: by 2002:a05:6000:18a6:b0:492:b1c5:6db6 with SMTP id ffacd0b85a97d-4d1e8dbce01mr6315101f8f.6.1796398530020; Fri, 04 Dec 2026 07:35:30 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d174fe6fbcsm5922841f8f.58.2026.12.04.07.35.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 07:35:29 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , John Fastabend , Viktor Malik Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 1/4] bpf: Refuse the vmlinux prototype for a module function Date: Fri, 4 Dec 2026 15:47:00 +0000 Message-ID: <20261204154705.1411391-2-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204154705.1411391-1-a.s.protopopov@gmail.com> References: <20261204154705.1411391-1-a.s.protopopov@gmail.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A program can be loaded with the BTF of a module and the ID of a vmlinux function. It then attaches to the function of the same name in the module, but with the prototype of the vmlinux one. When the two prototypes differ, the program reads the arguments as wrong types. And if the module function takes more arguments, the trampoline doesn't save them, so the BPF programs can clobber them. Refuse the ID of a vmlinux function with the BTF of a module when the module has its own function of that name, which might have another prototype. A module function of the same prototype usually has no ID of its own, as the dedup of BTF merges it into the vmlinux one, and is still attached to with the vmlinux ID. Fixes: 31bf1dbccfb0 ("bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules") Signed-off-by: Anton Protopopov --- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++-------- kernel/bpf/verifier.c | 11 +++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 6014282feacb..d82efac8a4d0 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -254,6 +254,8 @@ int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); bool btf_type_is_void(const struct btf_type *t); bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t); s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); +bool btf_is_base_func(const struct btf *btf, u32 id); +bool btf_has_own_func(const struct btf *btf, u32 id); s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); struct btf *btf_get_module_btf(const struct module *module); __u32 btf_relocate_id(const struct btf *btf, __u32 id); diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 3597ba1b5594..12cf8b9c7d3c 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -664,19 +664,13 @@ static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *name return btf_nr_types(btf); } -s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) +/* Look up only the types of btf, not of its base */ +static s32 btf_find_by_name_kind_own(const struct btf *btf, const char *name, u8 kind) { - const struct btf *base_btf = btf_base_btf(btf); const struct btf_type *t; const char *tname; s32 id, total; - if (base_btf) { - id = btf_find_by_name_kind(base_btf, name, kind); - if (id > 0) - return id; - } - total = btf_nr_types(btf); if (btf->named_start_id > 0 && name[0]) { id = btf_find_by_name_kind_bsearch(btf, name); @@ -707,6 +701,36 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) return -ENOENT; } +s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind) +{ + const struct btf *base_btf = btf_base_btf(btf); + s32 id; + + if (base_btf) { + id = btf_find_by_name_kind(base_btf, name, kind); + if (id > 0) + return id; + } + + return btf_find_by_name_kind_own(btf, name, kind); +} + +/* Whether id is a function of the base of the split btf */ +bool btf_is_base_func(const struct btf *btf, u32 id) +{ + return btf->base_btf && id < btf->start_id && + btf_type_is_func(btf_type_by_id(btf, id)); +} + +/* Whether btf has a function of its own with the name of the type id */ +bool btf_has_own_func(const struct btf *btf, u32 id) +{ + const struct btf_type *t = btf_type_by_id(btf, id); + + return t && btf_find_by_name_kind_own(btf, btf_name_by_offset(btf, t->name_off), + BTF_KIND_FUNC) > 0; +} + s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p) { struct btf *btf; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 832f2efcbce4..a6bff31c462c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -26209,6 +26209,17 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); return -EINVAL; } + /* + * The target is looked up by name in the module, so a vmlinux function + * might have the wrong prototype for it. Take it only if the module has + * no function of its own of that name: the dedup of BTF merges a module + * function into the vmlinux one of the same prototype. + */ + if (!tgt_prog && btf_is_base_func(btf, btf_id) && btf_has_own_func(btf, btf_id)) { + bpf_log(log, "Cannot attach to vmlinux target %u:%s with the BTF of a module.\n", + btf_id, tname); + return -EINVAL; + } if (tgt_prog) { struct bpf_prog_aux *aux = tgt_prog->aux; enum bpf_cgroup_storage_type stype; -- 2.43.0