Skip to contentSkip to conversation

ksmbd: fix racy issue from using ->d_parent and ->d_name

PATCH Superseded Namjae Jeon 2 participants 2 messages Feb 8, 2022

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH 1/1 ksmbd: fix racy issue from using ->d_parent and ->d_name

Al pointed out that ksmbd has racy issue from using ->d_parent and ->d_name in ksmbd_vfs_unlink and smb2_vfs_rename(). and he suggested changing from the way it start with dget_parent(), which can cause retry loop and unexpected errors, to find the parent of child, lock it and then look for a child in locked directory.

This patch introduce a new helper(vfs_path_parent_lookup()) to avoid out of share access and export vfs functions like the following ones to use vfs_path_parent_lookup() and filename_parentat().
- __lookup_hash().
- getname_kernel() and putname().
- filename_parentat()

---
 fs/internal.h                |   3 +
 fs/ksmbd/mgmt/user_session.c |   2 +-
 fs/ksmbd/smb2pdu.c           | 124 ++--------
 fs/ksmbd/vfs.c               | 465 +++++++++++++++--------------------
 fs/ksmbd/vfs.h               |  18 +-
 fs/ksmbd/vfs_cache.c         |   5 +-
 fs/namei.c                   |  43 +++-
 include/linux/namei.h        |   4 +
 8 files changed, 272 insertions(+), 392 deletions(-)
fs/internal.h +3 -0
index 8590c973c2f4..633a950cce1f 100644
@@ -58,6 +58,9 @@ extern int finish_clean_context(struct fs_context *fc);
  */
 extern int filename_lookup(int dfd, struct filename *name, unsigned flags,
 			   struct path *path, struct path *root);
+int vfs_path_parent_lookup(struct dentry *dentry, struct vfsmount *mnt,
+			   struct filename *filename, unsigned int flags,
+			   struct path *parent, struct qstr *last, int *type);
 extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
 			   const char *, unsigned int, struct path *);
 int do_rmdir(int dfd, struct filename *name);
fs/ksmbd/mgmt/user_session.c +1 -1
index 8d8ffd8c6f19..f4279f450053 100644
@@ -161,8 +161,8 @@ void ksmbd_session_destroy(struct ksmbd_session *sess)
 	if (sess->user)
 		ksmbd_free_user(sess->user);
 
-	ksmbd_tree_conn_session_logoff(sess);
 	ksmbd_destroy_file_table(&sess->file_table);
+	ksmbd_tree_conn_session_logoff(sess);
 	ksmbd_session_rpc_clear_list(sess);
 	free_channel_list(sess);
 	kfree(sess->Preauth_HashValue);
fs/ksmbd/smb2pdu.c +22 -102

196 lines not on this page · raw message

fs/ksmbd/vfs.c +196 -269

548 lines not on this page · raw message

fs/ksmbd/vfs.h +8 -10
index 8c37aaf936ab..8e800e5f2e7b 100644
@@ -69,11 +69,11 @@ struct ksmbd_kstat {
 	__le32			file_attributes;
 };
 
-int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
-			  struct dentry *child);
-int ksmbd_vfs_may_delete(struct user_namespace *user_ns, struct dentry *dentry);
-int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
-				   struct dentry *dentry, __le32 *daccess);
+int ksmbd_vfs_may_delete(struct ksmbd_share_config *share, char *filename);
+void ksmbd_vfs_query_maximal_access(struct ksmbd_share_config *share,
+				    struct user_namespace *user_ns,
+				    struct dentry *dentry, char *filename,
+				    __le32 *daccess);
 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode);
 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode);
 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp,
@@ -82,12 +82,11 @@ int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
 		    char *buf, size_t count, loff_t *pos, bool sync,
 		    ssize_t *written);
 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id);
-int ksmbd_vfs_remove_file(struct ksmbd_work *work, char *name);
 int ksmbd_vfs_link(struct ksmbd_work *work,
 		   const char *oldname, const char *newname);
 int ksmbd_vfs_getattr(struct path *path, struct kstat *stat);
-int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
-			char *newname);
+int ksmbd_vfs_rename(struct ksmbd_work *work, struct path *path, char *newname,
+		     int flags);
 int ksmbd_vfs_truncate(struct ksmbd_work *work,
 		       struct ksmbd_file *fp, loff_t size);
 struct srv_copychunk;
