Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318080; b=vnPZflzj9aNOsf1UmEwHrT+7MhAsbi4nFXA8Ecasdv3iE4Stmqpr1z32mfNfjSyHSioD OH5OBzx0F/yfHtoR2WAX3rLGuKJ24Kf/bksQ7l9LImNS/WSqcBBu0EIwSibqUBorVvnjg W7yMCPuZZ2EVN9hMjDmbyHMBLck47TlG5UvwJdAHrbXX4maorjrUooe3NElgUR1yd/cOJ ABDzmBk9R5JxcW2/kWI7vClR/feELpzj5EZN8RE9FeNUR5WwQNdwuvDIDV5KgCqQ0ywzu WU3cuQ6vw0qGxRnrsF9WsG6O2a5y3DHoIl27BaE8QJ8fCJwk1yg3QtlIaUMZr5to2sw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318080; 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=ptALbWnfZUhoY+RBN6wBloUMXZj5aB4i8ZInlbdyIQ0=; b=zDeqPNuTBQfdFPTPFZ5piwGhDDKSJMVdswArdZrPL+/8jBshEvcIxKg0J1xR8r3tsE5r LX0atM8ob5nIu+yvJdTVvFotzaFO/BTBd5mcuisGh/+sllI/emS+Rs9XSO+75+BBYzil8 gJnY2Vl+r4GH9bY4huyDV+9wObkI/aXGhuCMXGPDPphFGr/REsQhCJuWPzayXiB4+WT5j unjWmRqGPnhzc0yPu6yk7+ETifNP6ZrwqCoOVER2ya/RfZ1UOCiBPh0pTa2mUCiALj0oK RNLSNs3stI6MR1+ya/KayXCXckv2Eje6YKgkLK+pzb4CtCcp6iVox5ozPEDCheN2pcw== 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=Qtp6TNQ7; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 3CA76606A332 for ; Thu, 3 Dec 2026 17:14:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3CA76606A332 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=Qtp6TNQ7 Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-491971ac844so75882f8f.2 for ; Thu, 03 Dec 2026 09:14:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796318079; x=1796922879; 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=ptALbWnfZUhoY+RBN6wBloUMXZj5aB4i8ZInlbdyIQ0=; b=Qtp6TNQ7NKlM7Cpc9wbkqQnuoQHismhS+hPzwDMPcHLhukNjxoDul80d+XUjIwxHIc MDrM2TJ338iX59YwZlVR2Hm/HYwY7IuAAP/40p3HtAkkZsUFTIWS32VM2EdSF0qxTCYs /y5pSlgomwKUevM8kErYdPZIdu800DsVeiCF6gyvfzkA/lk7PI5+6gOdeHuNHw6XkBmi C3t+5/TJPfNtPpv5wRPfJzPvMiM3MtsTJDYyZ/77fMaBPn+4FdqJz9WFuaJ5KWZEiuMP zHiB2dA7rv0X98Swy9rgjJa7RWnM37WwcimpFtxIWB37U1NN0+geYCs/VM8cYH+pZw8h /ZXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796318079; x=1796922879; 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=ptALbWnfZUhoY+RBN6wBloUMXZj5aB4i8ZInlbdyIQ0=; b=eXA5PHw7MWRON/5LAcx67FHYcLwuc11yK8PafKfMRkcTkc/Z33lKi6NnkyXvpBhLvk AH50RZHtFrHgYZIMK0sgUg+YB19RP06ADRuozzOUrxHldrToyZHKPhiKTMNAigPDYplU ePrx5zl0wzb3l0+ipvk3Pj1/z0b5MPCDBDycUu/pvUmB0gFntuA+B7fuXYiW0XonG4jy eCflTL48YpSsN1Y2fbvxIQERDs6fwzOdbc/xKzymUPubbQAVfv+VWrIl0bAa8WLt3vdU FE1fcztwihHrlZAQIAlYTDdT5r+b5pPX2W7zCshr9xZWQbNzP+c7UBiLKREy0KmBmEch 8Cqw== X-Gm-Message-State: APuU4Z9g/rQWccs/BwzWvJDHTrwbGzwO6i5aNZxNQcwwt3KqyvhhWgkP gZTwbqOr4izIEF7+GDKzEJQi2nPcWZnFEGw/nD0tjMD0YJ4evsLSB5JP+IZD5vo6 X-Gm-Gg: AZFdTZf0dxol9zyjEucZFPO3qzmcYMVLlTejEucwjvrfcfLU6DGkrM8P0OVAKuFQfqk MdfqwMAcjKupuaJooACCmpKM/4aiJzSeaCb6nrJG8XVLzbeUKomDJ5D3Jl4QrVD+8BkPKHfpaB+ vq0L6O8sIOyuk1gwqYgQ30HvAbyoXMI71Yjvge0rPlbmVwSo8RyeGcvfRX3CvvVqlip31161DMN jjGt9PS0PrW2T25wIN2p1bkR8+Yc5GblUWnDXpiB9+LWJVri1QLjYnT8nwfsni0t2hcoFLWo43j 94QoJRLHZs8NPApZVdi5n0ypGA57wOifpGxq6ZoCpdTzXj7HZ9VuFaACT2dmdvV58lbG6gCt6ie nkSWrXos/FLfBWlIHQdUsubnAjxeNObz2sgE+9Ansp+cv8G93ajR+z2zRqrM059SaSx4CgPKsJ4 7VokA4I/Q6iGs+lFD6fJtZ1+Rqvd2TSpLHH5q3fEhGlM4Gw6JKCIPt9W5ogASrXeXR5TRtqu+bp bJRYcI7QdVB3z3L+QgUG69BeWW8MnNdBc04ACVCJp9vSCykDS05J+MSAPApJERfn/K+OjZKfHVS 6YGsijdqtnX9Lpv2eDIBFV1DA4W3NFO/LFTqK/hYjl0h1/5QNkelkk2qzrIU9oy8Mr3F X-Received: by 2002:a05:600c:4e16:b0:4bb:69b2:d3df with SMTP id 5b1f17b1804b1-4bb88813024mr869941465e9.25.1796318078518; Thu, 03 Dec 2026 09:14:38 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb9067846esm906409615e9.4.2026.12.03.09.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:14:38 -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 v1 bpf-next 1/4] bpf: Refuse the vmlinux prototype for a module function Date: Thu, 3 Dec 2026 17:26:12 +0000 Message-ID: <20261203172617.1137735-2-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261203172617.1137735-1-a.s.protopopov@gmail.com> References: <20261203172617.1137735-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: 290248a5b7d8 ("bpf: Allow to specify kernel module BTFs when attaching BPF programs") Signed-off-by: Anton Protopopov --- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++-------- kernel/bpf/syscall.c | 14 ++++++++++++++ 3 files changed, 48 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/syscall.c b/kernel/bpf/syscall.c index b9cd3d50dee5..e0a04f64f2b1 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3069,6 +3069,20 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr goto put_token; } } + /* + * 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 (btf_is_module(attach_btf) && + btf_is_base_func(attach_btf, attach_btf_id) && + btf_has_own_func(attach_btf, attach_btf_id)) { + btf_put(attach_btf); + err = -EINVAL; + goto put_token; + } } else if (dst_prog->aux->is_entry && dst_prog->aux->func_info[0].type_id != attr->attach_btf_id) { /* what an entry calls is a function of its owner */ -- 2.43.0