Skip to content

Commit b51593c

Browse files
rhvgoyalAl Viro
authored and
Al Viro
committed
init/do_mounts.c: Harden split_fs_names() against buffer overflow
split_fs_names() currently takes comma separate list of filesystems and converts it into individual filesystem strings. Pleaces these strings in the input buffer passed by caller and returns number of strings. If caller manages to pass input string bigger than buffer, then we can write beyond the buffer. Or if string just fits buffer, we will still write beyond the buffer as we append a '\0' byte at the end. Pass size of input buffer to split_fs_names() and put enough checks in place so such buffer overrun possibilities do not occur. This patch does few things. - Add a parameter "size" to split_fs_names(). This specifies size of input buffer. - Use strlcpy() (instead of strcpy()) so that we can't go beyond buffer size. If input string "names" is larger than passed in buffer, input string will be truncated to fit in buffer. - Stop appending extra '\0' character at the end and avoid one possibility of going beyond the input buffer size. - Do not use extra loop to count number of strings. - Previously if one passed "rootfstype=foo,,bar", split_fs_names() will return only 1 string "foo" (and "bar" will be truncated due to extra ,). After this patch, now split_fs_names() will return 3 strings ("foo", zero-sized-string, and "bar"). Callers of split_fs_names() have been modified to check for zero sized string and skip to next one. Reported-by: xu xin <[email protected]> Signed-off-by: Vivek Goyal <[email protected]> Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent e4e737b commit b51593c

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

init/do_mounts.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,20 +338,19 @@ __setup("rootflags=", root_data_setup);
338338
__setup("rootfstype=", fs_names_setup);
339339
__setup("rootdelay=", root_delay_setup);
340340

341-
static int __init split_fs_names(char *page, char *names)
341+
/* This can return zero length strings. Caller should check */
342+
static int __init split_fs_names(char *page, size_t size, char *names)
342343
{
343-
int count = 0;
344+
int count = 1;
344345
char *p = page;
345346

346-
strcpy(p, root_fs_names);
347+
strlcpy(p, root_fs_names, size);
347348
while (*p++) {
348-
if (p[-1] == ',')
349+
if (p[-1] == ',') {
349350
p[-1] = '\0';
351+
count++;
352+
}
350353
}
351-
*p = '\0';
352-
353-
for (p = page; *p; p += strlen(p)+1)
354-
count++;
355354

356355
return count;
357356
}
@@ -404,12 +403,16 @@ void __init mount_block_root(char *name, int flags)
404403
scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
405404
MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
406405
if (root_fs_names)
407-
num_fs = split_fs_names(fs_names, root_fs_names);
406+
num_fs = split_fs_names(fs_names, PAGE_SIZE, root_fs_names);
408407
else
409408
num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
410409
retry:
411410
for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1) {
412-
int err = do_mount_root(name, p, flags, root_mount_data);
411+
int err;
412+
413+
if (!*p)
414+
continue;
415+
err = do_mount_root(name, p, flags, root_mount_data);
413416
switch (err) {
414417
case 0:
415418
goto out;
@@ -543,10 +546,12 @@ static int __init mount_nodev_root(void)
543546
fs_names = (void *)__get_free_page(GFP_KERNEL);
544547
if (!fs_names)
545548
return -EINVAL;
546-
num_fs = split_fs_names(fs_names, root_fs_names);
549+
num_fs = split_fs_names(fs_names, PAGE_SIZE, root_fs_names);
547550

548551
for (i = 0, fstype = fs_names; i < num_fs;
549552
i++, fstype += strlen(fstype) + 1) {
553+
if (!*fstype)
554+
continue;
550555
if (!fs_is_nodev(fstype))
551556
continue;
552557
err = do_mount_root(root_device_name, fstype, root_mountflags,

0 commit comments

Comments
 (0)