Skip to contentSkip to conversation

Btrfs: avoid buffer overrun in btrfs_printk

PATCH In review Jim Meyering 2 participants 2 messages Apr 26, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: avoid buffer overrun in btrfs_printk

The buffer read-overrun would be triggered by a printk format starting with <N>, where N is a single digit. NUL-terminate after strncpy. Use memcpy, not strncpy, since we know the string we're copying fits in the destination buffer and contains no NUL byte.

---
 fs/btrfs/super.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/super.c +2 -1
index 5ddf172..626f574 100644
@@ -180,23 +180,24 @@ const char *logtypes[] = {
 void btrfs_printk(struct btrfs_fs_info *fs_info, const char *fmt, ...)
 {
 	struct super_block *sb = fs_info->sb;
 	char lvl[4];
 	struct va_format vaf;
 	va_list args;
 	const char *type = logtypes[4];
 
 	va_start(args, fmt);
 
 	if (fmt[0] == '<' && isdigit(fmt[1]) && fmt[2] == '>') {
-		strncpy(lvl, fmt, 3);
+		memcpy(lvl, fmt, 3);
+		lvl[3] = '\0';
 		fmt += 3;
 		type = logtypes[fmt[1] - '0'];
 	} else
 		*lvl = '\0';
 
 	vaf.fmt = fmt;
 	vaf.va = &args;
 	printk("%sBTRFS %s (device %s): %pV", lvl, type, sb->s_id, &vaf);
 }
 
 /*

--
1.7.10.336.gc5e31

Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jim Meyering

The buffer read-overrun would be triggered by a printk format starting with <N>, where N is a single digit. NUL-terminate after strncpy. Use memcpy, not strncpy, since we know the string we're copying fits in the destination buffer and contains no NUL byte.

I just noticed this but could you send to [email protected] from now on? Thanks,

Josef