@@ -129,8 +128,7 @@ struct file_allocated_range_buffer;
 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length,
 			 struct file_allocated_range_buffer *ranges,
 			 unsigned int in_count, unsigned int *out_count);
-int ksmbd_vfs_unlink(struct user_namespace *user_ns,
-		     struct dentry *dir, struct dentry *dentry);
+int ksmbd_vfs_unlink(struct ksmbd_share_config *share, char *filename);
 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat);
 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
 				struct user_namespace *user_ns,
fs/ksmbd/vfs_cache.c +1 -4
index 29c1db66bd0f..70b7c96002b9 100644
@@ -243,7 +243,6 @@ void ksmbd_release_inode_hash(void)
 
 static void __ksmbd_inode_close(struct ksmbd_file *fp)
 {
-	struct dentry *dir, *dentry;
 	struct ksmbd_inode *ci = fp->f_ci;
 	int err;
 	struct file *filp;
@@ -262,11 +261,9 @@ static void __ksmbd_inode_close(struct ksmbd_file *fp)
 	if (atomic_dec_and_test(&ci->m_count)) {
 		write_lock(&ci->m_lock);
 		if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
-			dentry = filp->f_path.dentry;
-			dir = dentry->d_parent;
 			ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING);
 			write_unlock(&ci->m_lock);
-			ksmbd_vfs_unlink(file_mnt_user_ns(filp), dir, dentry);
+			ksmbd_vfs_unlink(fp->tcon->share_conf, fp->filename);
 			write_lock(&ci->m_lock);
 		}
 		write_unlock(&ci->m_lock);
fs/namei.c +37 -6

91 lines not on this page · raw message

include/linux/namei.h +4 -0
index e89329bb3134..3d9a46df2dbd 100644
@@ -57,6 +57,10 @@ static inline int user_path_at(int dfd, const char __user *name, unsigned flags,
 	return user_path_at_empty(dfd, name, flags, path, NULL);
 }
 
+struct dentry *__lookup_hash(const struct qstr *name, struct dentry *base,
+			     unsigned int flags);
+int filename_parentat(int dfd, struct filename *name, unsigned int flags,
+		      struct path *parent, struct qstr *last, int *type);
 extern int kern_path(const char *, unsigned, struct path *);
 
 extern struct dentry *kern_path_create(int, const char *, struct path *, unsigned int);
Signature · 1 line
2.25.1
#1 lore raw
I love your patch! Perhaps something to improve:

