From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 09/13] fs: bpkfs: allow opening a file more than once
Date: Wed, 18 Nov 2026 15:51:59 +0100 [thread overview]
Message-ID: <20261118-fs-conformance-fixes-v1-9-59b59ec15d47@pengutronix.de> (raw)
In-Reply-To: <20261118-fs-conformance-fixes-v1-0-59b59ec15d47@pengutronix.de>
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@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
next prev parent reply other threads:[~2026-11-18 14:53 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 14:51 [PATCH 00/13] fs: fixes found by file system conformance tests Sascha Hauer
2026-11-18 14:51 ` [PATCH 01/13] fs: ramfs: zero the rest of a chunk when truncating Sascha Hauer
2026-11-18 14:51 ` [PATCH 02/13] fs: fat: zero-fill files grown by truncate Sascha Hauer
2026-11-18 14:51 ` [PATCH 03/13] fs: keep the file size when O_TRUNC fails Sascha Hauer
2026-11-18 14:51 ` [PATCH 04/13] fs: legacy: return -EPERM when a driver can't create symlinks Sascha Hauer
2026-11-18 14:51 ` [PATCH 05/13] fs: uimagefs: allow opening a file more than once Sascha Hauer
2026-11-18 14:51 ` [PATCH 06/13] fs: nfs: drop the write and truncate stubs Sascha Hauer
2026-11-18 14:51 ` [PATCH 07/13] fs: fat: fix building without CONFIG_FS_FAT_LFN Sascha Hauer
2026-11-18 14:51 ` [PATCH 08/13] fs: bpkfs: skip the data of ignored duplicates Sascha Hauer
2026-11-18 14:51 ` Sascha Hauer [this message]
2026-11-18 14:52 ` [PATCH 10/13] fs: bpkfs: make room for the NUL after the checksum string Sascha Hauer
2026-11-18 14:52 ` [PATCH 11/13] fs: pstore: read from the file position Sascha Hauer
2026-11-18 14:52 ` [PATCH 12/13] fs: pstore: don't free the records when unmounting Sascha Hauer
2026-11-18 14:52 ` [PATCH 13/13] fs: efivarfs: zero-fill variables grown by truncate 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=20261118-fs-conformance-fixes-v1-9-59b59ec15d47@pengutronix.de \
--to=s.hauer@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