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

Heap overflow in get_rootdev #266

Open
enriktigasna opened this issue Mar 3, 2025 · 0 comments
Open

Heap overflow in get_rootdev #266

enriktigasna opened this issue Mar 3, 2025 · 0 comments

Comments

@enriktigasna
Copy link

enriktigasna commented Mar 3, 2025

Vulnerable code:

char *get_rootdev()
{
#if defined(_WIN32) || defined(WITH_ANDROID)
    return NULL;
#else
    struct stat sb;
    int fd, ret;
    char buf[PATH_MAX + 1];
    char *uevent, *ptr;
    char *rootdev;

    if (stat("/", &sb) == -1)
        return NULL;

    snprintf(buf, PATH_MAX, "/sys/dev/block/%u:%u/uevent",
        major(sb.st_dev), minor(sb.st_dev));

    fd = open(buf, O_RDONLY);

    if (fd < 0)
        return NULL;

    ret = lseek(fd, (off_t)0, SEEK_END);
    (void)lseek(fd, (off_t)0, SEEK_SET);

    if (ret == -1) {
        close(fd);
        return NULL;
    }

    uevent = malloc(ret + 1);
    ASSERT(uevent);

    uevent[ret] = '\0';

    ret = read(fd, uevent, ret);
    close(fd);

    ptr = strstr(uevent, "DEVNAME");
    if (!ptr)
        goto out_free;

    ret = sscanf(ptr, "DEVNAME=%s\n", buf);
...

When in f2fs, it checks for if a device is mounted, it reaches this code. This code opens the file /sys/dev/block/%u:%u/uevent, and unsafely reads into ptr, which is the size of how much is left from the file after DEVNAME. If you construct it so that DEVNAME is at the end of the file, then it will not allocate enough for the buffer, and it will be overflown.

This can be exploited if an attacker can corrupt headers of a mounted system, or write arbitrary files into this directory in for example a non-FHS compliant system. Under certain conditions this can lead to arbitrary code execution or control of dynamic allocation.

Affected files:

  • src/f2fs/libf2fs.c
@enriktigasna enriktigasna changed the title OOB stack write vulnerability in f2fs get_rootdev Heap overflow in get_rootdev Mar 3, 2025
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

No branches or pull requests

1 participant