Skip to content

Conversation

@DemiMarie
Copy link
Contributor

Description

ASN.1 requires tags to be greater than zero, and multi-byte tags are not supported by this library. Check that tags taken from input data are valid. Tags provided by the caller are assumed valid.

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

  • changelog provided, or not required
  • backport done, or not required
  • tests provided, or not required

Notes for the submitter

Please refer to the contributing guidelines, especially the
checklist for PR contributors.

ASN.1 requires tags to be greater than zero, and multi-byte tags are
not supported by this library.  Check that tags taken from input data
are valid.  Tags provided by the caller are assumed valid.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
@bensze01 bensze01 added bug needs-work needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. component-x509 needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) priority-medium Medium priority - this can be reviewed as time permits labels Jun 7, 2023
@bensze01
Copy link
Contributor

bensze01 commented Jun 7, 2023

Please rebase on top of development to fix the CI issues (See #7699), and provide a non-regression test.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

This is a very minor robustness improvement and I'm not convinced it's worth the trouble.

If it is worth it, we need some non-regression tests.

It's not really a bug fix, only a robustness improvement so it doesn't need a changelog entry or a backport.

return 0;
}

static int mbedtls_asn1_bad_tag(int tag)
Copy link
Contributor

Choose a reason for hiding this comment

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

In this library, as you note, a tag is always a single byte.

Suggested change
static int mbedtls_asn1_bad_tag(int tag)
static int mbedtls_asn1_bad_tag(unsigned char tag)


static int mbedtls_asn1_bad_tag(int tag)
{
return tag < 1 || (tag & ~0xE0) >= 0x1F;
Copy link
Contributor

Choose a reason for hiding this comment

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

tag is always ≤255, so (tag & ~0xE0) >= 0x1F can't happen. tag is always ≥0 so tag < 1 is equivalent to tag == 0.

Suggested change
return tag < 1 || (tag & ~0xE0) >= 0x1F;
return tag == 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(tag & ~0xE0) == 0x1F can indeed happen (consider tag == 0xFF). Also tag < 1 should be (tag & 0xDF) == 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, my bad, I'd reasoned as if it was > 0x1F rather than >= 0x1F. It would indeed have been more readable with ==. And please be consistent with 0xDF vs 0xE0. In general, please avoid negation when not needed, especially if it leads to double negation. A is_good_tag function is likely to make the code more readable than is_bad_tag.

while (*p < end) {
unsigned char const tag = *(*p)++;

if (mbedtls_asn1_bad_tag(tag)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why we'd specifically reject 0 here, but accept all other tags. The caller will need to filter tags anyway.

I'm not sure why mbedtls_asn1_traverse_sequence_of is a public function anyway. (I approved it in ARMmbed/mbed-crypto#263, but now I don't know why. Maybe because it's unit-tested and at the time we couldn't unit-test private functions? But unit tests for mbedtls_asn1_get_sequence_of would have sufficed. Or maybe because it was needed for future X.509 work that we never finished?) Inside the library, it's only used by mbedtls_asn1_get_sequence_of which doesn't need all this flexibility, in particular tag will be constrained to one specific value. So I don't think we need to make this function more robust.

params->tag = **p;
(*p)++;

if (mbedtls_asn1_bad_tag(params->tag)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only useful if the caller doesn't subsequently check the tag, which seems unlikely. If the caller doesn't check the tag, then with this patch we'll reject a guaranteed-invalid tag but not a potentially valid tag that is not understood, so the benefit is very limited.

@gilles-peskine-arm gilles-peskine-arm added enhancement size-xs Estimated task size: extra small (a few hours at most) and removed bug needs-review Every commit must be reviewed by at least two team members, needs-backports Backports are missing or are pending review and approval. needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) labels Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component-x509 enhancement needs-ci Needs to pass CI tests needs-work priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants