Skip to content

Commit d565197

Browse files
namjaejeongregkh
authored andcommitted
ksmbd: fix possible deadlock in smb2_open
[ Upstream commit 864fb5d ] [ 8743.393379] ====================================================== [ 8743.393385] WARNING: possible circular locking dependency detected [ 8743.393391] 6.4.0-rc1+ #11 Tainted: G OE [ 8743.393397] ------------------------------------------------------ [ 8743.393402] kworker/0:2/12921 is trying to acquire lock: [ 8743.393408] ffff888127a14460 (sb_writers#8){.+.+}-{0:0}, at: ksmbd_vfs_setxattr+0x3d/0xd0 [ksmbd] [ 8743.393510] but task is already holding lock: [ 8743.393515] ffff8880360d97f0 (&type->i_mutex_dir_key#6/1){+.+.}-{3:3}, at: ksmbd_vfs_kern_path_locked+0x181/0x670 [ksmbd] [ 8743.393618] which lock already depends on the new lock. [ 8743.393623] the existing dependency chain (in reverse order) is: [ 8743.393628] -> #1 (&type->i_mutex_dir_key#6/1){+.+.}-{3:3}: [ 8743.393648] down_write_nested+0x9a/0x1b0 [ 8743.393660] filename_create+0x128/0x270 [ 8743.393670] do_mkdirat+0xab/0x1f0 [ 8743.393680] __x64_sys_mkdir+0x47/0x60 [ 8743.393690] do_syscall_64+0x5d/0x90 [ 8743.393701] entry_SYSCALL_64_after_hwframe+0x72/0xdc [ 8743.393711] -> #0 (sb_writers#8){.+.+}-{0:0}: [ 8743.393728] __lock_acquire+0x2201/0x3b80 [ 8743.393737] lock_acquire+0x18f/0x440 [ 8743.393746] mnt_want_write+0x5f/0x240 [ 8743.393755] ksmbd_vfs_setxattr+0x3d/0xd0 [ksmbd] [ 8743.393839] ksmbd_vfs_set_dos_attrib_xattr+0xcc/0x110 [ksmbd] [ 8743.393924] compat_ksmbd_vfs_set_dos_attrib_xattr+0x39/0x50 [ksmbd] [ 8743.394010] smb2_open+0x3432/0x3cc0 [ksmbd] [ 8743.394099] handle_ksmbd_work+0x2c9/0x7b0 [ksmbd] [ 8743.394187] process_one_work+0x65a/0xb30 [ 8743.394198] worker_thread+0x2cf/0x700 [ 8743.394209] kthread+0x1ad/0x1f0 [ 8743.394218] ret_from_fork+0x29/0x50 This patch add mnt_want_write() above parent inode lock and remove nested mnt_want_write calls in smb2_open(). Fixes: 40b268d ("ksmbd: add mnt_want_write to ksmbd vfs functions") Cc: [email protected] Reported-by: Marios Makassikis <[email protected]> Signed-off-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent aabc944 commit d565197

File tree

5 files changed

+75
-59
lines changed

5 files changed

+75
-59
lines changed

fs/smb/server/smb2pdu.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,8 @@ static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
23802380
rc = 0;
23812381
} else {
23822382
rc = ksmbd_vfs_setxattr(user_ns, path, attr_name, value,
2383-
le16_to_cpu(eabuf->EaValueLength), 0);
2383+
le16_to_cpu(eabuf->EaValueLength),
2384+
0, true);
23842385
if (rc < 0) {
23852386
ksmbd_debug(SMB,
23862387
"ksmbd_vfs_setxattr is failed(%d)\n",
@@ -2443,7 +2444,7 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
24432444
return -EBADF;
24442445
}
24452446

2446-
rc = ksmbd_vfs_setxattr(user_ns, path, xattr_stream_name, NULL, 0, 0);
2447+
rc = ksmbd_vfs_setxattr(user_ns, path, xattr_stream_name, NULL, 0, 0, false);
24472448
if (rc < 0)
24482449
pr_err("Failed to store XATTR stream name :%d\n", rc);
24492450
return 0;
@@ -2518,7 +2519,7 @@ static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *
25182519
da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
25192520
XATTR_DOSINFO_ITIME;
25202521

2521-
rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), path, &da);
2522+
rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt), path, &da, false);
25222523
if (rc)
25232524
ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
25242525
}
@@ -2608,7 +2609,7 @@ static int smb2_create_sd_buffer(struct ksmbd_work *work,
26082609
sizeof(struct create_sd_buf_req))
26092610
return -EINVAL;
26102611
return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2611-
le32_to_cpu(sd_buf->ccontext.DataLength), true);
2612+
le32_to_cpu(sd_buf->ccontext.DataLength), true, false);
26122613
}
26132614

