Skip to content

Commit 23bcea8

Browse files
namjaejeonroxanan1996
authored andcommitted
ksmbd: fix possible deadlock in smb2_open
BugLink: https://bugs.launchpad.net/bugs/2052406 [ 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: Greg Kroah-Hartman <[email protected]> Signed-off-by: Portia Stephens <[email protected]> Signed-off-by: Roxana Nicolescu <[email protected]>
1 parent b0bea6f commit 23bcea8

File tree

5 files changed

+75
-59
lines changed

5 files changed

+75
-59
lines changed

fs/ksmbd/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,
@@ -3149,7 +3150,8 @@ int smb2_open(struct ksmbd_work *work)
31493150
user_ns,
31503151
&path,
31513152
pntsd,
3152-
pntsd_size);
3153+
pntsd_size,
3154+
false);
31533155
kfree(pntsd);
31543156
if (rc)
31553157
pr_err("failed to store ntacl in xattr : %d\n",
@@ -3225,12 +3227,6 @@ int smb2_open(struct ksmbd_work *work)
32253227
if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
32263228
ksmbd_fd_set_delete_on_close(fp, file_info);
32273229

3228-
if (need_truncate) {
3229-
rc = smb2_create_truncate(&path);
3230-
if (rc)
3231-
goto err_out;
3232-
}
3233-
32343230
if (req->CreateContextsOffset) {
32353231
struct create_alloc_size_req *az_req;
32363232

@@ -3395,11 +3391,12 @@ int smb2_open(struct ksmbd_work *work)
33953391
}
33963392

33973393
err_out:
3398-
if (file_present || created) {
3399-
inode_unlock(d_inode(parent_path.dentry));
3400-
path_put(&path);
3401-
path_put(&parent_path);
3402-
}
3394+
if (file_present || created)
3395+
ksmbd_vfs_kern_path_unlock(&parent_path, &path);
3396+
3397+
if (fp && need_truncate)
3398+
rc = smb2_create_truncate(&fp->filp->f_path);
3399+
34033400
ksmbd_revert_fsids(work);
34043401
err_out1:
34053402
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");
@@ -6015,7 +6011,7 @@ static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
60156011
fp->saccess |= FILE_SHARE_DELETE_LE;
60166012

60176013
return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
6018-
buf_len, false);
6014+
buf_len, false, true);
60196015
}
60206016

60216017
/**
@@ -7585,7 +7581,8 @@ static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
75857581

75867582
da.attr = le32_to_cpu(fp->f_ci->m_fattr);
75877583
ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
7588-
&fp->filp->f_path, &da);
7584+
&fp->filp->f_path,
7585+
&da, true);
75897586
if (ret)
75907587
fp->f_ci->m_fattr = old_fattr;
75917588
}

fs/ksmbd/smbacl.c

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

1186-
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, pntsd_size);
1186+
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, pntsd_size, false);
11871187
kfree(pntsd);
11881188
}
11891189

@@ -1375,7 +1375,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
13751375

13761376
int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
13771377
const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
1378-
bool type_check)
1378+
bool type_check, bool get_write)
13791379
{
13801380
int rc;
13811381
struct smb_fattr fattr = {{0}};
@@ -1435,7 +1435,8 @@ int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
14351435
if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
14361436
/* Update WinACL in xattr */
14371437
ksmbd_vfs_remove_sd_xattrs(user_ns, path);
1438-
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, ntsd_len);
1438+
ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, ntsd_len,
1439+
get_write);
14391440
}
14401441

14411442
out:

fs/ksmbd/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/ksmbd/vfs.c

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

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

124131
err_out:
125132
inode_unlock(d_inode(parent_path->dentry));
133+
mnt_drop_write(parent_path->mnt);
126134
path_put(parent_path);
127135
putname(filename);
128136
return -ENOENT;
@@ -451,7 +459,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
451459
fp->stream.name,
452460
(void *)stream_buf,
453461
size,
454-
0);
462+
0,
463+
true);
455464
if (err < 0)
456465
goto out;
457466

@@ -593,10 +602,6 @@ int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
593602
goto out_err;
594603
}
595604

596-
err = mnt_want_write(path->mnt);
597-
if (err)
598-
goto out_err;
599-
600605
user_ns = mnt_user_ns(path->mnt);
601606
if (S_ISDIR(d_inode(path->dentry)->i_mode)) {
602607
err = vfs_rmdir(user_ns, d_inode(parent), path->dentry);
@@ -607,7 +612,6 @@ int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path)
607612
if (err)
608613
ksmbd_debug(VFS, "unlink failed, err %d\n", err);
609614
}
610-
mnt_drop_write(path->mnt);
611615

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

