Skip to contentSkip to conversation

ubifs: BUG realpath string must be ended with NULL

PATCH New Ricardo Ribalda Delgado 4 participants 15 messages Apr 27, 2009 – Apr 28, 2009 UBI file system (UBIFS)

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 ubifs: BUG realpath string must be ended with NULL

If the memory used to copy the link_make is "dirty" the string wont be ended with NULL, throwing out multiple memory bugs.

---
 fs/ubifs/ubifs.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/ubifs/ubifs.c +1 -0
index 32f9ff8..427d84a 100644
@@ -641,6 +641,7 @@ int ubifs_load(char *filename, u32 addr, u32 size)
 	ui = ubifs_inode(inode);
 	if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
 		memcpy(link_name, ui->data, ui->data_len);
+		link_name[ui->data_len] = '\0';
 		printf("%s is linked to %s!\n", filename, link_name);
 		ubifs_iput(inode);
 
Signature · 1 line
1.6.2.4
#1 lore raw

PATCH 2/3 lib_generic: gunzip: New function zunzip

Separate gunzip in

gunzip: Find the end of the header and call zunzip.
zunzip: Inflate gunzip block without header.

UBI fs blocks can be compresed in lzo, zlib or no-compression. The current implementation of u-boot supported all the compressions but there was a bug in the implementation of the zlib blocks.

UBIFS's Zlib blocks do not have header but they were compressed using gunzip, a function used to decompress gunzip files/sectors with a header.

This patch adds a new function zunzip that uncompress a zlib block with no header.

---
v2: Better doc

 lib_generic/gunzip.c |   27 ++++++++++++++++++++-------
 1 files changed, 20 insertions(+), 7 deletions(-)
lib_generic/gunzip.c +20 -7
index 01a4031..d59a448 100644
@@ -39,6 +39,8 @@
 int gunzip(void *, int, unsigned char *, unsigned long *);
 void *zalloc(void *, unsigned, unsigned);
 void zfree(void *, void *, unsigned);
+int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
+						int stoponerr, int offset);
 
 void *zalloc(void *x, unsigned items, unsigned size)
 {
@@ -59,8 +61,7 @@ void zfree(void *x, void *addr, unsigned nb)
 
 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
 {
-	z_stream s;
-	int r, i, flags;
+	int i, flags;
 
 	/* skip header */
 	i = 10;
@@ -84,6 +85,18 @@ int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
 		return (-1);
 	}
 
+	return zunzip(dst, dstlen, src, lenp, 1, i);
+}
+
+/*
+ * Uncompress blocks compressed with zlib without headers
+ */
+int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
+						int stoponerr, int offset)
+{
+	z_stream s;
+	int r;
+
 	s.zalloc = zalloc;
 	s.zfree = zfree;
 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
@@ -95,14 +108,14 @@ int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
 	r = inflateInit2(&s, -MAX_WBITS);
 	if (r != Z_OK) {
 		printf ("Error: inflateInit2() returned %d\n", r);
-		return (-1);
+		return -1;
 	}
-	s.next_in = src + i;
-	s.avail_in = *lenp - i;
+	s.next_in = src + offset;
+	s.avail_in = *lenp - offset;
 	s.next_out = dst;
 	s.avail_out = dstlen;
 	r = inflate(&s, Z_FINISH);
-	if (r != Z_STREAM_END) {
+	if ((r != Z_STREAM_END) && (stoponerr==1)) {
 		printf ("Error: inflate() returned %d\n", r);
 		inflateEnd(&s);
 		return (-1);
@@ -110,5 +123,5 @@ int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
 	*lenp = s.next_out - (unsigned char *) dst;
 	inflateEnd(&s);
 
-	return (0);
+	return 0;
 }
Signature · 1 line
1.6.2.4
#2 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Blocks compressed with zlib dont have the full gzip header.

Without this patch, block compressed with zlib cannot be readed!

---
v3: rename patch 2-> patch 3
v2: remove unused parts..

 fs/ubifs/ubifs.c |    7 +++++--
 fs/ubifs/ubifs.h |    2 --
 2 files changed, 5 insertions(+), 4 deletions(-)
fs/ubifs/ubifs.c +5 -2
index 427d84a..739fb04 100644
@@ -24,6 +24,7 @@
  */
 
 #include "ubifs.h"
+#include <u-boot/zlib.h>
 
 #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
 #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
@@ -33,15 +34,17 @@ DECLARE_GLOBAL_DATA_PTR;
 
 /* compress.c */
 
+int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
+						int stoponerr, int offset);
 /*
- * We need a wrapper for gunzip() because the parameters are
+ * We need a wrapper for zunzip() because the parameters are
  * incompatible with the lzo decompressor.
  */
 static int gzip_decompress(const unsigned char *in, size_t in_len,
 			   unsigned char *out, size_t *out_len)
 {
 	unsigned long len = in_len;
-	return gunzip(out, *out_len, (unsigned char *)in, &len);
+	return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
 }
 
 /* Fake description object for the "none" compressor */
fs/ubifs/ubifs.h +0 -2
index 91351de..a68e4c1 100644
@@ -2172,6 +2172,4 @@ int ubifs_decompress(const void *buf, int len, void *out, int *out_len,
 /* todo: Move these to a common U-Boot header */
 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
 			  unsigned char *out, size_t *out_len);
-int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
-
 #endif /* !__UBIFS_H__ */
Signature · 1 line
1.6.2.4
#3 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Dear Ricardo Ribalda Delgado,

Quoted 5 lines · In message <[email protected]> you wrote:

Blocks compressed with zlib dont have the full gzip header.

Without this patch, block compressed with zlib cannot be readed!

...

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR; · line 38

Quoted 11 lines

#include "ubifs.h"

fs/ubifs/ubifs.c +3 -0
+#include <u-boot/zlib.h>
 
 #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
 #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
@@ -33,15 +34,17 @@ DECLARE_GLOBAL_DATA_PTR;
 
 /* compress.c */
 
+int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
+						int stoponerr, int offset);

The prototype declaration should be in the neader file. Please remove here.

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR; · line 48

Quoted 11 lines
fs/ubifs/ubifs.c +2 -2
- * We need a wrapper for gunzip() because the parameters are
+ * We need a wrapper for zunzip() because the parameters are
  * incompatible with the lzo decompressor.
  */
 static int gzip_decompress(const unsigned char *in, size_t in_len,
 			   unsigned char *out, size_t *out_len)
 {
 	unsigned long len = in_len;
-	return gunzip(out, *out_len, (unsigned char *)in, &len);
+	return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
 }

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
panic: kernel trap (ignored)
#4 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Quoted 7 lines · Wolfgang Denk
Quoted 7 lines

#include "ubifs.h"

fs/ubifs/ubifs.c +1 -0
+#include <u-boot/zlib.h>
 
 #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
 #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
@@ -33,15 +34,17 @@ DECLARE_GLOBAL_DATA_PTR;
 
Quoted 7 lines

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR;

Quoted 4 lines
fs/ubifs/ubifs.c +1 -0
 /* compress.c */
 
+int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long
 *lenp, +						int stoponerr, int offset);

The prototype declaration should be in the neader file. Please remove here.

Ack.

Best regards,
Stefan

=====================================================================
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: office at denx.de
=====================================================================

#5 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

In reply to Wolfgang Denk
Quoted 7 lines · On Monday 27 April 2009 08:36:55 Wolfgang Denk wrote:
Quoted 6 lines · In message Ricardo Ribalda Delgado

Blocks compressed with zlib dont have the full gzip header.

Without this patch, block compressed with zlib cannot be readed!

Quoted 15 lines

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR; · line 48

Quoted 11 lines

- * We need a wrapper for gunzip() because the parameters are
+ * We need a wrapper for zunzip() because the parameters are
* incompatible with the lzo decompressor.
*/
static int gzip_decompress(const unsigned char *in, size_t in_len,

fs/ubifs/ubifs.c +1 -1
 			   unsigned char *out, size_t *out_len)
 {
 	unsigned long len = in_len;
-	return gunzip(out, *out_len, (unsigned char *)in, &len);
+	return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
 }

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

