[PATCH master 1/3] environment: don't leak environment path buffer

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jul 17 00:29:11 PDT 2024


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 at 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




More information about the barebox mailing list