Skip to content
Smart contracts Audit finding Intermediate

Taiko SGX Attestation - Improper validation in certchain decoding

The certificate chain decoding logic in the SGX attestation process incorrectly validates the notBefore tag, allowing any value to be accepted. A malicious attestor could supply a certificate with an invalid notBefore tag, potentially bypassing intended time format checks.

zzebra83 Follow

Originally disclosed on Code4rena

Submitted by zzebra83

https://github.com/code-423n4/2024-03-taiko/blob/0d081a40e0b9637eddf8e760fabbecc250f23599/packages/protocol/contracts/automata-attestation/lib/PEMCertChainLib.sol#L135

https://github.com/code-423n4/2024-03-taiko/blob/0d081a40e0b9637eddf8e760fabbecc250f23599/packages/protocol/contracts/verifiers/SgxVerifier.sol#L115-L136

As part of of its ZK proof setup, Taiko leverages SGX provers. it also enables remote SGX attestation and this is possible via leveraging code from Automata, which provides a modular attestation layer extending machine-level trust to Ethereum via the AutomataDcapV3Attestation repo, which is in scope of this audit.

Anyone with SGX hardware can register their instance to be an SGX prover in the Taiko Network via calling the registerInstance function in SgxVerifier.sol. This is why attestation is critical to prove the reliability and trustworthiness of the SGX prover.

The attestation process of SGX provers is a multi fold process, and starts with calling the verifyParsedQuote function in AutomataDcapV3Attestation.sol. One of the steps involves decoding the certchain provided by the SGX prover, as seen below:

        // Step 4: Parse Quote CertChain
    IPEMCertChainLib.ECSha256Certificate[] memory parsedQuoteCerts;
    TCBInfoStruct.TCBInfo memory fetchedTcbInfo;
    {
        // 536k gas
        parsedQuoteCerts = new IPEMCertChainLib.ECSha256Certificate[](3);
        for (uint256 i; i < 3; ++i) {
            bool isPckCert = i == 0; // additional parsing for PCKCert
            bool certDecodedSuccessfully;
            // todo! move decodeCert offchain
            (certDecodedSuccessfully, parsedQuoteCerts[i]) = pemCertLib.decodeCert(
                authDataV3.certification.decodedCertDataArray[i], isPckCert
            );
            if (!certDecodedSuccessfully) {
                return (false, retData);
            }
        }
    }

after this step is executed, a number of other steps are done including:

Step 5: basic PCK and TCB check Step 6: Verify TCB Level Step 7: Verify cert chain for PCK Step 8: Verify the local attestation sig and qe report sig

The decoding of the certchain happens through the EMCertChainLib lib, and this involves a number of steps, one of which is to validate the decoded notBefore and notAfter tags of the certificate:

        {
        uint256 notBeforePtr = der.firstChildOf(tbsPtr);
        uint256 notAfterPtr = der.nextSiblingOf(notBeforePtr);
        bytes1 notBeforeTag = der[notBeforePtr.ixs()];
        bytes1 notAfterTag = der[notAfterPtr.ixs()];
        if (
            (notBeforeTag != 0x17 && notBeforeTag == 0x18)
                || (notAfterTag != 0x17 && notAfterTag != 0x18)
        ) {
            return (false, cert);
        }
        cert.notBefore = X509DateUtils.toTimestamp(der.bytesAt(notBeforePtr));
        cert.notAfter = X509DateUtils.toTimestamp(der.bytesAt(notAfterPtr));
    }

These fields determine the time format, whether the notBeforePtr and notAfterPtr are in UTC or generalized time, and are used to ensure consistency in timestamps used to determine the validity period of the certificate.

However the validation can fail because the logic above is faulty, as it will allow the attestor to pass in any value for the notBefore tag, indeeed the condition of:

(notBeforeTag != 0x17 && notBeforeTag == 0x18)

will allow the attestor to pass in any beforetag because the condition will always be false.

Consider if we pass an invalid tag of 0x10:

  1. notBeforeTag != 0x17 is True.
  2. notBeforeTag == 0x18 is False.
  3. full condition is False.

I believe the original intention was to ensure the beforeTag is strictly 0x17 or 0x18, just as with the afterTag. Because of this oversight, a malicious attestor could pass in any notBefore Tag as part of their certificate.

This issue requires attention given the significance of the attestation process of SGX provers within Taiko’s ZK setup. The whole point of attestation is to prove the SGX provers are secure, untampered, and trustworthy, and improper validation related to certificate validity periods can have unforeseen consequences.

Recommended Mitigation Steps

Update the condition as below:

            if (
            (notBeforeTag != 0x17 && notBeforeTag != 0x18)
                || (notAfterTag != 0x17 && notAfterTag != 0x18)
        ) {
            return (false, cert);

smtmfft (Taiko) confirmed and commented:

I think this is a valid catch, already submitted a fix.


The original report as published on Code4rena, by zzebra83. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely a logical error in the condition: the check for notBeforeTag uses '&&' instead of '||', making the condition always false and thus accepting any tag.

Impact

A malicious attestor could pass an invalid notBefore tag, potentially undermining the certificate validity period checks and the trustworthiness of SGX provers.

What the attacker needed

SGX hardware; ability to submit attestation

The fix

The condition should be updated to check that notBeforeTag is neither 0x17 nor 0x18, using '!=' instead of '==' and '||' instead of '&&'.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a computer proves it is trustworthy using special hardware, it sends a certificate. The code that checks the certificate's start date has a mistake: it accepts any format for that date instead of only the two allowed formats. This means a bad actor could use a certificate with a wrong date format and still pass the check.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by zzebra83

View all

3 more disclosed reports, best first.

More from Taiko

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.