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: Prevent OOB with specially crafted /proc/modules #227

Closed
wants to merge 1 commit into from

Conversation

stoeckmann
Copy link
Contributor

Do not access memory out of bounds if the first character read by fgets is NUL. Treat such a character as EOL instead. This is a purely defensive measure since /proc/modules should normaly not contain such characters.

Proof of Concept:

  1. Compile kmod with address sanitizer

  2. Create a tool which chroots to read custom /proc/modules

cat > poc.c << EOF
#include <err.h>
#include <libkmod.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
        struct kmod_ctx *ctx;
        struct kmod_list *list = NULL;
        const char *null_config = NULL;

        if (argc != 2)
                errx(1, "usage: poc chrootdir");

        ctx = kmod_new(NULL, &null_config);
        if (ctx == NULL)
                errx(1, "kmod_new");

        if (chroot(argv[1]) || chdir("/"))
                err(1, "chroot");

        if (kmod_module_new_from_loaded(ctx, &list) < 0)
                err(1, "kmod_mdule_new_from_loaded");

        kmod_unref(ctx);

        return 0;
}
EOF
cc -fsanitize=address -lkmod -o poc poc.c
  1. Create a /proc/modules which starts with a NUL character
TMPDIR=$(mktemp -d)
mkdir -p $TMPDIR/proc
echo -e "\x00" > $TMPDIR/proc/modules
  1. Run tool
sudo ./poc $TMPDIR

You can see the following line:

SUMMARY: AddressSanitizer: stack-buffer-overflow (/usr/lib/libkmod.so.2+0xac1e8)

Do not access memory out of bounds if the first character read by fgets
is NUL. Treat such a character as EOL instead. This is a purely
defensive measure since /proc/modules should normaly not contain such
characters.

Signed-off-by: Tobias Stoeckmann <[email protected]>
lucasdemarchi pushed a commit that referenced this pull request Nov 7, 2024
Do not access memory out of bounds if the first character read by fgets
is NUL. Treat such a character as EOL instead. This is a purely
defensive measure since /proc/modules should normaly not contain such
characters.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Link: #227
Signed-off-by: Lucas De Marchi <[email protected]>
@lucasdemarchi
Copy link
Contributor

Applied, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants