Skip to content

Commit a1f46c9

Browse files
namjaejeonSteve French
authored andcommitted
ksmbd: fix use-after-free in ksmbd_session_rpc_open
A UAF issue can occur due to a race condition between ksmbd_session_rpc_open() and __session_rpc_close(). Add rpc_lock to the session to protect it. Cc: stable@vger.kernel.org Reported-by: Norbert Szetei <norbert@doyensec.com> Tested-by: Norbert Szetei <norbert@doyensec.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent af5226a commit a1f46c9

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

fs/smb/server/mgmt/user_session.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ static void ksmbd_session_rpc_clear_list(struct ksmbd_session *sess)
5959
struct ksmbd_session_rpc *entry;
6060
long index;
6161

62+
down_write(&sess->rpc_lock);
6263
xa_for_each(&sess->rpc_handle_list, index, entry) {
6364
xa_erase(&sess->rpc_handle_list, index);
6465
__session_rpc_close(sess, entry);
6566
}
67+
up_write(&sess->rpc_lock);
6668

6769
xa_destroy(&sess->rpc_handle_list);
6870
}
@@ -92,7 +94,7 @@ int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name)
9294
{
9395
struct ksmbd_session_rpc *entry, *old;
9496
struct ksmbd_rpc_command *resp;
95-
int method;
97+
int method, id;
9698

9799
method = __rpc_method(rpc_name);
98100
if (!method)
@@ -102,36 +104,41 @@ int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name)
102104
if (!entry)
103105
return -ENOMEM;
104106

107+
down_read(&sess->rpc_lock);
105108
entry->method = method;
106-
entry->id = ksmbd_ipc_id_alloc();
107-
if (entry->id < 0)
109+
entry->id = id = ksmbd_ipc_id_alloc();
110+
if (id < 0)
108111
goto free_entry;
109-
old = xa_store(&sess->rpc_handle_list, entry->id, entry, KSMBD_DEFAULT_GFP);
112+
old = xa_store(&sess->rpc_handle_list, id, entry, KSMBD_DEFAULT_GFP);
110113
if (xa_is_err(old))
111114
goto free_id;
112115

113-
resp = ksmbd_rpc_open(sess, entry->id);
116+
resp = ksmbd_rpc_open(sess, id);
114117
if (!resp)
115118
goto erase_xa;
116119

120+
up_read(&sess->rpc_lock);
117121
kvfree(resp);
118-
return entry->id;
122+
return id;
119123
erase_xa:
120124
xa_erase(&sess->rpc_handle_list, entry->id);
121125
free_id:
122126
ksmbd_rpc_id_free(entry->id);
123127
free_entry:
124128
kfree(entry);
129+
up_read(&sess->rpc_lock);
125130
return -EINVAL;
126131
}
127132

128133
void ksmbd_session_rpc_close(struct ksmbd_session *sess, int id)
129134
{
130135
struct ksmbd_session_rpc *entry;
131136

137+
down_write(&sess->rpc_lock);
132138
entry = xa_erase(&sess->rpc_handle_list, id);
133139
if (entry)
134140
__session_rpc_close(sess, entry);
141+
up_write(&sess->rpc_lock);
135142
}
136143

137144
int ksmbd_session_rpc_method(struct ksmbd_session *sess, int id)
@@ -439,6 +446,7 @@ static struct ksmbd_session *__session_create(int protocol)
439446
sess->sequence_number = 1;
440447
rwlock_init(&sess->tree_conns_lock);
441448
atomic_set(&sess->refcnt, 2);
449+
init_rwsem(&sess->rpc_lock);
442450

443451
ret = __init_smb2_session(sess);
444452
if (ret)

fs/smb/server/mgmt/user_session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct ksmbd_session {
6363
rwlock_t tree_conns_lock;
6464

6565
atomic_t refcnt;
66+
struct rw_semaphore rpc_lock;
6667
};
6768

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

0 commit comments

Comments
 (0)