not sure i follow ... what do you propose changing ?

easylogo can be changed to use this (a few Blackfin boards) because atm, compression support with that is implemented by running gzip. this is because i wasnt able to figure out how to get the decompression routines in u- boot to play with a zlib compressed stream (i.e. no gzip header).
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090427/8ce93e7a/attachment.pgp

#6 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Dear Mike Frysinger,

Quoted 17 lines · In message <[email protected]> you wrote:
Quoted 16 lines

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR; · line 48

Quoted 12 lines

- * We need a wrapper for gunzip() because the parameters are
+ * We need a wrapper for zunzip() because the parameters are
* incompatible with the lzo decompressor.
*/
static int gzip_decompress(const unsigned char *in, size_t in_len,

fs/ubifs/ubifs.c +1 -1
 			   unsigned char *out, size_t *out_len)
 {
 	unsigned long len = in_len;
-	return gunzip(out, *out_len, (unsigned char *)in, &len);
+	return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
 }

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

Quoted 1 line

not sure i follow ... what do you propose changing ?

The comment above seems to indicate that gzip_decompress() is only (?) needed because the zunzip() parameters are incompatible with those of the lzo decompressor.

So if we create zunzip() just for this purpose, then why not create it in a way that the parameters are compatible with those of the lzo decompressor, so we can omit this whole gzip_decompress() wrapper function?

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The philosophy exam was a piece of cake  -  which  was  a  bit  of  a
surprise, actually, because I was expecting some questions on a sheet
of paper.
#7 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Quoted 19 lines · On Monday 27 April 2009 15:46:25 Wolfgang Denk wrote:
Quoted 16 lines · In message Mike Frysinger
Quoted 7 lines
Quoted 7 lines

- * We need a wrapper for gunzip() because the parameters are
+ * We need a wrapper for zunzip() because the parameters are
* incompatible with the lzo decompressor.
*/
static int gzip_decompress(const unsigned char *in, size_t in_len,
unsigned char *out, size_t *out_len)
{

Quoted 8 lines

fs/ubifs/ubifs.c DECLARE_GLOBAL_DATA_PTR; · line 48

Quoted 4 lines
fs/ubifs/ubifs.c +1 -1
 	unsigned long len = in_len;
-	return gunzip(out, *out_len, (unsigned char *)in, &len);
+	return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
 }

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

Quoted 1 line

not sure i follow ... what do you propose changing ?

Quoted 8 lines

The comment above seems to indicate that gzip_decompress() is only (?) needed because the zunzip() parameters are incompatible with those of the lzo decompressor.

So if we create zunzip() just for this purpose, then why not create it in a way that the parameters are compatible with those of the lzo decompressor, so we can omit this whole gzip_decompress() wrapper function?

that should be a follow up change, but the wrapper already exists today regardless of Ricardo's fixes. i dont think his changes should be held up to address that.

that direction should probably cover:
- fix gunzip code to use size_t's instead of unsigned long
- re-order lzo arguments to match zlib rather than other way around since lzo
has very few consumers
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090427/95255cfb/attachment-0001.pgp

#8 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Dear Mike Frysinger,

Quoted 11 lines · In message <[email protected]> you wrote:

that should be a follow up change, but the wrapper already exists today regardless of Ricardo's fixes. i dont think his changes should be held up to
address that.

that direction should probably cover:
- fix gunzip code to use size_t's instead of unsigned long
- re-order lzo arguments to match zlib rather than other way around since
lzo
has very few consumers

OK, makes sense to me, too.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Confound these ancestors.... They've stolen our best ideas!"
- Ben Jonson
#9 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Hello

What is the situation now? We apply this patch and then make lzo parameter compatible? I am a bit lost....

Signature · 2 lines
Ricardo Ribalda
http://www.eps.uam.es/~rribalda/
#10 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Quoted 2 lines · On Tuesday 28 April 2009 03:42:30 Ricardo Ribalda Delgado wrote:

What is the situation now? We apply this patch and then make lzo parameter compatible? I am a bit lost....

i think so ... your latest patch looked OK to me
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090428/9ec4d9f2/attachment.pgp

