mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] fs: Check for correct open mode
Date: Fri, 22 Jul 2016 12:00:11 +0200	[thread overview]
Message-ID: <1469181611-16622-1-git-send-email-s.hauer@pengutronix.de> (raw)

Check that readonly openen files is not written to and writeonly
files is not read from.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 fs/fs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/fs.c b/fs/fs.c
index f63a2dd..b7e7c63 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -738,6 +738,11 @@ static ssize_t __read(FILE *f, void *buf, size_t count)
 	struct fs_driver_d *fsdrv;
 	int ret;
 
+	if ((f->flags & O_ACCMODE) == O_WRONLY) {
+		ret = -EBADF;
+		goto out;
+	}
+
 	fsdrv = f->fsdev->driver;
 
 	if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size)
@@ -747,7 +752,7 @@ static ssize_t __read(FILE *f, void *buf, size_t count)
 		return 0;
 
 	ret = fsdrv->read(&f->fsdev->dev, f, buf, count);
-
+out:
 	if (ret < 0)
 		errno = -ret;
 	return ret;
@@ -796,6 +801,11 @@ static ssize_t __write(FILE *f, const void *buf, size_t count)
 	struct fs_driver_d *fsdrv;
 	int ret;
 
+	if (!(f->flags & O_ACCMODE)) {
+		ret = -EBADF;
+		goto out;
+	}
+
 	fsdrv = f->fsdev->driver;
 	if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size) {
 		ret = fsdrv->truncate(&f->fsdev->dev, f, f->pos + count);
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2016-07-22 10:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1469181611-16622-1-git-send-email-s.hauer@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