Skip to content

Commit 3b9aace

Browse files
committed
improve validation of risc0 verifier info
Signed-off-by: Jun Kimura <[email protected]>
1 parent 12ba197 commit 3b9aace

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

contracts/ILCPClientErrors.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ interface ILCPClientErrors {
6868
error LCPClientZKDCAPInvalidNextTcbEvaluationDataNumberInfo();
6969
error LCPClientZKDCAPInvalidVerifierInfos();
7070
error LCPClientZKDCAPInvalidVerifierInfoLength();
71-
error LCPClientZKDCAPInvalidVerifierInfoZKVMType();
71+
error LCPClientZKDCAPInvalidVerifierInfoRisc0Header();
7272
error LCPClientZKDCAPUnsupportedZKVMType();
7373
error LCPClientZKDCAPRisc0ImageIdNotSet();
7474
error LCPClientZKDCAPUnexpectedIntelRootCAHash();

contracts/LCPClientZKDCAPBase.sol

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ abstract contract LCPClientZKDCAPBase is LCPClientBase {
264264
// The format is as follows:
265265
// - First byte (0): zkVM type identifier.
266266
// - Remaining bytes (1–N): zkVM-specific data.
267+
// - N must be greater than or equal to 32.
267268
//
268269
// Currently, only RISC Zero zkVM (type=1) is supported, with the following format:
269270
//
@@ -273,12 +274,16 @@ abstract contract LCPClientZKDCAPBase is LCPClientBase {
273274
// | 1–31 | Reserved (set as zero) |
274275
// | 32–63 | Image ID |
275276
uint256 vlen = verifierInfo.length;
276-
if (vlen == 0) {
277+
if (vlen < 32) {
277278
revert LCPClientZKDCAPInvalidVerifierInfoLength();
278279
}
279280
// Currently, the client only supports RISC Zero zkVM
280-
if (uint8(verifierInfo[0]) != ZKVM_TYPE_RISC_ZERO) {
281-
revert LCPClientZKDCAPInvalidVerifierInfoZKVMType();
281+
bytes32 header;
282+
assembly {
283+
header := mload(add(verifierInfo, 32))
284+
}
285+
if (header != bytes32(bytes1(ZKVM_TYPE_RISC_ZERO))) {
286+
revert LCPClientZKDCAPInvalidVerifierInfoRisc0Header();
282287
}
283288
// risc0 verifier info should be 64 bytes
284289
if (vlen != 64) {

test/LCPClientZKDCAPTest.t.sol

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,43 @@ contract LCPClientZKDCAPTest is BasicTest {
376376
lc.zkDCAPRegisterEnclaveKey(clientId, msgData);
377377
}
378378

379+
function testRegisterEnclaveKeyInvalidRisc0Header() public {
380+
string memory clientId = "lcp-zkdcap";
381+
TestLCPClientZKDCAPExtended lc = new TestLCPClientZKDCAPExtended(
382+
address(this), false, ZKDCAPTestHelper.dummyIntelRootCACert(), address(new NopRiscZeroVerifier())
383+
);
384+
{
385+
IbcLightclientsLcpV1ClientState.Data memory clientState = defaultClientState();
386+
clientState.zkdcap_verifier_infos[0][0] = 0x00;
387+
bytes memory clientStateBytes = LCPProtoMarshaler.marshal(clientState);
388+
bytes memory consensusStateBytes = LCPProtoMarshaler.marshal(defaultConsensusState());
389+
vm.expectRevert(
390+
abi.encodeWithSelector(ILCPClientErrors.LCPClientZKDCAPInvalidVerifierInfoRisc0Header.selector)
391+
);
392+
lc.initializeClient(clientId, clientStateBytes, consensusStateBytes);
393+
}
394+
{
395+
IbcLightclientsLcpV1ClientState.Data memory clientState = defaultClientState();
396+
clientState.zkdcap_verifier_infos[0][0] = 0x02;
397+
bytes memory clientStateBytes = LCPProtoMarshaler.marshal(clientState);
398+
bytes memory consensusStateBytes = LCPProtoMarshaler.marshal(defaultConsensusState());
399+
vm.expectRevert(
400+
abi.encodeWithSelector(ILCPClientErrors.LCPClientZKDCAPInvalidVerifierInfoRisc0Header.selector)
401+
);
402+
lc.initializeClient(clientId, clientStateBytes, consensusStateBytes);
403+
}
404+
{
405+
IbcLightclientsLcpV1ClientState.Data memory clientState = defaultClientState();
406+
clientState.zkdcap_verifier_infos[0][1] = 0x01;
407+
bytes memory clientStateBytes = LCPProtoMarshaler.marshal(clientState);
408+
bytes memory consensusStateBytes = LCPProtoMarshaler.marshal(defaultConsensusState());
409+
vm.expectRevert(
410+
abi.encodeWithSelector(ILCPClientErrors.LCPClientZKDCAPInvalidVerifierInfoRisc0Header.selector)
411+
);
412+
lc.initializeClient(clientId, clientStateBytes, consensusStateBytes);
413+
}
414+
}
415+
379416
function testRegisterEnclaveKeyEnclaveDebugMismatch() public {
380417
string memory clientId = "lcp-zkdcap";
381418
// developmentMode=false but output.enclaveDebugEnabled is set to true

0 commit comments

Comments
 (0)