From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 582 invoked from network); 9 Jan 2022 03:13:06 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 9 Jan 2022 03:13:06 -0000 Received: (qmail 18284 invoked by uid 550); 9 Jan 2022 03:13:04 -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 18244 invoked from network); 9 Jan 2022 03:13:03 -0000 Date: Sat, 8 Jan 2022 22:12:51 -0500 From: Rich Felker To: Kaihang Zhang Cc: musl@lists.openwall.com, 2010267516@qq.com Message-ID: <20220109031250.GN7074@brightrain.aerifal.cx> References: <20211015122000.2490-1-kaihang.zhang@smartx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211015122000.2490-1-kaihang.zhang@smartx.com> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [PATCH v2] fix: Truncate the too-long mntent in function getmntent_r On Fri, Oct 15, 2021 at 08:20:00AM -0400, Kaihang Zhang wrote: > In function getmntent_r in source misc/mntent.c, entry that is too long > will be truncated rather than discarded. The caller can tell by errno > whether the supplied buffer is too small, and retry from the beginning > of the file. > --- > src/misc/mntent.c | 53 +++++++++++++++++++++++++++++------------------ > 1 file changed, 33 insertions(+), 20 deletions(-) > > diff --git a/src/misc/mntent.c b/src/misc/mntent.c > index eabb8200..085ce45d 100644 > --- a/src/misc/mntent.c > +++ b/src/misc/mntent.c > @@ -21,12 +21,12 @@ int endmntent(FILE *f) > > struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen) > { > - int cnt, n[8], use_internal = (linebuf == SENTINEL); > - > - mnt->mnt_freq = 0; > - mnt->mnt_passno = 0; > + int use_internal = (linebuf == SENTINEL); > + char *sub; > > do { > + char *end_ptr; > + > if (use_internal) { > getline(&internal_buf, &internal_bufsize, f); > linebuf = internal_buf; > @@ -34,25 +34,38 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle > fgets(linebuf, buflen, f); > } > if (feof(f) || ferror(f)) return 0; > - if (!strchr(linebuf, '\n')) { > + > + end_ptr = strchr(linebuf, '\n'); > + if (end_ptr != NULL) { > + while ((end_ptr[-1] == ' ' || end_ptr[-1] == '\t') && end_ptr != linebuf) end_ptr--; > + *end_ptr = '\0'; Unless I'm misreading, this seems to invoke UB by reading the [-1] index before checking if that's valid. It could be fixed by just changing the order of the comparison expressions, but I'm not clear why this needs to be done anyway. > + } else { > fscanf(f, "%*[^\n]%*[\n]"); > errno = ERANGE; > - return 0; > } > - cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d", > - n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, > - &mnt->mnt_freq, &mnt->mnt_passno); > - } while (cnt < 2 || linebuf[n[0]] == '#'); > - > - linebuf[n[1]] = 0; > - linebuf[n[3]] = 0; > - linebuf[n[5]] = 0; > - linebuf[n[7]] = 0; > - > - mnt->mnt_fsname = linebuf+n[0]; > - mnt->mnt_dir = linebuf+n[2]; > - mnt->mnt_type = linebuf+n[4]; > - mnt->mnt_opts = linebuf+n[6]; > + > + linebuf += strspn(linebuf, " \t"); > + } while (linebuf[0] == '\0' || linebuf[0] == '#'); > + > + mnt->mnt_fsname = strsep(&linebuf, " \t"); > + > + if (linebuf) linebuf += strspn(linebuf, " \t"); > + sub = strsep(&linebuf, " \t"); > + mnt->mnt_dir = sub ? sub : (char *) ""; "" already has type char[1] and decays to char *; no cast is needed here. > + > + if (linebuf) linebuf += strspn(linebuf, " \t"); > + sub = strsep (&linebuf, " \t"); > + mnt->mnt_type = sub ? sub : (char *) ""; > + > + if (linebuf) linebuf += strspn(linebuf, " \t"); > + sub = strsep(&linebuf, " \t"); > + mnt->mnt_opts = sub ? sub : (char *) ""; > + > + switch (linebuf ? sscanf(linebuf, " %d %d", &mnt->mnt_freq, &mnt->mnt_passno) : 0) { > + case 0: mnt->mnt_freq = 0; > + case 1: mnt->mnt_passno = 0; > + case 2: break; > + } > > return mnt; > } > -- > 2.25.4 This is gratuitously rewriting a lot of parsing logic in a form that doesn't seem like it's better, and even if it were, the change is orthogonal to fixing the behavior. I'm sorry for taking so long to get back to you and say this clearly. I do want to move forward on this because I know folks have been waiting on an upstream fix for a long time. But I need a patch accompanied by a clear explanation of the behavioral changes it's making, and just those changes. Or if we're in agreement on what the behavioral changes should be, I can just write the patch. The patch by Alyssa Ross is more minimal and better documented, but I'm not sure it covers everything you're concerned about. Could you let me know whether it does? I'll follow up on that thread about whether there are open issues with it. Rich