[PATCH v3 8/9] fs: Report errors for out-of-bounds protect operations

Oleksij Rempel o.rempel at pengutronix.de
Wed Jun 11 23:58:11 PDT 2025


The protect() function previously masked errors for out-of-bounds
requests. It would either return success for offsets already beyond
file/partition size or clamp the count for ranges extending beyond the
boundary. This behavior was introduced by commit 6815e0d05480 ("fs:
limit flash erase and protect to the partiton boundary") to address SPI
flash wrap-around issues.

This masking prevented shell commands from reporting failures for
invalid ranges, which could mislead users and scripts. For example,
underlying driver errors like -EINVAL from NVMEM were not propagated.

This patch modifies protect() to return appropriate error codes (-ENXIO
or -EINVAL) for such out-of-bounds conditions, instead of silently
succeeding or clamping.

Fixes: 6815e0d05480 ("fs: limit flash erase and protect to the partiton boundary")
Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
---
 fs/fs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 7bc94d7b9e6c..dd6af3cef874 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -652,9 +652,9 @@ int protect(int fd, size_t count, loff_t offset, int prot)
 	if (IS_ERR(f))
 		return -errno;
 	if (offset >= f->f_size)
-		return 0;
-	if (count > f->f_size - offset)
-		count = f->f_size - offset;
+		return errno_set(-ENXIO);
+	if (!count  || count > f->f_size - offset)
+		return errno_set(-EINVAL);
 
 	fsdrv = f->fsdev->driver;
 
-- 
2.39.5




More information about the barebox mailing list