mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 1/3] environment: don't leak environment path buffer
Date: Wed, 17 Jul 2024 09:29:11 +0200	[thread overview]
Message-ID: <20240717072913.2830995-1-a.fatoum@pengutronix.de> (raw)

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




             reply	other threads:[~2024-07-17  7:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-17  7:29 Ahmad Fatoum [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240717072913.2830995-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox