From: Zhang Tianci <zhangtianci1@huawei.com>
To: <musl@lists.openwall.com>
Cc: <zhangtianci1@huawei.com>, <yunlong.song@huawei.com>
Subject: [musl] [PATCH] stdio: Fix fdopen bug
Date: Wed, 19 Feb 2020 10:37:29 +0800 [thread overview]
Message-ID: <20200219023729.37349-1-zhangtianci1@huawei.com> (raw)
Currently, in musl the fdopen doesn't check the consistence between
fd's mode and corresponding file's mode.
For example,
int fd = open("file1", O_RDONLY);
FILE *f = fdopen(fd, "W")
In musl, above code will be Okay.
While according to POSIX, above code (fdopen) will return EINVAL.
Signed-off-by: Zhang Tianci <zhangtianci1@huawei.com>
---
src/stdio/__fdopen.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
index 116e78e..23c4ffd 100644
--- a/src/stdio/__fdopen.c
+++ b/src/stdio/__fdopen.c
@@ -26,6 +26,16 @@ FILE *__fdopen(int fd, const char *mode)
/* Impose mode restrictions */
if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;
+ int fd_flag = __syscall(SYS_fcntl, fd, F_GETFL);
+
+ if (fd_flag == -1) return 0;
+
+ if (((fd_flag & O_ACCMODE) == O_RDONLY && !(f->flags & F_NORD)) ||
+ ((fd_flag & O_ACCMODE) == O_WRONLY && !(f->flags & F_NOWR))) {
+ errno = EINVAL;
+ return 0;
+ }
+
/* Apply close-on-exec flag */
if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
--
2.17.1
next reply other threads:[~2020-02-19 2:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-19 2:37 Zhang Tianci [this message]
2020-02-19 3:44 ` Rich Felker
2020-02-19 6:47 zhangtianci
2020-02-19 14:16 ` Rich Felker
2020-02-19 14:20 ` Jens Gustedt
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=20200219023729.37349-1-zhangtianci1@huawei.com \
--to=zhangtianci1@huawei.com \
--cc=musl@lists.openwall.com \
--cc=yunlong.song@huawei.com \
/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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).