stdio: Fix fdopen bug

Submitted by Zhang Tianci on Feb. 19, 2020, 2:37 a.m.

Details

Message ID 20200219023729.37349-1-zhangtianci1@huawei.com
State New
Series "stdio: Fix fdopen bug"
Headers show

Commit Message

Zhang Tianci Feb. 19, 2020, 2:37 a.m.
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(+)

Patch hide | download patch | download mbox

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);
 

Comments

Rich Felker Feb. 19, 2020, 3:44 a.m.
On Wed, Feb 19, 2020 at 10:37:29AM +0800, Zhang Tianci wrote:
> 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

Per POSIX this is a "may fail" not a "shall fail". Testing for this is
more costly (see added code/syscalls in the patch) and serves no
purpose, which is why it's not done.

Rich