Skip to content

Conversation

@xhanulik
Copy link
Member

This PR adds a fuzzer for testing functionality from the pkcs15-init tool.

It splits the input into two parts - the first is parsed as a profile file (via fuzz_pkcs15init_bind that works similarly to sc_pkcs15init_bind, but parsing the profile as string not as file). The second is set of APDUs for reader operations (as in sc_pkcs15_reader.c). Then there are do_* functions working as some of the operations in pkcs15-init (sc_pkcs15init_* for storing pin, data, generating keys and erasing card).

Corpus file contains profile part put together from pkcs15.profile and myeid.profile, zero byte for separation and APDUs from MyEID card (generation described in README file by @Jakuje) trimmed to fit operations.

Also, there are some fixes for locally found bugs, mainly memory leaks.

Checklist
  • New files have a LGPL 2.1 license statement
  • PKCS#11 module is tested
  • Windows minidriver is tested
  • macOS tokend is tested

By creating new object in sc_pkcs15init_init_prkdf(), ecparams
will be copied into new allocated structure and freed with
object in sc_pkcs15_free_object().

Name of the curve in pkcs15-init.c needs to be freed separately
after sc_pkcs15init_generate_key() ends.
object = NULL;
r = SC_SUCCESS;

LOG_FUNC_RETURN(ctx, r);
Copy link
Member

Choose a reason for hiding this comment

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

The above two lines can be simplified as LOG_FUNC_RETURN(ctx, SC_SUCCESS);.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok

Copy link
Member

Choose a reason for hiding this comment

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

The similar pattern is used in other functions you changed in the pkcs15-lib.c. Can you check also these? Either by using SC_SUCCESS explicitly or falling through to the only return, what would make more sense.

@xhanulik xhanulik force-pushed the pkcs15init_fuzz branch 2 times, most recently from f468c27 to d2d8fa9 Compare February 23, 2022 15:20
sc_unlock(g_p15card->card);
if (algorithm == SC_ALGORITHM_EC)
/* allocated in parse_alg_spec() */
free(keygen_args.prkey_args.key.u.ec.params.named_curve);
Copy link
Member

Choose a reason for hiding this comment

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

It looks strange to have this here.

In parse_alg_spec(), would it cause any problems if we take (part of) the user supplied spec for u.ec.params.named_curve avoiding the strdup() in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

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

I replaced the free with sc_pkcs15_free_prkey(). Removing strdup() would probably also mean to remove const from spec param and I!m not sure about all places where named_curve is used then.

@frankmorgner
Copy link
Member

The CI fuzzer also shows some other problem that's still present.

@Jakuje
Copy link
Member

Jakuje commented Mar 8, 2022

@frankmorgner do you have some more comments or can we merge this? It looks like the new fuzzer is still a bit unstable, but unfortunately the CIFuzz did not provide reproducer so we will have to handle it as it on the go.

@frankmorgner
Copy link
Member

It's fine, we can merge

@Jakuje Jakuje merged commit 3d1acdf into OpenSC:master Mar 8, 2022
@xhanulik xhanulik deleted the pkcs15init_fuzz branch July 19, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants