diff options
author | Matthew L. Curry <matthew.curry@gmail.com> | 2020-10-11 05:02:24 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-13 18:37:32 +0200 |
commit | 909687ddf0cfbcc3bbc4679f2c67af80bcb954d0 (patch) | |
tree | 1d2ba5c7dd2bd5041a04a9f73ee3fe1417182d4c /Userland/cp.cpp | |
parent | d94f674bbb04be38931f1915156e91a2f3a3faeb (diff) | |
download | serenity-909687ddf0cfbcc3bbc4679f2c67af80bcb954d0.zip |
Userland/cp: Disallow copying directories into themselves
This patch causes cp to investigate whether a directory is being copied
into a subdirectory of itself. It uses realpath(3) to ensure that links
do not confound detection.
Diffstat (limited to 'Userland/cp.cpp')
-rw-r--r-- | Userland/cp.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Userland/cp.cpp b/Userland/cp.cpp index 8f0b6286b0..a19a883834 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -29,6 +29,7 @@ #include <AK/StringBuilder.h> #include <LibCore/ArgsParser.h> #include <LibCore/DirIterator.h> +#include <LibCore/File.h> #include <assert.h> #include <fcntl.h> #include <stdio.h> @@ -174,6 +175,16 @@ bool copy_directory(String src_path, String dst_path) perror("cp: mkdir"); return false; } + + String src_rp = Core::File::real_path_for(src_path); + String dst_rp = Core::File::real_path_for(dst_path); + + if (!dst_rp.is_empty() && dst_rp.starts_with(src_rp)) { + fprintf(stderr, "cp: Cannot copy %s into itself (%s)\n", + src_path.characters(), dst_path.characters()); + return false; + } + Core::DirIterator di(src_path, Core::DirIterator::SkipDots); if (di.has_error()) { fprintf(stderr, "cp: DirIterator: %s\n", di.error_string()); |