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: Fix overflow in kmod_module_hex_to_str #236

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
35 changes: 18 additions & 17 deletions libkmod/libkmod-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <sys/types.h>
#include <sys/wait.h>

#include <shared/strbuf.h>
#include <shared/util.h>

#include "libkmod.h"
Expand Down Expand Up @@ -1798,29 +1799,29 @@

static char *kmod_module_hex_to_str(const char *hex, size_t len)
{
char *str;
int i;
int j;
static const char digits[] = "0123456789ABCDEF";
struct strbuf sbuf;
const size_t line_limit = 20;
size_t str_len;

str_len = len * 3; /* XX: or XX\0 */
str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */
strbuf_init(&sbuf);

str = malloc(str_len);
if (str == NULL)
return NULL;

for (i = 0, j = 0; i < (int)len; i++) {
j += sprintf(str + j, "%02X", (unsigned char)hex[i]);
if (i < (int)len - 1) {
str[j++] = ':';
for (size_t i = 0; i < len; i++) {
if (!strbuf_pushchar(&sbuf, digits[(hex[i] >> 4) & 0x0F]) ||
!strbuf_pushchar(&sbuf, digits[hex[i] & 0x0F]))
goto fail;

Check warning on line 1811 in libkmod/libkmod-module.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-module.c#L1811

Added line #L1811 was not covered by tests
if (i < len - 1) {
if (!strbuf_pushchar(&sbuf, ':'))
goto fail;

Check warning on line 1814 in libkmod/libkmod-module.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-module.c#L1814

Added line #L1814 was not covered by tests

if ((i + 1) % line_limit == 0)
j += sprintf(str + j, "\n\t\t");
if ((i + 1) % line_limit == 0 &&
!strbuf_pushchars(&sbuf, "\n\t\t"))
goto fail;

Check warning on line 1818 in libkmod/libkmod-module.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-module.c#L1818

Added line #L1818 was not covered by tests
}
}
return str;
return strbuf_steal(&sbuf);
fail:
strbuf_release(&sbuf);
return NULL;

Check warning on line 1824 in libkmod/libkmod-module.c

View check run for this annotation

Codecov / codecov/patch

libkmod/libkmod-module.c#L1822-L1824

Added lines #L1822 - L1824 were not covered by tests
}

static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list,
Expand Down