26142615
static void ksmbd_acls_fattr(struct smb_fattr *fattr,
@@ -3152,7 +3153,8 @@ int smb2_open(struct ksmbd_work *work)
31523153
user_ns,
31533154
&path,
31543155
pntsd,
3155-
pntsd_size);
3156+
pntsd_size,
3157+
false);
31563158
kfree(pntsd);
31573159
if (rc)
31583160
pr_err("failed to store ntacl in xattr : %d\n",
@@ -3228,12 +3230,6 @@ int smb2_open(struct ksmbd_work *work)
32283230
if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
32293231
ksmbd_fd_set_delete_on_close(fp, file_info);
32303232

3231-
if (need_truncate) {
3232-
rc = smb2_create_truncate(&path);
3233-
if (rc)
3234-
goto err_out;
3235-
}
3236-
32373233
if (req->CreateContextsOffset) {
32383234
struct create_alloc_size_req *az_req;
32393235

@@ -3398,11 +3394,12 @@ int smb2_open(struct ksmbd_work *work)
33983394
}
33993395

34003396
err_out:
3401-
if (file_present || created) {
3402-
inode_unlock(d_inode(parent_path.dentry));
3403-
path_put(&path);
3404-
path_put(&parent_path);
3405-
}
3397+
if (file_present || created)
3398+
ksmbd_vfs_kern_path_unlock(&parent_path, &path);
3399+
3400+
if (fp && need_truncate)
3401+
rc = smb2_create_truncate(&fp->filp->f_path);
3402+
34063403
ksmbd_revert_fsids(work);
34073404
err_out1:
34083405
if (!rc) {
@@ -5537,7 +5534,7 @@ static int smb2_rename(struct ksmbd_work *work,
55375534
rc = ksmbd_vfs_setxattr(file_mnt_user_ns(fp->filp),
55385535
&fp->filp->f_path,
55395536
xattr_stream_name,
5540-
NULL, 0, 0);
5537+
NULL, 0, 0, true);
55415538
if (rc < 0) {
55425539
pr_err("failed to store stream name in xattr: %d\n",
55435540
rc);
@@ -5630,11 +5627,9 @@ static int smb2_create_link(struct ksmbd_work *work,
56305627
if (rc)
56315628
rc = -EINVAL;
56325629
out:
5633-
if (file_present) {
5634-
inode_unlock(d_inode(parent_path.dentry));
5635-
path_put(&path);
5636-
path_put(&parent_path);
5637-
}
5630+
if (file_present)
5631+
ksmbd_vfs_kern_path_unlock(&parent_path, &path);
5632+
56385633
if (!IS_ERR(link_name))
56395634
kfree(link_name);
56405635
kfree(pathname);
@@ -5701,7 +5696,8 @@ static int set_file_basic_info(struct ksmbd_file *fp,
57015696
da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
57025697
XATTR_DOSINFO_ITIME;
57035698

5704-
rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, &filp->f_path, &da);
5699+
rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns, &filp->f_path, &da,
5700+
true);
57055701
if (rc)
57065702
ksmbd_debug(SMB,
57075703
"failed to restore file attribute in EA\n");
@@ -6013,7 +6009,7 @@ static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
60136009
fp->saccess |= FILE_SHARE_DELETE_LE;
60146010

60156011
return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
6016-
buf_len, false);
6012+
buf_len, false, true);
60176013
}
60186014

