Skip to contentSkip to conversation

ksmbd: read create context data at its DataOffset

PATCH Applied Ian Bridges 2 participants 2 messages Nov 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: read create context data at its DataOffset

ksmbd reads most create context data at a fixed offset instead of the client's DataOffset. When the data starts later, ksmbd reads padding or other bytes. For a security descriptor, zero padding can be parsed as having no DACL, so CREATE succeeds without applying the client's descriptor.

Use DataOffset for security descriptor, extended attribute, lease, durable handle, POSIX and allocation size contexts. The context lookup already validates DataOffset and DataLength. Check each payload's minimum length and keep the existing parser checks. Add smb2_context_data() and a structure for the durable reconnect data, and remove the request structures that no longer have users. Contexts using the usual offset behave as before.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")

---

Notes:
Tested on ksmbd-for-next 3bc1fb9c3b20 with KASAN. Moving context data 8 or 16 bytes beyond its usual offset now gives the same results as the usual layout. At the usual offset, a DataLength one byte too short is rejected with and without the patch. smbtorture results match the base.

This fix was developed with LLM assistance.

 fs/smb/server/oplock.c  |  30 +++++-------
 fs/smb/server/oplock.h  |  10 ++++
 fs/smb/server/smb2pdu.c | 102 +++++++++++++++++-----------------------
 fs/smb/server/smb2pdu.h |  20 ++------
 4 files changed, 67 insertions(+), 95 deletions(-)
fs/smb/server/oplock.c +11 -19
index c933d1e5f545..7c567180dbe1 100644
@@ -2321,36 +2321,28 @@ struct lease_ctx_info *parse_lease_state(void *open_req)
 		return ERR_PTR(-ENOMEM);
 
 	if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) {
-		struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
+		struct lease_context_v2 *lc = smb2_context_data(cc);
 
-		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
-		    sizeof(struct create_lease_v2) - 4)
-			goto err_out;
-
-		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
-		lreq->req_state = lc->lcontext.LeaseState;
-		lreq->flags = lc->lcontext.LeaseFlags;
-		lreq->epoch = lc->lcontext.Epoch;
-		lreq->duration = lc->lcontext.LeaseDuration;
+		memcpy(lreq->lease_key, lc->LeaseKey, SMB2_LEASE_KEY_SIZE);
+		lreq->req_state = lc->LeaseState;
+		lreq->flags = lc->LeaseFlags;
+		lreq->epoch = lc->Epoch;
+		lreq->duration = lc->LeaseDuration;
 		if (!lease_state_valid(lreq->req_state) ||
 		    !lease_v2_flags_valid(lreq->flags))
 			goto err_out;
 		lreq->req_state = lease_state_grantable(lreq->req_state);
 		if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
-			memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey,
+			memcpy(lreq->parent_lease_key, lc->ParentLeaseKey,
 			       SMB2_LEASE_KEY_SIZE);
 		lreq->version = 2;
 	} else if (sizeof(struct lease_context) == le32_to_cpu(cc->DataLength)) {
-		struct create_lease *lc = (struct create_lease *)cc;
-
-		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
-		    sizeof(struct create_lease))
-			goto err_out;
+		struct lease_context *lc = smb2_context_data(cc);
 
-		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
-		lreq->req_state = lc->lcontext.LeaseState;
+		memcpy(lreq->lease_key, lc->LeaseKey, SMB2_LEASE_KEY_SIZE);
+		lreq->req_state = lc->LeaseState;
 		lreq->flags = 0;
-		lreq->duration = lc->lcontext.LeaseDuration;
+		lreq->duration = lc->LeaseDuration;
 		if (!lease_state_valid(lreq->req_state))
 			goto err_out;
 		lreq->req_state = lease_state_grantable(lreq->req_state);
fs/smb/server/oplock.h +10 -0
index d81013c514e3..945effd5240a 100644
@@ -123,6 +123,16 @@ void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp);
 void create_aapl_rsp_buf(char *cc, __u64 vol_caps, __u64 req_bitmap,
 			 bool readdir_attr_v2);
 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len);
+
+/*
+ * The data of a context smb2_find_context_vals() returned. It starts at
+ * DataOffset, and its DataLength bytes lie inside the context.
+ */
+static inline void *smb2_context_data(struct create_context *cc)
+{
+	return (char *)cc + le16_to_cpu(cc->DataOffset);
+}
+
 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
 					  char *lease_key);
 int find_same_lease_key(struct ksmbd_conn *conn, struct ksmbd_inode *ci,
fs/smb/server/smb2pdu.c +42 -60

250 lines not on this page · raw message

fs/smb/server/smb2pdu.h +4 -16
index 8c6b170c2716..3edd7747b96f 100644
@@ -140,10 +140,10 @@ struct create_aapl_rsp {
 
 #define DURABLE_HANDLE_MAX_TIMEOUT	300000
 
-struct create_alloc_size_req {
-	struct create_context_hdr ccontext;
-	__u8   Name[8];
-	__le64 AllocationSize;
+/* See MS-SMB2 2.2.13.2.4, the data of a DHnC create context */
+struct durable_reconnect_context {
+	__u64 PersistentFileId;
+	__u64 VolatileFileId;
 } __packed;
 
 struct create_durable_rsp {
@@ -338,18 +338,6 @@ struct smb2_ea_info {
 	/* optionally followed by value */
 } __packed; /* level 15 Query */
 
-struct create_ea_buf_req {
-	struct create_context_hdr ccontext;
-	__u8   Name[8];
-	struct smb2_ea_info ea;
-} __packed;
-
-struct create_sd_buf_req {
-	struct create_context_hdr ccontext;
-	__u8   Name[8];
-	struct smb_ntsd ntsd;
-} __packed;
-
 /* See POSIX-FSCC 2.2.1 */
 struct smb2_posix_info {
 	__le32 NextEntryOffset;
Signature · 1 line
2.47.3
maintainer Applied
#1 lore raw
Quoted 15 lines · Ian Bridges

ksmbd reads most create context data at a fixed offset instead of the client's DataOffset. When the data starts later, ksmbd reads padding or other bytes. For a security descriptor, zero padding can be parsed as having no DACL, so CREATE succeeds without applying the client's descriptor.

Use DataOffset for security descriptor, extended attribute, lease, durable handle, POSIX and allocation size contexts. The context lookup already validates DataOffset and DataLength. Check each payload's minimum length and keep the existing parser checks. Add smb2_context_data() and a structure for the durable reconnect data, and remove the request structures that no longer have users. Contexts using the usual offset behave as before.

Quoted 4 lines
Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")

Applied it to #ksmbd-for-next.
Thanks.