The branch, master has been updated
       via  294a1a8 CVE-2017-2619: s3: smbd: Use the new non_widelink_open() 
function.
       via  4301505 CVE-2017-2619: s3: smbd: Add the core functions to prevent 
symlink open races.
       via  592e7fa CVE-2017-2619: s3: smbd: Move special handling of symlink 
errno's into a utility function.
       via  6ff5f0a CVE-2017-2619: s3: smbd: Remove O_NOFOLLOW guards. We 
insist on O_NOFOLLOW existing.
       via  49ed0b0 CVE-2017-2619: s3: smbd: Correctly fallback to 
open_dir_safely if FDOPENDIR not supported on system.
       via  f6dfdf7 CVE-2017-2619: s3: smbd: Move the reference counting and 
destructor setup to just before retuning success.
       via  e68e73b CVE-2017-2619: s3: smbd: OpenDir_fsp() - Fix memory leak on 
error.
       via  86f1523 CVE-2017-2619: s3: smbd: OpenDir_fsp() use early returns.
       via  05a9898 CVE-2017-2619: s3: smbd: Create and use open_dir_safely(). 
Use from OpenDir().
       via  682d597 CVE-2017-2619: s3: smbd: Opendir_internal() early return if 
SMB_VFS_OPENDIR failed.
       via  3461518 CVE-2017-2619: s3: smbd: Create wrapper function for 
OpenDir in preparation for making robust.
       via  0a982be CVE-2017-2619: s4/torture: add SMB2_FIND tests with 
SMB2_CONTINUE_FLAG_REOPEN flag
       via  47b6b6f CVE-2017-2619: s3/smbd: re-open directory after 
dptr_CloseDir()
      from  1e0df57 libwbclient: add WBC_SID_NAME_LABEL

https://git.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 294a1a8374d413d8af506e962c1d310d9ee79ae5
Author: Jeremy Allison <[email protected]>
Date:   Thu Dec 15 13:06:31 2016 -0800

    CVE-2017-2619: s3: smbd: Use the new non_widelink_open() function.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>
    
    Autobuild-User(master): Karolin Seeger <[email protected]>
    Autobuild-Date(master): Thu Mar 23 22:55:04 CET 2017 on sn-devel-144

commit 4301505d977449d1995699010bb04682009aa105
Author: Jeremy Allison <[email protected]>
Date:   Thu Dec 15 13:04:46 2016 -0800

    CVE-2017-2619: s3: smbd: Add the core functions to prevent symlink open 
races.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 592e7fa67b137099b894759e8aa579eea0eb9c4c
Author: Jeremy Allison <[email protected]>
Date:   Thu Dec 15 12:56:08 2016 -0800

    CVE-2017-2619: s3: smbd: Move special handling of symlink errno's into a 
utility function.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 6ff5f0a75a9ec1aeccf50dff97d4c328fd65f219
Author: Jeremy Allison <[email protected]>
Date:   Thu Dec 15 12:52:13 2016 -0800

    CVE-2017-2619: s3: smbd: Remove O_NOFOLLOW guards. We insist on O_NOFOLLOW 
existing.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 49ed0b01d1310f09e321122effed571b8ba852cb
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 12:35:32 2016 -0800

    CVE-2017-2619: s3: smbd: Correctly fallback to open_dir_safely if FDOPENDIR 
not supported on system.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit f6dfdf7d31967076dbd51ceb35145d28381a6f92
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 12:32:07 2016 -0800

    CVE-2017-2619: s3: smbd: Move the reference counting and destructor setup 
to just before retuning success.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit e68e73b8d3c164b882d3cc02685f99f2ea3d2880
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 12:15:59 2016 -0800

    CVE-2017-2619: s3: smbd: OpenDir_fsp() - Fix memory leak on error.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 86f15237a13a4dd2bca7c9cd81a9e54029d0becc
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 12:13:20 2016 -0800

    CVE-2017-2619: s3: smbd: OpenDir_fsp() use early returns.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 05a9898ddae5566884357f8f66e15e5ba102ef49
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 16:35:00 2016 -0800

    CVE-2017-2619: s3: smbd: Create and use open_dir_safely(). Use from 
OpenDir().
    
    Hardens OpenDir against TOC/TOU races.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 682d597ee3b23e9f49e124e1e04e2b288c9a053f
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 16:25:26 2016 -0800

    CVE-2017-2619: s3: smbd: Opendir_internal() early return if SMB_VFS_OPENDIR 