60196015
/**
@@ -7583,7 +7579,8 @@ static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
75837579

75847580
da.attr = le32_to_cpu(fp->f_ci->m_fattr);
75857581
ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
7586-
&fp->filp->f_path, &da);
7582+
&fp->filp->f_path,
7583+
&da, true);
75877584
if (ret)
75887585
fp->f_ci->m_fattr = old_fattr;
75897586
}

fs/smb/server/smbacl.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ int smb_inherit_dacl(struct ksmbd_conn *conn,
11851185
pntsd_size += sizeof(struct smb_acl) + nt_size;
11861186
}
11871187

1188-
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, pntsd_size);
1188+
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, pntsd_size, false);
11891189
kfree(pntsd);
11901190
}
11911191

@@ -1377,7 +1377,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
13771377

13781378
int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
13791379
const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
1380-
bool type_check)
1380+
bool type_check, bool get_write)
13811381
{
13821382
int rc;
13831383
struct smb_fattr fattr = {{0}};
@@ -1437,7 +1437,8 @@ int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
14371437
if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
14381438
/* Update WinACL in xattr */
14391439
ksmbd_vfs_remove_sd_xattrs(user_ns, path);
1440-
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, ntsd_len);
1440+
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, ntsd_len,
1441+
get_write);
14411442
}
14421443

14431444
out:

fs/smb/server/smbacl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
207207
__le32 *pdaccess, int uid);
208208
int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
209209
const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
210-
bool type_check);
210+
bool type_check, bool get_write);
211211
void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid);
212212
void ksmbd_init_domain(u32 *sub_auth);
213213

fs/smb/server/vfs.c

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
9898
return -ENOENT;
9999
}
100100

101+
err = mnt_want_write(parent_path->mnt);
102+
if (err) {
103+
path_put(parent_path);
104+
putname(filename);
105+
return -ENOENT;
106+
}
107+
101108
inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT);
102109
d = lookup_one_qstr_excl(&last, parent_path->dentry, 0);
103110
if (IS_ERR(d))
@@ -124,6 +131,7 @@ static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
124131

125132
err_out:
126133
inode_unlock(d_inode(parent_path->dentry));
134+
mnt_drop_write(parent_path->mnt);
127135
path_put(parent_path);
128136
putname(filename);
129137
return -ENOENT;
@@ -452,7 +460,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
452460
fp->stream.name,
453461
(void *)stream_buf,
454462
size,
455-
0);
463+
0,
464+
true);
456465
if (err < 0)
457466
goto out;
458467

@@ -594,10 +603,6 @@ int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
594603
goto out_err;
595604
}
596605

597-
err = mnt_want_write(path->mnt);
598-
if (err)
599-
goto out_err;
600-
601606
user_ns = mnt_user_ns(path->mnt);
602607
if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
603608
err = vfs_rmdir(user_ns, d_inode(parent), path->dentry);
@@ -608,7 +613,6 @@ int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
608613
if (err)
609614
ksmbd_debug(VFS, "unlink failed, err %d\n", err);
610615
}
611-
mnt_drop_write(path->mnt);
612616

613617
out_err:
614618
ksmbd_revert_fsids(work);
@@ -908,18 +912,22 @@ ssize_t ksmbd_vfs_getxattr(struct user_namespace *user_ns,
908912
* @attr_value: xattr value to set
909913
* @attr_size: size of xattr value
910914
* @flags: destination buffer length
915+
* @get_write: get write access to a mount
911916
*
912917
* Return: 0 on success, otherwise error
913918
*/
914919
int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
915920
const struct path *path, const char *attr_name,
916-
void *attr_value, size_t attr_size, int flags)
921+
void *attr_value, size_t attr_size, int flags,
922+
bool get_write)
917923
{
918924
int err;
919925

920-
err = mnt_want_write(path->mnt);
921-
if (err)
922-
return err;
926+
if (get_write == true) {
927+
err = mnt_want_write(path->mnt);
928+
if (err)
929+
return err;
930+
}
923931

924932
err = vfs_setxattr(user_ns,
925933
path->dentry,
@@ -929,7 +937,8 @@ int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
929937
flags);
930938
if (err)
931939
ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
932-
mnt_drop_write(path->mnt);
940+
if (get_write == true)
941+
mnt_drop_write(path->mnt);
933942
return err;
934943
}
935944

