@@ -326,13 +326,9 @@ int smb2_set_rsp_credits(struct ksmbd_work *work)
326326 if (hdr -> Command == SMB2_NEGOTIATE )
327327 aux_max = 1 ;
328328 else
329- aux_max = conn -> vals -> max_credits - credit_charge ;
329+ aux_max = conn -> vals -> max_credits - conn -> total_credits ;
330330 credits_granted = min_t (unsigned short , credits_requested , aux_max );
331331
332- if (conn -> vals -> max_credits - conn -> total_credits < credits_granted )
333- credits_granted = conn -> vals -> max_credits -
334- conn -> total_credits ;
335-
336332 conn -> total_credits += credits_granted ;
337333 work -> credits_granted += credits_granted ;
338334
@@ -849,13 +845,14 @@ static void assemble_neg_contexts(struct ksmbd_conn *conn,
849845
850846static __le32 decode_preauth_ctxt (struct ksmbd_conn * conn ,
851847 struct smb2_preauth_neg_context * pneg_ctxt ,
852- int len_of_ctxts )
848+ int ctxt_len )
853849{
854850 /*
855851 * sizeof(smb2_preauth_neg_context) assumes SMB311_SALT_SIZE Salt,
856852 * which may not be present. Only check for used HashAlgorithms[1].
857853 */
858- if (len_of_ctxts < MIN_PREAUTH_CTXT_DATA_LEN )
854+ if (ctxt_len <
855+ sizeof (struct smb2_neg_context ) + MIN_PREAUTH_CTXT_DATA_LEN )
859856 return STATUS_INVALID_PARAMETER ;
860857
861858 if (pneg_ctxt -> HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512 )
@@ -867,15 +864,23 @@ static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
867864
868865static void decode_encrypt_ctxt (struct ksmbd_conn * conn ,
869866 struct smb2_encryption_neg_context * pneg_ctxt ,
870- int len_of_ctxts )
867+ int ctxt_len )
871868{
872- int cph_cnt = le16_to_cpu (pneg_ctxt -> CipherCount );
873- int i , cphs_size = cph_cnt * sizeof (__le16 );
869+ int cph_cnt ;
870+ int i , cphs_size ;
871+
872+ if (sizeof (struct smb2_encryption_neg_context ) > ctxt_len ) {
873+ pr_err ("Invalid SMB2_ENCRYPTION_CAPABILITIES context size\n" );
874+ return ;
875+ }
874876
875877 conn -> cipher_type = 0 ;
876878
879+ cph_cnt = le16_to_cpu (pneg_ctxt -> CipherCount );
880+ cphs_size = cph_cnt * sizeof (__le16 );
881+
877882 if (sizeof (struct smb2_encryption_neg_context ) + cphs_size >
878- len_of_ctxts ) {
883+ ctxt_len ) {
879884 pr_err ("Invalid cipher count(%d)\n" , cph_cnt );
880885 return ;
881886 }
@@ -923,15 +928,22 @@ static void decode_compress_ctxt(struct ksmbd_conn *conn,
923928
924929static void decode_sign_cap_ctxt (struct ksmbd_conn * conn ,
925930 struct smb2_signing_capabilities * pneg_ctxt ,
926- int len_of_ctxts )
931+ int ctxt_len )
927932{
928- int sign_algo_cnt = le16_to_cpu (pneg_ctxt -> SigningAlgorithmCount );
929- int i , sign_alos_size = sign_algo_cnt * sizeof (__le16 );
933+ int sign_algo_cnt ;
934+ int i , sign_alos_size ;
935+
936+ if (sizeof (struct smb2_signing_capabilities ) > ctxt_len ) {
937+ pr_err ("Invalid SMB2_SIGNING_CAPABILITIES context length\n" );
938+ return ;
939+ }
930940
931941 conn -> signing_negotiated = false;
942+ sign_algo_cnt = le16_to_cpu (pneg_ctxt -> SigningAlgorithmCount );
943+ sign_alos_size = sign_algo_cnt * sizeof (__le16 );
932944
933945 if (sizeof (struct smb2_signing_capabilities ) + sign_alos_size >
934- len_of_ctxts ) {
946+ ctxt_len ) {
935947 pr_err ("Invalid signing algorithm count(%d)\n" , sign_algo_cnt );
936948 return ;
937949 }
@@ -969,18 +981,16 @@ static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
969981 len_of_ctxts = len_of_smb - offset ;
970982
971983 while (i ++ < neg_ctxt_cnt ) {
972- int clen ;
973-
974- /* check that offset is not beyond end of SMB */
975- if (len_of_ctxts == 0 )
976- break ;
984+ int clen , ctxt_len ;
977985
978986 if (len_of_ctxts < sizeof (struct smb2_neg_context ))
979987 break ;
980988
981989 pctx = (struct smb2_neg_context * )((char * )pctx + offset );
982990 clen = le16_to_cpu (pctx -> DataLength );
983- if (clen + sizeof (struct smb2_neg_context ) > len_of_ctxts )
991+ ctxt_len = clen + sizeof (struct smb2_neg_context );
992+
993+ if (ctxt_len > len_of_ctxts )
984994 break ;
985995
986996 if (pctx -> ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES ) {
@@ -991,7 +1001,7 @@ static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
9911001
9921002 status = decode_preauth_ctxt (conn ,
9931003 (struct smb2_preauth_neg_context * )pctx ,
994- len_of_ctxts );
1004+ ctxt_len );
9951005 if (status != STATUS_SUCCESS )
9961006 break ;
9971007 } else if (pctx -> ContextType == SMB2_ENCRYPTION_CAPABILITIES ) {
@@ -1002,7 +1012,7 @@ static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
10021012
10031013 decode_encrypt_ctxt (conn ,
10041014 (struct smb2_encryption_neg_context * )pctx ,
1005- len_of_ctxts );
1015+ ctxt_len );
10061016 } else if (pctx -> ContextType == SMB2_COMPRESSION_CAPABILITIES ) {
10071017 ksmbd_debug (SMB ,
10081018 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n" );
@@ -1021,9 +1031,10 @@ static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
10211031 } else if (pctx -> ContextType == SMB2_SIGNING_CAPABILITIES ) {
10221032 ksmbd_debug (SMB ,
10231033 "deassemble SMB2_SIGNING_CAPABILITIES context\n" );
1034+
10241035 decode_sign_cap_ctxt (conn ,
10251036 (struct smb2_signing_capabilities * )pctx ,
1026- len_of_ctxts );
1037+ ctxt_len );
10271038 }
10281039
10291040 /* offsets must be 8 byte aligned */
@@ -1057,16 +1068,16 @@ int smb2_handle_negotiate(struct ksmbd_work *work)
10571068 return rc ;
10581069 }
10591070
1060- if (req -> DialectCount == 0 ) {
1061- pr_err ("malformed packet\n" );
1071+ smb2_buf_len = get_rfc1002_len (work -> request_buf );
1072+ smb2_neg_size = offsetof(struct smb2_negotiate_req , Dialects );
1073+ if (smb2_neg_size > smb2_buf_len ) {
10621074 rsp -> hdr .Status = STATUS_INVALID_PARAMETER ;
10631075 rc = - EINVAL ;
10641076 goto err_out ;
10651077 }
10661078
1067- smb2_buf_len = get_rfc1002_len (work -> request_buf );
1068- smb2_neg_size = offsetof(struct smb2_negotiate_req , Dialects );
1069- if (smb2_neg_size > smb2_buf_len ) {
1079+ if (req -> DialectCount == 0 ) {
1080+ pr_err ("malformed packet\n" );
10701081 rsp -> hdr .Status = STATUS_INVALID_PARAMETER ;
10711082 rc = - EINVAL ;
10721083 goto err_out ;
@@ -4358,21 +4369,6 @@ static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
43584369 return 0 ;
43594370}
43604371
4361- static unsigned long long get_allocation_size (struct inode * inode ,
4362- struct kstat * stat )
4363- {
4364- unsigned long long alloc_size = 0 ;
4365-
4366- if (!S_ISDIR (stat -> mode )) {
4367- if ((inode -> i_blocks << 9 ) <= stat -> size )
4368- alloc_size = stat -> size ;
4369- else
4370- alloc_size = inode -> i_blocks << 9 ;
4371- }
4372-
4373- return alloc_size ;
4374- }
4375-
43764372static void get_file_standard_info (struct smb2_query_info_rsp * rsp ,
43774373 struct ksmbd_file * fp , void * rsp_org )
43784374{
@@ -4387,7 +4383,7 @@ static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
43874383 sinfo = (struct smb2_file_standard_info * )rsp -> Buffer ;
43884384 delete_pending = ksmbd_inode_pending_delete (fp );
43894385
4390- sinfo -> AllocationSize = cpu_to_le64 (get_allocation_size ( inode , & stat ) );
4386+ sinfo -> AllocationSize = cpu_to_le64 (inode -> i_blocks << 9 );
43914387 sinfo -> EndOfFile = S_ISDIR (stat .mode ) ? 0 : cpu_to_le64 (stat .size );
43924388 sinfo -> NumberOfLinks = cpu_to_le32 (get_nlink (& stat ) - delete_pending );
43934389 sinfo -> DeletePending = delete_pending ;
@@ -4452,7 +4448,7 @@ static int get_file_all_info(struct ksmbd_work *work,
44524448 file_info -> Attributes = fp -> f_ci -> m_fattr ;
44534449 file_info -> Pad1 = 0 ;
44544450 file_info -> AllocationSize =
4455- cpu_to_le64 (get_allocation_size ( inode , & stat ) );
4451+ cpu_to_le64 (inode -> i_blocks << 9 );
44564452 file_info -> EndOfFile = S_ISDIR (stat .mode ) ? 0 : cpu_to_le64 (stat .size );
44574453 file_info -> NumberOfLinks =
44584454 cpu_to_le32 (get_nlink (& stat ) - delete_pending );
@@ -4641,7 +4637,7 @@ static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
46414637 file_info -> ChangeTime = cpu_to_le64 (time );
46424638 file_info -> Attributes = fp -> f_ci -> m_fattr ;
46434639 file_info -> AllocationSize =
4644- cpu_to_le64 (get_allocation_size ( inode , & stat ) );
4640+ cpu_to_le64 (inode -> i_blocks << 9 );
46454641 file_info -> EndOfFile = S_ISDIR (stat .mode ) ? 0 : cpu_to_le64 (stat .size );
46464642 file_info -> Reserved = cpu_to_le32 (0 );
46474643 rsp -> OutputBufferLength =
@@ -5506,7 +5502,7 @@ static int smb2_create_link(struct ksmbd_work *work,
55065502{
55075503 char * link_name = NULL , * target_name = NULL , * pathname = NULL ;
55085504 struct path path ;
5509- bool file_present = true ;
5505+ bool file_present = false ;
55105506 int rc ;
55115507
55125508 if (buf_len < (u64 )sizeof (struct smb2_file_link_info ) +
@@ -5539,8 +5535,8 @@ static int smb2_create_link(struct ksmbd_work *work,
55395535 if (rc ) {
55405536 if (rc != - ENOENT )
55415537 goto out ;
5542- file_present = false;
5543- }
5538+ } else
5539+ file_present = true;
55445540
55455541 if (file_info -> ReplaceIfExists ) {
55465542 if (file_present ) {
0 commit comments