diff --git a/lib/auth/fuzz_test.go b/lib/auth/fuzz_test.go index 3c3fd8d758db3..9085f529333c2 100644 --- a/lib/auth/fuzz_test.go +++ b/lib/auth/fuzz_test.go @@ -17,12 +17,78 @@ limitations under the License. package auth import ( + "encoding/base64" "testing" "github.com/stretchr/testify/require" ) +func unverifiedBase64Bytes(str string) []byte { + bytes, _ := base64.StdEncoding.DecodeString(str) + return bytes +} + func FuzzParseAndVerifyIID(f *testing.F) { + f.Add(unverifiedBase64Bytes("WDcwMA==")) + f.Add(unverifiedBase64Bytes("N0IwQUMwMDAwMDAwMDAwMDAwMDA3Q0FBQTAwMDAwMEEwQVgwMDAwMDAwMEMwMDAwQTIyQTBBMDAw" + + "MDAwMDAwMDAwMDAwMDAwMFlJMA==")) + // Inputs from unit tests + f.Add(unverifiedBase64Bytes("YmFkIGRvY3VtZW50")) + f.Add(unverifiedBase64Bytes("TUlBR0NTcUdTSWIzRFFFSEFxQ0FNSUFDQVFFeER6QU5CZ2xnaGtnQlpRTUVBZ0VGQURDQUJna3Fo" + + "a2lHOXcwQkJ3R2dnQ1NBQklJQgoyM3NLSUNBaVlXTmpiM1Z1ZEVsa0lpQTZJQ0l5TnpnMU56WXlN" + + "akEwTlRNaUxBb2dJQ0poY21Ob2FYUmxZM1IxY21VaUlEb2dJbmc0Ck5sODJOQ0lzQ2lBZ0ltRjJZ" + + "V2xzWVdKcGJHbDBlVnB2Ym1VaUlEb2dJblZ6TFhkbGMzUXRNbUVpTEFvZ0lDSmlhV3hzYVc1blVI" + + "SnYKWkhWamRITWlJRG9nYm5Wc2JDd0tJQ0FpWkdWMmNHRjVVSEp2WkhWamRFTnZaR1Z6SWlBNklH" + + "NTFiR3dzQ2lBZ0ltMWhjbXRsZEhCcwpZV05sVUhKdlpIVmpkRU52WkdWeklpQTZJRzUxYkd3c0Np" + + "QWdJbWx0WVdkbFNXUWlJRG9nSW1GdGFTMHdabUU1WlRGbU5qUXhOREpqClpHVXhOeUlzQ2lBZ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kp3SUpmaSsyaElWanlLQU90akFOQmdrcWhraUc5dzBCQVFzRkFB" + + "U0NBUUFCS21naEFUZzgKVlhrZGlJR2NUSVBmS3JjMnYvekVJZExVQWkrRXc1bHJHVVZqbk5xclA5" + + "aXJHSzRkOXNWdGN1LzhVS3A5UkRvZUpPUTZJL3BSY3d2VApQSlZIbGhHbkx5eWJyNVpWcWt4aUMw" + + "OUdBU05uUGUxMmR6Q0trS0QycnZXNm1HUjkxY3hwTTk0WHFpNVVBL1pScWlYd3BIbzNMRUNOCkd1" + + "MzhIcGR2NnNCZ0QvYXYyT2hkK3ZFSDJ6dllWa3A3WmZuRnVETFdSU0JRWmdtS3dWS1ZkT2pyTW1Q" + + "MzJ2YjN2emhNQnVPaitqYlEKUndFbVlJa1JhRUdOYnJaZ2F0ak1KWW1UV3VMRzI2endzM2F2T0s2" + + "a0w2dTM4RFYzd0pQVkIvRzBJcmE1TXZDL29qR3lhK0RyVm5nVwpWVVArM2pnZW5QcmQ3T3lDV1BT" + + "d09vT0JNaFNsQUFBQUFBQUE=")) + f.Fuzz(func(t *testing.T, iidBytes []byte) { require.NotPanics(t, func() { parseAndVerifyIID(iidBytes) diff --git a/lib/auth/join_ec2.go b/lib/auth/join_ec2.go index 1e4b1d0609cb3..1470972d3e329 100644 --- a/lib/auth/join_ec2.go +++ b/lib/auth/join_ec2.go @@ -123,7 +123,15 @@ func checkInstanceRunning(ctx context.Context, instanceID, region, IAMRole strin // parseAndVerifyIID parses the given Instance Identity Document and checks that // the signature is valid. -func parseAndVerifyIID(iidBytes []byte) (*imds.InstanceIdentityDocument, error) { +func parseAndVerifyIID(iidBytes []byte) (iid *imds.InstanceIdentityDocument, err error) { + // handle potential panic from pkcs7 index out of range panic + defer func() { + if r := recover(); r != nil { + iid = nil + err = fmt.Errorf("unable to parse PKCS7") + } + }() + sigPEM := fmt.Sprintf("-----BEGIN PKCS7-----\n%s\n-----END PKCS7-----", string(iidBytes)) sigBER, _ := pem.Decode([]byte(sigPEM)) if sigBER == nil { @@ -135,8 +143,8 @@ func parseAndVerifyIID(iidBytes []byte) (*imds.InstanceIdentityDocument, error) return nil, trace.Wrap(err) } - var iid imds.InstanceIdentityDocument - if err := json.Unmarshal(p7.Content, &iid); err != nil { + iid = &imds.InstanceIdentityDocument{} + if err := json.Unmarshal(p7.Content, iid); err != nil { return nil, trace.Wrap(err) } @@ -154,7 +162,7 @@ func parseAndVerifyIID(iidBytes []byte) (*imds.InstanceIdentityDocument, error) return nil, trace.AccessDenied("invalid signature") } - return &iid, nil + return iid, nil } func checkPendingTime(iid *imds.InstanceIdentityDocument, provisionToken types.ProvisionToken, clock clockwork.Clock) error {