Hi Namjae,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.17-rc3 next-20220208]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Namjae-Jeon/ksmbd-fix-racy-issue-from-using-d_parent-and-d_name/20220208-092438
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 555f3d7be91a873114c9656069f1a9fa476ec41a
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20220209/202202090207.MiyIsofZ-lkp(a)intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project e8bff9ae54a55b4dbfeb6ba55f723abbd81bf494)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/f4cb65c1c670f5332092a7eb75d569bbd4e46a5f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Namjae-Jeon/ksmbd-fix-racy-issue-from-using-d_parent-and-d_name/20220208-092438 git checkout f4cb65c1c670f5332092a7eb75d569bbd4e46a5f
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash fs/ksmbd/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> fs/ksmbd/vfs.c:654:6: warning: variable 'old_dentry' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (d_is_symlink(new_path.dentry)) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   fs/ksmbd/vfs.c:719:7: note: uninitialized use occurs here
           dput(old_dentry);
                ^~~~~~~~~~
   fs/ksmbd/vfs.c:654:2: note: remove the 'if' if its condition is always false
           if (d_is_symlink(new_path.dentry)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   fs/ksmbd/vfs.c:604:27: note: initialize the variable 'old_dentry' to silence this warning
           struct dentry *old_dentry, *new_dentry, *trap;
                                    ^
                                     = NULL
>> fs/ksmbd/vfs.c:654:6: warning: variable 'new_dentry' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (d_is_symlink(new_path.dentry)) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   fs/ksmbd/vfs.c:717:7: note: uninitialized use occurs here
           dput(new_dentry);
                ^~~~~~~~~~
   fs/ksmbd/vfs.c:654:2: note: remove the 'if' if its condition is always false
           if (d_is_symlink(new_path.dentry)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   fs/ksmbd/vfs.c:604:40: note: initialize the variable 'new_dentry' to silence this warning
           struct dentry *old_dentry, *new_dentry, *trap;
                                                 ^
                                                  = NULL

2 warnings generated.

vim +654 fs/ksmbd/vfs.c

600
601 int ksmbd_vfs_rename(struct ksmbd_work *work, struct path *path, char *newname,
602 int flags)
603 {
604 struct dentry *old_dentry, *new_dentry, *trap;
605 struct path old_path, new_path;
606 struct qstr old_last, new_last;
607 struct renamedata rd;
608 struct filename *from, *to;
609 struct ksmbd_share_config *share_conf = work->tcon->share_conf;
610 struct ksmbd_file *parent_fp;
611 int old_type, new_type;
612 int err, lookup_flags = LOOKUP_NO_SYMLINKS;
613 char *pathname, *abs_oldname;
614
615 if (ksmbd_override_fsids(work))
616 return -ENOMEM;
617
618 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
619 if (!pathname) {
620 ksmbd_revert_fsids(work);
621 return -ENOMEM;
622 }
623
624 abs_oldname = d_path(path, pathname, PATH_MAX);
625 if (IS_ERR(abs_oldname)) {
626 err = -EINVAL;
627 goto free_pathname;
628 }
629
630 from = getname_kernel(abs_oldname);
631 if (IS_ERR(from)) {
632 err = PTR_ERR(from);
633 goto free_pathname;
634 }
635
636 to = getname_kernel(newname);
637 if (IS_ERR(to)) {
638 err = PTR_ERR(to);
639 goto putname_from;
640 }
641
642 err = filename_parentat(AT_FDCWD, from, lookup_flags, &old_path,
643 &old_last, &old_type);
644 if (err)
645 goto putnames;
646
647 err = vfs_path_parent_lookup(share_conf->vfs_path.dentry,

   648					     share_conf->vfs_path.mnt, to,
   649					     lookup_flags | LOOKUP_BENEATH,
   650					     &new_path, &new_last, &new_type);
   651		if (err)
   652			goto out1;
   653	
 > 654		if (d_is_symlink(new_path.dentry)) {

655 err = -EACCES;
656 goto out4;
657 }
658
659 trap = lock_rename(old_path.dentry, new_path.dentry);
660 old_dentry = __lookup_hash(&old_last, old_path.dentry, 0);
661 if (IS_ERR(old_dentry)) {
662 err = PTR_ERR(old_dentry);
663 goto out2;
664 }
665 if (d_is_negative(old_dentry)) {
666 err = -ENOENT;
667 goto out3;
668 }
669
670 new_dentry = __lookup_hash(&new_last, new_path.dentry,
671 LOOKUP_RENAME_TARGET);
672 if (IS_ERR(new_dentry)) {
673 err = PTR_ERR(new_dentry);
674 goto out3;
675 }
676
677 if (d_is_symlink(new_dentry)) {
678 err = -EACCES;
679 goto out4;
680 }
681
682 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) {
683 err = -EEXIST;
684 goto out4;
685 }
686
687 if (old_dentry == trap) {
688 err = -EINVAL;
689 goto out4;
690 }
691
692 if (new_dentry == trap) {
693 err = -ENOTEMPTY;
694 goto out4;
695 }
696
697 parent_fp = ksmbd_lookup_fd_inode(old_path.dentry->d_inode);
698 if (parent_fp) {
699 if (parent_fp->daccess & FILE_DELETE_LE) {
700 pr_err("parent dir is opened with delete access\n");
701 err = -ESHARE;
702 goto out4;
703 }
704 }
705
706 rd.old_mnt_userns = mnt_user_ns(old_path.mnt),
707 rd.old_dir = old_path.dentry->d_inode,
708 rd.old_dentry = old_dentry,
709 rd.new_mnt_userns = mnt_user_ns(new_path.mnt),
710 rd.new_dir = new_path.dentry->d_inode,
711 rd.new_dentry = new_dentry,
712 rd.flags = flags,
713 err = vfs_rename(&rd);
714 if (err)
715 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err);
716 out4:
717 dput(new_dentry);
718 out3:
719 dput(old_dentry);
720 out2:
721 unlock_rename(new_path.dentry, old_path.dentry);
722 path_put(&new_path);
723 out1:
724 path_put(&old_path);
725
726 putnames:
727 putname(to);
728 putname_from:
729 putname(from);
730 free_pathname:
731 kfree(pathname);
732 ksmbd_revert_fsids(work);
733 return err;
734 }
735

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org