tar: protect against Symlink+Hardlink attack
function old new delta create_links_from_list 93 190 +97 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
1 parent
5da20102b5
commit
382008b3f0
2 files changed
+81
-12
No files matched your search
@@ -17,26 +17,64 @@ void FAST_FUNC create_or_remember_link(llist_t **link_placeholders,
|
||||
return;
|
||||
}
|
||||
if (symlink(target, linkname) != 0) {
|
||||
/* shared message */
|
||||
// shared message
|
||||
bb_perror_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
"sym", linkname, target
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static int all_dir_components_are_dirs(char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
char *slash;
|
||||
|
||||
slash = path;
|
||||
while ((slash = strchr(slash, '/')) != NULL) {
|
||||
int ok;
|
||||
*slash = '\0';
|
||||
ok = (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
|
||||
*slash++ = '/';
|
||||
if (!ok)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FAST_FUNC create_links_from_list(llist_t *list)
|
||||
{
|
||||
// This idea sounds better (fewer file ops):
|
||||
// Create hardlinks first, then symlinks.
|
||||
// ^^^ but it breaks the "hardlink to symlink" case:
|
||||
// mkdir dir
|
||||
// >dir/a
|
||||
// ln -s ../dir/a dir/b
|
||||
// ln dir/b dir/c
|
||||
// mkdir new; tar cf - dir/a dir/b dir/c | tar -C new -xvf -
|
||||
// On extract, 'c' will have no 'b' to be a hardlink to.
|
||||
|
||||
while (list) {
|
||||
char *target;
|
||||
char *linkname = list->data + 1;
|
||||
|
||||
target = list->data + 1 + strlen(list->data + 1) + 1;
|
||||
if ((*list->data ? link : symlink) (target, list->data + 1)) {
|
||||
/* shared message */
|
||||
bb_error_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
*list->data ? "hard" : "sym",
|
||||
list->data + 1, target
|
||||
target = linkname + strlen(linkname) + 1;
|
||||
|
||||
// Check whether the linkname has suspicious components:
|
||||
// SYMLINK_OUTSIDE_EXTRACT_DIR/etc/passwd sort of thing.
|
||||
// (Our delayed creation of symlinks is not defending against it,
|
||||
// we could create SYMLINK_OUTSIDE_EXTRACT_DIR ourself!)
|
||||
if (!all_dir_components_are_dirs(linkname)) {
|
||||
errno = ENOTDIR;
|
||||
goto err;
|
||||
}
|
||||
if ((list->data[0] ? link : symlink) (target, linkname)) {
|
||||
// shared message
|
||||
err:
|
||||
bb_perror_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
list->data[0] ? "hard" : "sym",
|
||||
linkname, target
|
||||
);
|
||||
/* Note: GNU tar 1.34 errors out only _after_ all links are (attempted to be) created */
|
||||
// Note: GNU tar 1.34 errors out only _after_ all links are (attempted to be) created
|
||||
}
|
||||
list = list->link;
|
||||
}
|
||||
|
||||
+35
-4
@@ -304,7 +304,7 @@ optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
|
||||
testing "tar does not extract into symlinks" "\
|
||||
>>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
|
||||
" "\
|
||||
tar: can't create symlink 'passwd' to '/tmp/passwd'
|
||||
tar: can't create symlink 'passwd' to '/tmp/passwd': File exists
|
||||
0
|
||||
" \
|
||||
"" "\
|
||||
@@ -324,7 +324,7 @@ optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
|
||||
testing "tar -k does not extract into symlinks" "\
|
||||
>>/tmp/passwd && uudecode -o input && tar xf input -k 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
|
||||
" "\
|
||||
tar: can't create symlink 'passwd' to '/tmp/passwd'
|
||||
tar: can't create symlink 'passwd' to '/tmp/passwd': File exists
|
||||
0
|
||||
" \
|
||||
"" "\
|
||||
@@ -374,7 +374,7 @@ ls symlink/bb_test_evilfile
|
||||
anything.txt
|
||||
symlink
|
||||
symlink/bb_test_evilfile
|
||||
tar: can't create symlink 'symlink' to '/tmp'
|
||||
tar: can't create symlink 'symlink' to '/tmp': File exists
|
||||
1
|
||||
ls: /tmp/bb_test_evilfile: No such file or directory
|
||||
ls: bb_test_evilfile: No such file or directory
|
||||
@@ -400,7 +400,7 @@ mkdir dir
|
||||
ln -s ../dir/a dir/b
|
||||
ln dir/b dir/c
|
||||
mkdir new
|
||||
tar cf - dir/* | tar -C new -xvf - 2>&1
|
||||
tar cf - dir/a dir/b dir/c | tar -C new -xvf - 2>&1
|
||||
' "\
|
||||
dir/a
|
||||
dir/b
|
||||
@@ -410,4 +410,35 @@ dir/c
|
||||
SKIP=
|
||||
cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
|
||||
|
||||
mkdir tar.tempdir && cd tar.tempdir || exit 1
|
||||
testing "tar Symlink+Hardlink attack: create symlink-to-outside and then create a hardlink in it" '\
|
||||
exec 2>&1
|
||||
mkdir 2tar || exit 1
|
||||
(
|
||||
cd 2tar || exit 1
|
||||
ln -s .. gate
|
||||
ln -s gate alias
|
||||
ln -s alias portal
|
||||
> seed
|
||||
ln seed portal/ESCAPED
|
||||
#^^^ the ESCAPED file is actually in ..!
|
||||
tar cf 2tar.tar \
|
||||
alias \
|
||||
gate \
|
||||
portal \
|
||||
seed \
|
||||
portal/ESCAPED
|
||||
rm portal/ESCAPED alias gate portal seed
|
||||
tar xf 2tar.tar
|
||||
echo $?
|
||||
)
|
||||
test -e ESCAPED && echo "ESCAPED!"
|
||||
' "\
|
||||
tar: can't create directory 'portal': No such file or directory
|
||||
tar: can't create hardlink 'portal/ESCAPED' to 'seed': Not a directory
|
||||
1
|
||||
" \
|
||||
"" ""
|
||||
cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
|
||||
|
||||
exit $FAILCOUNT
|
||||
Reference in new issue
Block a user