mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/3] environment: don't leak environment path buffer
@ 2024-07-17  7:29 Ahmad Fatoum
  2024-07-17  7:29 ` [PATCH master 2/3] partition: fix use of uninitialized variable in error message Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-07-17  7:29 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

LeakSanitizer on sandbox reports that we always leak the environment
path. Restructure the code, so this is avoided.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/barebox.c | 49 +++++++++++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 19 deletions(-)

diff --git a/drivers/of/barebox.c b/drivers/of/barebox.c
index 560d9c0d15e0..ed5d171e43a8 100644
--- a/drivers/of/barebox.c
+++ b/drivers/of/barebox.c
@@ -15,60 +15,71 @@
 
 #define ENV_MNT_DIR "/boot"	/* If env on filesystem, where to mount */
 
-/* If dev describes a file on a fs, mount the fs and change devpath to
- * point to the file's path.  Otherwise leave devpath alone.  Does
- * nothing in env in a file support isn't enabled.  */
-static int environment_check_mount(struct device *dev, char **devpath)
+/* If dev describes a file on a fs, mount the fs and return a pointer
+ * to the file's path.  Otherwise return an error code or NULL if the
+ * device path should be used.
+ * Does nothing in env in a file support isn't enabled.
+ */
+static char *environment_check_mount(struct device *dev, const char *devpath)
 {
 	const char *filepath;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_OF_BAREBOX_ENV_IN_FS))
-		return 0;
+		return NULL;
 
 	ret = of_property_read_string(dev->of_node, "file-path", &filepath);
 	if (ret == -EINVAL) {
 		/* No file-path so just use device-path */
-		return 0;
+		return NULL;
 	} else if (ret) {
 		/* file-path property exists, but has error */
 		dev_err(dev, "Problem with file-path property\n");
-		return ret;
+		return ERR_PTR(ret);
 	}
 
 	/* Get device env is on and mount it */
 	mkdir(ENV_MNT_DIR, 0777);
-	ret = mount(*devpath, "fat", ENV_MNT_DIR, NULL);
+	ret = mount(devpath, "fat", ENV_MNT_DIR, NULL);
 	if (ret) {
 		dev_err(dev, "Failed to load environment: mount %s failed (%d)\n",
-			*devpath, ret);
-		return ret;
+			devpath, ret);
+		return ERR_PTR(ret);
 	}
 
 	/* Set env to be in a file on the now mounted device */
 	dev_dbg(dev, "Loading default env from %s on device %s\n",
-		filepath, *devpath);
-	*devpath = basprintf("%s/%s", ENV_MNT_DIR, filepath);
-	return 0;
+		filepath, devpath);
+
+	return basprintf("%s/%s", ENV_MNT_DIR, filepath);
 }
 
 static int environment_probe(struct device *dev)
 {
-	char *path;
+	char *devpath, *filepath;
 	int ret;
 
-	ret = of_find_path(dev->of_node, "device-path", &path,
+	ret = of_find_path(dev->of_node, "device-path", &devpath,
 			   OF_FIND_PATH_FLAGS_BB);
 	if (ret)
 		return ret;
 
 	/* Do we need to mount a fs and find env there? */
-	ret = environment_check_mount(dev, &path);
-	if (ret)
+	filepath = environment_check_mount(dev, devpath);
+	if (IS_ERR(filepath)) {
+		free(devpath);
 		return ret;
+	}
 
-	dev_dbg(dev, "Setting default environment path to %s\n", path);
-	default_environment_path_set(path);
+	if (filepath)
+		free(devpath);
+	else
+		filepath = devpath;
+
+	dev_dbg(dev, "Setting default environment path to %s\n", filepath);
+	default_environment_path_set(filepath);
+
+	free(filepath);
 
 	return 0;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-07-19  6:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-17  7:29 [PATCH master 1/3] environment: don't leak environment path buffer Ahmad Fatoum
2024-07-17  7:29 ` [PATCH master 2/3] partition: fix use of uninitialized variable in error message Ahmad Fatoum
2024-07-17  7:29 ` [PATCH master 3/3] of: fdt: harden against corrupted reserve map entries Ahmad Fatoum
2024-07-19  6:45 ` [PATCH master 1/3] environment: don't leak environment path buffer Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox