Skip to contentSkip to conversation

fat: fix buffer overflow in vfat_create_shortname()

PATCH In review Nikolaus Schulz 2 participants 7 messages Mar 25, 2010 – Mar 31, 2010 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fat: fix buffer overflow in vfat_create_shortname()

When using the string representation of a random counter as part of the base name, ensure that it is no longer than 4 bytes.

Since we are repeatedly decrementing the counter in a loop until we have found a unique base name, the counter may wrap around zero; therefore, it is not enough to mask its higher bits before entering the loop, this must be done inside the loop.

---
 fs/fat/namei_vfat.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
fs/fat/namei_vfat.c +3 -3
index c1ef501..a448ee5 100644
@@ -309,7 +309,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 {
 	struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
 	wchar_t *ip, *ext_start, *end, *name_start;
-	unsigned char base[9], ext[4], buf[8], *p;
+	unsigned char base[9], ext[4], buf[5], *p;
 	unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
 	int chl, chi;
 	int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
@@ -467,7 +467,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 			return 0;
 	}
 
-	i = jiffies & 0xffff;
+	i = jiffies;
 	sz = (jiffies >> 16) & 0x7;
 	if (baselen > 2) {
 		baselen = numtail2_baselen;
@@ -476,7 +476,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 	name_res[baselen + 4] = '~';
 	name_res[baselen + 5] = '1' + sz;
 	while (1) {
-		sprintf(buf, "%04X", i);
+		sprintf(buf, "%04X", i & 0xffff);
 		memcpy(&name_res[baselen], buf, 4);
 		if (vfat_find_form(dir, name_res) < 0)
 			break;
Signature · 1 line
1.7.0
maintainer
#1 lore raw
Quoted 7 lines · Nikolaus Schulz

When using the string representation of a random counter as part of the base name, ensure that it is no longer than 4 bytes.

Since we are repeatedly decrementing the counter in a loop until we have found a unique base name, the counter may wrap around zero; therefore, it is not enough to mask its higher bits before entering the loop, this must be done inside the loop.

This logic seems to still be strange after applying this patch. However, anyway, your patch is much better off than current one. So, I'll apply this in the next merge window.

Or should we apply this immediately?

Thanks.

Quoted 37 lines
---
 fs/fat/namei_vfat.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
fs/fat/namei_vfat.c +3 -3
index c1ef501..a448ee5 100644
@@ -309,7 +309,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 {
 	struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
 	wchar_t *ip, *ext_start, *end, *name_start;
-	unsigned char base[9], ext[4], buf[8], *p;
+	unsigned char base[9], ext[4], buf[5], *p;
 	unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
 	int chl, chi;
 	int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
@@ -467,7 +467,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 			return 0;
 	}
 
-	i = jiffies & 0xffff;
+	i = jiffies;
 	sz = (jiffies >> 16) & 0x7;
 	if (baselen > 2) {
 		baselen = numtail2_baselen;
@@ -476,7 +476,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 	name_res[baselen + 4] = '~';
 	name_res[baselen + 5] = '1' + sz;
 	while (1) {
-		sprintf(buf, "%04X", i);
+		sprintf(buf, "%04X", i & 0xffff);
 		memcpy(&name_res[baselen], buf, 4);
 		if (vfat_find_form(dir, name_res) < 0)
 			break;
Signature · 1 line
OGAWA Hirofumi <[email protected]>
#2 lore raw
Quoted 11 lines · OGAWA Hirofumi
Quoted 7 lines · Nikolaus Schulz

When using the string representation of a random counter as part of the base name, ensure that it is no longer than 4 bytes.

Since we are repeatedly decrementing the counter in a loop until we have found a unique base name, the counter may wrap around zero; therefore, it is not enough to mask its higher bits before entering the loop, this must be done inside the loop.

This logic seems to still be strange after applying this patch.

Yeah, I find that code not terribly convincing either. :)

Quoted 6 lines

However, anyway, your patch is much better off than current one. So, I'll apply this in the next merge window.

Or should we apply this immediately?

Thanks.

Given that this fixes a stack corruption which triggers the gcc stack smashing protection and thus basically a crash, I vote for not postponing it, but applying it immediately.

Quoted 41 lines
Quoted 37 lines
---
 fs/fat/namei_vfat.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
fs/fat/namei_vfat.c +3 -3
index c1ef501..a448ee5 100644
@@ -309,7 +309,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 {
 	struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
 	wchar_t *ip, *ext_start, *end, *name_start;
-	unsigned char base[9], ext[4], buf[8], *p;
+	unsigned char base[9], ext[4], buf[5], *p;
 	unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
 	int chl, chi;
 	int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
@@ -467,7 +467,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 			return 0;
 	}
 
-	i = jiffies & 0xffff;
+	i = jiffies;
 	sz = (jiffies >> 16) & 0x7;
 	if (baselen > 2) {
 		baselen = numtail2_baselen;
@@ -476,7 +476,7 @@ static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
 	name_res[baselen + 4] = '~';
 	name_res[baselen + 5] = '1' + sz;
 	while (1) {
-		sprintf(buf, "%04X", i);
+		sprintf(buf, "%04X", i & 0xffff);
 		memcpy(&name_res[baselen], buf, 4);
 		if (vfat_find_form(dir, name_res) < 0)
 			break;
Signature · 1 line
OGAWA Hirofumi <[email protected]>
maintainer
#3 lore raw
Quoted 10 lines · Nikolaus Schulz
Quoted 6 lines

However, anyway, your patch is much better off than current one. So, I'll apply this in the next merge window.

Or should we apply this immediately?

Thanks.

Given that this fixes a stack corruption which triggers the gcc stack smashing protection and thus basically a crash, I vote for not postponing it, but applying it immediately.

Um..., where is a stack corruption? sprintf() overflow? But, it's actually snprintf(), not sprintf()...

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
maintainer
#4 lore raw
Quoted 7 lines · OGAWA Hirofumi
Quoted 5 lines · Nikolaus Schulz
Quoted 5 lines

However, anyway, your patch is much better off than current one. So, I'll apply this in the next merge window.

Or should we apply this immediately?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks.

Given that this fixes a stack corruption which triggers the gcc stack smashing protection and thus basically a crash, I vote for not postponing it, but applying it immediately.

Um..., where is a stack corruption? sprintf() overflow? But, it's actually snprintf(), not sprintf()...

Whoops, it was really sprintf().

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#5 lore raw
Quoted 10 lines · OGAWA Hirofumi
Quoted 9 lines · OGAWA Hirofumi
Quoted 7 lines · Nikolaus Schulz
Quoted 6 lines

However, anyway, your patch is much better off than current one. So, I'll apply this in the next merge window.

Or should we apply this immediately?

Thanks.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Given that this fixes a stack corruption which triggers the gcc stack smashing protection and thus basically a crash, I vote for not postponing it, but applying it immediately.

Um..., where is a stack corruption? sprintf() overflow? But, it's actually snprintf(), not sprintf()...

Whoops, it was really sprintf().

Ping?

I really think this should go into 2.6.34 and the next stable kernel updates.

Nikolaus

maintainer
#6 lore raw

PATCH 1/1 fat: fix buffer overflow in vfat_create_shortname()

Quoted 4 lines · Nikolaus Schulz

Ping?

I really think this should go into 2.6.34 and the next stable kernel updates.

I was testing a bit.

OK, please apply.

From: Nikolaus Schulz <[email protected]>

When using the string representation of a random counter as part of the base name, ensure that it is no longer than 4 bytes.

Since we are repeatedly decrementing the counter in a loop until we have found a unique base name, the counter may wrap around zero; therefore, it is not enough to mask its higher bits before entering the loop, this must be done inside the loop.

---

 fs/fat/namei_vfat.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/fat/namei_vfat.c renamed from fs/fat/namei_vfat.c~fat-fix-numtail-logic +3 -3
diff -puN fs/fat/namei_vfat.c~fat-fix-numtail-logic fs/fat/namei_vfat.c
@@ -309,7 +309,7 @@ static int vfat_create_shortname(struct
 {
 	struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
 	wchar_t *ip, *ext_start, *end, *name_start;
-	unsigned char base[9], ext[4], buf[8], *p;
+	unsigned char base[9], ext[4], buf[5], *p;
 	unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
 	int chl, chi;
 	int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
@@ -467,7 +467,7 @@ static int vfat_create_shortname(struct
 			return 0;
 	}
 
-	i = jiffies & 0xffff;
+	i = jiffies;
 	sz = (jiffies >> 16) & 0x7;
 	if (baselen > 2) {
 		baselen = numtail2_baselen;
@@ -476,7 +476,7 @@ static int vfat_create_shortname(struct
 	name_res[baselen + 4] = '~';
 	name_res[baselen + 5] = '1' + sz;
 	while (1) {
-		sprintf(buf, "%04X", i);
+		snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
 		memcpy(&name_res[baselen], buf, 4);
 		if (vfat_find_form(dir, name_res) < 0)
 			break;

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>