From: Siddhesh Poyarekar <siddhesh@sourceware.org>
To: Andreas Schwab <schwab@linux-m68k.org>
Cc: Siddhesh Poyarekar via Libc-alpha <libc-alpha@sourceware.org>,
fweimer@redhat.com, Qualys Security Advisory <qsa@qualys.com>
Subject: Re: [PATCH 3/3] getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
Date: Tue, 18 Jan 2022 20:10:41 +0530 [thread overview]
Message-ID: <4d1dd48b-20e9-10a9-d438-586298efa075@sourceware.org> (raw)
In-Reply-To: <87o8496t9h.fsf@igel.home>
On 18/01/2022 19:27, Andreas Schwab wrote:
> On Jan 18 2022, Siddhesh Poyarekar wrote:
>
>> Without the patch
>
> What's your point? I don't understand what you are trying to say.
>
Let me start over.
getcwd in its current form may underflow and overflow the user supplied
buffer when *all* of the following conditions are met:
- The buffer size (i.e. the second argument of getcwd) is 1 byte
- The current working directory is too long
- '/' is also mounted on the current working directory
Sequence of events:
- In sysdeps/unix/sysv/linux/getcwd.c, the syscall returns ENAMETOOLONG
because the kernel checks for name length before it checks buffer size
- The code falls back to the generic getcwd in sysdeps/posix
- In the generic func, the buf[0] is set to '\0' on line 250
- this while loop on line 262 is bypassed:
while (!(thisdev == rootdev && thisino == rootino))
since the rootfs (/) is bind mounted onto the directory and the flow
goes on to line 449, where it puts a '/' in the byte before the buffer.
- Finally on line 458, it moves 2 bytes (the underflowed byte and the
'\0') to the buf[0] and buf[1], resulting in a 1 byte buffer overflow.
- buf is returned on line 469 and errno is not set.
This fix avoids the underflow+overflow by shortcircuiting early and
returns NULL, setting errno to ERANGE for 1 byte buffers because they
can never be reasonably used to return a valid path.
Siddhesh
next prev parent reply other threads:[~2022-01-18 14:40 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-18 9:07 [PATCH 0/3] Fixes for CVE-2021-3998 and CVE-2021-3999 Siddhesh Poyarekar
2022-01-18 9:07 ` [PATCH 1/3] support: Add helpers to create paths longer than PATH_MAX Siddhesh Poyarekar
2022-01-18 19:42 ` Adhemerval Zanella
2022-01-19 1:33 ` Siddhesh Poyarekar
2022-01-18 9:07 ` [PATCH 2/3] realpath: Set errno to ENAMETOOLONG for result larger than PATH_MAX [BZ #28770] Siddhesh Poyarekar
2022-01-18 19:43 ` Adhemerval Zanella
2022-01-18 9:07 ` [PATCH 3/3] getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999) Siddhesh Poyarekar
2022-01-18 11:52 ` Andreas Schwab
2022-01-18 13:10 ` Siddhesh Poyarekar
2022-01-18 13:13 ` Andreas Schwab
2022-01-18 13:16 ` Siddhesh Poyarekar
2022-01-18 13:30 ` Andreas Schwab
2022-01-18 13:33 ` Siddhesh Poyarekar
2022-01-18 13:41 ` Andreas Schwab
2022-01-18 13:45 ` Siddhesh Poyarekar
2022-01-18 13:56 ` Siddhesh Poyarekar
2022-01-18 13:57 ` Andreas Schwab
2022-01-18 14:40 ` Siddhesh Poyarekar [this message]
2022-01-18 13:59 ` Adhemerval Zanella
2022-01-18 14:44 ` Siddhesh Poyarekar
2022-01-18 16:30 ` Adhemerval Zanella
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=4d1dd48b-20e9-10a9-d438-586298efa075@sourceware.org \
--to=siddhesh@sourceware.org \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=qsa@qualys.com \
--cc=schwab@linux-m68k.org \
/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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).