[PATCH 09/13] fs: bpkfs: allow opening a file more than once

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


bpkfs keeps the descriptor of the package a data file is read from in
the bpkfs_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("hw_id_1/kernel"); fd2 = open("hw_id_1/kernel");
  close(fd1);
  read(fd2) -> -EBADF

Keep the descriptor in a structure of its own for each open file, as
uimagefs does.

Fixes: 2f677b9b1ed5 ("fs: add BPKFS support")
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 fs/bpkfs.c      | 33 +++++++++++++++++++++++++--------
 include/bpkfs.h |  1 -
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/fs/bpkfs.c b/fs/bpkfs.c
index 62d0229f8e..eabba22cce 100644
--- a/fs/bpkfs.c
+++ b/fs/bpkfs.c
@@ -127,11 +127,19 @@ static struct bpkfs_handle_data *bpkfs_get_by_type(
 	return NULL;
 }
 
+/* an open file, the same one can be open more than once */
+struct bpkfs_file {
+	struct bpkfs_handle_data *d;
+	/* the package for data files, -1 otherwise */
+	int fd;
+};
+
 static int bpkfs_open(struct device *dev, struct file *f, const char *filename)
 {
 	struct bpkfs_handle *priv = dev->priv;
 	struct bpkfs_handle_data *d;
 	struct bpkfs_handle_hw *h;
+	struct bpkfs_file *bf;
 	const char *dir, *file;
 	int ret = -EINVAL;
 	char *tmp = xstrdup(filename);
@@ -151,16 +159,21 @@ static int bpkfs_open(struct device *dev, struct file *f, const char *filename)
 	if (!d)
 		goto out;
 
+	bf = xzalloc(sizeof(*bf));
+	bf->d = d;
+	bf->fd = -1;
+
 	if (!bpkfs_is_crc_file(d)) {
-		d->fd = open(priv->filename, O_RDONLY);
-		if (d->fd < 0) {
-			ret = d->fd;
+		bf->fd = open(priv->filename, O_RDONLY);
+		if (bf->fd < 0) {
+			ret = bf->fd;
+			free(bf);
 			goto out;
 		}
 	}
 
 	f->f_size = d->size;
-	f->private_data = d;
+	f->private_data = bf;
 	ret = 0;
 
 out:
@@ -171,9 +184,12 @@ static int bpkfs_open(struct device *dev, struct file *f, const char *filename)
 
 static int bpkfs_close(struct device *dev, struct file *file)
 {
-	struct bpkfs_handle_data *d = file->private_data;
+	struct bpkfs_file *bf = file->private_data;
+
+	if (bf->fd >= 0)
+		close(bf->fd);
 
-	close(d->fd);
+	free(bf);
 
 	return 0;
 }
@@ -181,13 +197,14 @@ static int bpkfs_close(struct device *dev, struct file *file)
 static int bpkfs_read(struct file *file, void *buf,
 		      size_t insize)
 {
-	struct bpkfs_handle_data *d = file->private_data;
+	struct bpkfs_file *bf = file->private_data;
+	struct bpkfs_handle_data *d = bf->d;
 
 	if (bpkfs_is_crc_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(bf->fd, buf, insize, d->offset + file->f_pos);
 	}
 }
 
diff --git a/include/bpkfs.h b/include/bpkfs.h
index c80dc211c6..252cee90a6 100644
--- a/include/bpkfs.h
+++ b/include/bpkfs.h
@@ -46,7 +46,6 @@ struct bpkfs_handle_data {
 	uint32_t hw_id;
 	uint64_t size;
 
-	int fd;
 	size_t offset; /* offset in the image */
 	size_t pos; /* pos in the data */
 	uint32_t crc;

-- 
2.47.3




More information about the barebox mailing list