Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

jbd2: Avoid printing outside the boundary of the buffer

Theoretically possible that "%pg" will take all room for the j_devname
and hence the "-%lu" will go outside the boundary due to unconditional
sprintf() in use. To make this code more robust, replace two sequential
s*printf():s by a single call and then replace forbidden character.
It's possible to do this way, because '/' won't ever be in the result
of "-%lu".

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230605170553.7835-2-andriy.shevchenko@linux.intel.com

authored by

Andy Shevchenko and committed by
Kees Cook
7afb6d8f 8515e4a7

+2 -4
+2 -4
fs/jbd2/journal.c
··· 1491 1491 { 1492 1492 journal_t *journal; 1493 1493 sector_t blocknr; 1494 - char *p; 1495 1494 int err = 0; 1496 1495 1497 1496 blocknr = 0; ··· 1514 1515 1515 1516 journal->j_inode = inode; 1516 1517 snprintf(journal->j_devname, sizeof(journal->j_devname), 1517 - "%pg", journal->j_dev); 1518 - p = strreplace(journal->j_devname, '/', '!'); 1519 - sprintf(p, "-%lu", journal->j_inode->i_ino); 1518 + "%pg-%lu", journal->j_dev, journal->j_inode->i_ino); 1519 + strreplace(journal->j_devname, '/', '!'); 1520 1520 jbd2_stats_proc_init(journal); 1521 1521 1522 1522 return journal;