Bug 28770 (CVE-2021-3998)

Summary: Unexpected return value from realpath() for too long results (CVE-2021-3998)
Product: glibc Reporter: Siddhesh Poyarekar <siddhesh>
Component: libcAssignee: Siddhesh Poyarekar <siddhesh>
Status: RESOLVED FIXED    
Severity: normal CC: aurelien, drepper.fsp, fweimer, pgowda.cve, sam
Priority: P2 Flags: siddhesh: security+
Version: 2.35   
Target Milestone: 2.35   
See Also: https://bugzilla.redhat.com/show_bug.cgi?id=2024633
Host: Target:
Build: Last reconfirmed:

Description Siddhesh Poyarekar 2022-01-12 17:45:23 UTC
When the resolved_path argument for realpath is non-NULL and the result is longer than PATH_MAX, the return value is an allocated string instead of resolved_path, which may result in a memory leak since the caller expects resolved_path.

Another problem with this behaviour is that if the caller uses resolved_path instead of the return value from realpath; it may potentially end up using uninitialized memory.

The expected behaviour in case of result being greater than PATH_MAX is to return NULL and set ENAMETOOLONG.
Comment 1 Siddhesh Poyarekar 2022-01-21 17:45:08 UTC
Fixed on trunk:

commit ee8d5e33adb284601c00c94687bc907e10aec9bb
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Thu Jan 13 11:28:36 2022 +0530

    realpath: Set errno to ENAMETOOLONG for result larger than PATH_MAX [BZ #28770]

    realpath returns an allocated string when the result exceeds PATH_MAX,
    which is unexpected when its second argument is not NULL.  This results
    in the second argument (resolved) being uninitialized and also results
    in a memory leak since the caller expects resolved to be the same as the
    returned value.

    Return NULL and set errno to ENAMETOOLONG if the result exceeds
    PATH_MAX.  This fixes [BZ #28770], which is CVE-2021-3998.

    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Comment 2 Sourceware Commits 2022-01-24 16:11:39 UTC
The master branch has been updated by Siddhesh Poyarekar <siddhesh@sourceware.org>:

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

commit 84d2d0fe20bdf94feed82b21b4d7d136db471f03
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Mon Jan 24 21:36:41 2022 +0530

    realpath: Avoid overwriting preexisting error (CVE-2021-3998)
    
    Set errno and failure for paths that are too long only if no other error
    occurred earlier.
    
    Related: BZ #28770
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Comment 3 Sourceware Commits 2022-01-24 16:15:37 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=d084965adc7baa8ea804427cccf973cea556d697

commit d084965adc7baa8ea804427cccf973cea556d697
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Mon Jan 24 21:36:41 2022 +0530

    realpath: Avoid overwriting preexisting error (CVE-2021-3998)
    
    Set errno and failure for paths that are too long only if no other error
    occurred earlier.
    
    Related: BZ #28770
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
    (cherry picked from commit 84d2d0fe20bdf94feed82b21b4d7d136db471f03)
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:14 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=41980af2d77ecaef34ef5470dc76f6137279e47f

commit 41980af2d77ecaef34ef5470dc76f6137279e47f
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
Date:   Mon Jan 24 21:36:41 2022 +0530

    realpath: Avoid overwriting preexisting error (CVE-2021-3998)
    
    Set errno and failure for paths that are too long only if no other error
    occurred earlier.
    
    Related: BZ #28770
    
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
    (cherry picked from commit 84d2d0fe20bdf94feed82b21b4d7d136db471f03)