Skip to content

Commit 33b235a

Browse files
namjaejeonSteve French
authored andcommitted
ksmbd: fix race condition between tree conn lookup and disconnect
if thread A in smb2_write is using work-tcon, other thread B use smb2_tree_disconnect free the tcon, then thread A will use free'd tcon. Time + Thread A | Thread A smb2_write | smb2_tree_disconnect | | | kfree(tree_conn) | // UAF! | work->tcon->share_conf | + This patch add state, reference count and lock for tree conn to fix race condition issue. Reported-by: luosili <[email protected]> Signed-off-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 75ac9a3 commit 33b235a

File tree

6 files changed

+90
-17
lines changed

6 files changed

+90
-17
lines changed

fs/smb/server/mgmt/tree_connect.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ ksmbd_tree_conn_connect(struct ksmbd_conn *conn, struct ksmbd_session *sess,
7373

7474
tree_conn->user = sess->user;
7575
tree_conn->share_conf = sc;
76+
tree_conn->t_state = TREE_NEW;
7677
status.tree_conn = tree_conn;
78+
atomic_set(&tree_conn->refcount, 1);
79+
init_waitqueue_head(&tree_conn->refcount_q);
7780

7881
ret = xa_err(xa_store(&sess->tree_conns, tree_conn->id, tree_conn,
7982
GFP_KERNEL));
@@ -93,14 +96,33 @@ ksmbd_tree_conn_connect(struct ksmbd_conn *conn, struct ksmbd_session *sess,
9396
return status;
9497
}
9598

99+
void ksmbd_tree_connect_put(struct ksmbd_tree_connect *tcon)
100+
{
101+
/*
102+
* Checking waitqueue to releasing tree connect on
103+
* tree disconnect. waitqueue_active is safe because it
104+
* uses atomic operation for condition.
105+
*/
106+
if (!atomic_dec_return(&tcon->refcount) &&
107+
waitqueue_active(&tcon->refcount_q))
108+
wake_up(&tcon->refcount_q);
109+
}
110+
96111
int ksmbd_tree_conn_disconnect(struct ksmbd_session *sess,
97112
struct ksmbd_tree_connect *tree_conn)
98113
{
99114
int ret;
100115

116+
write_lock(&sess->tree_conns_lock);
117+
xa_erase(&sess->tree_conns, tree_conn->id);
118+
write_unlock(&sess->tree_conns_lock);
119+
120+
if (!atomic_dec_and_test(&tree_conn->refcount))
121+
wait_event(tree_conn->refcount_q,
122+
atomic_read(&tree_conn->refcount) == 0);
123+
101124
ret = ksmbd_ipc_tree_disconnect_request(sess->id, tree_conn->id);
102125
ksmbd_release_tree_conn_id(sess, tree_conn->id);
103-
xa_erase(&sess->tree_conns, tree_conn->id);
104126
ksmbd_share_config_put(tree_conn->share_conf);
105127
kfree(tree_conn);
106128
return ret;
@@ -111,11 +133,15 @@ struct ksmbd_tree_connect *ksmbd_tree_conn_lookup(struct ksmbd_session *sess,
111133
{
112134
struct ksmbd_tree_connect *tcon;
113135

136+
read_lock(&sess->tree_conns_lock);
114137
tcon = xa_load(&sess->tree_conns, id);
115138
if (tcon) {
116-
if (test_bit(TREE_CONN_EXPIRE, &tcon->status))
139+
if (tcon->t_state != TREE_CONNECTED)
140+
tcon = NULL;
141+
else if (!atomic_inc_not_zero(&tcon->refcount))
117142
tcon = NULL;
118143
}
144+
read_unlock(&sess->tree_conns_lock);
119145

120146
return tcon;
121147
}
@@ -129,8 +155,18 @@ int ksmbd_tree_conn_session_logoff(struct ksmbd_session *sess)
129155
if (!sess)
130156
return -EINVAL;
131157

132-
xa_for_each(&sess->tree_conns, id, tc)
158+
xa_for_each(&sess->tree_conns, id, tc) {
159+
write_lock(&sess->tree_conns_lock);
160+
if (tc->t_state == TREE_DISCONNECTED) {
161+
write_unlock(&sess->tree_conns_lock);
162+
ret = -ENOENT;
163+
continue;
164+
}
165+
tc->t_state = TREE_DISCONNECTED;
166+
write_unlock(&sess->tree_conns_lock);
167+
133168
ret |= ksmbd_tree_conn_disconnect(sess, tc);
169+
}
134170
xa_destroy(&sess->tree_conns);
135171
return ret;
136172
}

fs/smb/server/mgmt/tree_connect.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ struct ksmbd_share_config;
1414
struct ksmbd_user;
1515
struct ksmbd_conn;
1616

17-
#define TREE_CONN_EXPIRE 1
17+
enum {
18+
TREE_NEW = 0,
19+
TREE_CONNECTED,
20+
TREE_DISCONNECTED
21+
};
1822

1923
struct ksmbd_tree_connect {
2024
int id;
@@ -27,7 +31,9 @@ struct ksmbd_tree_connect {
2731

2832
int maximal_access;
2933
bool posix_extensions;
30-
unsigned long status;
34+
atomic_t refcount;
35+
wait_queue_head_t refcount_q;
36+
unsigned int t_state;
3137
};
3238

3339
struct ksmbd_tree_conn_status {
@@ -46,6 +52,7 @@ struct ksmbd_session;
4652
struct ksmbd_tree_conn_status
4753
ksmbd_tree_conn_connect(struct ksmbd_conn *conn, struct ksmbd_session *sess,
4854
const char *share_name);
55+
void ksmbd_tree_connect_put(struct ksmbd_tree_connect *tcon);
4956

5057
int ksmbd_tree_conn_disconnect(struct ksmbd_session *sess,
5158
struct ksmbd_tree_connect *tree_conn);

fs/smb/server/mgmt/user_session.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ static struct ksmbd_session *__session_create(int protocol)
355355
xa_init(&sess->ksmbd_chann_list);
356356
xa_init(&sess->rpc_handle_list);
357357
sess->sequence_number = 1;
358+
rwlock_init(&sess->tree_conns_lock);
358359

359360
ret = __init_smb2_session(sess);
360361
if (ret)

fs/smb/server/mgmt/user_session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct ksmbd_session {
6060

6161
struct ksmbd_file_table file_table;
6262
unsigned long last_active;
63+
rwlock_t tree_conns_lock;
6364
};
6465

6566
static inline int test_session_flag(struct ksmbd_session *sess, int bit)

fs/smb/server/server.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
241241
} while (is_chained == true);
242242

243243
send:
244+
if (work->tcon)
245+
ksmbd_tree_connect_put(work->tcon);
244246
smb3_preauth_hash_rsp(work);
245247
if (work->sess && work->sess->enc && work->encrypted &&
246248
conn->ops->encrypt_resp) {

fs/smb/server/smb2pdu.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,9 @@ int smb2_tree_connect(struct ksmbd_work *work)
19931993
if (conn->posix_ext_supported)
19941994
status.tree_conn->posix_extensions = true;
19951995

1996+
write_lock(&sess->tree_conns_lock);
1997+
status.tree_conn->t_state = TREE_CONNECTED;
1998+
write_unlock(&sess->tree_conns_lock);
19961999
rsp->StructureSize = cpu_to_le16(16);
19972000
out_err1:
19982001
rsp->Capabilities = 0;
@@ -2122,27 +2125,50 @@ int smb2_tree_disconnect(struct ksmbd_work *work)
21222125

21232126
ksmbd_debug(SMB, "request\n");
21242127

2128+
if (!tcon) {
2129+
ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2130+
2131+
rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2132+
err = -ENOENT;
2133+
goto err_out;
2134+
}
2135+
2136+
ksmbd_close_tree_conn_fds(work);
2137+
2138+
write_lock(&sess->tree_conns_lock);
2139+
if (tcon->t_state == TREE_DISCONNECTED) {
2140+
write_unlock(&sess->tree_conns_lock);
2141+
rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2142+
err = -ENOENT;
2143+
goto err_out;
2144+
}
2145+
2146+
WARN_ON_ONCE(atomic_dec_and_test(&tcon->refcount));
2147+
tcon->t_state = TREE_DISCONNECTED;
2148+
write_unlock(&sess->tree_conns_lock);
2149+
2150+
err = ksmbd_tree_conn_disconnect(sess, tcon);
2151+
if (err) {
2152+
rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2153+
goto err_out;
2154+
}
2155+
2156+
work->tcon = NULL;
2157+
21252158
rsp->StructureSize = cpu_to_le16(4);
21262159
err = ksmbd_iov_pin_rsp(work, rsp,
21272160
sizeof(struct smb2_tree_disconnect_rsp));
21282161
if (err) {
21292162
rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
2130-
smb2_set_err_rsp(work);
2131-
return err;
2163+
goto err_out;
21322164
}
21332165

2134-
if (!tcon || test_and_set_bit(TREE_CONN_EXPIRE, &tcon->status)) {
2135-
ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2166+
return 0;
21362167

2137-
rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2138-
smb2_set_err_rsp(work);
2139-
return -ENOENT;
2140-
}
2168+
err_out:
2169+
smb2_set_err_rsp(work);
2170+
return err;
21412171

2142-
ksmbd_close_tree_conn_fds(work);
2143-
ksmbd_tree_conn_disconnect(sess, tcon);
2144-
work->tcon = NULL;
2145-
return 0;
21462172
}
21472173

21482174
/**

0 commit comments

Comments
 (0)