[PATCH 05/13] fs: uimagefs: allow opening a file more than once

Sascha Hauer s.hauer at pengutronix.de
Wed Nov 18 06:51:55 PST 2026


uimagefs keeps the descriptor of the uImage a data file is read from in
the uimagefs_handle_data of that file, which all opens of the file
share. Opening it a second time overwrites the descriptor of the first,
which is leaked, and closing either closes the one the other reads
from:

  fd1 = open("data0"); fd2 = open("data0");
  close(fd1);
  read(fd2) -> -EBADF

Keep the descriptor in a structure of its own for each open file.

Fixes: 12158a28d4 ("fs: add uimagefs")

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 fs/uimagefs.c      | 36 ++++++++++++++++++++++++++++--------
 include/uimagefs.h |  1 -
 2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/fs/uimagefs.c b/fs/uimagefs.c
index b805b357ac..52dc7182ef 100644
--- a/fs/uimagefs.c
+++ b/fs/uimagefs.c
@@ -68,10 +68,18 @@ static struct uimagefs_handle_data *uimagefs_get_by_name(
 	return NULL;
 }
 
+/* an open file, the same one can be open more than once */
+struct uimagefs_file {
+	struct uimagefs_handle_data *d;
+	/* the uImage for data files, -1 otherwise */
+	int fd;
+};
+
 static int uimagefs_open(struct device *dev, struct file *file, const char *filename)
 {
 	struct uimagefs_handle *priv = dev->priv;
 	struct uimagefs_handle_data *d;
+	struct uimagefs_file *uf;
 
 	if (filename[0] == '/')
 		filename++;
@@ -80,36 +88,48 @@ static int uimagefs_open(struct device *dev, struct file *file, const char *file
 	if (!d)
 		return -EINVAL;
 
+	uf = xzalloc(sizeof(*uf));
+	uf->d = d;
+	uf->fd = -1;
+
 	if (uimagefs_is_data_file(d)) {
-		d->fd = open(priv->filename, O_RDONLY);
-		if (d->fd < 0)
-			return d->fd;
+		uf->fd = open(priv->filename, O_RDONLY);
+		if (uf->fd < 0) {
+			int ret = uf->fd;
+
+			free(uf);
+			return ret;
+		}
 	}
 
 	file->f_size = d->size;
-	file->private_data = d;
+	file->private_data = uf;
 
 	return 0;
 }
 
 static int uimagefs_close(struct device *dev, struct file *file)
 {
-	struct uimagefs_handle_data *d = file->private_data;
+	struct uimagefs_file *uf = file->private_data;
+
+	if (uf->fd >= 0)
+		close(uf->fd);
 
-	close(d->fd);
+	free(uf);
 
 	return 0;
 }
 
 static int uimagefs_read(struct file *file, void *buf, size_t insize)
 {
-	struct uimagefs_handle_data *d = file->private_data;
+	struct uimagefs_file *uf = file->private_data;
+	struct uimagefs_handle_data *d = uf->d;
 
 	if (!uimagefs_is_data_file(d)) {
 		memcpy(buf, &d->data[file->f_pos], insize);
 		return insize;
 	} else {
-		return pread(d->fd, buf, insize, d->offset + file->f_pos);
+		return pread(uf->fd, buf, insize, d->offset + file->f_pos);
 	}
 }
 
diff --git a/include/uimagefs.h b/include/uimagefs.h
index 6b34982e4e..88cc84b634 100644
--- a/include/uimagefs.h
+++ b/include/uimagefs.h
@@ -31,7 +31,6 @@ struct uimagefs_handle_data {
 	enum uimagefs_type type;
 	uint64_t size;
 
-	int fd;
 	size_t offset; /* offset in the image */
 	size_t pos; /* pos in the data */
 

-- 
2.47.3




More information about the barebox mailing list