[PATCH v3] ksmbd: build a response when the connection state stops a request
From: Andreas Claesson via B4 Relay
Date: Fri Dec 04 2026 - 09:18:58 EST
From: Andreas Claesson <andreas@xxxxxxxxxxxxx>
__process_request() does not run a request when the connection is
exiting or needs to reconnect. check_conn_state() then writes
STATUS_CONNECTION_DISCONNECTED at the start of the response buffer,
through a struct smb_hdr, which is the SMB1 header, and
__process_request() returns SERVER_HANDLER_CONTINUE, so
__handle_ksmbd_work() goes on to set the credits and to decide whether
to sign the response.
Since commit e2b76ab8b5c9 ("ksmbd: add support for read compound") a
response is only sent if it is pinned with ksmbd_iov_pin_rsp(), and
check_conn_state() does not pin one.
For a single request nothing is sent. On a session that does not
require signing nothing is signed either, because the command value
that is passed to is_sign_req() has not been read yet and is still
zero, which smb2_is_sign_req() takes for a NEGOTIATE.
For a later command of a compound, that value is still the one of the
previous command, so a signed compound is signed. The status is not in
the response of that command either: with the SMB1 layout it overwrites
part of StructureSize, CreditCharge and the low byte of Status in the
header of the first response. The signing code then does not find the
response it is asked to sign:
WARNING: fs/smb/server/smb2pdu.c:13151 at smb2_get_sign_rsp_iov+0x13d/0x260
Workqueue: ksmbd-io handle_ksmbd_work
Call Trace:
smb3_set_sign_rsp+0x2ba/0x510
handle_ksmbd_work+0x10a2/0x27a0
A client with a session can trigger this. One connection sends a signed
compound of a LOCK that blocks and a NEGOTIATE. A second connection
then sends a SESSION_SETUP with the PreviousSessionId of that session.
destroy_previous_session() marks the first connection as
KSMBD_SESS_NEED_RECONNECT and wakes the blocked LOCK, and the NEGOTIATE
that follows it is stopped by check_conn_state(). A second NEGOTIATE on
the connection of the compound, which sets it exiting, has the same
effect on the command after the blocked LOCK.
Build the response with the set_rsp_status() operation, which writes
the status into the response of the current command and pins an error
response, and return SERVER_HANDLER_ABORT, as the other early error
paths of __process_request() do. This skips the credit and signing
steps of __handle_ksmbd_work(), which must not run for a request whose
MessageId, CreditCharge and signature have not been checked.
The stopped request is not charged. Its response ends the work, so it
carries the credits that the earlier commands of a compound were
granted.
The response is signed only if the request is signed and passes
validation and the signature check. Otherwise a single request that is
signed, or whose session requires signing, gets no response, and a
later command of a compound gets an unsigned response: the previous
response already points to it, and dropping the message would also
drop the earlier responses. An encrypted request is answered encrypted,
as before.
Move the handling of the last response of a compound out of
is_chained_smb2_message() into smb2_end_compound_rsp(), so that it can
be used here before signing.
Fixes: e2b76ab8b5c9 ("ksmbd: add support for read compound")
Assisted-by: Claude:claude-mythos-5-2
Signed-off-by: Andreas Claesson <andreas@xxxxxxxxxxxxx>
---
Found with syzkaller, using local descriptions for ksmbd.
Tested on ksmbd-for-next (87ff512becd4) in QEMU with KASAN, with the
PreviousSessionId reproducer from the commit message and variants of
it.
Without the patch: the warning above. The compound response carries
only the LOCK response, with status 0xc00000c0 instead of 0xc000007e.
With the patch: no warning. The LOCK response has status 0xc000007e and
is followed by one response with STATUS_CONNECTION_DISCONNECTED, which
carries the 64 credits that the LOCK was granted (v2: 0).
- stopped NEGOTIATE, also with a wrong signature, with a CreditCharge
of 65535 and on a session that requires signing: not signed
- stopped ECHO with a valid signature: signed, and the signature
verifies with the signing key of the session
- stopped ECHO with a wrong signature: not signed
Not tested: the case of a single request needs a race with the state
change and was not reached, so the path that sends no response has not
run.
Namjae, one point differs from what you asked for. A later command of
a compound that cannot be authenticated is still answered, unsigned,
instead of not at all. The response before it already has its
NextCommand set, and send_no_response would drop the responses to the
earlier commands as well. ksmbd_sign_expired_rsp() leaves a chained
error response unsigned in the same way. If you prefer to drop the
whole compound response in that case, I will change it.
Ian, I did not carry your Reviewed-by and Tested-by, because the code
changed.
---
Changes in v3:
- Do not charge the stopped request, and return the credits that the
earlier commands of a compound were granted in its response (Namjae).
- Sign the response if the request is signed, after the request has
been validated and its signature checked. Send no response to a
single request that cannot be authenticated (Namjae).
- Move the handling of the last response of a compound from
is_chained_smb2_message() into smb2_end_compound_rsp(), and use it
for the response to the stopped request.
- Link to v2: https://patch.msgid.link/20261202-ksmbd-conn-state-rsp-v2-1-055ed29234dd@xxxxxxxxxxxxx
Changes in v2:
- Stop processing the work after the error response is built, so that
the credit and signing steps do not run for a request that was not
verified. v1 signed the response to a stopped command without checking
its signature, and pinned the error response twice when the credit
step failed. Both were found by the Sashiko review and confirmed by
Ian Bridges.
- Link to v1: https://patch.msgid.link/20261201-ksmbd-conn-state-rsp-v1-1-9a38d57ab0d5@xxxxxxxxxxxxx
---
fs/smb/server/server.c | 62 ++++++++++++++++++++++++++++++++++++++++---------
fs/smb/server/smb2pdu.c | 45 +++++++++++++++++++++--------------
fs/smb/server/smb2pdu.h | 1 +
3 files changed, 80 insertions(+), 28 deletions(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 962f12e969ba..0d1edfa1cb34 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -96,15 +96,8 @@ char *ksmbd_work_group(void)
*/
static inline int check_conn_state(struct ksmbd_work *work)
{
- struct smb_hdr *rsp_hdr;
-
- if (ksmbd_conn_exiting(work->conn) ||
- ksmbd_conn_need_reconnect(work->conn)) {
- rsp_hdr = smb_get_msg(work->response_buf);
- rsp_hdr->Status.CifsError = STATUS_CONNECTION_DISCONNECTED;
- return 1;
- }
- return 0;
+ return ksmbd_conn_exiting(work->conn) ||
+ ksmbd_conn_need_reconnect(work->conn);
}
/*
@@ -167,6 +160,51 @@ static int ksmbd_set_failed_rsp_credits(struct ksmbd_work *work)
return 0;
}
+/*
+ * The connection state stops this request before it is validated. Answer
+ * it with STATUS_CONNECTION_DISCONNECTED and do not charge it. The response
+ * ends the work, so it carries the credits that the earlier commands of a
+ * compound were granted.
+ *
+ * If the request is signed, or its session requires signing, the response
+ * is signed, but only after the request has been validated and its
+ * signature checked. A single request that cannot be authenticated gets no
+ * response. A later command of a compound is then still answered, unsigned:
+ * the previous response already points to a next one, and dropping the
+ * message would also drop the responses to the commands before it.
+ */
+static void ksmbd_conn_state_rsp(struct ksmbd_work *work)
+{
+ struct ksmbd_conn *conn = work->conn;
+ struct smb2_hdr *rsp_hdr;
+ bool sign = false;
+
+ if (work->sess && !work->encrypted && conn->ops->is_sign_req) {
+ bool signed_req;
+
+ signed_req = conn->ops->is_sign_req(work,
+ conn->ops->get_cmd_val(work));
+ if (signed_req && !ksmbd_verify_smb_message(work) &&
+ conn->ops->check_sign_req(work)) {
+ sign = true;
+ } else if ((signed_req || work->sess->sign) &&
+ !work->next_smb2_rcv_hdr_off) {
+ work->send_no_response = 1;
+ return;
+ }
+ }
+
+ conn->ops->set_rsp_status(work, STATUS_CONNECTION_DISCONNECTED);
+ if (!conn->ops->set_rsp_credits)
+ return;
+
+ smb2_end_compound_rsp(work);
+ rsp_hdr = ksmbd_resp_buf_curr(work);
+ rsp_hdr->CreditRequest = cpu_to_le16(work->credits_granted);
+ if (sign)
+ conn->ops->set_sign_rsp(work);
+}
+
#define SERVER_HANDLER_CONTINUE 0
#define SERVER_HANDLER_ABORT 1
@@ -178,8 +216,10 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,
bool signed_req;
int ret;
- if (check_conn_state(work))
- return SERVER_HANDLER_CONTINUE;
+ if (check_conn_state(work)) {
+ ksmbd_conn_state_rsp(work);
+ return SERVER_HANDLER_ABORT;
+ }
if (ksmbd_verify_smb_message(work)) {
conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 89aad1acb370..75045b3db8d5 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -676,6 +676,32 @@ static bool smb2_compound_has_failed(struct ksmbd_work *work,
return true;
}
+/**
+ * smb2_end_compound_rsp() - finish the last response of a compound
+ * @work: smb work containing the response buffer
+ *
+ * The response that was built last is the last one of a compound, also when
+ * the compound ends early. Align it to 8 bytes and make it the current
+ * response, which is the one the caller signs. A single response is left as
+ * it is.
+ */
+void smb2_end_compound_rsp(struct ksmbd_work *work)
+{
+ unsigned int len;
+
+ if (!work->next_smb2_rcv_hdr_off)
+ return;
+
+ len = ALIGN(get_rfc1002_len(work->response_buf), 8);
+ len = len - get_rfc1002_len(work->response_buf);
+ if (len) {
+ ksmbd_debug(SMB, "padding len %u\n", len);
+ work->iov[work->iov_idx].iov_len += len;
+ inc_rfc1001_len(work->response_buf, len);
+ }
+ work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off;
+}
+
/**
* is_chained_smb2_message() - check for chained command
* @work: smb work containing smb request buffer
@@ -685,7 +711,7 @@ static bool smb2_compound_has_failed(struct ksmbd_work *work,
bool is_chained_smb2_message(struct ksmbd_work *work)
{
struct smb2_hdr *hdr = smb_get_msg(work->request_buf);
- unsigned int len, next_cmd;
+ unsigned int next_cmd;
if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
return false;
@@ -727,22 +753,7 @@ bool is_chained_smb2_message(struct ksmbd_work *work)
}
last_rsp:
- if (work->next_smb2_rcv_hdr_off) {
- /*
- * This is the last response of a compound, also when the
- * compound ends early because the next command does not fit.
- * Align it to 8 bytes and make it the current response, which
- * is the one the caller signs.
- */
- len = ALIGN(get_rfc1002_len(work->response_buf), 8);
- len = len - get_rfc1002_len(work->response_buf);
- if (len) {
- ksmbd_debug(SMB, "padding len %u\n", len);
- work->iov[work->iov_idx].iov_len += len;
- inc_rfc1001_len(work->response_buf, len);
- }
- work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off;
- }
+ smb2_end_compound_rsp(work);
return false;
}
diff --git a/fs/smb/server/smb2pdu.h b/fs/smb/server/smb2pdu.h
index 3edd7747b96f..d454f9813b97 100644
--- a/fs/smb/server/smb2pdu.h
+++ b/fs/smb/server/smb2pdu.h
@@ -386,6 +386,7 @@ int init_smb2_rsp_hdr(struct ksmbd_work *work);
bool smb2_check_compound_fid(struct ksmbd_work *work);
int smb2_allocate_rsp_buf(struct ksmbd_work *work);
bool is_chained_smb2_message(struct ksmbd_work *work);
+void smb2_end_compound_rsp(struct ksmbd_work *work);
int init_smb2_neg_rsp(struct ksmbd_work *work);
void smb2_set_err_rsp(struct ksmbd_work *work);
int smb2_check_user_session(struct ksmbd_work *work);
---
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
change-id: 20261201-ksmbd-conn-state-rsp-372fd8567ae0
Best regards,
--
Andreas Claesson <andreas@xxxxxxxxxxxxx>