Skip to contentSkip to conversation

initramfs: strcpy destination string overflow

PATCH In review Evgeny Kuznetsov 5 participants 8 messages Oct 5, 2010 – Oct 6, 2010 VFS

lore
Share & tools
lore
#0 lore raw

cover initramfs: strcpy destination string overflow

Hi,

Here is patch which fixes bug in /init/initramfs.c file. Function "strcpy" is used without check for maximum allowed source string length and could cause destination string overflow.

Thanks,
Best Regards,
Evgeny

Evgeny Kuznetsov (1):
initramfs: strcpy destination string overflow

 init/initramfs.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
#1 lore raw

PATCH 1/1 initramfs: strcpy destination string overflow

From: Evgeny Kuznetsov <[email protected]>

Function "strcpy()" is used without check for maximum allowed source string length and could cause destination string overflow. "strcpy()" is replaced by "strlcpy()" to prevent destination string overflow.

---
 init/initramfs.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
init/initramfs.c +1 -1
index 4b9c202..3e68568 100644
@@ -56,7 +56,7 @@ static char __init *find_link(int major, int minor, int ino,
 	q->minor = minor;
 	q->ino = ino;
 	q->mode = mode;
-	strcpy(q->name, name);
+	strlcpy(q->name, name, sizeof(q->name));
 	q->next = NULL;
 	*p = q;
 	return NULL;
Signature · 1 line
1.6.3.3
Reviewed-by on v1 1/1
#2 lore raw
Quoted 8 lines · Evgeny Kuznetsov

From: Evgeny Kuznetsov <[email protected]>

Function "strcpy()" is used without check for maximum allowed source string length and could cause destination string overflow. "strcpy()" is replaced by "strlcpy()" to prevent destination string overflow.

Looks good to me,

Quoted 25 lines

---
init/initramfs.c | 2 +-

1 files changed, 1 insertions(+), 1 deletions(-)
init/initramfs.c +1 -1
index 4b9c202..3e68568 100644
@@ -56,7 +56,7 @@ static char __init *find_link(int major, int minor, int ino,
 	q->minor = minor;
 	q->ino = ino;
 	q->mode = mode;
-	strcpy(q->name, name);
+	strlcpy(q->name, name, sizeof(q->name));
 	q->next = NULL;
 	*p = q;
 	return NULL;
Signature · 7 lines
1.6.3.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#3 lore raw
In reply to Evgeny Kuznetsov
Quoted 6 lines · Evgeny Kuznetsov

From: Evgeny Kuznetsov <[email protected]>

Function "strcpy()" is used without check for maximum allowed source string length and could cause destination string overflow. "strcpy()" is replaced by "strlcpy()" to prevent destination string overflow.

I think this is wrong.

If the name is too long to fit in the hash table, we should not create a new hash entry at all, because we'd be returning the wrong (truncated) name when we find it next time.

So it would be much better to just do

if (strlen(name) >= sizeof(q->name))

      return NULL;

in find_link(), because as far as I can tell, the hard-linking is always just an optimization.

Comments? Peter?

Linus

#4 lore raw
Quoted 17 lines · Linus Torvalds
Quoted 6 lines · Evgeny Kuznetsov

From: Evgeny Kuznetsov <[email protected]>

Function "strcpy()" is used without check for maximum allowed source string length and could cause destination string overflow. "strcpy()" is replaced by "strlcpy()" to prevent destination string overflow.

I think this is wrong.

If the name is too long to fit in the hash table, we should not create a new hash entry at all, because we'd be returning the wrong (truncated) name when we find it next time.

So it would be much better to just do

Quoted 7 lines
   if (strlen(name) >= sizeof(q->name))
      return NULL;

in find_link(), because as far as I can tell, the hard-linking is always just an optimization.

Comments? Peter?

The hard-linking is unfortunately not just an optimization; if you ignore it you will create one copy of the file and some number of zero-length files, since the data isn't duplicated. As such, printk'ing an error message and ignoring it is probably the right thing to do. Truncation is definitely wrong.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
maintainer
#5 lore raw
In reply to Linus Torvalds
Quoted 8 lines · Linus Torvalds

I think this is wrong.

If the name is too long to fit in the hash table, we should not create a new hash entry at all, because we'd be returning the wrong (truncated) name when we find it next time.

So it would be much better to just do

Quoted 7 lines
   if (strlen(name) >= sizeof(q->name))
      return NULL;

in find_link(), because as far as I can tell, the hard-linking is always just an optimization.

Comments? Peter?

Take a look at struct hash definition. That sizeof is PATH_MAX and do_header() will reject an entry with name longer than that. IOW, the whole thing is a non-issue; we can add

	if (strlen(name) >= PATH_MAX)
		BUG();

if we really care, but that's it.

As a side note, it looks like we need a fat warning about blind "improvements" of that kind in CodingStyle; cargo-cult replacements like that can easily hide real bugs... ;-/

#6 lore raw
Quoted 11 lines · Al Viro

Take a look at struct hash definition. That sizeof is PATH_MAX and do_header() will reject an entry with name longer than that. IOW, the whole thing is a non-issue; we can add

	if (strlen(name) >= PATH_MAX)
		BUG();

if we really care, but that's it.

As a side note, it looks like we need a fat warning about blind "improvements" of that kind in CodingStyle; cargo-cult replacements like that can easily hide real bugs... ;-/

BUG_ON(strlen(name) >= PATH_MAX);

... would work for me.

OK, not an issue...

-hpa

#7 lore raw
Quoted 9 lines · ext H. Peter Anvin
Quoted 8 lines · Al Viro

Take a look at struct hash definition. That sizeof is PATH_MAX and do_header() will reject an entry with name longer than that. IOW, the whole thing is a non-issue; we can add

	if (strlen(name) >= PATH_MAX)
		BUG();

if we really care, but that's it.

Quoted 5 lines
Quoted 3 lines

As a side note, it looks like we need a fat warning about blind "improvements" of that kind in CodingStyle; cargo-cult replacements like that can easily hide real bugs... ;-/

BUG_ON(strlen(name) >= PATH_MAX);

Should it be:

BUG_ON(strlen(name) >= N_ALIGN(PATH_MAX));

-Evgeny

Quoted 6 lines

... would work for me.

OK, not an issue...

-hpa