mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Matthias Goergens <matthias.goergens@gmail.com>
To: musl@lists.openwall.com
Cc: Matthias Goergens <matthias.goergens@gmail.com>
Subject: [musl] [PATCH] Fix UB in getmntent_r on extremely long lines
Date: Thu, 30 Mar 2023 16:12:20 +0800	[thread overview]
Message-ID: <20230330081220.1128115-1-matthias.goergens@gmail.com> (raw)

8974ef2124118e4ed8cad7ee0534b36e5c584c4e tried to fix mishandling of
extremely long lines.

Here's the relevant code snippet:

```
		len = strlen(linebuf);
		if (len > INT_MAX) continue;
		for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
		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 (linebuf[n[0]] == '#' || n[1]==len);
```

Alas, that introduced undefined behaviour: if the very first line
handled in the function is extremely long, `n` stays uninitialised, and
thus accessing `n[0]` and `n[1]` is UB.

If we handle a few sane lines before hitting a crazy long line, we don't
hit C-level undefined behaviour, but the function arguably still does
the wrong thing.

The man page says:

> The getmntent() and getmntent_r() functions return a pointer to the
> mntent structure or NULL on failure.

So this patch does exactly that: return NULL to inform the caller that
an error occured.
---
 src/misc/mntent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/misc/mntent.c b/src/misc/mntent.c
index d404fbe3..d91c4964 100644
--- a/src/misc/mntent.c
+++ b/src/misc/mntent.c
@@ -43,7 +43,7 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle
 		}
 
 		len = strlen(linebuf);
-		if (len > INT_MAX) continue;
+		if (len > INT_MAX) return NULL;
 		for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
 		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,
-- 
2.40.0


             reply	other threads:[~2023-03-30  8:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-30  8:12 Matthias Goergens [this message]
2023-03-30 13:52 ` Rich Felker

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=20230330081220.1128115-1-matthias.goergens@gmail.com \
    --to=matthias.goergens@gmail.com \
    --cc=musl@lists.openwall.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).