smb: client: guarantee refcounted children from parent session
Subsystems (2)
PATCH 1/4 smb: client: guarantee refcounted children from parent session
Avoid potential use-after-free bugs when walking DFS referrals, mounting and performing DFS failover by ensuring that all children from parent @tcon->ses are also refcounted. They're all needed across the entire DFS mount. Get rid of @tcon->dfs_ses_list while we're at it, too.
--- fs/smb/client/cifsglob.h | 2 -- fs/smb/client/cifsproto.h | 20 +++++++-------- fs/smb/client/connect.c | 25 +++++++++++++++---- fs/smb/client/dfs.c | 51 ++++++++++++++++++--------------------- fs/smb/client/dfs.h | 33 ++++++++++++++++--------- fs/smb/client/dfs_cache.c | 11 +-------- fs/smb/client/misc.c | 6 ----- 7 files changed, 76 insertions(+), 72 deletions(-)
fs/smb/client/cifsglob.h +0 -2
@@ -1281,7 +1281,6 @@ struct cifs_tcon {struct cached_fids *cfids;/* BB add field for back pointer to sb struct(s)? */ #ifdef CONFIG_CIFS_DFS_UPCALL -struct list_head dfs_ses_list;struct delayed_work dfs_cache_work;#endif struct delayed_work query_interfaces;/* query interfaces workqueue job */
@@ -1804,7 +1803,6 @@ struct cifs_mount_ctx {struct TCP_Server_Info *server;struct cifs_ses *ses;struct cifs_tcon *tcon; -struct list_head dfs_ses_list; };static inline void __free_dfs_info_param (struct dfs_info3_param *param)
fs/smb/client/cifsproto.h +10 -10
@@ -725,31 +725,31 @@ struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon);void cifs_put_tcon_super (struct super_block *sb);int cifs_wait_for_server_reconnect (struct TCP_Server_Info *server,bool retry); -/* Put references of @ses and @ses->dfs_root_ses */ +/* Put references of @ses and its children */ static inline void cifs_put_smb_ses (struct cifs_ses *ses) { -struct cifs_ses *rses = ses->dfs_root_ses; +struct cifs_ses *next; -__cifs_put_smb_ses (ses); -if (rses) -__cifs_put_smb_ses (rses); +do { + next = ses->dfs_root_ses; +__cifs_put_smb_ses (ses); + }while ((ses = next)); } -/* Get an active reference of @ses and @ses->dfs_root_ses. +/* Get an active reference of @ses and its children. * * NOTE: make sure to call this function when incrementing reference count of * @ses to ensure that any DFS root session attached to it (@ses->dfs_root_ses) * will also get its reference count incremented. * -* cifs_put_smb_ses() will put both references, so call it when you're done. +* cifs_put_smb_ses() will put all references, so call it when you're done. */ static inline void cifs_smb_ses_inc_refcount (struct cifs_ses *ses) {lockdep_assert_held (&cifs_tcp_ses_lock); - ses->ses_count++; -if (ses->dfs_root_ses) - ses->dfs_root_ses->ses_count++; +for (; ses; ses = ses->dfs_root_ses) + ses->ses_count++; }static inline bool dfs_src_pathname_equal (const char *s1,const char *s2)
fs/smb/client/connect.c +20 -5
@@ -1866,6 +1866,9 @@ static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx) ctx->sectype != ses->sectype)return 0 ; +if (ctx->dfs_root_ses != ses->dfs_root_ses) +return 0 ; +/* * If an existing session is limited to less channels than * requested, it should not be reused
@@ -2358,9 +2361,9 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)* need to lock before changing something in the session. */ spin_lock (&cifs_tcp_ses_lock); +if (ctx->dfs_root_ses) +cifs_smb_ses_inc_refcount (ctx->dfs_root_ses); ses->dfs_root_ses = ctx->dfs_root_ses; -if (ses->dfs_root_ses) - ses->dfs_root_ses->ses_count++;list_add (&ses->smb_ses_list, &server->smb_ses_list);spin_unlock (&cifs_tcp_ses_lock);
@@ -3311,6 +3314,9 @@ void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)cifs_put_smb_ses (mnt_ctx->ses); else if (mnt_ctx->server) cifs_put_tcp_session(mnt_ctx->server, 0); + mnt_ctx->ses =NULL ; + mnt_ctx->tcon =NULL ; + mnt_ctx->server =NULL ; mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS ;free_xid (mnt_ctx->xid); }
@@ -3589,8 +3595,6 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)bool isdfs;int rc; -INIT_LIST_HEAD (&mnt_ctx.dfs_ses_list); - rc =dfs_mount_share (&mnt_ctx, &isdfs);if (rc) goto error;
@@ -3621,7 +3625,6 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)return rc; error: -dfs_put_root_smb_sessions (&mnt_ctx.dfs_ses_list);cifs_mount_put_conns (&mnt_ctx);return rc; }
@@ -3636,6 +3639,18 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) goto error; rc =cifs_mount_get_tcon (&mnt_ctx); +if (!rc) { +/* +* Prevent superblock from being created with any missing +* connections. +*/ +if (WARN_ON (!mnt_ctx->server)) + rc = -EHOSTDOWN ; +else if (WARN_ON (!mnt_ctx->ses)) + rc = -EACCES ; +else if (WARN_ON (!mnt_ctx->tcon)) + rc = -ENOENT ; + }if (rc) goto error;
fs/smb/client/dfs.c +24 -27
109 lines not on this page · raw message
fs/smb/client/dfs.h +21 -12
@@ -7,7 +7,9 @@#define _CIFS_DFS_H#include "cifsglob.h" +#include "cifsproto.h" #include "fs_context.h" +#include "dfs_cache.h" #include "cifs_unicode.h" #include <linux/namei.h>
@@ -114,11 +116,6 @@ static inline void ref_walk_set_tgt_hint(struct dfs_ref_walk *rw) ref_walk_tit(rw)); } -struct dfs_root_ses { -struct list_head list; -struct cifs_ses *ses; -}; -int dfs_parse_target_referral (const char *full_path,const struct dfs_info3_param *ref,struct smb3_fs_context *ctx);int dfs_mount_share (struct cifs_mount_ctx *mnt_ctx,bool *isdfs);
@@ -133,20 +130,32 @@ static inline int dfs_get_referral(struct cifs_mount_ctx *mnt_ctx, const char *p {struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; +struct cifs_ses *rses = ctx->dfs_root_ses ?: mnt_ctx->ses; -return dfs_cache_find (mnt_ctx->xid, ctx->dfs_root_ses, cifs_sb->local_nls, +return dfs_cache_find (mnt_ctx->xid, rses, cifs_sb->local_nls,cifs_remap (cifs_sb), path, ref, tl); } -static inline void dfs_put_root_smb_sessions (struct list_head *head) +/* +* cifs_get_smb_ses() already guarantees an active reference of +* @ses->dfs_root_ses when a new session is created, so we need to put extra +* references of all DFS root sessions that were used across the mount process +* in dfs_mount_share(). +*/ +static inline void dfs_put_root_smb_sessions (struct cifs_mount_ctx *mnt_ctx) { -struct dfs_root_ses *root, *tmp; +const struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; +struct cifs_ses *ses = ctx->dfs_root_ses; +struct cifs_ses *cur; -list_for_each_entry_safe (root, tmp, head, list) { -list_del_init (&root->list); -cifs_put_smb_ses (root->ses); -kfree (root); +if (!ses) +return ; + +for (cur = ses; cur; cur = cur->dfs_root_ses) { +if (cur->dfs_root_ses) +cifs_put_smb_ses (cur->dfs_root_ses); } +cifs_put_smb_ses (ses); }#endif /* _CIFS_DFS_H */
fs/smb/client/dfs_cache.c +1 -10
@@ -1278,21 +1278,12 @@ int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb)void dfs_cache_refresh (struct work_struct *work) {struct TCP_Server_Info *server; -struct dfs_root_ses *rses;struct cifs_tcon *tcon;struct cifs_ses *ses; tcon = container_of(work, struct cifs_tcon, dfs_cache_work.work); - ses = tcon->ses; - server = ses->server; -mutex_lock (&server->refpath_lock); -if (server->leaf_fullpath) -__refresh_tcon (server->leaf_fullpath +1 , ses, false); -mutex_unlock (&server->refpath_lock); - -list_for_each_entry (rses, &tcon->dfs_ses_list, list) { - ses = rses->ses; +for (ses = tcon->ses; ses; ses = ses->dfs_root_ses) { server = ses->server;mutex_lock (&server->refpath_lock);if (server->leaf_fullpath)
fs/smb/client/misc.c +0 -6
@@ -138,9 +138,6 @@ tcon_info_alloc(bool dir_leases_enabled)atomic_set (&ret_buf->num_local_opens,0 );atomic_set (&ret_buf->num_remote_opens,0 ); ret_buf->stats_from_time =ktime_get_real_seconds (); -#ifdef CONFIG_CIFS_DFS_UPCALL -INIT_LIST_HEAD (&ret_buf->dfs_ses_list); -#endif return ret_buf; }
@@ -156,9 +153,6 @@ tconInfoFree(struct cifs_tcon *tcon)atomic_dec (&tconInfoAllocCount);kfree (tcon->nativeFileSystem);kfree_sensitive (tcon->password); -#ifdef CONFIG_CIFS_DFS_UPCALL -dfs_put_root_smb_sessions (&tcon->dfs_ses_list); -#endif kfree (tcon->origin_fullpath);kfree (tcon); }
Signature · 1 line
2.44.0
PATCH 2/4 smb: client: refresh referral without acquiring refpath_lock
Avoid refreshing DFS referral with refpath_lock acquired as the I/O could block for a while due to a potentially disconnected or slow DFS root server and then making other threads - that use same @server and don't require a DFS root server - unable to make any progress.
--- fs/smb/client/dfs_cache.c | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-)
fs/smb/client/dfs_cache.c +24 -20
@@ -1172,8 +1172,8 @@ static bool is_ses_good(struct cifs_ses *ses)return ret; } -/* Refresh dfs referral of tcon and mark it for reconnect if needed */ -static int __refresh_tcon (const char *path,struct cifs_ses *ses,bool force_refresh) +/* Refresh dfs referral of @ses and mark it for reconnect if needed */ +static void __refresh_ses_referral (struct cifs_ses *ses,bool force_refresh) {struct TCP_Server_Info *server = ses->server;DFS_CACHE_TGT_LIST (old_tl);
@@ -1181,10 +1181,21 @@ static int __refresh_tcon(const char *path, struct cifs_ses *ses, bool force_refbool needs_refresh = false;struct cache_entry *ce;unsigned int xid; +char *path =NULL ;int rc =0 ; xid =get_xid (); +mutex_lock (&server->refpath_lock); +if (server->leaf_fullpath) { + path =kstrdup (server->leaf_fullpath +1 ,GFP_ATOMIC ); +if (!path) + rc = -ENOMEM ; + } +mutex_unlock (&server->refpath_lock); +if (!path) + goto out; +down_read (&htable_rw_lock); ce =lookup_cache_entry (path); needs_refresh = force_refresh ||IS_ERR (ce) ||cache_entry_expired (ce);
@@ -1218,19 +1229,17 @@ static int __refresh_tcon(const char *path, struct cifs_ses *ses, bool force_reffree_xid (xid);dfs_cache_free_tgts (&old_tl);dfs_cache_free_tgts (&new_tl); -return rc; +kfree (path); } -static int refresh_tcon (struct cifs_tcon *tcon,bool force_refresh) +static inline void refresh_ses_referral (struct cifs_ses *ses) { -struct TCP_Server_Info *server = tcon->ses->server; -struct cifs_ses *ses = tcon->ses; +__refresh_ses_referral (ses, false); +} -mutex_lock (&server->refpath_lock); -if (server->leaf_fullpath) -__refresh_tcon (server->leaf_fullpath +1 , ses, force_refresh); -mutex_unlock (&server->refpath_lock); -return 0 ; +static inline void force_refresh_ses_referral (struct cifs_ses *ses) +{ +__refresh_ses_referral (ses, true); }/**
@@ -1271,25 +1280,20 @@ int dfs_cache_remount_fs(struct cifs_sb_info *cifs_sb)*/ cifs_sb->mnt_cifs_flags |=CIFS_MOUNT_USE_PREFIX_PATH ; -return refresh_tcon (tcon, true); +force_refresh_ses_referral (tcon->ses); +return 0 ; }/* Refresh all DFS referrals related to DFS tcon */ void dfs_cache_refresh (struct work_struct *work) { -struct TCP_Server_Info *server;struct cifs_tcon *tcon;struct cifs_ses *ses; tcon = container_of(work, struct cifs_tcon, dfs_cache_work.work); -for (ses = tcon->ses; ses; ses = ses->dfs_root_ses) { - server = ses->server; -mutex_lock (&server->refpath_lock); -if (server->leaf_fullpath) -__refresh_tcon (server->leaf_fullpath +1 , ses, false); -mutex_unlock (&server->refpath_lock); - } +for (ses = tcon->ses; ses; ses = ses->dfs_root_ses) +refresh_ses_referral (ses);queue_delayed_work (dfscache_wq, &tcon->dfs_cache_work,atomic_read (&dfs_cache_ttl) *HZ );
Signature · 1 line
2.44.0
PATCH 3/4 smb: client: handle DFS tcons in cifs_construct_tcon()
In reply to Paulo AlcantaraThe tcons created by cifs_construct_tcon() on multiuser mounts must also be able to failover and refresh DFS referrals, so set the appropriate fields in order to get a full DFS tcon. They could be shared among different superblocks later, too.
--- fs/smb/client/connect.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
fs/smb/client/connect.c +26 -0
@@ -3995,6 +3995,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)struct cifs_ses *ses;struct cifs_tcon *tcon =NULL ;struct smb3_fs_context *ctx; +char *origin_fullpath =NULL ; ctx =kzalloc (sizeof (*ctx),GFP_KERNEL );if (ctx ==NULL )
@@ -4018,6 +4019,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) ctx->sign = master_tcon->ses->sign; ctx->seal = master_tcon->seal; ctx->witness = master_tcon->use_witness; + ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses; rc =cifs_set_vol_auth (ctx, master_tcon->ses);if (rc) {
@@ -4037,12 +4039,35 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) goto out; } +spin_lock (&master_tcon->tc_lock); +if (master_tcon->origin_fullpath) { +spin_unlock (&master_tcon->tc_lock); + origin_fullpath =dfs_get_path (cifs_sb, cifs_sb->ctx->source); +if (IS_ERR (origin_fullpath)) { + tcon =ERR_CAST (origin_fullpath); + origin_fullpath =NULL ; +cifs_put_smb_ses (ses); + goto out; + } + }else { +spin_unlock (&master_tcon->tc_lock); + } + tcon =cifs_get_tcon (ses, ctx);if (IS_ERR (tcon)) {cifs_put_smb_ses (ses); goto out; } +if (origin_fullpath) { +spin_lock (&tcon->tc_lock); + tcon->origin_fullpath = origin_fullpath; +spin_unlock (&tcon->tc_lock); + origin_fullpath =NULL ; +queue_delayed_work (dfscache_wq, &tcon->dfs_cache_work, +dfs_cache_get_ttl () *HZ ); + } +#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY if (cap_unix (ses))reset_cifs_unix_caps (0 , tcon,NULL , ctx);
@@ -4051,6 +4076,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) out:kfree (ctx->username);kfree_sensitive (ctx->password); +kfree (origin_fullpath);kfree (ctx);return tcon;
Signature · 1 line
2.44.0
PATCH 4/4 smb: client: serialise cifs_construct_tcon() with cifs_mount_mutex
In reply to Paulo AlcantaraSerialise cifs_construct_tcon() with cifs_mount_mutex to handle parallel mounts that may end up reusing the session and tcon created by it.
--- fs/smb/client/connect.c | 13 ++++++++++++- fs/smb/client/fs_context.c | 6 +++--- fs/smb/client/fs_context.h | 12 ++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-)
fs/smb/client/connect.c +12 -1
@@ -3988,7 +3988,7 @@ cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) }static struct cifs_tcon * -cifs_construct_tcon (struct cifs_sb_info *cifs_sb,kuid_t fsuid) +__cifs_construct_tcon (struct cifs_sb_info *cifs_sb,kuid_t fsuid) {int rc;struct cifs_tcon *master_tcon =cifs_sb_master_tcon (cifs_sb);
@@ -4082,6 +4082,17 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)return tcon; } +static struct cifs_tcon * +cifs_construct_tcon (struct cifs_sb_info *cifs_sb,kuid_t fsuid) +{ +struct cifs_tcon *ret; + +cifs_mount_lock (); + ret =__cifs_construct_tcon (cifs_sb, fsuid); +cifs_mount_unlock (); +return ret; +} +struct cifs_tcon *cifs_sb_master_tcon (struct cifs_sb_info *cifs_sb) {
fs/smb/client/fs_context.c +3 -3
@@ -37,7 +37,7 @@#include "rfc1002pdu.h" #include "fs_context.h" -static DEFINE_MUTEX (cifs_mount_mutex); +DEFINE_MUTEX (cifs_mount_mutex);static const match_table_t cifs_smb_version_tokens = { { Smb_1,SMB1_VERSION_STRING },
@@ -783,9 +783,9 @@ static int smb3_get_tree(struct fs_context *fc)if (err)return err; -mutex_lock (&cifs_mount_mutex); +cifs_mount_lock (); ret =smb3_get_tree_common (fc); -mutex_unlock (&cifs_mount_mutex); +cifs_mount_unlock ();return ret; }
fs/smb/client/fs_context.h +12 -0
@@ -304,4 +304,16 @@ extern void smb3_update_mnt_flags(struct cifs_sb_info *cifs_sb);#define MAX_CACHED_FIDS 16extern char *cifs_sanitize_prepath (char *prepath,gfp_t gfp); +extern struct mutex cifs_mount_mutex; + +static inline void cifs_mount_lock (void ) +{ +mutex_lock (&cifs_mount_mutex); +} + +static inline void cifs_mount_unlock (void ) +{ +mutex_unlock (&cifs_mount_mutex); +} +#endif
Signature · 1 line
2.44.0