* [PATCH 2/8] bootstrap: Fix potential memory leak in 'read_image_head'
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:13 ` [PATCH 3/8] bootstrap_read_devfs(): Check for errors from devfs_add_partition() Andrey Smirnov
` (6 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Original version of 'read_image_head' would not free the memory
allocated for barebox header in cases of any failure. Fix this by
adding a dedicated resourse de-allocation code path.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 704680a..82c7d21 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -35,7 +35,7 @@ static void *read_image_head(const char *name)
cdev = cdev_open(name, O_RDONLY);
if (!cdev) {
bootstrap_err("failed to open partition\n");
- return NULL;
+ goto free_header;
}
ret = cdev_read(cdev, header, BAREBOX_HEAD_SIZE, 0, 0);
@@ -43,10 +43,14 @@ static void *read_image_head(const char *name)
if (ret != BAREBOX_HEAD_SIZE) {
bootstrap_err("failed to read from partition\n");
- return NULL;
+ goto free_header;
}
return header;
+
+free_header:
+ free(header);
+ return NULL;
}
static unsigned int get_image_size(void *head)
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 3/8] bootstrap_read_devfs(): Check for errors from devfs_add_partition()
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
2015-05-03 20:13 ` [PATCH 2/8] bootstrap: Fix potential memory leak in 'read_image_head' Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-04 7:35 ` Sascha Hauer
2015-05-03 20:13 ` [PATCH 4/8] bootstrap_read_devfs(): Close file after we are done with it Andrey Smirnov
` (5 subsequent siblings)
7 siblings, 1 reply; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Check for errors returned by devfs_add_partition() and bail if there
are any.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 82c7d21..de274a0 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -82,10 +82,17 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
int ret;
int size = 0;
void *to, *header;
- struct cdev *cdev;
+ struct cdev *cdev, partition;
char *partname = "x";
- devfs_add_partition(devname, offset, max_size, DEVFS_PARTITION_FIXED, partname);
+ partition = devfs_add_partition(devname, offset, max_size,
+ DEVFS_PARTITION_FIXED, partname);
+ if (IS_ERR_OR_NULL(partition)) {
+ bootstrap_err("%s: failed to add partition (%ld)\n",
+ devname, PTR_ERR(partition));
+ return NULL;
+ }
+
if (use_bb) {
dev_add_bb_dev(partname, "bbx");
partname = "bbx";
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 3/8] bootstrap_read_devfs(): Check for errors from devfs_add_partition()
2015-05-03 20:13 ` [PATCH 3/8] bootstrap_read_devfs(): Check for errors from devfs_add_partition() Andrey Smirnov
@ 2015-05-04 7:35 ` Sascha Hauer
0 siblings, 0 replies; 11+ messages in thread
From: Sascha Hauer @ 2015-05-04 7:35 UTC (permalink / raw)
To: Andrey Smirnov; +Cc: barebox
On Sun, May 03, 2015 at 01:13:14PM -0700, Andrey Smirnov wrote:
> Check for errors returned by devfs_add_partition() and bail if there
> are any.
>
> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
> ---
> lib/bootstrap/devfs.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
> index 82c7d21..de274a0 100644
> --- a/lib/bootstrap/devfs.c
> +++ b/lib/bootstrap/devfs.c
> @@ -82,10 +82,17 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
> int ret;
> int size = 0;
> void *to, *header;
> - struct cdev *cdev;
> + struct cdev *cdev, partition;
> char *partname = "x";
>
> - devfs_add_partition(devname, offset, max_size, DEVFS_PARTITION_FIXED, partname);
> + partition = devfs_add_partition(devname, offset, max_size,
> + DEVFS_PARTITION_FIXED, partname);
> + if (IS_ERR_OR_NULL(partition)) {
IS_ERR please. devfs_add_partition() will not return a NULL pointer.
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 4/8] bootstrap_read_devfs(): Close file after we are done with it
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
2015-05-03 20:13 ` [PATCH 2/8] bootstrap: Fix potential memory leak in 'read_image_head' Andrey Smirnov
2015-05-03 20:13 ` [PATCH 3/8] bootstrap_read_devfs(): Check for errors from devfs_add_partition() Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:13 ` [PATCH 5/8] bootstrap_read_devfs(): Fix potential memory leak Andrey Smirnov
` (4 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index de274a0..4176819 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -120,6 +120,8 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
}
ret = cdev_read(cdev, to, size, 0, 0);
+ cdev_close(cdev);
+
if (ret != size) {
bootstrap_err("%s: failed to read from %s\n", devname, partname);
return NULL;
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 5/8] bootstrap_read_devfs(): Fix potential memory leak
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
` (2 preceding siblings ...)
2015-05-03 20:13 ` [PATCH 4/8] bootstrap_read_devfs(): Close file after we are done with it Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:13 ` [PATCH 6/8] bootstrap_read_devfs(): Check for errors from dev_add_bb_dev() Andrey Smirnov
` (3 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
In case of a failure in one of the cdev_* functions original version
of bootstrap_read_devfs would not release memory allocated for barebox
header or memory allocated for the image. This commit fixes this by
adding resource deallocation code.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 4176819..f7f91d6 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -81,7 +81,7 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
{
int ret;
int size = 0;
- void *to, *header;
+ void *to, *header, *result = NULL;
struct cdev *cdev, partition;
char *partname = "x";
@@ -116,16 +116,21 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
cdev = cdev_open(partname, O_RDONLY);
if (!cdev) {
bootstrap_err("%s: failed to open %s\n", devname, partname);
- return NULL;
+ goto free_memory;
}
ret = cdev_read(cdev, to, size, 0, 0);
cdev_close(cdev);
- if (ret != size) {
+ if (ret != size)
bootstrap_err("%s: failed to read from %s\n", devname, partname);
- return NULL;
- }
+ else
+ result = to;
+
+free_memory:
+ free(header);
+ if (!result)
+ free(to);
- return to;
+ return result;
}
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 6/8] bootstrap_read_devfs(): Check for errors from dev_add_bb_dev()
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
` (3 preceding siblings ...)
2015-05-03 20:13 ` [PATCH 5/8] bootstrap_read_devfs(): Fix potential memory leak Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:13 ` [PATCH 7/8] bootstrap_read_devfs(): Remove all partitions upon function completion Andrey Smirnov
` (2 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Check for errors returned by dev_add_bb_dev() and bail if there
are any.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index f7f91d6..359981e 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -94,7 +94,14 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
}
if (use_bb) {
- dev_add_bb_dev(partname, "bbx");
+ ret = dev_add_bb_dev(partname, "bbx");
+ if (ret) {
+ bootstrap_err(
+ "%s: failed to add bad block aware partition (%d)\n",
+ devname, ret);
+ goto exit;
+ }
+
partname = "bbx";
}
@@ -131,6 +138,6 @@ free_memory:
free(header);
if (!result)
free(to);
-
+exit:
return result;
}
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 7/8] bootstrap_read_devfs(): Remove all partitions upon function completion
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
` (4 preceding siblings ...)
2015-05-03 20:13 ` [PATCH 6/8] bootstrap_read_devfs(): Check for errors from dev_add_bb_dev() Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:13 ` [PATCH 8/8] bootstrap: Warn if image size in BB header is zero Andrey Smirnov
2015-05-03 20:28 ` [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Marc Kleine-Budde
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Bootstrap_read_devfs does not remove the devices it creates during the
course of its execution which might be considered as a resource
leak. Remedy that by adding the code to remove those devices upon
function completion.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 359981e..d7b5185 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -99,7 +99,7 @@ void* bootstrap_read_devfs(char *devname, bool use_bb, int offset,
bootstrap_err(
"%s: failed to add bad block aware partition (%d)\n",
devname, ret);
- goto exit;
+ goto delete_devfs_partition;
}
partname = "bbx";
@@ -138,6 +138,15 @@ free_memory:
free(header);
if (!result)
free(to);
+
+ if (use_bb) {
+ dev_remove_bb_dev(partname);
+ partname = "x";
+ }
+
+delete_devfs_partition:
+ devfs_del_partition(partname);
+
exit:
return result;
}
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 8/8] bootstrap: Warn if image size in BB header is zero
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
` (5 preceding siblings ...)
2015-05-03 20:13 ` [PATCH 7/8] bootstrap_read_devfs(): Remove all partitions upon function completion Andrey Smirnov
@ 2015-05-03 20:13 ` Andrey Smirnov
2015-05-03 20:28 ` [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Marc Kleine-Budde
7 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 20:13 UTC (permalink / raw)
To: barebox; +Cc: Andrey Smirnov
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
lib/bootstrap/devfs.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index d7b5185..d071eb3 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -58,8 +58,12 @@ static unsigned int get_image_size(void *head)
unsigned int ret = 0;
unsigned int *psize = head + BAREBOX_HEAD_SIZE_OFFSET;
- if (is_barebox_head(head))
+ if (is_barebox_head(head)) {
ret = *psize;
+ if (!ret)
+ bootstrap_err(
+ "image has correct magic, but the length is zero\n");
+ }
debug("Detected barebox image size %u\n", ret);
return ret;
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation
2015-05-03 20:13 [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Andrey Smirnov
` (6 preceding siblings ...)
2015-05-03 20:13 ` [PATCH 8/8] bootstrap: Warn if image size in BB header is zero Andrey Smirnov
@ 2015-05-03 20:28 ` Marc Kleine-Budde
2015-05-03 21:49 ` Andrey Smirnov
7 siblings, 1 reply; 11+ messages in thread
From: Marc Kleine-Budde @ 2015-05-03 20:28 UTC (permalink / raw)
To: Andrey Smirnov, barebox
[-- Attachment #1.1: Type: text/plain, Size: 658 bytes --]
On 05/03/2015 10:13 PM, Andrey Smirnov wrote:
> If called with '-b' option 'imx-image' tool prepends barebox header to
> the image, but the tool does not fill the data at image size offset
> correctly. Call 'fix_size', right after 'imx-image' to fix that.
What about fixing the tool instead? The imx-image tool in used
imx-habv4, too, see images/Makefile.imxhabv4.
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 801 bytes --]
[-- Attachment #2: Type: text/plain, Size: 149 bytes --]
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation
2015-05-03 20:28 ` [PATCH 1/8] Makefile.lib: Fix i.MX image size after generation Marc Kleine-Budde
@ 2015-05-03 21:49 ` Andrey Smirnov
0 siblings, 0 replies; 11+ messages in thread
From: Andrey Smirnov @ 2015-05-03 21:49 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: barebox
On Sun, May 3, 2015 at 1:28 PM, Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> On 05/03/2015 10:13 PM, Andrey Smirnov wrote:
>> If called with '-b' option 'imx-image' tool prepends barebox header to
>> the image, but the tool does not fill the data at image size offset
>> correctly. Call 'fix_size', right after 'imx-image' to fix that.
>
> What about fixing the tool instead? The imx-image tool in used
> imx-habv4, too, see images/Makefile.imxhabv4.
>
I wasn't sure what would be the most appropriate way to fix this, so I
chose the path of a least resistance. But now that I know that the
tool is used in multiple places -- I agree -- it makes more sense to
fix the problem at its root. I will send the updated patch shortly.
Andrey
> Marc
>
> --
> Pengutronix e.K. | Marc Kleine-Budde |
> Industrial Linux Solutions | Phone: +49-231-2826-924 |
> Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
> Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
>
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 11+ messages in thread