919-
err = mnt_want_write(path->mnt);
920-
if (err)
921-
return err;
925+
if (get_write == true) {
926+
err = mnt_want_write(path->mnt);
927+
if (err)
928+
return err;
929+
}
922930

923931
err = vfs_setxattr(user_ns,
924932
path->dentry,
@@ -928,7 +936,8 @@ int ksmbd_vfs_setxattr(struct user_namespace *user_ns,
928936
flags);
929937
if (err)
930938
ksmbd_debug(VFS, "setxattr failed, err %d\n", err);
931-
mnt_drop_write(path->mnt);
939+
if (get_write == true)
940+
mnt_drop_write(path->mnt);
932941
return err;
933942
}
934943

@@ -1254,6 +1263,13 @@ int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
12541263
}
12551264

12561265
if (!err) {
1266+
err = mnt_want_write(parent_path->mnt);
1267+
if (err) {
1268+
path_put(path);
1269+
path_put(parent_path);
1270+
return err;
1271+
}
1272+
12571273
err = ksmbd_vfs_lock_parent(parent_path->dentry, path->dentry);
12581274
if (err) {
12591275
path_put(path);
@@ -1263,6 +1279,14 @@ int ksmbd_vfs_kern_path_locked(struct ksmbd_work *work, char *name,
12631279
return err;
12641280
}
12651281

1282+
void ksmbd_vfs_kern_path_unlock(struct path *parent_path, struct path *path)
1283+
{
1284+
inode_unlock(d_inode(parent_path->dentry));
1285+
mnt_drop_write(parent_path->mnt);
1286+
path_put(path);
1287+
path_put(parent_path);
1288+
}
1289+
12661290
struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work,
12671291
const char *name,
12681292
unsigned int flags,
@@ -1412,7 +1436,8 @@ static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct user_namespac
14121436
int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
14131437
struct user_namespace *user_ns,
14141438
const struct path *path,
1415-
struct smb_ntsd *pntsd, int len)
1439+
struct smb_ntsd *pntsd, int len,
1440+
bool get_write)
14161441
{
14171442
int rc;
14181443
struct ndr sd_ndr = {0}, acl_ndr = {0};
@@ -1472,7 +1497,7 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
14721497

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

@@ -1561,7 +1586,8 @@ int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
15611586

15621587
int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
15631588
const struct path *path,
1564-
struct xattr_dos_attrib *da)
1589+
struct xattr_dos_attrib *da,
1590+
bool get_write)
15651591
{
15661592
struct ndr n;
15671593
int err;
@@ -1571,7 +1597,7 @@ int ksmbd_vfs_set_dos_attrib_xattr(struct user_namespace *user_ns,
15711597
return err;
15721598

15731599
err = ksmbd_vfs_setxattr(user_ns, path, XATTR_NAME_DOS_ATTRIBUTE,
1574-
(void *)n.data, n.offset, 0);
1600+
(void *)n.data, n.offset, 0, get_write);
15751601
if (err)
15761602
ksmbd_debug(SMB, "failed to store dos attribute in xattr\n");
15771603
kfree(n.data);
@@ -1841,10 +1867,6 @@ int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
18411867
}
18421868
posix_state_to_acl(&acl_state, acls->a_entries);
18431869

1844-
rc = mnt_want_write(path->mnt);
1845-
if (rc)
1846-
goto out_err;
1847-
18481870
rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
18491871
if (rc < 0)
18501872
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
@@ -1857,9 +1879,7 @@ int ksmbd_vfs_set_init_posix_acl(struct user_namespace *user_ns,
18571879
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
18581880
rc);
18591881
}
1860-
mnt_drop_write(path->mnt);
18611882

1862-
out_err:
18631883
free_acl_state(&acl_state);
18641884
posix_acl_release(acls);
18651885
return rc;
@@ -1888,10 +1908,6 @@ int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
18881908
}
18891909
}
18901910

1891-
rc = mnt_want_write(path->mnt);
1892-
if (rc)
1893-
goto out_err;
1894-
18951911
rc = set_posix_acl(user_ns, inode, ACL_TYPE_ACCESS, acls);
18961912
if (rc < 0)
18971913
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
@@ -1903,9 +1919,7 @@ int ksmbd_vfs_inherit_posix_acl(struct user_namespace *user_ns,
19031919
ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
19041920
rc);
19051921
}
1906-
mnt_drop_write(path->mnt);
19071922

1908-
out_err:
19091923
posix_acl_release(acls);
19101924
return rc;
19111925
}

0 commit comments

Comments
 (0)