From: Andy Shevchenko Date: Mon, 5 Jun 2023 17:05:51 +0000 (+0300) Subject: jbd2: Avoid printing outside the boundary of the buffer X-Git-Tag: v6.6.17~4409^2~13 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7afb6d8fa81fd8d332f70ead5e35d8c90abb8165;p=platform%2Fkernel%2Flinux-rpi.git 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 Signed-off-by: Andy Shevchenko Signed-off-by: Kees Cook Link: https://lore.kernel.org/r/20230605170553.7835-2-andriy.shevchenko@linux.intel.com --- diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c index 8ae4191..6e17f8f 100644 --- a/fs/jbd2/journal.c +++ b/fs/jbd2/journal.c @@ -1491,7 +1491,6 @@ journal_t *jbd2_journal_init_inode(struct inode *inode) { journal_t *journal; sector_t blocknr; - char *p; int err = 0; blocknr = 0; @@ -1515,9 +1514,8 @@ journal_t *jbd2_journal_init_inode(struct inode *inode) journal->j_inode = inode; snprintf(journal->j_devname, sizeof(journal->j_devname), - "%pg", journal->j_dev); - p = strreplace(journal->j_devname, '/', '!'); - sprintf(p, "-%lu", journal->j_inode->i_ino); + "%pg-%lu", journal->j_dev, journal->j_inode->i_ino); + strreplace(journal->j_devname, '/', '!'); jbd2_stats_proc_init(journal); return journal;