[Bug libstdc++/104161] Potential Security Vulnerability: remove_all and symbolic link

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 25 21:09:28 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104161

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:c8bd4dc8212e43b2f9af08b80df97f90cdb0df4f

commit r12-6866-gc8bd4dc8212e43b2f9af08b80df97f90cdb0df4f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sun Jan 23 21:45:16 2022 +0000

    libstdc++: Avoid symlink race in filesystem::remove_all [PR104161]

    This adds a new internal flag to the filesystem::directory_iterator
    constructor that makes it fail if the path is a symlink that resolves to
    a directory. This prevents filesystem::remove_all from following a
    symlink to a directory, rather than deleting the symlink itself.

    We can also use that new flag in recursive_directory_iterator to ensure
    that we don't follow symlinks if the follow_directory_symlink option is
    not set.

    This also moves an error check in filesystem::remove_all after the while
    loop, so that errors from the directory_iterator constructor are
    reproted, instead of continuing to the filesystem::remove call below.

    libstdc++-v3/ChangeLog:

            PR libstdc++/104161
            * acinclude.m4 (GLIBCXX_CHECK_FILESYSTEM_DEPS): Check for
            fdopendir.
            * config.h.in: Regenerate.
            * configure: Regenerate.
            * src/c++17/fs_dir.cc (_Dir): Add nofollow flag to constructor
            and pass it to base class constructor.
            (directory_iterator): Pass nofollow flag to _Dir constructor.
            (fs::recursive_directory_iterator::increment): Likewise.
            * src/c++17/fs_ops.cc (do_remove_all): Use nofollow option for
            directory_iterator constructor. Move error check outside loop.
            * src/filesystem/dir-common.h (_Dir_base): Add nofollow flag to
            constructor and when it's set use ::open with O_NOFOLLOW and
            O_DIRECTORY.
            * src/filesystem/dir.cc (_Dir): Add nofollow flag to constructor
            and pass it to base class constructor.
            (directory_iterator): Pass nofollow flag to _Dir constructor.
            (fs::recursive_directory_iterator::increment): Likewise.
            * src/filesystem/ops.cc (remove_all): Use nofollow option for
            directory_iterator constructor. Move error check outside loop.


More information about the Gcc-bugs mailing list