Skip to content

ksmbd-tools: fix race conditions and thread safety in user management#337

Open
yskzalloc wants to merge 3 commits into
cifsd-team:masterfrom
yskzalloc:v1-fix-race
Open

ksmbd-tools: fix race conditions and thread safety in user management#337
yskzalloc wants to merge 3 commits into
cifsd-team:masterfrom
yskzalloc:v1-fix-race

Conversation

@yskzalloc
Copy link
Copy Markdown

This patch series addresses several race conditions and thread safety issues discovered in the user management subsystem tools/management/user.c of ksmbd-tools.

Since the ksmbd_user object can be accessed concurrently by multiple threads processing simultaneous login/logout requests or administrative actions, it is crucial that all modifications and reads of shared fields (such as flags, failed_login_count, and ref_count) are properly synchronized using the object's update_lock.

The series resolves these thread safety issues.

  • ksmbd-tools: Fix potential data race in __handle_login_request()
  • ksmbd-tools: Fix potential data race in usm_add_guest_account()
  • ksmbd-tools: Fix potential data race in usm_handle_logout_request()

Signed-off-by: Yunseong Kim yunseong.kim@est.tech

yskzalloc added 3 commits May 3, 2026 00:42
usm_handle_logout_request() updates user->failed_login_count
and user->flags when processing a logout request. However, it modifies
these fields without holding the user->update_lock.

Since the ksmbd_user object can be accessed concurrently by multiple
threads handling simultaneous login or logout requests for the same user,
this lockless modification leads to a data race, potentially corrupting
the login failure count or user state flags.

Fix this by acquiring the user->update_lock writer lock before updating
these fields to ensure thread safety.

Fixes: a114451 ("ksmbd-tools: throttle session setup failures to avoid dictionary attacks")
Signed-off-by: Yunseong Kim <yunseong.kim@est.tech>
usm_add_guest_account() sets the KSMBD_USER_FLAG_GUEST_ACCOUNT flag
for a newly created guest user. However, it calls set_user_flag()
without holding the user->update_lock.

Modifying the user's flags locklessly at this stage can result in
a data race if another thread accesses the user object simultaneously.

Fix this by acquiring the user->update_lock writer lock before setting
the guest account flag to ensure thread-safe flag modification.

Fixes: 54d2a02 ("ksmbd-tools: simplify config parser")
Signed-off-by: Yunseong Kim <yunseong.kim@est.tech>
__handle_login_request() reads user->flags and user->ngroups
to construct the login response status. However, these reads are
performed without holding the user->update_lock.

Since user->flags can be modified concurrently by other threads (for
example, during a logout request processing or when setting guest account
flags), this lockless access can lead to reading an inconsistent or
stale user state.

Fix this by acquiring the user->update_lock reader lock before accessing
these fields to ensure thread safety. As a minor cleanup, combine the
status flag assignments into a single line.

Fixes: c2b6ac1 ("cifsd-tools: factor out user login request handling")
Signed-off-by: Yunseong Kim <yunseong.kim@est.tech>
@namjaejeon
Copy link
Copy Markdown
Member

@kzall0c Okay. How did you reproduce this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants