Skip to content

Commit 734e996

Browse files
hac-vsmfrench
authored andcommitted
smb: client: fix potential UAF in smb2_close_cached_fid()
find_or_create_cached_dir() could grab a new reference after kref_put() had seen the refcount drop to zero but before cfid_list_lock is acquired in smb2_close_cached_fid(), leading to use-after-free. Switch to kref_put_lock() so cfid_release() is called with cfid_list_lock held, closing that gap. Fixes: ebe98f1 ("cifs: enable caching of directories for which a lease is held") Cc: stable@vger.kernel.org Reported-by: Jay Shin <jaeshin@redhat.com> Reviewed-by: Paulo Alcantara (Red Hat) <pc@manguebit.org> Signed-off-by: Henrique Carvalho <henrique.carvalho@suse.com> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 6146a0f commit 734e996

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

fs/smb/client/cached_dir.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,11 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
388388
* lease. Release one here, and the second below.
389389
*/
390390
cfid->has_lease = false;
391-
kref_put(&cfid->refcount, smb2_close_cached_fid);
391+
close_cached_dir(cfid);
392392
}
393393
spin_unlock(&cfids->cfid_list_lock);
394394

395-
kref_put(&cfid->refcount, smb2_close_cached_fid);
395+
close_cached_dir(cfid);
396396
} else {
397397
*ret_cfid = cfid;
398398
atomic_inc(&tcon->num_remote_opens);
@@ -438,12 +438,14 @@ int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
438438

439439
static void
440440
smb2_close_cached_fid(struct kref *ref)
441+
__releases(&cfid->cfids->cfid_list_lock)
441442
{
442443
struct cached_fid *cfid = container_of(ref, struct cached_fid,
443444
refcount);
444445
int rc;
445446

446-
spin_lock(&cfid->cfids->cfid_list_lock);
447+
lockdep_assert_held(&cfid->cfids->cfid_list_lock);
448+
447449
if (cfid->on_list) {
448450
list_del(&cfid->entry);
449451
cfid->on_list = false;
@@ -478,7 +480,7 @@ void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
478480
spin_lock(&cfid->cfids->cfid_list_lock);
479481
if (cfid->has_lease) {
480482
cfid->has_lease = false;
481-
kref_put(&cfid->refcount, smb2_close_cached_fid);
483+
close_cached_dir(cfid);
482484
}
483485
spin_unlock(&cfid->cfids->cfid_list_lock);
484486
close_cached_dir(cfid);
@@ -487,7 +489,7 @@ void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
487489

488490
void close_cached_dir(struct cached_fid *cfid)
489491
{
490-
kref_put(&cfid->refcount, smb2_close_cached_fid);
492+
kref_put_lock(&cfid->refcount, smb2_close_cached_fid, &cfid->cfids->cfid_list_lock);
491493
}
492494

493495
/*
@@ -596,7 +598,7 @@ cached_dir_offload_close(struct work_struct *work)
596598

597599
WARN_ON(cfid->on_list);
598600

599-
kref_put(&cfid->refcount, smb2_close_cached_fid);
601+
close_cached_dir(cfid);
600602
cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cached_close);
601603
}
602604

@@ -762,7 +764,7 @@ static void cfids_laundromat_worker(struct work_struct *work)
762764
* Drop the ref-count from above, either the lease-ref (if there
763765
* was one) or the extra one acquired.
764766
*/
765-
kref_put(&cfid->refcount, smb2_close_cached_fid);
767+
close_cached_dir(cfid);
766768
}
767769
queue_delayed_work(cfid_put_wq, &cfids->laundromat_work,
768770
dir_cache_timeout * HZ);

0 commit comments

Comments
 (0)