From nobody Thu Dec 18 12:33:52 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25B6B3191D3 for ; Mon, 3 Nov 2025 16:20:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762186811; cv=none; b=nPpMYdkODcgx3liQ+vaqWhC+Frd9cAYW1+QP/I4JoCAocZdinJbEkJyd/8fjIjSSqe0Rv1k7uqdbF5/c9VKQ1ChbD7gAHUAaX1I/3fEkI8YFpClXnDiRG1N5Aj+Z57//5w9lrnRHY0oG2p4wEZrYeS2S9UyALCexMV/VdafzXxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762186811; c=relaxed/simple; bh=tuyLXUvi/ESqwW2v6QjC+olXsEHZK/Kv3IdYAAA8beM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Rl7X5sajhNKZ4PwEMw2uFUiCcEd1BxW7O7stb7dmp7v6Bz/FRRLTkYS6bC5saDFmKPjCboNBSy9gUtyPyTldQRmGRmKeW0VDnSUDMUliEFbU820jvOvLnd8UvaXmf2bWxEX9LPslRtmXgllVDrHEwZytOpOxVpulaPmN5KJgGS0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RVmVEh0L; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RVmVEh0L" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-429c5c8ae3bso1950746f8f.0 for ; Mon, 03 Nov 2025 08:20:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1762186806; x=1762791606; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6+VzLCYAN+u3C81ZeCic6e0B5W6UCDTRzeShogr9CFM=; b=RVmVEh0LByx8eUVYFmsZLGJ3IFZZf5bQUYcdmr1Bwf/mlp7+Zkzrv1gTT/3WJdEocq 6h/uCbp7EvTatnQCw5LU+KTO42vlJPY/qeba7nZVmwhQImN4Sj7js/6soiraBuSQRJxJ 2xKSQ5yT2l2nFuVPBGU4Z7Hx73S4tM/f+RCJJ7Ukk5tj/pOzN5WcA/y2CSWvRtfovJIE RiXgpyb2Qg3Z0ZuGCeUJl4KUs/SxWsS99zWpVVvYk99u4pGnGIXpS3y2tk6uK3/XYMW0 3E7K3zLweCq4QBmy0rrFz21akcPmj9utMTJkVAbPo31kvqqVorOFNhaSyYYz/0C//T7T 4qSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762186806; x=1762791606; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6+VzLCYAN+u3C81ZeCic6e0B5W6UCDTRzeShogr9CFM=; b=c5/KmNTdx6cFBWpHuiAR9oghUmQtzoEk3ItN0HHEmmfEhizjQHi15nrWFAdbLiol91 pl2MsVy/rohwZTElqjaEl2LVQ428jIj7cwPqrEfz2u6fqRNDgecFqr9s7zG3gCuKV5ij PYuAU1h6EaVjd74aKsi0wNHKM0kOmDzOYxHAhKS96uGyLSLHL1tF1e4IlxVvs67LJaPI 60zhkkhEMe4MzFyrafpd1zIOdT4MjchFx7gZMomjSBhyzaxVC/T1iaWsypA6rlHFqwuc szHd2Lu+VW44Fbms6+qokj6IYLB4GdqReTlaqUHYUq/gnXfJBw9FU98f9RZy5fCPdI3c +AKA== X-Forwarded-Encrypted: i=1; AJvYcCWl7AVWFLKCB2LTbJDkJ6BHCuh5VeX8LZDWjL93p7artxyDapWio0Bt9FO1ZYjXJgqM2RtE0XNz/LuWxgw=@vger.kernel.org X-Gm-Message-State: AOJu0YxnmKxBkC6gZutLVujafiHgOGnyTlAOgowMkpJds3eYGM1ETerj Ny/vTo2V1VALwJU8swvnrFQwWVKliZUCN9zGBKUF1GKTytqF5G4+42E5tU40ucN1MnDVQu3hEl6 1apg2Pl3W8IgMCPbezg== X-Google-Smtp-Source: AGHT+IHsA0v7E+SnXH75SrlLLdZznHgP2mGDuTma0AhnTccBK/k4OR/i3RdgETyahE/jGzsOOaCC+90khENn44c= X-Received: from wmbjt1.prod.google.com ([2002:a05:600c:5681:b0:477:1186:dfaf]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:22c7:b0:429:d79f:c86b with SMTP id ffacd0b85a97d-429d79fcb0cmr2277909f8f.58.1762186806407; Mon, 03 Nov 2025 08:20:06 -0800 (PST) Date: Mon, 3 Nov 2025 16:19:50 +0000 In-Reply-To: <20251103161954.1351784-1-sidnayyar@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251103161954.1351784-1-sidnayyar@google.com> X-Mailer: git-send-email 2.51.1.930.gacf6e81ea2-goog Message-ID: <20251103161954.1351784-5-sidnayyar@google.com> Subject: [PATCH v3 4/8] module loader: use kflagstab instead of *_gpl sections From: Siddharth Nayyar To: petr.pavlu@suse.com, corbet@lwn.net Cc: arnd@arndb.de, linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, mcgrof@kernel.org, nathan@kernel.org, nicolas.schier@linux.dev, samitolvanen@google.com, sidnayyar@google.com, maennich@google.com, gprocida@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Read __kflagstab section for vmlinux and modules to determine whether kernel symbols are GPL only. Signed-off-by: Siddharth Nayyar Reviewed-by: Petr Pavlu --- include/linux/module.h | 1 + kernel/module/internal.h | 1 + kernel/module/main.c | 55 +++++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 3319a5269d28..9ba6ce433ac3 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -415,6 +415,7 @@ struct module { /* Exported symbols */ const struct kernel_symbol *syms; const u32 *crcs; + const u8 *flagstab; unsigned int num_syms; =20 #ifdef CONFIG_ARCH_USES_CFI_TRAPS diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 618202578b42..69b84510e097 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -57,6 +57,7 @@ extern const struct kernel_symbol __start___ksymtab_gpl[]; extern const struct kernel_symbol __stop___ksymtab_gpl[]; extern const u32 __start___kcrctab[]; extern const u32 __start___kcrctab_gpl[]; +extern const u8 __start___kflagstab[]; =20 #define KMOD_PATH_LEN 256 extern char modprobe_path[]; diff --git a/kernel/module/main.c b/kernel/module/main.c index c66b26184936..4197af526087 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -87,7 +88,7 @@ struct mod_tree_root mod_tree __cacheline_aligned =3D { struct symsearch { const struct kernel_symbol *start, *stop; const u32 *crcs; - enum mod_license license; + const u8 *flagstab; }; =20 /* @@ -364,19 +365,21 @@ static bool find_exported_symbol_in_section(const str= uct symsearch *syms, struct find_symbol_arg *fsa) { struct kernel_symbol *sym; - - if (!fsa->gplok && syms->license =3D=3D GPL_ONLY) - return false; + u8 sym_flags; =20 sym =3D bsearch(fsa->name, syms->start, syms->stop - syms->start, sizeof(struct kernel_symbol), cmp_name); if (!sym) return false; =20 + sym_flags =3D *(syms->flagstab + (sym - syms->start)); + if (!fsa->gplok && (sym_flags & KSYM_FLAG_GPL_ONLY)) + return false; + fsa->owner =3D owner; fsa->crc =3D symversion(syms->crcs, sym - syms->start); fsa->sym =3D sym; - fsa->license =3D syms->license; + fsa->license =3D (sym_flags & KSYM_FLAG_GPL_ONLY) ? GPL_ONLY : NOT_GPL_ON= LY; =20 return true; } @@ -387,36 +390,31 @@ static bool find_exported_symbol_in_section(const str= uct symsearch *syms, */ bool find_symbol(struct find_symbol_arg *fsa) { - static const struct symsearch arr[] =3D { - { __start___ksymtab, __stop___ksymtab, __start___kcrctab, - NOT_GPL_ONLY }, - { __start___ksymtab_gpl, __stop___ksymtab_gpl, - __start___kcrctab_gpl, - GPL_ONLY }, + const struct symsearch syms =3D { + .start =3D __start___ksymtab, + .stop =3D __stop___ksymtab, + .crcs =3D __start___kcrctab, + .flagstab =3D __start___kflagstab, }; struct module *mod; - unsigned int i; =20 - for (i =3D 0; i < ARRAY_SIZE(arr); i++) - if (find_exported_symbol_in_section(&arr[i], NULL, fsa)) - return true; + if (find_exported_symbol_in_section(&syms, NULL, fsa)) + return true; =20 list_for_each_entry_rcu(mod, &modules, list, lockdep_is_held(&module_mutex)) { - struct symsearch arr[] =3D { - { mod->syms, mod->syms + mod->num_syms, mod->crcs, - NOT_GPL_ONLY }, - { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms, - mod->gpl_crcs, - GPL_ONLY }, + const struct symsearch syms =3D { + .start =3D mod->syms, + .stop =3D mod->syms + mod->num_syms, + .crcs =3D mod->crcs, + .flagstab =3D mod->flagstab, }; =20 if (mod->state =3D=3D MODULE_STATE_UNFORMED) continue; =20 - for (i =3D 0; i < ARRAY_SIZE(arr); i++) - if (find_exported_symbol_in_section(&arr[i], mod, fsa)) - return true; + if (find_exported_symbol_in_section(&syms, mod, fsa)) + return true; } =20 pr_debug("Failed to find symbol %s\n", fsa->name); @@ -2607,6 +2605,7 @@ static int find_module_sections(struct module *mod, s= truct load_info *info) sizeof(*mod->gpl_syms), &mod->num_gpl_syms); mod->gpl_crcs =3D section_addr(info, "__kcrctab_gpl"); + mod->flagstab =3D section_addr(info, "__kflagstab"); =20 #ifdef CONFIG_CONSTRUCTORS mod->ctors =3D section_objs(info, ".ctors", @@ -2810,8 +2809,12 @@ static int move_module(struct module *mod, struct lo= ad_info *info) return ret; } =20 -static int check_export_symbol_versions(struct module *mod) +static int check_export_symbol_sections(struct module *mod) { + if (mod->num_syms && !mod->flagstab) { + pr_err("%s: no flags for exported symbols\n", mod->name); + return -ENOEXEC; + } #ifdef CONFIG_MODVERSIONS if ((mod->num_syms && !mod->crcs) || (mod->num_gpl_syms && !mod->gpl_crcs)) { @@ -3427,7 +3430,7 @@ static int load_module(struct load_info *info, const = char __user *uargs, if (err) goto free_unload; =20 - err =3D check_export_symbol_versions(mod); + err =3D check_export_symbol_sections(mod); if (err) goto free_unload; =20 --=20 2.51.1.930.gacf6e81ea2-goog