From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by inbox.vuxu.org (OpenSMTPD) with SMTP id d0a84c97 for ; Wed, 19 Feb 2020 06:48:12 +0000 (UTC) Received: (qmail 15969 invoked by uid 550); 19 Feb 2020 06:48:10 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 15951 invoked from network); 19 Feb 2020 06:48:09 -0000 From: zhangtianci To: "musl@lists.openwall.com" CC: "Songyunlong (Euler)" Thread-Topic: [musl] [PATCH] stdio: Fix fdopen bug Thread-Index: AdXm7jxXQ8np9l1eQwe3NpDH3Z820g== Date: Wed, 19 Feb 2020 06:47:53 +0000 Message-ID: <2dd69ee3939f469ab98a07d5337a8bf0@huawei.com> Accept-Language: en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.108.222.106] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-CFilter-Loop: Reflected Subject: Re: [musl] [PATCH] stdio: Fix fdopen bug > 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 =3D open("file1", O_RDONLY); > > FILE *f =3D 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 > > --- > > 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 =3D (*mode =3D=3D 'r') ? F_NOWR : F_= NORD; > > > > + int fd_flag =3D __syscall(SYS_fcntl, fd, F_GETFL); > > + > > + if (fd_flag =3D=3D -1) return 0; > > + > > + if (((fd_flag & O_ACCMODE) =3D=3D O_RDONLY && !(f->flags & F_NORD)) > || > > + ((fd_flag & O_ACCMODE) =3D=3D O_WRONLY && !(f->flags & > F_NOWR))) { > > + errno =3D EINVAL; > > + return 0; > > + } > > + > > /* Apply close-on-exec flag */ > > if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, > > FD_CLOEXEC); > > > > -- > > 2.17.1 >=20 > Per POSIX this is a "may fail" not a "shall fail". Testing for this is mo= re costly > (see added code/syscalls in the patch) and serves no purpose, which is wh= y > it's not done. >=20 > Rich POSIX's require on fdopen: The application shall ensure that the mode of the stream as expressed = by the mode argument is allowed by the file access mode of the open file desc= ription=20 to which fildes refers. So I think the example above should return EINVAL.