#11 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

In reply to Wolfgang Denk

Hello Wolfgang

Quoted 4 lines

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

The purpose of zunzip is to use it in more places. Like Mike Frysinger said:

<Mike>
this really should be a common function not specific to ubifs as there are many other opportunities for things to be compressed directly with zlib and not through gzip (splash/video images come to mind).
</Mike>

Also, in order to be "parameter compatible" with lzo, we should keep this wrapper (am I right Stefan?)

Best regards

Signature · 2 lines
Ricardo Ribalda
http://www.eps.uam.es/~rribalda/
#12 lore raw

PATCH 3/3 ubifs: BUG: Blocks commpressed with zlib

Dear Ricardo Ribalda Delgado,

Quoted 7 lines · In message <[email protected]> you…
Quoted 4 lines

If the only purpose of zunzip() is to be used here, then why do we not make the parameters fit the intended purpose, thus avoiding an additional wrapper?

The purpose of zunzip is to use it in more places. Like Mike Frysinger said:

Quoted 8 lines

<Mike>
this really should be a common function not specific to ubifs as there are many other opportunities for things to be compressed directly with zlib and not through gzip (splash/video images come to mind).
</Mike>

Also, in order to be "parameter compatible" with lzo, we should keep this wrapper (am I right Stefan?)

But cannot these other pleaces use a parameter-compatible version as well, or even better (because they might then eventually use lzo as well, at least more easily) ?

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Hiring experienced unix people is  like  a  built-in  filter  against
idiots. Hiring experienced NT people provides no such guarantee.
            -- Miguel Cruz in WgL96.349$CC.122704 at typhoon2.ba-dsg.net
#13 lore raw

PATCH 2/3 lib_generic: gunzip: New function zunzip

In reply to Ricardo Ribalda Delgado

Dear Ricardo Ribalda Delgado,

Quoted 17 lines · In message <[email protected]> you wrote:

Separate gunzip in

gunzip: Find the end of the header and call zunzip.
zunzip: Inflate gunzip block without header.

UBI fs blocks can be compresed in lzo, zlib or no-compression. The current implementation of u-boot supported all the compressions but there was a bug in the implementation of the zlib blocks.

UBIFS's Zlib blocks do not have header but they were compressed using gunzip, a function used to decompress gunzip files/sectors with a header.

This patch adds a new function zunzip that uncompress a zlib block with no header.

...

lib_generic/gunzip.c int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp) · line 118

Quoted 2 lines
lib_generic/gunzip.c +1 -1
-	if (r != Z_STREAM_END) {
+	if ((r != Z_STREAM_END) && (stoponerr==1)) {

I already asked about this change, which ios unrelated to this patch.

Please explain why this is needed, and why you think this should not be split off into a separate commit?

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"More software projects have gone awry for lack of calendar time than
for all other causes combined."
                         - Fred Brooks, Jr., _The Mythical Man Month_
#14 lore raw

PATCH 1/3 ubifs: BUG realpath string must be ended with NULL

In reply to Ricardo Ribalda Delgado

Dear Ricardo Ribalda Delgado,

Quoted 2 lines · In message <[email protected]> you wrote:

If the memory used to copy the link_make is "dirty" the string wont be ended with NULL, throwing out multiple memory bugs.

What is "link_make" ? Do you mean "link_name" ?

Quoted 21 lines
---
 fs/ubifs/ubifs.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/ubifs/ubifs.c +1 -0
index 32f9ff8..427d84a 100644
@@ -641,6 +641,7 @@ int ubifs_load(char *filename, u32 addr, u32 size)
 	ui = ubifs_inode(inode);
 	if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
 		memcpy(link_name, ui->data, ui->data_len);
+		link_name[ui->data_len] = '\0';
 		printf("%s is linked to %s!\n", filename, link_name);
 		ubifs_iput(inode);
 
Signature · 1 line
1.6.2.4

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Unix is like a toll road on which you have to stop every 50  feet  to
pay another nickel. But hey! You only feel 5 cents poorer each time.
                 - Larry Wall in <[email protected]>