From 14374c5fb5b0bfce379823b24aaecad27e85a3e4 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Mar 2017 10:46:47 -0700 Subject: [PATCH 1/6] s3: smbd: Fix incorrect logic exposed by fix for the security bug 12496 (CVE-2017-2619). In a UNIX filesystem, the names "." and ".." by definition can *never* be symlinks - they are already reserved names. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Uri Simchoni (cherry picked from commit ae17bebd250bdde5614b2ac17e53512f19fe9b68) --- source3/smbd/vfs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c index 35f560b8676..5133fe5c2fd 100644 --- a/source3/smbd/vfs.c +++ b/source3/smbd/vfs.c @@ -1307,8 +1307,11 @@ NTSTATUS check_reduced_name(connection_struct *conn, const char *fname) /* fname can't have changed in resolved_path. */ const char *p = &resolved_name[rootdir_len]; - /* *p can be '\0' if fname was "." */ - if (*p == '\0' && ISDOT(fname)) { + /* + * UNIX filesystem semantics, names consisting + * only of "." or ".." CANNOT be symlinks. + */ + if (ISDOT(fname) || ISDOTDOT(fname)) { goto out; } -- 2.12.2.564.g063fe858b8-goog From 6b51a72471358b8a9793ef4040759282658a6631 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Mar 2017 11:48:25 -0700 Subject: [PATCH 2/6] s3: Test for CVE-2017-2619 regression with "follow symlinks = no". BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Uri Simchoni Back-ported from commit 782172a9bef0040981d20e49519b13dd744df6a0 --- selftest/target/Samba3.pm | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm index 013e8d5502e..d080a910b50 100755 --- a/selftest/target/Samba3.pm +++ b/selftest/target/Samba3.pm @@ -1245,6 +1245,9 @@ sub provision($$$$$$$$) my $shadow_shrdir="$shadow_basedir/share"; push(@dirs,$shadow_shrdir); + my $nosymlinks_shrdir="$shrdir/nosymlinks"; + push(@dirs,$nosymlinks_shrdir); + # this gets autocreated by winbindd my $wbsockdir="$prefix_abs/winbindd"; my $wbsockprivdir="$lockdir/winbindd_privileged"; @@ -1858,6 +1861,10 @@ sub provision($$$$$$$$) copy = tmp acl_xattr:ignore system acls = yes acl_xattr:default acl style = posix +[nosymlinks] + copy = tmp + path = $nosymlinks_shrdir + follow symlinks = no [acl_xattr_ign_sysacl_windows] copy = tmp acl_xattr:ignore system acls = yes -- 2.12.2.564.g063fe858b8-goog From 7ce8757df6cb59c1ae0b14f1b4061b8695db919b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Mar 2017 22:07:50 -0700 Subject: [PATCH 3/6] s3: Fixup test for CVE-2017-2619 regression with "follow symlinks = no" Use correct bash operators (not string operators). Add missing "return". BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 037297a1c50e90a0092e3b94f472623f41ccc015) --- source3/script/tests/test_smbclient_s3.sh | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/source3/script/tests/test_smbclient_s3.sh b/source3/script/tests/test_smbclient_s3.sh index 22849bd5031..330ceead09c 100755 --- a/source3/script/tests/test_smbclient_s3.sh +++ b/source3/script/tests/test_smbclient_s3.sh @@ -1096,6 +1096,76 @@ EOF fi } +# Test follow symlinks can't access symlinks +test_nosymlinks() +{ +# Setup test dirs. + slink_name="$LOCAL_PATH/nosymlinks/source" + slink_target="$LOCAL_PATH/nosymlinks/target" + mkdir_target="$LOCAL_PATH/nosymlinks/a" + + rm -f $slink_target + rm -f $slink_name + rm -rf $mkdir_target + + touch $slink_target + ln -s $slink_target $slink_name + +# Getting a file through a symlink name should fail. + tmpfile=$PREFIX/smbclient_interactive_prompt_commands + cat > $tmpfile < $tmpfile < Date: Mon, 27 Mar 2017 17:04:58 -0700 Subject: [PATCH 4/6] s3: smbd: Fix "follow symlink = no" regression part 2. Add an extra paramter to cwd_name to check_reduced_name(). If cwd_name == NULL then fname is a client given path relative to the root path of the share. If cwd_name != NULL then fname is a client given path relative to cwd_name. cwd_name is relative to the root path of the share. Not yet used, logic added in the next commit. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit 83e30cb48859b412b76572b6a3ba84d8fde167af) --- source3/smbd/filename.c | 2 +- source3/smbd/open.c | 2 +- source3/smbd/proto.h | 4 +++- source3/smbd/vfs.c | 10 +++++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/source3/smbd/filename.c b/source3/smbd/filename.c index efe52a04328..2d85e8de0b5 100644 --- a/source3/smbd/filename.c +++ b/source3/smbd/filename.c @@ -1242,7 +1242,7 @@ NTSTATUS check_name(connection_struct *conn, const char *name) } if (!lp_widelinks(SNUM(conn)) || !lp_follow_symlinks(SNUM(conn))) { - status = check_reduced_name(conn,name); + status = check_reduced_name(conn, NULL, name); if (!NT_STATUS_IS_OK(status)) { DEBUG(5,("check_name: name %s failed with %s\n",name, nt_errstr(status))); diff --git a/source3/smbd/open.c b/source3/smbd/open.c index 08d14cbb72f..c62ffe47612 100644 --- a/source3/smbd/open.c +++ b/source3/smbd/open.c @@ -549,7 +549,7 @@ static int non_widelink_open(struct connection_struct *conn, } /* Ensure the relative path is below the share. */ - status = check_reduced_name(conn, final_component); + status = check_reduced_name(conn, parent_dir, final_component); if (!NT_STATUS_IS_OK(status)) { saved_errno = map_errno_from_nt_status(status); goto out; diff --git a/source3/smbd/proto.h b/source3/smbd/proto.h index 50ede9d6993..61bd33aae8c 100644 --- a/source3/smbd/proto.h +++ b/source3/smbd/proto.h @@ -1227,7 +1227,9 @@ const char *vfs_readdirname(connection_struct *conn, void *p, SMB_STRUCT_STAT *sbuf, char **talloced); int vfs_ChDir(connection_struct *conn, const char *path); char *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn); -NTSTATUS check_reduced_name(connection_struct *conn, const char *fname); +NTSTATUS check_reduced_name(connection_struct *conn, + const char *cwd_name, + const char *fname); NTSTATUS check_reduced_name_with_privilege(connection_struct *conn, const char *fname, struct smb_request *smbreq); diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c index 5133fe5c2fd..95a8fc1ba62 100644 --- a/source3/smbd/vfs.c +++ b/source3/smbd/vfs.c @@ -1179,9 +1179,17 @@ NTSTATUS check_reduced_name_with_privilege(connection_struct *conn, /******************************************************************* Reduce a file name, removing .. elements and checking that it is below dir in the heirachy. This uses realpath. + + If cwd_name == NULL then fname is a client given path relative + to the root path of the share. + + If cwd_name != NULL then fname is a client given path relative + to cwd_name. cwd_name is relative to the root path of the share. ********************************************************************/ -NTSTATUS check_reduced_name(connection_struct *conn, const char *fname) +NTSTATUS check_reduced_name(connection_struct *conn, + const char *cwd_name, + const char *fname) { char *resolved_name = NULL; bool allow_symlinks = true; -- 2.12.2.564.g063fe858b8-goog From 8aa3a986110485793a00115998a9e2acd86be5dd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Mar 2017 17:09:38 -0700 Subject: [PATCH 5/6] s3: smbd: Fix "follow symlink = no" regression part 2. Use the cwd_name parameter to reconstruct the original client name for symlink testing. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme (cherry picked from commit e182a4d39e86c9694e255efdf6ee2ea3ccb9af4a) --- source3/smbd/vfs.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c index 95a8fc1ba62..b7364b7c7bd 100644 --- a/source3/smbd/vfs.c +++ b/source3/smbd/vfs.c @@ -1192,6 +1192,7 @@ NTSTATUS check_reduced_name(connection_struct *conn, const char *fname) { char *resolved_name = NULL; + char *new_fname = NULL; bool allow_symlinks = true; bool allow_widelinks = false; @@ -1333,11 +1334,32 @@ NTSTATUS check_reduced_name(connection_struct *conn, } p++; + + /* + * If cwd_name is present and not ".", + * then fname is relative to that, not + * the root of the share. Make sure the + * path we check is the one the client + * sent (cwd_name+fname). + */ + if (cwd_name != NULL && !ISDOT(cwd_name)) { + new_fname = talloc_asprintf(talloc_tos(), + "%s/%s", + cwd_name, + fname); + if (new_fname == NULL) { + SAFE_FREE(resolved_name); + return NT_STATUS_NO_MEMORY; + } + fname = new_fname; + } + if (strcmp(fname, p)!=0) { DEBUG(2, ("check_reduced_name: Bad access " "attempt: %s is a symlink to %s\n", fname, p)); SAFE_FREE(resolved_name); + TALLOC_FREE(new_fname); return NT_STATUS_ACCESS_DENIED; } } @@ -1347,6 +1369,7 @@ NTSTATUS check_reduced_name(connection_struct *conn, DBG_INFO("%s reduced to %s\n", fname, resolved_name); SAFE_FREE(resolved_name); + TALLOC_FREE(new_fname); return NT_STATUS_OK; } -- 2.12.2.564.g063fe858b8-goog From ecca2cfade2c58f01f3dfeb8482f31d19c4b6d2c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 27 Mar 2017 22:10:29 -0700 Subject: [PATCH 6/6] s3: Test for CVE-2017-2619 regression with "follow symlinks = no" - part 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add tests for regular access. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12721 Signed-off-by: Jeremy Allison Reviewed-by: Ralph Boehme Autobuild-User(master): Ralph Böhme Autobuild-Date(master): Tue Mar 28 17:05:27 CEST 2017 on sn-devel-144 (cherry picked from commit 4e734fcd1bf82c08aa303ce44e9735acccffcf06) --- source3/script/tests/test_smbclient_s3.sh | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/source3/script/tests/test_smbclient_s3.sh b/source3/script/tests/test_smbclient_s3.sh index 330ceead09c..9bff883f63f 100755 --- a/source3/script/tests/test_smbclient_s3.sh +++ b/source3/script/tests/test_smbclient_s3.sh @@ -1103,14 +1103,22 @@ test_nosymlinks() slink_name="$LOCAL_PATH/nosymlinks/source" slink_target="$LOCAL_PATH/nosymlinks/target" mkdir_target="$LOCAL_PATH/nosymlinks/a" + dir1="$LOCAL_PATH/nosymlinks/foo" + dir2="$LOCAL_PATH/nosymlinks/foo/bar" + get_target="$LOCAL_PATH/nosymlinks/foo/bar/testfile" rm -f $slink_target rm -f $slink_name rm -rf $mkdir_target + rm -rf $dir1 touch $slink_target ln -s $slink_target $slink_name + mkdir $dir1 + mkdir $dir2 + touch $get_target + # Getting a file through a symlink name should fail. tmpfile=$PREFIX/smbclient_interactive_prompt_commands cat > $tmpfile < $tmpfile <