@@ -1253,6 +1262,13 @@ int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
12531262
}
12541263

12551264
if (!err) {
1265+
err = mnt_want_write(parent_path->mnt);
1266+
if (err) {
1267+
path_put(path);
1268+
path_put(parent_path);
1269+
return err;
1270+
}
1271+
12561272
err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry);
12571273
if (err) {
12581274
path_put(path);
@@ -1262,6 +1278,14 @@ int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
12621278
return err;
12631279
}
12641280

1281+
void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path)
1282+
{
1283+
inode_unlock(d_inode(parent_path->dentry));
1284+
mnt_drop_write(parent_path->mnt);
1285+
path_put(path);
1286+
path_put(parent_path);
1287+
}
1288+
12651289
struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
12661290
const char *name,
12671291
unsigned int flags,
@@ -1411,7 +1435,8 @@ static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespac
14111435
int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
14121436
struct user_namespace *user_ns,
14131437
const struct path *path,
1414-
struct smb_ntsd *pntsd, int len)
1438+
struct smb_ntsd *pntsd, int len,
1439+
bool get_write)
14151440
{
14161441
int rc;
14171442
struct ndr sd_ndr = {0}, acl_ndr = {0};
@@ -1471,7 +1496,7 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
14711496

14721497
rc = ksmbd_vfs_setxattr(user_ns, path,
14731498
XATTR_NAME_SD, sd_ndr.data,
1474-
sd_ndr.offset, 0);
1499+
sd_ndr.offset, 0, get_write);
14751500
if (rc < 0)
14761501
pr_err("Failed to store XATTR ntacl :%d\n", rc);
14771502

@@ -1560,7 +1585,8 @@ int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
15601585

15611586
int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
15621587
const struct path *path,
1563-
struct xattr_dos_attrib *da)
1588+
struct xattr_dos_attrib *da,
1589+
bool get_write)
15641590
{
15651591
struct ndr n;
15661592
int err;
@@ -1570,7 +1596,7 @@ int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
15701596
return err;
15711597

15721598
err = ksmbd_vfs_setxattr(user_ns, path, XATTR_NAME_DOS_ATTRIBUTE,
1573-
(void *)n.data, n.offset, 0);
1599+
(void *)n.data, n.offset, 0, get_write);
15741600
if (err)
15751601
ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
15761602
kfree(n.data);
@@ -1840,10 +1866,6 @@ int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
18401866
}
18411867
posix_state_to_acl(&acl_state, acls->a_entries);
18421868

1843-
rc = mnt_want_write(path->mnt);
1844-
if (rc)
1845-
goto out_err;
1846-
18471869
rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
18481870
if (rc < 0)
18491871
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
@@ -1856,9 +1878,7 @@ int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
18561878
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
18571879
rc);
18581880
}
1859-
mnt_drop_write(path->mnt);
18601881

1861-
out_err:
18621882
free_acl_state(&acl_state);
18631883
posix_acl_release(acls);
18641884
return rc;
@@ -1887,10 +1907,6 @@ int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
18871907
}
18881908
}
18891909

1890-
rc = mnt_want_write(path->mnt);
1891-
if (rc)
1892-
goto out_err;
1893-
18941910
rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
18951911
if (rc < 0)
18961912
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
@@ -1902,9 +1918,7 @@ int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
19021918
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
19031919
rc);
19041920
}
1905-
mnt_drop_write(path->mnt);
19061921

1907-
out_err:
19081922
posix_acl_release(acls);
19091923
return rc;
19101924
}

0 commit comments

Comments
 (0)