failed.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 34615180279fdeeeb3e91cc3b9574af91c3db589
Author: Jeremy Allison <[email protected]>
Date:   Mon Dec 19 11:55:56 2016 -0800

    CVE-2017-2619: s3: smbd: Create wrapper function for OpenDir in preparation 
for making robust.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Jeremy Allison <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 0a982be89f81ea2448bcd2398358d0503e9adea1
Author: Ralph Boehme <[email protected]>
Date:   Sun Mar 19 18:52:10 2017 +0100

    CVE-2017-2619: s4/torture: add SMB2_FIND tests with 
SMB2_CONTINUE_FLAG_REOPEN flag
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Ralph Boehme <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

commit 47b6b6f8f58efbabd7e4610f51db61dca2bc157c
Author: Ralph Boehme <[email protected]>
Date:   Sun Mar 19 15:58:17 2017 +0100

    CVE-2017-2619: s3/smbd: re-open directory after dptr_CloseDir()
    
    dptr_CloseDir() will close and invalidate the fsp's file descriptor, we
    have to reopen it.
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=12496
    
    Signed-off-by: Ralph Boehme <[email protected]>
    Reviewed-by: Uri Simchoni <[email protected]>

-----------------------------------------------------------------------

Summary of changes:
 source3/smbd/dir.c                  | 161 ++++++++++++++-----
 source3/smbd/open.c                 | 310 +++++++++++++++++++++++++++++++++---
 source3/smbd/smb2_query_directory.c |  17 ++
 source4/torture/smb2/dir.c          |  12 +-
 4 files changed, 434 insertions(+), 66 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source3/smbd/dir.c b/source3/smbd/dir.c
index 3c6f000..1348d12 100644
--- a/source3/smbd/dir.c
+++ b/source3/smbd/dir.c
@@ -1630,7 +1630,8 @@ static int smb_Dir_destructor(struct smb_Dir *dirp)
  Open a directory.
 ********************************************************************/
 
-struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
+static struct smb_Dir *OpenDir_internal(TALLOC_CTX *mem_ctx,
+                       connection_struct *conn,
                        const struct smb_filename *smb_dname,
                        const char *mask,
                        uint32_t attr)
@@ -1642,29 +1643,23 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, 
connection_struct *conn,
                return NULL;
        }
 
-       dirp->conn = conn;
-       dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
+       dirp->dir = SMB_VFS_OPENDIR(conn, smb_dname, mask, attr);
 
-       dirp->dir_smb_fname = cp_smb_filename(dirp, smb_dname);
-       if (!dirp->dir_smb_fname) {
-               errno = ENOMEM;
+       if (!dirp->dir) {
+               DEBUG(5,("OpenDir: Can't open %s. %s\n",
+                       smb_dname->base_name,
+                       strerror(errno) ));
                goto fail;
        }
 
+       dirp->conn = conn;
+       dirp->name_cache_size = lp_directory_name_cache_size(SNUM(conn));
+
        if (sconn && !sconn->using_smb2) {
                sconn->searches.dirhandles_open++;
        }
        talloc_set_destructor(dirp, smb_Dir_destructor);
 
-       dirp->dir = SMB_VFS_OPENDIR(conn, dirp->dir_smb_fname, mask, attr);
-
-       if (!dirp->dir) {
-               DEBUG(5,("OpenDir: Can't open %s. %s\n",
-                       dirp->dir_smb_fname->base_name,
-                       strerror(errno) ));
-               goto fail;
-       }
-
        return dirp;
 
   fail:
@@ -1672,6 +1667,87 @@ struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, 
connection_struct *conn,
        return NULL;
 }
 
