]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
UBUNTU: SAUCE: modpost: support arbitrary symbol length in modversion
authorGary Guo <gary@garyguo.net>
Wed, 11 Jan 2023 16:11:51 +0000 (16:11 +0000)
committerAndrea Righi <andrea.righi@canonical.com>
Tue, 14 Mar 2023 15:45:28 +0000 (16:45 +0100)
BugLink: https://bugs.launchpad.net/bugs/2007654
Currently modversion uses a fixed size array of size (64 - sizeof(long))
to store symbol names, thus placing a hard limit on length of symbols.
Rust symbols (which encodes crate and module names) can be quite a bit
longer. The length limit in kallsyms is increased to 512 for this reason.

It's a waste of space to simply expand the fixed array size to 512 in
modversion info entries. I therefore make it variably sized, with offset
to the next entry indicated by the initial "next" field.

In addition to supporting longer-than-56/60 byte symbols, this patch also
reduce the size for short symbols by getting rid of excessive 0 paddings.
There are still some zero paddings to ensure "next" and "crc" fields are
properly aligned.

This patch does have a tiny drawback that it makes ".mod.c" files generated
a bit less easy to read, as code like

"\x08\x00\x00\x00\x78\x56\x34\x12"
"symbol\0\0"

is generated as opposed to

{ 0x12345678, "symbol" },

because the structure is now variable-length. But hopefully nobody reads
the generated file :)

Link: b8a94bfb3395 ("kallsyms: increase maximum kernel symbol length to 512")
Link: https://github.com/Rust-for-Linux/linux/pull/379
Signed-off-by: Gary Guo <gary@garyguo.net>
(backported from https://lore.kernel.org/lkml/20230111161155.1349375-1-gary@garyguo.net/)
[ fix build error on s390x: always use a variable with TO_NATIVE() ]
[ add dummy NULL entry at the end of the modversion array ]
Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
arch/powerpc/kernel/module_64.c
include/linux/module.h
kernel/module/version.c
scripts/export_report.pl
scripts/mod/modpost.c

index ff045644f13ffffe5e83f693ebd38752d11cbfc9..ea6c830ed1e7e896631cdcfd9ab4fbf4ac4a6363 100644 (file)
@@ -234,12 +234,13 @@ static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
 static void dedotify_versions(struct modversion_info *vers,
                              unsigned long size)
 {
-       struct modversion_info *end;
+       struct modversion_info *end = (void *)vers + size;
 
-       for (end = (void *)vers + size; vers < end; vers++)
+       for (; vers < end && vers->next; vers = (void *)vers + vers->next) {
                if (vers->name[0] == '.') {
                        memmove(vers->name, vers->name+1, strlen(vers->name));
                }
+       }
 }
 
 /*
index 8c5909c0076c6e1e75fde683574769085cef6cc5..4744901bdf63f8c0b1641da7b23a5e6f32ccea50 100644 (file)
 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
 
 struct modversion_info {
-       unsigned long crc;
-       char name[MODULE_NAME_LEN];
-};
+       /* Offset of the next modversion entry in relation to this one. */
+       u32 next;
+       u32 crc;
+       char name[0];
+} __packed;
 
 struct module;
 struct exception_table_entry;
