Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: check license compatibility when building modules

Modules that uses GPL symbols can no longer be build with kbuild,
the build will fail during the modpost step.
When a GPL-incompatible module uses a EXPORT_SYMBOL_GPL_FUTURE symbol
then warn during modpost so author are actually notified.

The actual license compatibility check is shared with the kernel
to make sure it is in sync.

Patch originally from: Andreas Gruenbacher <agruen@suse.de> and
Ram Pai <linuxram@us.ibm.com>

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>

+85 -12
+14
include/linux/license.h
··· 1 + #ifndef __LICENSE_H 2 + #define __LICENSE_H 3 + 4 + static inline int license_is_gpl_compatible(const char *license) 5 + { 6 + return (strcmp(license, "GPL") == 0 7 + || strcmp(license, "GPL v2") == 0 8 + || strcmp(license, "GPL and additional rights") == 0 9 + || strcmp(license, "Dual BSD/GPL") == 0 10 + || strcmp(license, "Dual MIT/GPL") == 0 11 + || strcmp(license, "Dual MPL/GPL") == 0); 12 + } 13 + 14 + #endif
+1 -10
kernel/module.c
··· 43 43 #include <asm/uaccess.h> 44 44 #include <asm/semaphore.h> 45 45 #include <asm/cacheflush.h> 46 + #include <linux/license.h> 46 47 47 48 #if 0 48 49 #define DEBUGP printk ··· 1247 1246 if (m == 0) 1248 1247 mod->init_text_size = mod->init_size; 1249 1248 } 1250 - } 1251 - 1252 - static inline int license_is_gpl_compatible(const char *license) 1253 - { 1254 - return (strcmp(license, "GPL") == 0 1255 - || strcmp(license, "GPL v2") == 0 1256 - || strcmp(license, "GPL and additional rights") == 0 1257 - || strcmp(license, "Dual BSD/GPL") == 0 1258 - || strcmp(license, "Dual MIT/GPL") == 0 1259 - || strcmp(license, "Dual MPL/GPL") == 0); 1260 1249 } 1261 1250 1262 1251 static void set_license(struct module *mod, const char *license)
+69 -2
scripts/mod/modpost.c
··· 13 13 14 14 #include <ctype.h> 15 15 #include "modpost.h" 16 + #include "../../include/linux/license.h" 16 17 17 18 /* Are we using CONFIG_MODVERSIONS? */ 18 19 int modversions = 0; ··· 100 99 101 100 /* add to list */ 102 101 mod->name = p; 102 + mod->gpl_compatible = -1; 103 103 mod->next = modules; 104 104 modules = mod; 105 105 ··· 495 493 return string; 496 494 } 497 495 498 - static char *get_modinfo(void *modinfo, unsigned long modinfo_len, 499 - const char *tag) 496 + static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, 497 + const char *tag, char *info) 500 498 { 501 499 char *p; 502 500 unsigned int taglen = strlen(tag); 503 501 unsigned long size = modinfo_len; 502 + 503 + if (info) { 504 + size -= info - (char *)modinfo; 505 + modinfo = next_string(info, &size); 506 + } 504 507 505 508 for (p = modinfo; p; p = next_string(p, &size)) { 506 509 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 507 510 return p + taglen + 1; 508 511 } 509 512 return NULL; 513 + } 514 + 515 + static char *get_modinfo(void *modinfo, unsigned long modinfo_len, 516 + const char *tag) 517 + 518 + { 519 + return get_next_modinfo(modinfo, modinfo_len, tag, NULL); 510 520 } 511 521 512 522 /** ··· 995 981 { 996 982 const char *symname; 997 983 char *version; 984 + char *license; 998 985 struct module *mod; 999 986 struct elf_info info = { }; 1000 987 Elf_Sym *sym; ··· 1009 994 if (is_vmlinux(modname)) { 1010 995 have_vmlinux = 1; 1011 996 mod->skip = 1; 997 + } 998 + 999 + license = get_modinfo(info.modinfo, info.modinfo_len, "license"); 1000 + while (license) { 1001 + if (license_is_gpl_compatible(license)) 1002 + mod->gpl_compatible = 1; 1003 + else { 1004 + mod->gpl_compatible = 0; 1005 + break; 1006 + } 1007 + license = get_next_modinfo(info.modinfo, info.modinfo_len, 1008 + "license", license); 1012 1009 } 1013 1010 1014 1011 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { ··· 1077 1050 } 1078 1051 strncpy(buf->p + buf->pos, s, len); 1079 1052 buf->pos += len; 1053 + } 1054 + 1055 + void check_license(struct module *mod) 1056 + { 1057 + struct symbol *s, *exp; 1058 + 1059 + for (s = mod->unres; s; s = s->next) { 1060 + if (mod->gpl_compatible == 1) { 1061 + /* GPL-compatible modules may use all symbols */ 1062 + continue; 1063 + } 1064 + exp = find_symbol(s->name); 1065 + if (!exp || exp->module == mod) 1066 + continue; 1067 + const char *basename = strrchr(mod->name, '/'); 1068 + if (basename) 1069 + basename++; 1070 + switch (exp->export) { 1071 + case export_gpl: 1072 + fatal("modpost: GPL-incompatible module %s " 1073 + "uses GPL-only symbol '%s'\n", 1074 + basename ? basename : mod->name, 1075 + exp->name); 1076 + break; 1077 + case export_gpl_future: 1078 + warn("modpost: GPL-incompatible module %s " 1079 + "uses future GPL-only symbol '%s'\n", 1080 + basename ? basename : mod->name, 1081 + exp->name); 1082 + break; 1083 + case export_plain: /* ignore */ break; 1084 + case export_unknown: /* ignore */ break; 1085 + } 1086 + } 1080 1087 } 1081 1088 1082 1089 /** ··· 1384 1323 1385 1324 while (optind < argc) { 1386 1325 read_symbols(argv[optind++]); 1326 + } 1327 + 1328 + for (mod = modules; mod; mod = mod->next) { 1329 + if (mod->skip) 1330 + continue; 1331 + check_license(mod); 1387 1332 } 1388 1333 1389 1334 for (mod = modules; mod; mod = mod->next) {
+1
scripts/mod/modpost.h
··· 100 100 struct module { 101 101 struct module *next; 102 102 const char *name; 103 + int gpl_compatible; 103 104 struct symbol *unres; 104 105 int seen; 105 106 int skip;