Bug 28769 (CVE-2021-3999) - Off-by-one buffer overflow/underflow in getcwd() (CVE-2021-3999)
Summary: Off-by-one buffer overflow/underflow in getcwd() (CVE-2021-3999)
Status: RESOLVED FIXED
Alias: CVE-2021-3999
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.35
: P2 normal
Target Milestone: 2.35
Assignee: Siddhesh Poyarekar
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-12 13:44 UTC by Siddhesh Poyarekar
Modified: 2022-08-18 10:01 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
siddhesh: security+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Siddhesh Poyarekar 2022-01-12 13:44:34 UTC
For very long file names, the getcwd syscall returns ENAMETOOLONG, resulting in a call to __getcwd_generic.  That function in turn assumes a size that's suficient for at least a '/', so a size of 1 results in access that's one byte beyond bounds of buf.

We always need a minimum size of 2 bytes, so the glibc function should set ERANGE and return NULL for size of 1 byte.
Comment 1 Sourceware Commits 2022-01-24 06:01:30 UTC
The master branch has been updated by Siddhesh Poyarekar <siddhesh@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e

commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Fri Jan 21 23:32:56 2022 +0530

    getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
    
    No valid path returned by getcwd would fit into 1 byte, so reject the
    size early and return NULL with errno set to ERANGE.  This change is
    prompted by CVE-2021-3999, which describes a single byte buffer
    underflow and overflow 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 linux 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 resolves BZ #28769.
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
    Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Comment 2 Sourceware Commits 2022-01-24 06:46:46 UTC
The release/2.34/master branch has been updated by Siddhesh Poyarekar <siddhesh@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=472e799a5f2102bc0c3206dbd5a801765fceb39c

commit 472e799a5f2102bc0c3206dbd5a801765fceb39c
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Fri Jan 21 23:32:56 2022 +0530

    getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
    
    No valid path returned by getcwd would fit into 1 byte, so reject the
    size early and return NULL with errno set to ERANGE.  This change is
    prompted by CVE-2021-3999, which describes a single byte buffer
    underflow and overflow 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 linux 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 resolves BZ #28769.
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
    Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
    (cherry picked from commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e)
Comment 3 Siddhesh Poyarekar 2022-01-24 06:47:13 UTC
Fixed on trunk and 2.34.
Comment 4 Sourceware Commits 2022-01-24 22:46:02 UTC
The release/2.34/master branch has been updated by Aurelien Jarno <aurel32@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=1b9cd6a7214db1812a20eb3591cf42f9190a5d1a

commit 1b9cd6a7214db1812a20eb3591cf42f9190a5d1a
Author: Aurelien Jarno <aurelien@aurel32.net>
Date:   Mon Jan 24 23:45:03 2022 +0100

    NEWS: add bug entry for BZ #28769 and BZ #28770
Comment 5 Sourceware Commits 2022-01-24 22:49:09 UTC
The release/2.33/master branch has been updated by Aurelien Jarno <aurel32@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=bcdde07537d733eafe1f28cea3084003e140f5ff

commit bcdde07537d733eafe1f28cea3084003e140f5ff
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Fri Jan 21 23:32:56 2022 +0530

    getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
    
    No valid path returned by getcwd would fit into 1 byte, so reject the
    size early and return NULL with errno set to ERANGE.  This change is
    prompted by CVE-2021-3999, which describes a single byte buffer
    underflow and overflow 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 linux 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 resolves BZ #28769.
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
    Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
    (cherry picked from commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e)
Comment 6 Sourceware Commits 2022-08-18 10:01:16 UTC
The release/2.31/master branch has been updated by Aurelien Jarno <aurel32@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4ad1659d8c024953b11c2f0fef2219529171da7e

commit 4ad1659d8c024953b11c2f0fef2219529171da7e
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Fri Jan 21 23:32:56 2022 +0530

    getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
    
    No valid path returned by getcwd would fit into 1 byte, so reject the
    size early and return NULL with errno set to ERANGE.  This change is
    prompted by CVE-2021-3999, which describes a single byte buffer
    underflow and overflow 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 linux 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 resolves BZ #28769.
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
    Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
    (cherry picked from commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e)