index 53f43ac5a73e9d537a9e95ff97728a51fad0e797..5528f98c42dc2b052c2b5e9ddff20c90f5a7693e 100644 (file)
@@ -17,32 +17,30 @@ int check_version(const struct load_info *info,
 {
        Elf_Shdr *sechdrs = info->sechdrs;
        unsigned int versindex = info->index.vers;
-       unsigned int i, num_versions;
-       struct modversion_info *versions;
+       struct modversion_info *versions, *end;
+       u32 crcval;
 
        /* Exporting module didn't supply crcs?  OK, we're already tainted. */
        if (!crc)
                return 1;
+       crcval = *crc;
 
        /* No versions at all?  modprobe --force does this. */
        if (versindex == 0)
                return try_to_force_load(mod, symname) == 0;
 
        versions = (void *)sechdrs[versindex].sh_addr;
-       num_versions = sechdrs[versindex].sh_size
-               / sizeof(struct modversion_info);
+       end = (void *)versions + sechdrs[versindex].sh_size;
 
-       for (i = 0; i < num_versions; i++) {
-               u32 crcval;
-
-               if (strcmp(versions[i].name, symname) != 0)
+       for (; versions < end && versions->next;
+              versions = (void *)versions + versions->next) {
+               if (strcmp(versions->name, symname) != 0)
                        continue;
 
-               crcval = *crc;
-               if (versions[i].crc == crcval)
+               if (versions->crc == crcval)
                        return 1;
-               pr_debug("Found checksum %X vs module %lX\n",
-                        crcval, versions[i].crc);
+               pr_debug("Found checksum %X vs module %X\n",
+                        crcval, versions->crc);
                goto bad_version;
        }
 
index feb3d5542a62d90b7af4f041d98a3c4b5ac386c0..1117646f3141be07caa0db3172f592bdf683a159 100755 (executable)
@@ -116,18 +116,19 @@ foreach my $thismod (@allcfiles) {
        while ( <$module> ) {
                chomp;
                if ($state == 0) {
-                       $state = 1 if ($_ =~ /static const struct modversion_info/);
+                       $state = 1 if ($_ =~ /static const char ____versions/);
                        next;
                }
                if ($state == 1) {
-                       $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
+                       $state = 2 if ($_ =~ /__used __section\("__versions"\)/);
                        next;
                }
                if ($state == 2) {
-                       if ( $_ !~ /0x[0-9a-f]+,/ ) {
+                       if ( $_ !~ /\\0"/ ) {
+                               last if ($_ =~ /;/);
                                next;
                        }
-                       my $sym = (split /([,"])/,)[4];
+                       my $sym = (split /(["\\])/,)[2];
                        my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
                        $SYMBOL{ $sym } =  [ $module, $value+1, $symbol, $gpl];
                        push(@{$MODULE{$thismod}} , $sym);
index efff8078e39581349f72fd8fbe6e2c062e36d372..55335ae98f4f4e96b28429841fd729cb25d92ba3 100644 (file)
@@ -2046,13 +2046,17 @@ static void add_exported_symbols(struct buffer *buf, struct module *mod)
 static void add_versions(struct buffer *b, struct module *mod)
 {
        struct symbol *s;
+       unsigned int name_len;
+       unsigned int name_len_padded;
+       unsigned int tmp;
+       unsigned char *tmp_view = (unsigned char *)&tmp;
 
        if (!modversions)
                return;
 
        buf_printf(b, "\n");
-       buf_printf(b, "static const struct modversion_info ____versions[]\n");
-       buf_printf(b, "__used __section(\"__versions\") = {\n");
+       buf_printf(b, "static const char ____versions[]\n");
+       buf_printf(b, "__used __section(\"__versions\") =\n");
 
        list_for_each_entry(s, &mod->unresolved_symbols, list) {
                if (!s->module)
@@ -2062,16 +2066,27 @@ static void add_versions(struct buffer *b, struct module *mod)
                                s->name, mod->name);
                        continue;
                }
-               if (strlen(s->name) >= MODULE_NAME_LEN) {
-                       error("too long symbol \"%s\" [%s.ko]\n",
-                             s->name, mod->name);
-                       break;
-               }
-               buf_printf(b, "\t{ %#8x, \"%s\" },\n",
-                          s->crc, s->name);
+               name_len = strlen(s->name);
+               name_len_padded = (name_len + 1 + 3) & ~3;
+
+               /* Offset to next entry */
+               tmp = 8 + name_len_padded;
+               tmp = TO_NATIVE(tmp);
+               buf_printf(b, "\t\"\\x%02x\\x%02x\\x%02x\\x%02x",
+                          tmp_view[0], tmp_view[1], tmp_view[2], tmp_view[3]);
+
+               tmp = TO_NATIVE(s->crc);
+               buf_printf(b, "\\x%02x\\x%02x\\x%02x\\x%02x\"\n",
+                          tmp_view[0], tmp_view[1], tmp_view[2], tmp_view[3]);
+
+               buf_printf(b, "\t\"%s", s->name);
+               for (; name_len < name_len_padded; name_len++)
+                       buf_printf(b, "\\0");
+               buf_printf(b, "\"\n");
        }
 
-       buf_printf(b, "};\n");
+       /* Always end with a NULL entry */
+       buf_printf(b, "\t\"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\";\n");
 }
 
 static void add_depends(struct buffer *b, struct module *mod)