+/****************************************************************************
+ Open a directory handle by pathname, ensuring it's under the share path.
+****************************************************************************/
+
+static struct smb_Dir *open_dir_safely(TALLOC_CTX *ctx,
+                                       connection_struct *conn,
+                                       const struct smb_filename *smb_dname,
+                                       const char *wcard,
+                                       uint32_t attr)
+{
+       struct smb_Dir *dir_hnd = NULL;
+       struct smb_filename *smb_fname_cwd = NULL;
+       char *saved_dir = vfs_GetWd(ctx, conn);
+       NTSTATUS status;
+
+       if (saved_dir == NULL) {
+               return NULL;
+       }
+
+       if (vfs_ChDir(conn, smb_dname->base_name) == -1) {
+               goto out;
+       }
+
+       smb_fname_cwd = synthetic_smb_fname(talloc_tos(),
+                                       ".",
+                                       NULL,
+                                       NULL,
+                                       smb_dname->flags);
+       if (smb_fname_cwd == NULL) {
+               goto out;
+       }
+
+       /*
+        * Now the directory is pinned, use
+        * REALPATH to ensure we can access it.
+        */
+       status = check_name(conn, ".");
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       dir_hnd = OpenDir_internal(ctx,
+                               conn,
+                               smb_fname_cwd,
+                               wcard,
+                               attr);
+
+       if (dir_hnd == NULL) {
+               goto out;
+       }
+
+       /*
+        * OpenDir_internal only gets "." as the dir name.
+        * Store the real dir name here.
+        */
+
+       dir_hnd->dir_smb_fname = cp_smb_filename(dir_hnd, smb_dname);
+       if (!dir_hnd->dir_smb_fname) {
+               TALLOC_FREE(dir_hnd);
+               errno = ENOMEM;
+       }
+
+  out:
+
+       vfs_ChDir(conn, saved_dir);
+       TALLOC_FREE(saved_dir);
+       return dir_hnd;
+}
+
+struct smb_Dir *OpenDir(TALLOC_CTX *mem_ctx, connection_struct *conn,
+                       const struct smb_filename *smb_dname,
+                       const char *mask,
+                       uint32_t attr)
+{
+       return open_dir_safely(mem_ctx,
+                               conn,
+                               smb_dname,
+                               mask,
+                               attr);
+}
+
 /*******************************************************************
  Open a directory from an fsp.
 ********************************************************************/
@@ -1685,7 +1761,17 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, 
connection_struct *conn,
        struct smbd_server_connection *sconn = conn->sconn;
 
        if (!dirp) {
-               return NULL;
+               goto fail;
+       }
+
+       if (!fsp->is_directory) {
+               errno = EBADF;
+               goto fail;
+       }
+
+       if (fsp->fh->fd == -1) {
+               errno = EBADF;
+               goto fail;
        }
 
        dirp->conn = conn;
@@ -1697,40 +1783,33 @@ static struct smb_Dir *OpenDir_fsp(TALLOC_CTX *mem_ctx, 
connection_struct *conn,
                goto fail;
        }
 
