Buffer validation and compound handling patches
Subsystems (2)
v7 cover Buffer validation and compound handling patches
v2:
- update comments of smb2_get_data_area_len().
- fix wrong buffer size check in fsctl_query_iface_info_ioctl().
- fix 32bit overflow in smb2_set_info.
v3:
- add buffer check for ByteCount of smb negotiate request.
- Moved buffer check of to the top of loop to avoid unneeded behavior when out_buf_len is smaller than network_interface_info_ioctl_rsp.
- get correct out_buf_len which doesn't exceed max stream protocol length.
- subtract single smb2_lock_element for correct buffer size check in
ksmbd_smb2_check_message().
v4:
- use work->response_sz for out_buf_len calculation in smb2_ioctl.
- move smb2_neg size check to above to validate NegotiateContextOffset field.
- remove unneeded dialect checks in smb2_sess_setup() and smb2_handle_negotiate().
- split smb2_set_info patch into two patches(declaring smb2_file_basic_info and buffer check)
v5:
- remove PDU size validation from ksmbd_conn_handler_loop()
- add PDU size validation to ksmbd_smb2_check_message()
- fix compound non-related request handling
v6:
- check we can access ProtocolId in ksmbd_verify_smb_message()
- optimize tcon and session check functions for compound related PDUs
- drop patch that broke SMB1 negprot
- check credits after fully validating PDU size
v7:
- drop header size check in ksmbd_verify_smb_message()
- fix invalid read when accessing StructureSize2 in ksmbd_smb2_check_message()
- validate credit charge after validating SMB2 PDU body size
Ralph Boehme (9):
ksmbd: use ksmbd_req_buf_next() in ksmbd_verify_smb_message()
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: add and use ksmbd_smb2_cur_pdu_buflen() in
ksmbd_smb2_check_message()
ksmbd: check buffer is big enough to access the SMB2 PUD body size
field
ksmdb: validate credit charge after validating SMB2 PDU body size
ksmdb: use cmd helper variable in smb2_get_ksmbd_tcon()
ksmdb: make smb2_get_ksmbd_tcon() callable with chained PDUs
ksmbd: make smb2_check_user_session() callable for compound PDUs
ksmdb: move session and tcon validation to __process_request()
fs/ksmbd/ksmbd_work.h | 1 + fs/ksmbd/server.c | 46 +++++++++++++++++++++------------- fs/ksmbd/smb2misc.c | 58 +++++++++++++++++++++++++++---------------- fs/ksmbd/smb2pdu.c | 39 +++++++++++++++++++++++------ fs/ksmbd/smb2pdu.h | 1 + fs/ksmbd/smb_common.c | 2 +- 6 files changed, 101 insertions(+), 46 deletions(-)
Signature · 1 line
2.31.1
PATCH v7 1/9 ksmbd: use ksmbd_req_buf_next() in ksmbd_verify_smb_message()
No change in behaviour.
--- fs/ksmbd/smb_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ksmbd/smb_common.c +1 -1
@@ -132,7 +132,7 @@ int ksmbd_lookup_protocol_idx(char *str)*/ int ksmbd_verify_smb_message (struct ksmbd_work *work) { -struct smb2_hdr *smb2_hdr = work->request_buf + work->next_smb2_rcv_hdr_off; +struct smb2_hdr *smb2_hdr =ksmbd_req_buf_next (work);struct smb_hdr *hdr;if (smb2_hdr->ProtocolId ==SMB2_PROTO_NUMBER )
Signature · 1 line
2.31.1
Re: [PATCH v7 1/9] ksmbd: use ksmbd_req_buf_next() in ksmbd_verify_smb_message()
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 3 lines
No change in behaviour.
Signed-off-by Ralph Boehme <[email protected]>
Thanks for your patch!
PATCH v7 2/9 ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
In reply to Ralph BoehmeNo change in behaviour.
--- fs/ksmbd/smb2misc.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
fs/ksmbd/smb2misc.c +1 -6
@@ -329,17 +329,12 @@ static int smb2_validate_credit_charge(struct smb2_hdr *hdr)int ksmbd_smb2_check_message (struct ksmbd_work *work) { -struct smb2_pdu *pdu = work->request_buf; +struct smb2_pdu *pdu =ksmbd_req_buf_next (work);struct smb2_hdr *hdr = &pdu->hdr;int command;__u32 clc_len;/* calculated length */ __u32 len =get_rfc1002_len (pdu); -if (work->next_smb2_rcv_hdr_off) { - pdu =ksmbd_req_buf_next (work); - hdr = &pdu->hdr; - } -if (le32_to_cpu (hdr->NextCommand) >0 ) { len =le32_to_cpu (hdr->NextCommand); }else if (work->next_smb2_rcv_hdr_off) {
Signature · 1 line
2.31.1
Re: [PATCH v7 2/9] ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 8 lines
No change in behaviour.
Cc Namjae Jeon <[email protected]>Cc Tom Talpey <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Steve French <[email protected]>Cc Hyunchul Lee <[email protected]>Signed-off-by Ralph Boehme <[email protected]>
Thanks for your patch!
PATCH v7 3/9 ksmbd: add and use ksmbd_smb2_cur_pdu_buflen() in ksmbd_smb2_check_message()
In reply to Ralph BoehmeNo change in behaviour.
--- fs/ksmbd/smb2misc.c | 36 +++++++++++++++++++++++++++--------- fs/ksmbd/smb2pdu.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)
fs/ksmbd/smb2misc.c +27 -9
@@ -333,14 +333,7 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)struct smb2_hdr *hdr = &pdu->hdr;int command;__u32 clc_len;/* calculated length */ -__u32 len =get_rfc1002_len (pdu); - -if (le32_to_cpu (hdr->NextCommand) >0 ) { - len =le32_to_cpu (hdr->NextCommand); - }else if (work->next_smb2_rcv_hdr_off) { - len -= work->next_smb2_rcv_hdr_off; - len =round_up (len,8 ); - } +__u32 len =ksmbd_smb2_cur_pdu_buflen (work);if (check_smb2_hdr (hdr))return 1 ;
@@ -395,7 +388,7 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)* Some windows servers (win2016) will pad also the final * PDU in a compound to 8 bytes. */ -if (ALIGN (clc_len,8 ) == len) +if (ALIGN (clc_len,8 ) ==ALIGN (len,8 ))return 0 ;/*
@@ -427,3 +420,28 @@ int smb2_negotiate_request(struct ksmbd_work *work) {return ksmbd_smb_negotiate_common (work,SMB2_NEGOTIATE_HE ); } + +/** +* ksmbd_smb2_cur_pdu_buflen() - Get len of current SMB2 PDU buffer +* This returns the lenght including any possible padding. +* @work: smb work containing request buffer +*/ +unsigned int ksmbd_smb2_cur_pdu_buflen (struct ksmbd_work *work) +{ +struct smb2_hdr *hdr =ksmbd_req_buf_next (work); +unsigned int buf_len; +unsigned int pdu_len; + +if (hdr->NextCommand !=0 ) { +/* +* hdr->NextCommand has already been validated by +* init_chained_smb2_rsp(). +*/ +return __le32_to_cpu (hdr->NextCommand); + } + + buf_len =get_rfc1002_len (work->request_buf); + pdu_len = buf_len - work->next_smb2_rcv_hdr_off; +return pdu_len; +} +
fs/ksmbd/smb2pdu.h +1 -0
@@ -1680,6 +1680,7 @@ int smb2_set_rsp_credits(struct ksmbd_work *work);/* smb2 misc functions */ int ksmbd_smb2_check_message (struct ksmbd_work *work); +unsigned int ksmbd_smb2_cur_pdu_buflen (struct ksmbd_work *work);/* smb2 command handlers */ int smb2_handle_negotiate (struct ksmbd_work *work);
Signature · 1 line
2.31.1
Namjae Jeon
maintainerRe: [PATCH v7 3/9] ksmbd: add and use ksmbd_smb2_cur_pdu_buflen() in ksmbd_smb2_check_message()
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 34 lines
No change in behaviour.
Cc Namjae Jeon <[email protected]>Cc Tom Talpey <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Steve French <[email protected]>Cc Hyunchul Lee <[email protected]>Signed-off-by Ralph Boehme <[email protected]>--- fs/ksmbd/smb2misc.c | 36 +++++++++++++++++++++++++++--------- fs/ksmbd/smb2pdu.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)fs/ksmbd/smb2misc.c +1
-8@@ -333,14 +333,7 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)struct smb2_hdr *hdr = &pdu->hdr;int command;__u32 clc_len;/* calculated length */ -__u32 len =get_rfc1002_len (pdu); - -if (le32_to_cpu (hdr->NextCommand) >0 ) { - len =le32_to_cpu (hdr->NextCommand); - }else if (work->next_smb2_rcv_hdr_off) { - len -= work->next_smb2_rcv_hdr_off; - len =round_up (len,8 ); - } +__u32 len =ksmbd_smb2_cur_pdu_buflen (work);if (check_smb2_hdr (hdr))return 1 ;
fs/ksmbd/smb2misc.c int ksmbd_smb2_check_message(struct ksmbd_work *work) · line 391
Quoted 5 lines
fs/ksmbd/smb2misc.c +1
-1@@ -395,7 +388,7 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)* Some windows servers (win2016) will pad also the final * PDU in a compound to 8 bytes. */ -if (ALIGN (clc_len,8 ) == len) +if (ALIGN (clc_len,8 ) ==ALIGN (len,8 ))
Can I know why you align rfc1002 len with 8 here ?
Thanks!
Re: [PATCH v7 3/9] ksmbd: add and use ksmbd_smb2_cur_pdu_buflen() in ksmbd_smb2_check_message()
Am 05.10.21 um 09:29 schrieb Namjae Jeon:
Quoted 4 lines
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
fs/ksmbd/smb2misc.c int ksmbd_smb2_check_message(struct ksmbd_work *work) · line 391
Quoted 2 lines
fs/ksmbd/smb2misc.c +1
-1-if (ALIGN (clc_len,8 ) == len) +if (ALIGN (clc_len,8 ) ==ALIGN (len,8 ))Can I know why you align rfc1002 len with 8 here ?
this should match the previous behaviour where for compound requests we called round_up(len, 8).
This could be done differently though:
len = ksmbd_smb2_cur_pdu_buflen(work);
if (work->next_smb2_rcv_hdr_off)
len = ALIGN(len, 8);
and then just do
if (ALIGN(clc_len, 8) == len)
-slow
Signature · 2 lines
Ralph Boehme, Samba Team https://samba.org/ SerNet Samba Team Lead https://sernet.de/en/team-samba
Namjae Jeon
maintainerRe: [PATCH v7 3/9] ksmbd: add and use ksmbd_smb2_cur_pdu_buflen() in ksmbd_smb2_check_message()
2021-10-05 16:46 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 6 lines
Am 05.10.21 um 09:29 schrieb Namjae Jeon:
Quoted 4 lines
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
fs/ksmbd/smb2misc.c int ksmbd_smb2_check_message(struct ksmbd_work *work) · line 391
Quoted 2 lines
fs/ksmbd/smb2misc.c +1
-1-if (ALIGN (clc_len,8 ) == len) +if (ALIGN (clc_len,8 ) ==ALIGN (len,8 ))Can I know why you align rfc1002 len with 8 here ?
Quoted 8 lines
this should match the previous behaviour where for compound requests we called
round_up(len, 8).This could be done differently though:
len =
ksmbd_smb2_cur_pdu_buflen(work);
if (work->next_smb2_rcv_hdr_off)len = ALIGN(len, 8);
I think that this code is wrong, we don't need to add pad about last compound *request* length.
Thanks!
Quoted 11 lines
and then just do
if (ALIGN(clc_len, 8) == len)-slow
--
Ralph Boehme, Samba Team https://samba.org/
SerNet Samba Team Lead https://sernet.de/en/team-samba
PATCH v7 4/9 ksmbd: check buffer is big enough to access the SMB2 PUD body size field
In reply to Ralph Boehme--- fs/ksmbd/smb2misc.c | 3 +++ 1 file changed, 3 insertions(+)
fs/ksmbd/smb2misc.c +3 -0
@@ -350,6 +350,9 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)return 1 ; } +if (len <sizeof (struct smb2_pdu ) -4 ) +return 1 ; +if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {if (command !=SMB2_OPLOCK_BREAK_HE && (hdr->Status ==0 || pdu->StructureSize2 !=SMB2_ERROR_STRUCTURE_SIZE2_LE )) {
Signature · 1 line
2.31.1
PATCH v7 5/9 ksmdb: validate credit charge after validating SMB2 PDU body size
In reply to Ralph Boehmesmb2_validate_credit_charge() accesses fields in the SMB2 PDU body, but until smb2_calc_size() is called the PDU has not yet been verified to be large enough to access the PDU dynamic part length field.
--- fs/ksmbd/smb2misc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/ksmbd/smb2misc.c +6 -6
@@ -373,12 +373,6 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work) } } -if ((work->conn->vals->capabilities &SMB2_GLOBAL_CAP_LARGE_MTU ) && -smb2_validate_credit_charge (hdr)) { - work->conn->ops->set_rsp_status (work,STATUS_INVALID_PARAMETER ); -return 1 ; - } -if (smb2_calc_size (hdr, &clc_len))return 1 ;
@@ -416,6 +410,12 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)return 1 ; } +if ((work->conn->vals->capabilities &SMB2_GLOBAL_CAP_LARGE_MTU ) && +smb2_validate_credit_charge (hdr)) { + work->conn->ops->set_rsp_status (work,STATUS_INVALID_PARAMETER ); +return 1 ; + } +return 0 ; }
Signature · 1 line
2.31.1
Re: [PATCH v7 5/9] ksmdb: validate credit charge after validating SMB2 PDU body size
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 7 lines
smb2_validate_credit_charge()accesses fields in the SMB2 PDU body, but untilsmb2_calc_size()is called the PDU has not yet been verified to be large enough
to access the PDU dynamic part length field.Signed-off-by Ralph Boehme <[email protected]>
Thanks for your work!
PATCH v7 6/9 ksmdb: use cmd helper variable in smb2_get_ksmbd_tcon()
In reply to Ralph BoehmeNo change in behaviour.
--- fs/ksmbd/smb2pdu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/ksmbd/smb2pdu.c +4 -3
@@ -94,12 +94,13 @@ struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_connint smb2_get_ksmbd_tcon (struct ksmbd_work *work) {struct smb2_hdr *req_hdr = work->request_buf; +unsigned int cmd =le16_to_cpu (req_hdr->Command);int tree_id; work->tcon =NULL ; -if (work->conn->ops->get_cmd_val (work) ==SMB2_TREE_CONNECT_HE || - work->conn->ops->get_cmd_val (work) ==SMB2_CANCEL_HE || - work->conn->ops->get_cmd_val (work) ==SMB2_LOGOFF_HE ) { +if (cmd ==SMB2_TREE_CONNECT_HE || + cmd ==SMB2_CANCEL_HE || + cmd ==SMB2_LOGOFF_HE ) {ksmbd_debug (SMB ,"skip to check tree connect request\n" );return 0 ; }
Signature · 1 line
2.31.1
Re: [PATCH v7 6/9] ksmdb: use cmd helper variable in smb2_get_ksmbd_tcon()
2021-10-05 14:03 GMT+09:00, Ralph Boehme <[email protected]>:
Quoted 8 lines
No change in behaviour.
Cc Namjae Jeon <[email protected]>Cc Tom Talpey <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Steve French <[email protected]>Cc Hyunchul Lee <[email protected]>Signed-off-by Ralph Boehme <[email protected]>
Thanks!
PATCH v7 7/9 ksmdb: make smb2_get_ksmbd_tcon() callable with chained PDUs
In reply to Ralph BoehmeAlso track the tcon id of compound requests.
--- fs/ksmbd/ksmbd_work.h | 1 + fs/ksmbd/smb2pdu.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
fs/ksmbd/ksmbd_work.h +1 -0
@@ -46,6 +46,7 @@ struct ksmbd_work {u64 compound_fid;u64 compound_pfid;u64 compound_sid; +u32 compound_tid;const struct cred *saved_cred;
fs/ksmbd/smb2pdu.c +13 -1
@@ -97,7 +97,6 @@ int smb2_get_ksmbd_tcon(struct ksmbd_work *work)unsigned int cmd =le16_to_cpu (req_hdr->Command);int tree_id; - work->tcon =NULL ;if (cmd ==SMB2_TREE_CONNECT_HE || cmd ==SMB2_CANCEL_HE || cmd ==SMB2_LOGOFF_HE ) {
@@ -110,13 +109,26 @@ int smb2_get_ksmbd_tcon(struct ksmbd_work *work)return -ENOENT ; } +if (req_hdr->Flags &SMB2_FLAGS_RELATED_OPERATIONS ) { +if (!work->tcon) { +pr_err ("Missing tcon\n" ); +return -EINVAL ; + } +return 1 ; + } + + work->tcon =NULL ; + work->compound_tid =0 ; + tree_id =le32_to_cpu (req_hdr->Id.SyncId.TreeId); + work->tcon =ksmbd_tree_conn_lookup (work->sess, tree_id);if (!work->tcon) {pr_err ("Invalid tid %d\n" , tree_id);return -EINVAL ; } + work->compound_tid = tree_id;return 1 ; }
Signature · 1 line
2.31.1
PATCH v7 8/9 ksmbd: make smb2_check_user_session() callable for compound PDUs
In reply to Ralph Boehme--- fs/ksmbd/smb2pdu.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
fs/ksmbd/smb2pdu.c +15 -3
@@ -416,7 +416,6 @@ static void init_chained_smb2_rsp(struct ksmbd_work *work) work->compound_pfid =le64_to_cpu (((struct smb2_create_rsp *)rsp)-> PersistentFileId); - work->compound_sid =le64_to_cpu (rsp->SessionId); } len =get_rfc1002_len (work->response_buf) - work->next_smb2_rsp_hdr_off;
@@ -596,7 +595,6 @@ int smb2_check_user_session(struct ksmbd_work *work)unsigned int cmd = conn->ops->get_cmd_val (work);unsigned long long sess_id; - work->sess =NULL ;/* * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not * require a session id, so no need to validate user session's for
@@ -609,11 +607,25 @@ int smb2_check_user_session(struct ksmbd_work *work)if (!ksmbd_conn_good (work))return -EINVAL ; +if (req_hdr->Flags &SMB2_FLAGS_RELATED_OPERATIONS ) { +if (work->sess) { +pr_err ("Missing session\n" ); +return -EINVAL ; + } +return 1 ; + } + + work->sess =NULL ; + work->compound_sid =0 ; + sess_id =le64_to_cpu (req_hdr->SessionId); +/* Check for validity of user session */ work->sess =ksmbd_session_lookup_all (conn, sess_id); -if (work->sess) +if (work->sess) { + work->compound_sid = sess_id;return 1 ; + }ksmbd_debug (SMB ,"Invalid user session, Uid %llu\n" , sess_id);return -EINVAL ; }
Signature · 1 line
2.31.1
PATCH v7 9/9 ksmdb: move session and tcon validation to __process_request()
In reply to Ralph BoehmeFor compound non-related operations session id and tree id must be taken from earch PDU.
--- fs/ksmbd/server.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-)
fs/ksmbd/server.c +29 -17
@@ -101,6 +101,32 @@ static inline int check_conn_state(struct ksmbd_work *work)return 0 ; } +static int check_session_and_tcon (struct ksmbd_work *work) +{ +int rc; + +if (work->conn->ops->check_user_session ==NULL ) +return 0 ; + + rc = work->conn->ops->check_user_session (work); +if (rc <0 ) { + work->conn->ops->set_rsp_status (work, +STATUS_USER_SESSION_DELETED ); +return 1 ; + } +if (rc ==0 ) +return 0 ; + + rc = work->conn->ops->get_ksmbd_tcon (work); +if (rc <0 ) { + work->conn->ops->set_rsp_status (work, +STATUS_NETWORK_NAME_DELETED ); +return 1 ; + } + +return 0 ; +} +#define SERVER_HANDLER_CONTINUE 0#define SERVER_HANDLER_ABORT 1
@@ -117,6 +143,9 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,if (ksmbd_verify_smb_message (work))return SERVER_HANDLER_ABORT ; +if (check_session_and_tcon (work)) +return SERVER_HANDLER_ABORT ; + command = conn->ops->get_cmd_val (work); *cmd = command;
@@ -184,23 +213,6 @@ static void __handle_ksmbd_work(struct ksmbd_work *work, goto send; } -if (conn->ops->check_user_session) { - rc = conn->ops->check_user_session (work); -if (rc <0 ) { - command = conn->ops->get_cmd_val (work); - conn->ops->set_rsp_status (work, -STATUS_USER_SESSION_DELETED ); - goto send; - }else if (rc >0 ) { - rc = conn->ops->get_ksmbd_tcon (work); -if (rc <0 ) { - conn->ops->set_rsp_status (work, -STATUS_NETWORK_NAME_DELETED ); - goto send; - } - } - } - do { rc = __process_request(work, conn, &command); if (rc == SERVER_HANDLER_ABORT)
Signature · 1 line
2.31.1