ksmbd: fix transform header validation
Subsystems (2)
Namjae Jeon
maintainerPATCH 1/1 ksmbd: fix transform header validation
OriginalMessageSize and SessionId should be used after validating transform header in request buffer.
--- fs/ksmbd/smb2pdu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
fs/ksmbd/smb2pdu.c +9 -9
@@ -8455,16 +8455,8 @@ int smb3_decrypt_req(struct ksmbd_work *work)unsigned int buf_data_size = pdu_length +4 -sizeof (struct smb2_transform_hdr );struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; -unsigned int orig_len =le32_to_cpu (tr_hdr->OriginalMessageSize);int rc =0 ; - sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); -if (!sess) { -pr_err ("invalid session id(%llx) in transform header\n" , -le64_to_cpu (tr_hdr->SessionId)); -return -ECONNABORTED ; - } -if (pdu_length +4 <sizeof (struct smb2_transform_hdr ) +sizeof (struct smb2_hdr )) { pr_err("Transform message is too small (%u)\n",
@@ -8472,11 +8464,19 @@ int smb3_decrypt_req(struct ksmbd_work *work)return -ECONNABORTED ; } -if (pdu_length +4 < orig_len +sizeof (struct smb2_transform_hdr )) { +if (pdu_length +4 < +le32_to_cpu (tr_hdr->OriginalMessageSize) +sizeof (struct smb2_transform_hdr )) {pr_err ("Transform message is broken\n" );return -ECONNABORTED ; } + sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); +if (!sess) { +pr_err ("invalid session id(%llx) in transform header\n" , +le64_to_cpu (tr_hdr->SessionId)); +return -ECONNABORTED ; + } + iov[0 ].iov_base = buf; iov[0 ].iov_len =sizeof (struct smb2_transform_hdr ); iov[1 ].iov_base = buf +sizeof (struct smb2_transform_hdr );
Signature · 1 line
2.25.1
Quoted 2 lines · Namjae Jeon
OriginalMessageSize and SessionId should be used after validating transform header in request buffer.
I suggest rewording the log for clarity:
++ Validate that the transform and smb request headers are present ++ before checking OriginalMessageSize and SessionId fields.
Is there some reason you aren't using the buf_data_size that is already calculated, to verify these offsets? It seems like a lot of redundant, and therefore fragile, coding.
Quoted 55 lines
Cc Tom Talpey <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Ralph Böhme <[email protected]>Cc Steve French <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Hyunchul Lee <[email protected]>Signed-off-by Namjae Jeon <[email protected]>--- fs/ksmbd/smb2pdu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)fs/ksmbd/smb2pdu.c +9
-9@@ -8455,16 +8455,8 @@ int smb3_decrypt_req(struct ksmbd_work *work)unsigned int buf_data_size = pdu_length +4 -sizeof (struct smb2_transform_hdr );struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; -unsigned int orig_len =le32_to_cpu (tr_hdr->OriginalMessageSize);int rc =0 ; - sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); -if (!sess) { -pr_err ("invalid session id(%llx) in transform header\n" , -le64_to_cpu (tr_hdr->SessionId)); -return -ECONNABORTED ; - } -if (pdu_length +4 <sizeof (struct smb2_transform_hdr ) +sizeof (struct smb2_hdr )) { pr_err("Transform message is too small (%u)\n",@@ -8472,11 +8464,19 @@ int smb3_decrypt_req(struct ksmbd_work *work)return -ECONNABORTED ; } -if (pdu_length +4 < orig_len +sizeof (struct smb2_transform_hdr )) { +if (pdu_length +4 < +le32_to_cpu (tr_hdr->OriginalMessageSize) +sizeof (struct smb2_transform_hdr )) {pr_err ("Transform message is broken\n" );return -ECONNABORTED ; } + sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); +if (!sess) { +pr_err ("invalid session id(%llx) in transform header\n" , +le64_to_cpu (tr_hdr->SessionId)); +return -ECONNABORTED ; + } + iov[0 ].iov_base = buf; iov[0 ].iov_len =sizeof (struct smb2_transform_hdr ); iov[1 ].iov_base = buf +sizeof (struct smb2_transform_hdr );
Namjae Jeon
maintainer2021-09-30 0:18 GMT+09:00, Tom Talpey <[email protected]>:
Quoted 9 lines · Namjae Jeon
Quoted 2 lines
OriginalMessageSize and SessionId should be used after validating transform header in request buffer.
I suggest rewording the log for clarity:
++ Validate that the transform and smb request headers are present ++ before checking OriginalMessageSize and SessionId fields.
Looks more clear:) Thank you! I will update it.
Quoted 4 lines
Is there some reason you aren't using the
buf_data_sizethat is already calculated, to verify these offsets? It seems like a lot of redundant, and therefore fragile, coding.
I can't remember why I did this 3 years ago... I will change it on another patch after checking it.
Quoted 2 lines
Reviewed-by Tom Talpey <[email protected]>
Thanks for your review!
Quoted 59 lines
Quoted 58 lines
Cc Tom Talpey <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Ralph Böhme <[email protected]>Cc Steve French <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Hyunchul Lee <[email protected]>Signed-off-by Namjae Jeon <[email protected]>--- fs/ksmbd/smb2pdu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)fs/ksmbd/smb2pdu.c +9
-9@@ -8455,16 +8455,8 @@ int smb3_decrypt_req(struct ksmbd_work *work)unsigned int buf_data_size = pdu_length +4 -sizeof (struct smb2_transform_hdr );struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf; -unsigned int orig_len =le32_to_cpu (tr_hdr->OriginalMessageSize);int rc =0 ; - sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); -if (!sess) { -pr_err ("invalid session id(%llx) in transform header\n" , -le64_to_cpu (tr_hdr->SessionId)); -return -ECONNABORTED ; - } -if (pdu_length +4 <sizeof (struct smb2_transform_hdr ) +sizeof (struct smb2_hdr )) { pr_err("Transform message is too small (%u)\n",@@ -8472,11 +8464,19 @@ int smb3_decrypt_req(struct ksmbd_work *work)return -ECONNABORTED ; } -if (pdu_length +4 < orig_len +sizeof (struct smb2_transform_hdr )) { +if (pdu_length +4 < +le32_to_cpu (tr_hdr->OriginalMessageSize) +sizeof (struct smb2_transform_hdr )) {pr_err ("Transform message is broken\n" );return -ECONNABORTED ; } + sess =ksmbd_session_lookup_all (conn,le64_to_cpu (tr_hdr->SessionId)); +if (!sess) { +pr_err ("invalid session id(%llx) in transform header\n" , +le64_to_cpu (tr_hdr->SessionId)); +return -ECONNABORTED ; + } + iov[0 ].iov_base = buf; iov[0 ].iov_len =sizeof (struct smb2_transform_hdr );iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);