-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Guard from undefined behaviour in case of an INT_MAX max_pathlen #3192
Guard from undefined behaviour in case of an INT_MAX max_pathlen #3192
Conversation
Attaching a dump of certificates with the changed
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good to me
library/x509_crt.c
Outdated
@@ -524,6 +524,11 @@ static int x509_get_basic_constraints( unsigned char **p, | |||
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + | |||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); | |||
|
|||
// Do not accept max_pathlen equal to INT_MAX due to size constraints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: Use C-style comments; add a period at the end of sentences
library/x509_crt.c
Outdated
@@ -524,6 +524,11 @@ static int x509_get_basic_constraints( unsigned char **p, | |||
return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + | |||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); | |||
|
|||
// Do not accept max_pathlen equal to INT_MAX due to size constraints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the size constraints? Isn't this to avoid undefined behaviour when we would do signed integer overflow on line 532 below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is, maybe calling this a size constraint isn't precise enough? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the comment said what your commit message said: "to avoid signed integer overflow, which is undefined behavior" that'd be good enough
Zdn977+Sn5anAFGHDWeKo8GYaYGnPBQqkX0Q2EKWR7yrwcKMogOevxELogB0jRj3 | ||
L+nBpz7mO2J6XQ85ip+tLWAGCEHo0omAIQorAoCSqtLiaz47HxOdNK0hnM7V5k8P | ||
05AVhxDa3WqZ9FmMaDc8j8XqmOgKYVMC4/WS0g== | ||
-----END CERTIFICATE----- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pathlen is 2147483646, OK
mGrpywSV7RpZC2PznGFdqQehwwnOscz0cVeMQqGcMRH3D5Bk2SjVexCaPu47QSyE | ||
fNm6cATiNHjw/2dg5Aue7e4K+R6le+xY3Qy85Fq/lKDeMmbrJRrNyJ9lblCeihUd | ||
qhkAEPelpaq5ZRM6cYJQoo0Ak64j4svjOZeF0g== | ||
-----END CERTIFICATE----- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pathlen is 2147483647, OK
@@ -1798,6 +1798,14 @@ X509 CRT ASN1 (TBS, inv extBasicConstraint, no pathlen length) | |||
depends_on:MBEDTLS_RSA_C:MBEDTLS_SHA256_C | |||
x509parse_crt:"3081b030819aa0030201028204deadbeef300d06092a864886f70d01010b0500300c310a30080600130454657374301c170c303930313031303030303030170c303931323331323335393539300c310a30080600130454657374302a300d06092A864886F70D010101050003190030160210ffffffffffffffffffffffffffffffff0202ffffa100a200a314301230100603551d130101010406300402010102300d06092a864886f70d01010b0500030200ff":"":MBEDTLS_ERR_X509_INVALID_EXTENSIONS + MBEDTLS_ERR_ASN1_OUT_OF_DATA | |||
|
|||
X509 CRT ASN1 (inv extBasicConstraint, pathlen is INT_MAX) | |||
depends_on:MBEDTLS_RSA_C:MBEDTLS_SHA256_C |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI says you didn't depend on MBEDTLS_SHA1_C, but should
3f43101
to
ce50d9d
Compare
When parsing a certificate with the basic constraints extension the max_pathlen that was read from it was incremented regardless of its value. However, if the max_pathlen is equal to INT_MAX (which is highly unlikely), an undefined behaviour would occur. This commit adds a check to ensure that such value is not accepted as valid. Relevant tests for INT_MAX and INT_MAX-1 are also introduced. Certificates added in this commit were generated using the test_suite_x509write, function test_x509_crt_check. Input data taken from the "Certificate write check Server1 SHA1" test case, so the generated files are like the "server1.crt", but with the "is_ca" field set to 1 and max_pathlen as described by the file name. Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
ce50d9d
to
1605074
Compare
Only MbedOS fails on the CI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Only Mbed OS CI is failing, which is a known issue unrelated to this PR and being worked on. Good to go. |
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
…low fix Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
…low fix Backport of Mbed-TLS#3192 Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
…low fix Backport of Mbed-TLS#3192 Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
When parsing a certificate with the basic constraints extension, the
max_pathlen
that was read from it was incremented regardless of its value. However, if themax_pathlen
is equal toINT_MAX
(which is highly unlikely), an undefined behaviour would occur. This commit adds a check to ensure that such value is not accepted as valid. Relevant tests forINT_MAX
andINT_MAX-1
are also introduced.Certificates added in this commit were generated using the
test_suite_x509write
, functiontest_x509_crt_check
. Input data taken from the "Certificate write check Server1 SHA1" test case, so the generated files are like the "server1.crt", but with the "is_ca" field set to 1 and max_pathlen as described by the file name.This PR addresses IOTSSL-2774.