-       if (sconn && !sconn->using_smb2) {
-               sconn->searches.dirhandles_open++;
-       }
-       talloc_set_destructor(dirp, smb_Dir_destructor);
-
-       if (fsp->is_directory && fsp->fh->fd != -1) {
-               dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
-               if (dirp->dir != NULL) {
-                       dirp->fsp = fsp;
-               } else {
-                       DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s 
returned "
-                               "NULL (%s)\n",
-                               dirp->dir_smb_fname->base_name,
-                               strerror(errno)));
-                       if (errno != ENOSYS) {
-                               return NULL;
-                       }
+       dirp->dir = SMB_VFS_FDOPENDIR(fsp, mask, attr);
+       if (dirp->dir != NULL) {
+               dirp->fsp = fsp;
+       } else {
+               DEBUG(10,("OpenDir_fsp: SMB_VFS_FDOPENDIR on %s returned "
+                       "NULL (%s)\n",
+                       dirp->dir_smb_fname->base_name,
+                       strerror(errno)));
+               if (errno != ENOSYS) {
+                       goto fail;
                }
        }
 
        if (dirp->dir == NULL) {
-               /* FDOPENDIR didn't work. Use OPENDIR instead. */
-               dirp->dir = SMB_VFS_OPENDIR(conn,
-                                       dirp->dir_smb_fname,
+               /* FDOPENDIR is not supported. Use OPENDIR instead. */
+               TALLOC_FREE(dirp);
+               return open_dir_safely(mem_ctx,
+                                       conn,
+                                       fsp->fsp_name,
                                        mask,
                                        attr);
        }
 
-       if (!dirp->dir) {
-               DEBUG(5,("OpenDir_fsp: Can't open %s. %s\n",
-                       dirp->dir_smb_fname->base_name,
-                       strerror(errno) ));
-               goto fail;
+       if (sconn && !sconn->using_smb2) {
+               sconn->searches.dirhandles_open++;
        }
+       talloc_set_destructor(dirp, smb_Dir_destructor);
 
        return dirp;
 
diff --git a/source3/smbd/open.c b/source3/smbd/open.c
index 603a01d..a6e61e7 100644
--- a/source3/smbd/open.c
+++ b/source3/smbd/open.c
@@ -364,6 +364,269 @@ static NTSTATUS check_base_file_access(struct 
connection_struct *conn,
 }
 
 /****************************************************************************
+ Handle differing symlink errno's
+****************************************************************************/
+
+static int link_errno_convert(int err)
+{
+#if defined(ENOTSUP) && defined(OSF1)
+       /* handle special Tru64 errno */
+       if (err == ENOTSUP) {
+               err = ELOOP;
+       }
+#endif /* ENOTSUP */
+#ifdef EFTYPE
+       /* fix broken NetBSD errno */
+       if (err == EFTYPE) {
+               err = ELOOP;
+       }
+#endif /* EFTYPE */
+       /* fix broken FreeBSD errno */
+       if (err == EMLINK) {
+               err = ELOOP;
+       }
+       return err;
+}
+
+static int non_widelink_open(struct connection_struct *conn,
+                       const char *conn_rootdir,
+                       files_struct *fsp,
+                       struct smb_filename *smb_fname,
+                       int flags,
+                       mode_t mode,
+                       unsigned int link_depth);
+
+/****************************************************************************
+ Follow a symlink in userspace.
+****************************************************************************/
+
+static int process_symlink_open(struct connection_struct *conn,
+                       const char *conn_rootdir,
+                       files_struct *fsp,
+                       struct smb_filename *smb_fname,
+                       int flags,
+                       mode_t mode,
+                       unsigned int link_depth)
+{
+       int fd = -1;
+       char *link_target = NULL;
+       int link_len = -1;
+       char *oldwd = NULL;
+       size_t rootdir_len = 0;
+       char *resolved_name = NULL;
+       bool matched = false;
+       int saved_errno = 0;
+
+       /*
+        * Ensure we don't get stuck in a symlink loop.
+        */
+       link_depth++;
+       if (link_depth >= 20) {
+               errno = ELOOP;
+               goto out;
+       }
+
+       /* Allocate space for the link target. */
+       link_target = talloc_array(talloc_tos(), char, PATH_MAX);
+       if (link_target == NULL) {
+               errno = ENOMEM;
+               goto out;
+       }
+
+       /* Read the link target. */
+       link_len = SMB_VFS_READLINK(conn,
+                               smb_fname->base_name,
+                               link_target,
+                               PATH_MAX - 1);
+       if (link_len == -1) {
+               goto out;
+       }
+
+       /* Ensure it's at least null terminated. */
+       link_target[link_len] = '\0';
+
+       /* Convert to an absolute path. */
+       resolved_name = SMB_VFS_REALPATH(conn, link_target);
+       if (resolved_name == NULL) {
+               goto out;
+       }
+
+       /*
+        * We know conn_rootdir starts with '/' and
+        * does not end in '/'. FIXME ! Should we
+        * smb_assert this ?
+        */
+       rootdir_len = strlen(conn_rootdir);
+
+       matched = (strncmp(conn_rootdir, resolved_name, rootdir_len) == 0);
+       if (!matched) {
+               errno = EACCES;
+               goto out;
+       }
+
+       /*
+        * Turn into a path relative to the share root.
+        */
+       if (resolved_name[rootdir_len] == '\0') {
+               /* Link to the root of the share. */
+               smb_fname->base_name = talloc_strdup(talloc_tos(), ".");
+               if (smb_fname->base_name == NULL) {
+                       errno = ENOMEM;
+                       goto out;
+               }
+       } else if (resolved_name[rootdir_len] == '/') {
+               smb_fname->base_name = &resolved_name[rootdir_len+1];
+       } else {
+               errno = EACCES;
+               goto out;
+       }
+
+       oldwd = vfs_GetWd(talloc_tos(), conn);
+       if (oldwd == NULL) {
+               goto out;
+       }
+
+       /* Ensure we operate from the root of the share. */
+       if (vfs_ChDir(conn, conn_rootdir) == -1) {
+               goto out;
+       }
+
+       /* And do it all again.. */
+       fd = non_widelink_open(conn,
+                               conn_rootdir,
+                               fsp,
+                               smb_fname,
+                               flags,
+                               mode,
+                               link_depth);
+       if (fd == -1) {
+               saved_errno = errno;
+       }
+
+  out:
+
+       SAFE_FREE(resolved_name);
+       TALLOC_FREE(link_target);
+       if (oldwd != NULL) {
+               int ret = vfs_ChDir(conn, oldwd);
+               if (ret == -1) {
+                       smb_panic("unable to get back to old directory\n");
+               }
+               TALLOC_FREE(oldwd);
+       }
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
+       return fd;
+}
+
+/****************************************************************************
+ Non-widelink open.
+****************************************************************************/
+
+static int non_widelink_open(struct connection_struct *conn,
+                       const char *conn_rootdir,
+                       files_struct *fsp,
+                       struct smb_filename *smb_fname,
+                       int flags,
+                       mode_t mode,
+                       unsigned int link_depth)
+{
+       NTSTATUS status;
+       int fd = -1;
+       struct smb_filename *smb_fname_rel = NULL;
+       int saved_errno = 0;
+       char *oldwd = NULL;
+       char *parent_dir = NULL;
+       const char *final_component = NULL;
+
+       if (!parent_dirname(talloc_tos(),
+                       smb_fname->base_name,
+                       &parent_dir,
+                       &final_component)) {
+               goto out;
+       }
+
+       oldwd = vfs_GetWd(talloc_tos(), conn);
+       if (oldwd == NULL) {
+               goto out;
+       }
+
+       /* Pin parent directory in place. */
+       if (vfs_ChDir(conn, parent_dir) == -1) {
+               goto out;
+       }
+
+       /* Ensure the relative path is below the share. */
+       status = check_reduced_name(conn, final_component);
+       if (!NT_STATUS_IS_OK(status)) {
+               saved_errno = map_errno_from_nt_status(status);
+               goto out;
+       }
+
+       smb_fname_rel = synthetic_smb_fname(talloc_tos(),
+                               final_component,
+                               smb_fname->stream_name,
+                               &smb_fname->st,
+                               smb_fname->flags);
+
+       flags |= O_NOFOLLOW;
+
+       {
+               struct smb_filename *tmp_name = fsp->fsp_name;
+               fsp->fsp_name = smb_fname_rel;
+               fd = SMB_VFS_OPEN(conn, smb_fname_rel, fsp, flags, mode);
+               fsp->fsp_name = tmp_name;
+       }
+
+       if (fd == -1) {
+               saved_errno = link_errno_convert(errno);
+               if (saved_errno == ELOOP) {
+                       if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
+                               /* Never follow symlinks on posix open. */
+                               goto out;
+                       }
+                       if (!lp_follow_symlinks(SNUM(conn))) {
+                               /* Explicitly no symlinks. */
+                               goto out;
+                       }
+                       /*
+                        * We have a symlink. Follow in userspace
+                        * to ensure it's under the share definition.
+                        */
+                       fd = process_symlink_open(conn,
+                                       conn_rootdir,
+                                       fsp,
+                                       smb_fname_rel,
+                                       flags,
+                                       mode,
+                                       link_depth);
+                       if (fd == -1) {
+                               saved_errno =
+                                       link_errno_convert(errno);
+                       }
+               }
+       }
+
+  out:
+
+       TALLOC_FREE(parent_dir);
+       TALLOC_FREE(smb_fname_rel);
+
+       if (oldwd != NULL) {
+               int ret = vfs_ChDir(conn, oldwd);
+               if (ret == -1) {
+                       smb_panic("unable to get back to old directory\n");
+               }
+               TALLOC_FREE(oldwd);
+       }
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
+       return fd;
+}
+
+/****************************************************************************
  fd support routines - attempt to do a dos_open.
 ****************************************************************************/
 
@@ -375,8 +638,7 @@ NTSTATUS fd_open(struct connection_struct *conn,
        struct smb_filename *smb_fname = fsp->fsp_name;
        NTSTATUS status = NT_STATUS_OK;
 
-#ifdef O_NOFOLLOW
-       /* 
+       /*
         * Never follow symlinks on a POSIX client. The


-- 
Samba Shared Repository

Reply via email to