Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverity passkey fixes #7087

Closed

Conversation

justin-stephenson
Copy link
Contributor

@justin-stephenson justin-stephenson commented Dec 11, 2023

Fixes for the below. I tested the fixes submitting a scan.coverity.com build with my fork and it fixes these 2 defects.


2 new defect(s) introduced to SSSD/sssd found with Coverity Scan.


New defect(s) Reported-by: Coverity Scan
Showing 2 of 2 defect(s)


** CID 470375:  Memory - corruptions  (OVERRUN)
/home/runner/work/sssd/sssd/src/krb5_plugin/passkey/passkey_utils.c: 629 in sss_passkey_concat_credentials()


________________________________________________________________________________________________________
*** CID 470375:  Memory - corruptions  (OVERRUN)
/home/runner/work/sssd/sssd/src/krb5_plugin/passkey/passkey_utils.c: 629 in sss_passkey_concat_credentials()
623                 free(result_creds);
624                 goto done;
625             }
626     
627             result_creds = tmp_creds;
628     
>>>     CID 470375:  Memory - corruptions  (OVERRUN)
>>>     Overrunning dynamic array "result_creds" by passing it to a function that accesses it at byte "creds_len".
629             strncat(result_creds, creds[i], creds_len);
630         }
631     
632         *_creds_str = result_creds;
633     
634         ret = 0;
635     done:
636         return ret;

** CID 470374:  Resource leaks  (RESOURCE_LEAK)
/home/runner/work/sssd/sssd/src/krb5_plugin/passkey/passkey_clpreauth.c: 368 in sss_passkeycl_process()


________________________________________________________________________________________________________
*** CID 470374:  Resource leaks  (RESOURCE_LEAK)
/home/runner/work/sssd/sssd/src/krb5_plugin/passkey/passkey_clpreauth.c: 368 in sss_passkeycl_process()
362     done:
363         sss_passkey_message_free(reply_message);
364         sss_passkey_message_free(input_message);
365         if (reply != NULL) {
366             free(reply);
367         }
>>>     CID 470374:  Resource leaks  (RESOURCE_LEAK)
>>>     Variable "prompt_reply" going out of scope leaks the storage it points to.
368         return ret;
369     }
370     
371     krb5_error_code
372     clpreauth_passkey_initvt(krb5_context context,
373                              int maj_ver,

Copy link
Contributor

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take my inline comments lightly. Generally speaking, I think there is something wrong and that is why things don't add up in my mind and why I provided several contradictory inline comments.

As a general comment, I think you should consider treating all cases (creds[0] and creds[i]) the same just by differentiating them when calling snprintf(). Also, adding several unit tests for this function will help to detect memory related problems.

for (int i = 1; creds[i] != NULL; i++) {
strcat(result_creds, ",");
creds_len += strlen(creds[i]) + 1;
rc = snprintf(result_creds, total_sz, "%s", creds[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the second argument should be creds_buf_len and not total_sz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Fix for:

  CID 336599:  Memory - corruptions  (OVERRUN)
  Overrunning dynamic array "result_creds" by passing it to a
  function that accesses it at byte "creds_len".
Fix for:

  CID 470374:  Resource leaks  (RESOURCE_LEAK)
  Variable "prompt_reply" going out of scope leaks the storage
  it points to.
==367086== Conditional jump or move depends on uninitialised value(s)
==367086==    at 0x12BF1A31: string_get (load.c:894)
==367086==    by 0x12BF291D: stream_get.part.0 (load.c:158)
==367086==    by 0x12BF3182: UnknownInlinedFun (load.c:154)
==367086==    by 0x12BF3182: UnknownInlinedFun (load.c:227)
==367086==    by 0x12BF3182: lex_scan.isra.0 (load.c:573)
==367086==    by 0x12BF7F6A: parse_json (load.c:868)
==367086==    by 0x12BF80C8: json_loads (load.c:920)
==367086==    by 0x12BDDFD9: sss_passkey_message_from_reply_json (passkey_utils.c:544)
==367086==    by 0x12BDCA76: sss_passkeycl_process (passkey_clpreauth.c:321)
==367086==    by 0x4906215: UnknownInlinedFun (preauth2.c:352)
==367086==    by 0x4906215: UnknownInlinedFun (preauth2.c:679)
==367086==    by 0x4906215: k5_preauth (preauth2.c:1018)
==367086==    by 0x48F9489: UnknownInlinedFun (get_in_tkt.c:1351)
==367086==    by 0x48F9489: UnknownInlinedFun (get_in_tkt.c:1912)
==367086==    by 0x48F9489: krb5_init_creds_step (get_in_tkt.c:1868)
==367086==    by 0x48FA43A: k5_init_creds_get (get_in_tkt.c:564)
==367086==    by 0x48FB3EB: k5_get_init_creds (get_in_tkt.c:1978)
==367086==    by 0x48FB817: krb5_get_init_creds_password (gic_pwd.c:210)
@justin-stephenson
Copy link
Contributor Author

Take my inline comments lightly. Generally speaking, I think there is something wrong and that is why things don't add up in my mind and why I provided several contradictory inline comments.

As a general comment, I think you should consider treating all cases (creds[0] and creds[i]) the same just by differentiating them when calling snprintf(). Also, adding several unit tests for this function will help to detect memory related problems.

Updated, please check the latest version.

Copy link
Contributor

@ikerexxe ikerexxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the patches.

@alexey-tikhonov
Copy link
Member

Thank you, ACK.

@alexey-tikhonov
Copy link
Member

Pushed PR: #7087

  • master
    • 22d3569 - Passkey: Fix valgrind error and missing free
    • a134074 - Passkey: Fix coverity RESOURCE_LEAK
    • 1d33bde - Passkey: Fix coverity memory overrun error
  • sssd-2-9
    • 51f9031 - Passkey: Fix valgrind error and missing free
    • f5e3bb3 - Passkey: Fix coverity RESOURCE_LEAK
    • 02c1832 - Passkey: Fix coverity memory overrun error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
passkey Issues and PRs related to 'passkey' feature Pushed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants