Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libkmod: Use correct data types for ELF arithmetic #272

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions libkmod/libkmod-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,9 @@
/* array will be allocated with strings in a single malloc, just free *array */
int kmod_elf_get_modversions(const struct kmod_elf *elf, struct kmod_modversion **array)
{
size_t off, crclen, namlen, verlen;
uint64_t sec_off, size;
size_t i, count, crclen, namlen, verlen;
uint64_t off, sec_off, size;

Check warning on line 545 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L544-L545

Added lines #L544 - L545 were not covered by tests
struct kmod_modversion *a;
int i, count;

elf_get_modversion_lengths(elf, &verlen, &crclen, &namlen);

Expand All @@ -562,6 +561,10 @@
return -EINVAL;

count = size / verlen;
if (count > INT_MAX) {
ELFDBG(elf, "too many modversions: %zu\n", count);
return -EINVAL;
}

*array = a = malloc(sizeof(struct kmod_modversion) * count);
if (*array == NULL)
Expand All @@ -573,7 +576,7 @@
size_t nlen = strnlen(symbol, namlen);

if (nlen == namlen) {
ELFDBG(elf, "symbol name at index %d too long\n", i);
ELFDBG(elf, "symbol name at index %zu too long\n", i);
return -EINVAL;
}

Expand Down Expand Up @@ -704,8 +707,7 @@
uint64_t i, last, off, size;
const char *strings;
struct kmod_modversion *a;
int count;
size_t total_size;
size_t count, total_size;

*array = NULL;

Expand Down Expand Up @@ -740,6 +742,11 @@
}
}

if (count > INT_MAX) {
ELFDBG(elf, "too many symbols: %zu\n", count);
return -EINVAL;
}

/* sizeof(struct kmod_modversion) * count */
if (umulsz_overflow(sizeof(struct kmod_modversion), count, &total_size)) {
return -ENOMEM;
Expand Down Expand Up @@ -973,8 +980,7 @@
uint64_t versionslen, strtablen, symtablen, str_off, sym_off, ver_off;
uint64_t str_sec_off, sym_sec_off;
struct kmod_modversion *a;
size_t namlen, verlen, symlen, crclen;
int i, count, symcount, vercount;
size_t i, count, namlen, vercount, verlen, symcount, symlen, crclen;
bool handle_register_symbols;
uint8_t *visited_versions;
uint64_t *symcrcs;
Expand Down Expand Up @@ -1095,7 +1101,7 @@
if (name_off >= strtablen) {
ELFDBG(elf,
".strtab is %" PRIu64
" bytes, but .symtab entry %d wants to access offset %" PRIu32
" bytes, but .symtab entry %zu wants to access offset %" PRIu32
".\n",
strtablen, i, name_off);
free(visited_versions);
Expand All @@ -1105,7 +1111,7 @@

name = elf_get_mem(elf, str_off + name_off);
if (name[0] == '\0') {
ELFDBG(elf, "empty symbol name at index %d\n", i);
ELFDBG(elf, "empty symbol name at index %zu\n", i);

Check warning on line 1114 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L1114

Added line #L1114 was not covered by tests
continue;
}

Expand All @@ -1128,7 +1134,7 @@
nlen = strnlen(name, namlen);

if (nlen == namlen) {
ELFDBG(elf, "symbol name at index %d too long\n",
ELFDBG(elf, "symbol name at index %zu too long\n",

Check warning on line 1137 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L1137

Added line #L1137 was not covered by tests
i);
free(visited_versions);
free(symcrcs);
Expand All @@ -1140,6 +1146,14 @@
}
}

if (count > INT_MAX) {
ELFDBG(elf, "too many symbols: %zu\n", count);
free(visited_versions);
free(symcrcs);
*array = NULL;
return -EINVAL;

Check warning on line 1154 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L1150-L1154

Added lines #L1150 - L1154 were not covered by tests
}

if (count == 0) {
free(visited_versions);
free(symcrcs);
Expand Down Expand Up @@ -1201,7 +1215,7 @@

name = elf_get_mem(elf, str_off + name_off);
if (name[0] == '\0') {
ELFDBG(elf, "empty symbol name at index %d\n", i);
ELFDBG(elf, "empty symbol name at index %zu\n", i);

Check warning on line 1218 in libkmod/libkmod-elf.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-elf.c#L1218

Added line #L1218 was not covered by tests
continue;
}

Expand Down
Loading