ksmbd: fix racy issue from using ->d_parent and ->d_name
Subsystems (3)
Namjae Jeon
maintainerPATCH v2 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()
---
v2:
- add filename_lock to avoid racy issue from fp->filename. (Sergey Senozhatsky)
- fix warning: variable 'old_dentry' is used uninitialized (kernel test robot)
fs/internal.h | 3 + fs/ksmbd/mgmt/user_session.c | 2 +- fs/ksmbd/oplock.c | 3 + fs/ksmbd/smb2pdu.c | 136 +++------- fs/ksmbd/vfs.c | 473 +++++++++++++++-------------------- fs/ksmbd/vfs.h | 18 +- fs/ksmbd/vfs_cache.c | 8 +- fs/ksmbd/vfs_cache.h | 1 + fs/namei.c | 43 +++- include/linux/namei.h | 4 + 10 files changed, 298 insertions(+), 393 deletions(-)
fs/internal.h +3 -0
@@ -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
@@ -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/oplock.c +3 -0
@@ -1713,11 +1713,14 @@ int smb2_check_durable_oplock(struct ksmbd_file *fp, ret = -EBADF ; goto out; } +down_read (&fp->filename_lock);if (name &&strcmp (fp->filename, name)) { +up_read (&fp->filename_lock);pr_err ("invalid name reconnect %s\n" , name); ret = -EINVAL ; goto out; } +up_read (&fp->filename_lock); } out:if (opinfo)
fs/ksmbd/smb2pdu.c +34 -102
242 lines not on this page · raw message
fs/ksmbd/vfs.c +203 -270
572 lines not on this page · raw message
fs/ksmbd/vfs.h +8 -10
@@ -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 +4 -4
@@ -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,11 @@ 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); +down_read (&fp->filename_lock); +ksmbd_vfs_unlink (fp->tcon->share_conf, fp->filename); +up_read (&fp->filename_lock);write_lock (&ci->m_lock); }write_unlock (&ci->m_lock);
@@ -567,6 +566,7 @@ struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp)INIT_LIST_HEAD (&fp->lock_list);spin_lock_init (&fp->f_lock);atomic_set (&fp->refcount,1 ); +init_rwsem (&fp->filename_lock); fp->filp = filp; fp->conn = work->sess->conn;
fs/ksmbd/vfs_cache.h +1 -0
@@ -99,6 +99,7 @@ struct ksmbd_file {/* if ls is happening on directory, below is valid*/ struct ksmbd_readdir_data readdir_data;int dot_dotdot[2 ]; +struct rw_semaphore filename_lock; }; static inline void set_ctx_actor(struct dir_context *ctx,
fs/namei.c +37 -6
91 lines not on this page · raw message
include/linux/namei.h +4 -0
@@ -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
A message that is not in the archive (sent off-list or not yet received): CAH2r5msqTrGG2caTFCG4BL29obk86dfgocRJ3=F0YEaonE8JQg@mail.gmail.com
Al Viro
maintainerQuoted 1 line · Steve French
Do I need to update ksmbd-for-next?
I'll review tonight or tomorrow. Sorry about delay...
Al Viro
maintainerQuoted 6 lines · Namjae Jeon
Al pointed out that ksmbd has racy issue from using ->d_parent and ->d_name in
ksmbd_vfs_unlinkandsmb2_vfs_rename(). and he suggested changing from the way it start withdget_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.
Quoted 6 lines
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()andfilename_parentat().
-__lookup_hash().
-getname_kernel()andputname().
-filename_parentat()
First of all, your vfs_path_parent_lookup() calling conventions are wrong. You have 3 callers:
err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type); err = vfs_path_parent_lookup(share_conf->vfs_path.dentry, share_conf->vfs_path.mnt, to, lookup_flags | LOOKUP_BENEATH, &new_path, &new_last, &new_type); err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type);
Note that in all of them the first two arguments come from ->dentry and ->mnt of the same struct path instance. Now, look at the function itself:
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)
{
struct path root = {.mnt = mnt, .dentry = dentry};
return __filename_parentat(AT_FDCWD, filename, flags, parent, last,
type, &root);
}What about the __filename_parentat() last argument? It's declared as struct path *root and passed to set_nameidata(). No other uses. And set_nameidata() gets it via const struct path *root argument. IOW, it's not going to modify the contents of that struct path. Since you __filename_parentat() doesn't do anything else with its root argument, there's no reason not to make _that_ const struct path *, is there?
Now, if you do that, you can safely turn vfs_path_parent_lookup() take const struct path * instead of dentry/vfsmount pair of arguments and drop the local struct path instance in the vfs_path_parent_lookup() itself.
The fact that vfs_path_lookup() passes vfsmount and dentry separately doesn't mean you need to do the same - look at the existing callers of vfs_path_lookup() (outside of ksmbd itself) and you'll see the difference. Incidentally, this
fs/ksmbd/vfs.c:22:#include "../internal.h" /* for vfs_path_lookup */
had been a really bad idea. And no, nfsd doing the same is not a good thing either...
General rule: if it's exported, it's *NOT* internal.
Next:
Quoted 5 lines
index
077b8761d099..b094cd1d4951100644fs/ksmbd/oplock.c +0
-0@@ -1713,11 +1713,14 @@ int smb2_check_durable_oplock(struct ksmbd_file *fp, ret = -EBADF ;
fs/ksmbd/oplock.c int smb2_check_durable_oplock(struct ksmbd_file *fp, · line 1723
Quoted 10 lines
fs/ksmbd/oplock.c +3
-0goto out; } +down_read (&fp->filename_lock);if (name &&strcmp (fp->filename, name)) { +up_read (&fp->filename_lock);pr_err ("invalid name reconnect %s\n" , name); ret = -EINVAL ; goto out; } +up_read (&fp->filename_lock);
What assumptions do you make about those strings? Note that opened file is *NOT* guaranteed to have its pathname remain unchanged - having /tmp/foo/bar/baz/blah opened will not prevent mv /tmp/foo /tmp/barf and the file will remain opened (and working just fine). AFAICS, you only update it in smb2_rename(), which is not going to be called by mv(1) called by admin on server.
BTW, while grepping through the related code, convert_to_nt_pathname() is Not Nice(tm). Seriously, strlen(s) == 0 is not an idiomatic way to check that s is an empty string. What's more, return value of that function ends up passed to kfree(). Which is not a good thing to do to a string constant. That can be recovered by use of kfree_const() in get_file_all_info(), but.. ouch.
ksmbd_vfs_rename(): UGH.
* you allocate a buffer
* do d_path() into it
* then use getname_kernel() to allocate another one and copy the contents
into it. By that point the string might have nothing to do with the actual location of object, BTW (see above)
* then you use filename_parentat() (BTW, the need to export both it andvfs_path_parent_lookup() is an artefact of bad calling conventions - passing NULL as const struct path * would do the right thing, if not for the fact that with your calling conventions you have to pass a non-NULL pointer - that to a local struct path in your vfs_path_parent_lookup()).
* then you use vfs_path_parent_lookup() to find the new parent. OK, but...
you proceed to check if it has somehow returned you a symlink. Huh? How does one get a symlink from path_parentat() or anything that would use it? I would very much appreciate a reproducer for that.
* you use lock_rename() to lock both parents. Which relies upon the
caller having checked that they live on the same filesystem. Neither old nor new version do that, which means relatively easy deadlocks.
* look the last components up. NB: the old one might very well have
nothing to do with the path.dentry.
* do usual checks re loop prevention (with slightly unusual error
values, but whatever)
* call ksmbd_lookup_fd_inode() on the old parent. Then dereference
the return value (if non-NULL)... and never do anything else to it. How can that possibly work? What's there to prevent freeing of that struct ksmbd_file just as ksmbd_lookup_fd_inode() returns it? Looks like it's either a leak or use-after-free, and looking at ksmbd_lookup_fd_inode() it's probably the latter.
* proceed with vfs_rename(), drop the stuff you'd acquired and go
away.
ksmbd_vfs_unlink():
* const char *filename, please, unless you really modify it there.
* what the hell is that ihold/iput pair for?
I'm not sure that the set you'd exported is the right one, but that's secondary - I'd really like to understand what assumptions are you making about the ->filename contents, as well as the control flow from protocol request that demands rename to the actual call of vfs_rename(). Could you elaborate on that? I am not familiar with the protocol, other than bits and pieces I'd observed in fs/cifs code.
Namjae Jeon
maintainerHi Al,
2022-02-18 8:41 GMT+09:00, Al Viro <[email protected]>:
Quoted 43 lines · Namjae Jeon
Quoted 7 lines
Al pointed out that ksmbd has racy issue from using ->d_parent and ->d_name
inksmbd_vfs_unlinkandsmb2_vfs_rename(). and he suggested changing from the way it start withdget_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.Quoted 7 lines
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()andfilename_parentat().
-__lookup_hash().
-getname_kernel()andputname().
-filename_parentat()First of all, your
vfs_path_parent_lookup()calling conventions are wrong. You have 3 callers:err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type); err = vfs_path_parent_lookup(share_conf->vfs_path.dentry, share_conf->vfs_path.mnt, to, lookup_flags | LOOKUP_BENEATH, &new_path, &new_last, &new_type); err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type);Note that in all of them the first two arguments come from ->dentry and ->mnt of the same struct path instance. Now, look at the function itself:
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){ struct path root = {.mnt = mnt, .dentry = dentry}; return __filename_parentat(AT_FDCWD, filename, flags, parent, last, type, &root); }
Quoted 7 lines
What about the
__filename_parentat()last argument? It's declared as struct path *root and passed toset_nameidata(). No other uses. Andset_nameidata()gets it via const struct path *root argument. IOW, it's not going to modify the contents of that struct path. Since you__filename_parentat()doesn't do anything else with its root argument, there's no reason not to make _that_ const struct path *, is there?
Yep, No reason.
Quoted 5 lines
Now, if you do that, you can safely turn
vfs_path_parent_lookup()take const struct path * instead of dentry/vfsmount pair of arguments and drop the local struct path instance in thevfs_path_parent_lookup()itself.
Yes. I will change it.
Quoted 10 lines
The fact that
vfs_path_lookup()passes vfsmount and dentry separately doesn't mean you need to do the same - look at the existing callers ofvfs_path_lookup()(outside of ksmbd itself) and you'll see the difference. Incidentally, this
fs/ksmbd/vfs.c:22:#include "../internal.h" /* forvfs_path_lookup*/
had been a really bad idea. And no, nfsd doing the same is not a good thing either...General rule: if it's exported, it's *NOT* internal.
Okay. Then as another patch, I will move vfs_path_lookup prototype in internal.h to linux/namei.h.
Quoted 19 lines
Next:
Quoted 15 lines
index
077b8761d099..b094cd1d4951100644fs/ksmbd/oplock.c +2
-0@@ -1713,11 +1713,14 @@ int smb2_check_durable_oplock(struct ksmbd_file *fp, ret = -EBADF ; goto out; } +down_read (&fp->filename_lock);if (name &&strcmp (fp->filename, name)) { +up_read (&fp->filename_lock);pr_err ("invalid name reconnect %s\n" , name); ret = -EINVAL ; goto out; }
Quoted 8 lines
fs/ksmbd/oplock.c int smb2_check_durable_oplock(struct ksmbd_file · line 1724
Quoted 1 line
fs/ksmbd/oplock.c +1
-0+up_read (&fp->filename_lock);What assumptions do you make about those strings? Note that opened file is *NOT* guaranteed to have its pathname remain unchanged - having
/tmp/foo/bar/baz/blahopened will not prevent mv/tmp/foo/tmp/barfand the file will remain opened (and working just fine). AFAICS, you only update it insmb2_rename(), which is not going to be called by mv(1) called by admin on server.
Whenever a FILE_ALL_INFORMATION request is received from a client, ksmbd need to call d_path(then, removing the share path in pathname is required) to obtain pathname for windows. To avoid the issue you mentioned, we can remove the all uses of ->filename and calling d_path() whenever pathname is need.
Quoted 7 lines
BTW, while grepping through the related code,
convert_to_nt_pathname()is Not Nice(tm). Seriously, strlen(s) == 0 is not an idiomatic way to check that s is an empty string. What's more, return value of that function ends up passed tokfree(). Which is not a good thing to do to a string constant. That can be recovered by use ofkfree_const()inget_file_all_info(), but.. ouch.
Okay.
Quoted 7 lines
ksmbd_vfs_rename(): UGH.
* you allocate a buffer
* dod_path()into it
* then usegetname_kernel()to allocate another one and copy the contents
into it. By that point the string might have nothing to do with the actual location of object, BTW (see above)
Can we use dget_parent() and take_dentry_name_snapshot() for source file instead of d_path(), getname_kernel() and filename_parentat()? Because ksmbd receive fileid(ksmbd_file) for source file from client. [See control flow the below]
Quoted 7 lines
* then you use
filename_parentat()(BTW, the need to export both it andvfs_path_parent_lookup()is an artefact of bad calling conventions - passing
NULL as const struct path * would do the right thing, if not for the fact that
with your calling conventions you have to pass a non-NULL pointer - that to a local struct path in yourvfs_path_parent_lookup()).
Okay.
Quoted 5 lines
* then you use
vfs_path_parent_lookup()to find the new parent. OK,
but...
you proceed to check if it has somehow returned you a symlink. Huh? How does
one get a symlink frompath_parentat()or anything that would use it?
As security issues, We made it not to allow symlinks.
Quoted 5 lines
I would very much appreciate a reproducer for that.
* you uselock_rename()to lock both parents. Which relies upon the
caller having checked that they live on the same filesystem. Neither old nor
new version do that, which means relatively easy deadlocks.
Okay. will add the check for this.
Quoted 2 lines
* look the last components up. NB: the old one might very well have
nothing to do with the path.dentry.
Okay.
Quoted 2 lines
* do usual checks re loop prevention (with slightly unusual error
values, but whatever)
I understood that you pointed out to add retry_estale() check and retry.
Quoted 9 lines
* call
ksmbd_lookup_fd_inode()on the old parent. Then dereference
the return value (if non-NULL)... and never do anything else to it. How can
that possibly work? What's there to prevent freeing of that structksmbd_file
just asksmbd_lookup_fd_inode()returns it? Looks like it's either a leak or
use-after-free, and looking atksmbd_lookup_fd_inode()it's probably the latter.
Right. Need to increase reference count of ksmbd_file. I will fix it.
Quoted 2 lines
* proceed with
vfs_rename(), drop the stuff you'd acquired and go
away.
Okay.
Quoted 3 lines
ksmbd_vfs_unlink():
* const char *filename, please, unless you really modify it there.
Okay.
Quoted 1 line
* what the hell is that ihold/iput pair for?
I refered codes in do_unlinkat() for this. If this pair is not needed, I'll delete it,
but could you please tell me why it's needed in unlinkat() ?
Quoted 8 lines
I'm not sure that the set you'd exported is the right one, but that's secondary - I'd really like to understand what assumptions are you making about the ->filename contents, as well as the control flow from protocol request that demands rename to the actual call of
vfs_rename(). Could you elaborate on that? I am not familiar with the protocol, other than bits and pieces I'd observed in fs/cifs code.
As I said above, the uses of ->filename can be replaced with d_path(). control flow for rename is the following.
a. Receiving smb2 create(open) request from client.
- Getting struct file after calling vfs_path_lookup() anddentry_open() using pathname from client.
- create ksmbd_file and add struct file to fp->filp and generate
fileid for struct ksmbd_file.
- send smb2 create response included fileid of ksmbd_file to client.
b. Receiving smb2_set_info file(FILE_RENAME_INFORMATION) request from client.
- lookup ksmbd_file using fileid of request. This will source
file for rename.
- get absolute pathname for destination fille in smb2 set info
file for rename.
- find both parents of source and destination and lock and do rename...
c. Receiving smb2 close.
Thanks for your review!
Quoted 1 line
2022년 2월 18일 (금) 오후 5:02, Namjae Jeon <[email protected]>님이 작성:
Quoted 124 lines
Hi Al,
2022-02-18 8:41 GMT+09:00, Al Viro <[email protected]>:Quoted 43 lines · Namjae Jeon
Quoted 7 lines
Al pointed out that ksmbd has racy issue from using ->d_parent and ->d_name
inksmbd_vfs_unlinkandsmb2_vfs_rename(). and he suggested changing from the way it start withdget_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.Quoted 7 lines
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()andfilename_parentat().
-__lookup_hash().
-getname_kernel()andputname().
-filename_parentat()First of all, your
vfs_path_parent_lookup()calling conventions are wrong. You have 3 callers:err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type); err = vfs_path_parent_lookup(share_conf->vfs_path.dentry, share_conf->vfs_path.mnt, to, lookup_flags | LOOKUP_BENEATH, &new_path, &new_last, &new_type); err = vfs_path_parent_lookup(share->vfs_path.dentry, share->vfs_path.mnt, filename_struct, LOOKUP_NO_SYMLINKS | LOOKUP_BENEATH, &path, &last, &type);Note that in all of them the first two arguments come from ->dentry and ->mnt of the same struct path instance. Now, look at the function itself:
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){ struct path root = {.mnt = mnt, .dentry = dentry}; return __filename_parentat(AT_FDCWD, filename, flags, parent, last, type, &root); }Quoted 7 lines
What about the
__filename_parentat()last argument? It's declared as struct path *root and passed toset_nameidata(). No other uses. Andset_nameidata()gets it via const struct path *root argument. IOW, it's not going to modify the contents of that struct path. Since you__filename_parentat()doesn't do anything else with its root argument, there's no reason not to make _that_ const struct path *, is there?Yep, No reason.
Quoted 5 lines
Now, if you do that, you can safely turn
vfs_path_parent_lookup()take const struct path * instead of dentry/vfsmount pair of arguments and drop the local struct path instance in thevfs_path_parent_lookup()itself.Yes. I will change it.
Quoted 10 lines
The fact that
vfs_path_lookup()passes vfsmount and dentry separately doesn't mean you need to do the same - look at the existing callers ofvfs_path_lookup()(outside of ksmbd itself) and you'll see the difference. Incidentally, this
fs/ksmbd/vfs.c:22:#include "../internal.h" /* forvfs_path_lookup*/
had been a really bad idea. And no, nfsd doing the same is not a good thing either...General rule: if it's exported, it's *NOT* internal.
Okay. Then as another patch, I will move
vfs_path_lookupprototype ininternal.htolinux/namei.h.Quoted 19 lines
Next:
Quoted 15 lines
index
077b8761d099..b094cd1d4951100644fs/ksmbd/oplock.c +2
-0@@ -1713,11 +1713,14 @@ int smb2_check_durable_oplock(struct ksmbd_file *fp, ret = -EBADF ; goto out; } +down_read (&fp->filename_lock);if (name &&strcmp (fp->filename, name)) { +up_read (&fp->filename_lock);pr_err ("invalid name reconnect %s\n" , name); ret = -EINVAL ; goto out; }Quoted 8 lines
fs/ksmbd/oplock.c int smb2_check_durable_oplock(struct ksmbd_file · line 1724
Quoted 1 line
fs/ksmbd/oplock.c +1
-0+up_read (&fp->filename_lock);What assumptions do you make about those strings? Note that opened file is *NOT* guaranteed to have its pathname remain unchanged - having
/tmp/foo/bar/baz/blahopened will not prevent mv/tmp/foo/tmp/barfand the file will remain opened (and working just fine). AFAICS, you only update it insmb2_rename(), which is not going to be called by mv(1) called by admin on server.Whenever a
FILE_ALL_INFORMATIONrequest is received from a client, ksmbd need to calld_path(then, removing the share path in pathname is required)to obtain pathname for windows. To avoid the issue you mentioned, we can remove the all uses of ->filename and callingd_path()whenever pathname is need.Quoted 7 lines
BTW, while grepping through the related code,
convert_to_nt_pathname()is Not Nice(tm). Seriously, strlen(s) == 0 is not an idiomatic way to check that s is an empty string. What's more, return value of that function ends up passed tokfree(). Which is not a good thing to do to a string constant. That can be recovered by use ofkfree_const()inget_file_all_info(), but.. ouch.Okay.
Quoted 7 lines
ksmbd_vfs_rename(): UGH.
* you allocate a buffer
* dod_path()into it
* then usegetname_kernel()to allocate another one and copy the contents
into it. By that point the string might have nothing to do with the actual location of object, BTW (see above)
Quoted 5 lines
Can we use
dget_parent()andtake_dentry_name_snapshot()for source file instead ofd_path(),getname_kernel()andfilename_parentat()? Because ksmbd receivefileid(ksmbd_file)for source file from client. [See control flow the below]
As Namjae said, for the rename, a client sends FileId of a source file and an absolute path of a destination file. ksmbd can only get a dentry of the source file from FileId.
So I think we can use dget_parent() to get a parent dentry. And we have to verify the parent dentry by locking the parent inode, and calling take_dentry_name_snapshot() and lookup_one().
Quoted 82 lines
Quoted 7 lines
* then you use
filename_parentat()(BTW, the need to export both it andvfs_path_parent_lookup()is an artefact of bad calling conventions - passing
NULL as const struct path * would do the right thing, if not for the fact that
with your calling conventions you have to pass a non-NULL pointer - that to a local struct path in yourvfs_path_parent_lookup()).Okay.
Quoted 5 lines
* then you use
vfs_path_parent_lookup()to find the new parent. OK,
but...
you proceed to check if it has somehow returned you a symlink. Huh? How does
one get a symlink frompath_parentat()or anything that would use it?As security issues, We made it not to allow symlinks.
Quoted 5 lines
I would very much appreciate a reproducer for that.
* you uselock_rename()to lock both parents. Which relies upon the
caller having checked that they live on the same filesystem. Neither old nor
new version do that, which means relatively easy deadlocks.Okay. will add the check for this.
Quoted 2 lines
* look the last components up. NB: the old one might very well have
nothing to do with the path.dentry.Okay.
Quoted 2 lines
* do usual checks re loop prevention (with slightly unusual error
values, but whatever)I understood that you pointed out to add
retry_estale()check and retry.Quoted 9 lines
* call
ksmbd_lookup_fd_inode()on the old parent. Then dereference
the return value (if non-NULL)... and never do anything else to it. How can
that possibly work? What's there to prevent freeing of that structksmbd_file
just asksmbd_lookup_fd_inode()returns it? Looks like it's either a leak or
use-after-free, and looking atksmbd_lookup_fd_inode()it's probably the latter.Right. Need to increase reference count of
ksmbd_file. I will fix it.Quoted 2 lines
* proceed with
vfs_rename(), drop the stuff you'd acquired and go
away.Okay.
Quoted 3 lines
ksmbd_vfs_unlink():
* const char *filename, please, unless you really modify it there.Okay.
Quoted 1 line
* what the hell is that ihold/iput pair for?
I refered codes in
do_unlinkat()for this. If this pair is not needed, I'll delete it,
but could you please tell me why it's needed inunlinkat()?Quoted 8 lines
I'm not sure that the set you'd exported is the right one, but that's secondary - I'd really like to understand what assumptions are you making about the ->filename contents, as well as the control flow from protocol request that demands rename to the actual call of
vfs_rename(). Could you elaborate on that? I am not familiar with the protocol, other than bits and pieces I'd observed in fs/cifs code.As I said above, the uses of ->filename can be replaced with
d_path(). control flow for rename is the following.a. Receiving smb2 create(open) request from client.
- Getting struct file after callingvfs_path_lookup()anddentry_open()using pathname from client.
- createksmbd_fileand add struct file tofp->filpand generate
fileid for structksmbd_file.
- send smb2 create response included fileid ofksmbd_fileto client.
b. Receivingsmb2_set_infofile(FILE_RENAME_INFORMATION)request from client.
- lookupksmbd_fileusing fileid of request. This will source
file for rename.
- get absolute pathname for destination fille in smb2 set info
file for rename.
- find both parents of source and destination and lock and do rename...
c. Receiving smb2 close.Thanks for your review!
Quoted 1 line
Signature · 2 lines
Thanks, Hyunchul