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

Array out of bounds caused by eblock->nr_files #20

Open
chennbnbnb opened this issue May 20, 2022 · 0 comments
Open

Array out of bounds caused by eblock->nr_files #20

chennbnbnb opened this issue May 20, 2022 · 0 comments
Assignees

Comments

@chennbnbnb
Copy link

chennbnbnb commented May 20, 2022

function simplefs_create() miss check whether eblock->nr_files bigger than SIMPLEFS_MAX_SUBFILES
so when eblock->nr_files is large, eblock->extents[ei].ee_start will cause array out of bounds problem

static int simplefs_create(struct inode* dir, 
    struct dentry* dentry,
    umode_t mode, 
    bool excl)
{

    ci_dir = SIMPLEFS_INODE(dir); 
    sb = dir->i_sb; 
    bh = sb_bread(sb, ci_dir->ei_block); 
    eblock = (struct simplefs_file_ei_block*)bh->b_data;

    if (eblock->nr_files == SIMPLEFS_MAX_SUBFILES) {    //nr_files may be very large
        ret = -EMLINK;
        goto end;
    }
    ...;
    ei = eblock->nr_files / SIMPLEFS_FILES_PER_EXT; //ei may be very large
    bi = eblock->nr_files % SIMPLEFS_FILES_PER_EXT / SIMPLEFS_FILES_PER_BLOCK;
    fi = eblock->nr_files % SIMPLEFS_FILES_PER_BLOCK;

    if (!eblock->extents[ei].ee_start) {    //out of bound read
        ...;
    }
    ...;
}

To get a PoC, change function write_data_blocks() in mkfs.c like that,
nr_files of root dir will be very large

static int write_data_blocks(int fd, struct superblock* sb)
{
    uint32_t* tmp = calloc(1, SIMPLEFS_BLOCK_SIZE);
    tmp[0] = 0xdeadbeef;   //nr_files = 0xdeadbeef
    write(fd, tmp, SIMPLEFS_BLOCK_SIZE);
    return 0;
}

mount this disk image created by mkfs.simplefs and then try to create file in root dir, you will get a crash
image

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

3 participants