{
 "lead": "A rogue MidnightAdapter (anyone can deploy one via MidnightAdapterFactory for an attacker-owned parentVault/asset) can, because IKRCompatibleEnterGate.admit trusts factory.isCreated, reach admit() on a shared gate. Can it admit any account into a gated market, keep an admission past its take, use inForceDeallocate so isRatified approves another offer/market/buyer, enter with zero units or via nested calls, share one gate across markets/adapters, or block a real adapter's in-kind redemption?",
 "commit": "bbeda7b27ae80abcf070e0158cb8a31e87bb2286",
 "hasCandidates": false,
 "candidates": [],
 "lowInfo": [
  {
   "title": "A rogue adapter from a registered factory can reach IKRCompatibleEnterGate.admit() and set the shared transient `admitted` to any account, but cannot weaponize it (admission is confined to the adapter's own hardcoded reduceOnly force-deallocate sale, and the rogue adapter can hold no gated credit to sell).",
   "class": "Informational",
   "verified": true,
   "refs": [
    "src/adapters/midnight/gates/IKRCompatibleEnterGate.sol:40-48",
    "src/adapters/midnight/MidnightAdapter.sol:421-486",
    "src/adapters/midnight/MidnightAdapter.sol:510-527",
    "lib/midnight/src/Midnight.sol:380-393"
   ],
   "detail": "IKRCompatibleEnterGate.admit only checks admitEnabled, admitterFactory[adapterFactory] and adapterFactory.isCreated(msg.sender). MidnightAdapterFactory.createMidnightAdapter lets anyone deploy an adapter for an arbitrary parentVault, and that adapter's immutable `factory` is the same MidnightAdapterFactory. So if the gate's roleSetter has registered MidnightAdapterFactory as an admitter, a rogue adapter built on an attacker-owned fake vault passes admit()'s checks. The PoC confirms admit() is reachable: the gate emits Admit(rogue, evil, factory) for a NON-whitelisted `evil`. However the admission cannot be turned into a credit increase for a non-whitelisted account. The only place the adapter calls admit() is the forceDeallocate branch of deallocate(), which immediately takes the adapter's OWN offer built with reduceOnly:true, maker:self, tick:0 (MidnightAdapter.sol:430-470). A reduceOnly sell by a maker that holds no credit sets sellerDebtIncrease>0, so Midnight reverts with MakerCreditOrDebtIncreased (Midnight.sol:380-383) BEFORE any credit reaches the taker; the revert rolls back the transient `admitted`. The rogue adapter also cannot first acquire credit in the gated market: buying requires canIncreaseCredit(buyer=adapter) and the adapter is neither whitelisted on the subgate nor admitted during its own buy. The admission is transient and is additionally cleared in onBuy before Midnight transfers the loan token (MidnightAdapter.sol:520-526), so it cannot persist past the take, cannot be reused by a nested call (the sole in-window Midnight call is the adapter's own reverting sale), and cannot bleed into another market or another adapter's in-kind redemption (each is a separate transaction with its own transient state; honest adapters remain isCreated==true immutably). The sub-questions resolve as: admit any account = reachable but inert (no credit moves); keep admission past take = no (transient, reverted/cleared); inForceDeallocate approving another offer = no (it is the rogue adapter's own transient and only bypasses its own ratifier for its own offer); zero units / nested = no credit moves; share gate across markets/adapters = admission is per-gate but only usable inside the admitting adapter's own reverting sale; block a real adapter's IKR = no. This matches the documented trust model: 'Registered adapter factories are trusted ... their adapters must behave correctly', and MidnightAdapterFactory's adapters behave identically regardless of parentVault. Residual note for the team: admit()'s guard proves the *deployer* lineage of msg.sender but not that msg.sender's parentVault is a genuine Vault V2, so the adapter's `balanceOf(taker)>0` 'shareholder' check is meaningless for a rogue adapter; defense-in-depth would be to also require that the loan asset / vault be the one the gate expects, though no exploit follows from the gap today."
  }
 ],
 "covered": {
  "testsRun": [
   "Step 1 build/suite: forge test --match-path test/integration/MidnightAdapterIntegrationTest.sol -> 8 passed, 0 failed (existing suite confirmed green at the commit).",
   "testRogueAdapterCannotHandCreditToNonWhitelisted -> PASS (rogue admit reverts MakerCreditOrDebtIncreased; evil credit 0 before and after; honest adapter holds 10000e18 credit so the gated market is live).",
   "testHonestInKindRedemptionStillWorks -> PASS (non-whitelisted depositor, a real vault shareholder, receives 1000e18 credit via the honest adapter's admit path; positive control that the gate feature works as designed)."
  ],
  "passCounts": "integration suite 8/8; poc suite 2/2 (the two rogue-adapter tests). All green at commit bbeda7b.",
  "command": "cd vault-v2 && DECIMALS=18 TOKEN_TYPE=0 forge test --match-contract RogueAdapterGateTest --match-test \"testRogueAdapterCannotHandCreditToNonWhitelisted|testHonestInKindRedemptionStillWorks\" -vv",
  "output": "Ran 2 tests for test/poc/RogueAdapterGateTest.sol:RogueAdapterGateTest\n[PASS] testHonestInKindRedemptionStillWorks() (gas: 16160341)\nLogs:\n  honest IKR: credit received by shareholder (units): 1000000000000000000000\n\n[PASS] testRogueAdapterCannotHandCreditToNonWhitelisted() (gas: 20856004)\nLogs:\n  gated market exists; honest adapter credit: 10000000000000000000000\n  evil credit before (units): 0\n  evil credit after  (units): 0\n\nSuite result: ok. 2 passed; 0 failed; 0 skipped; finished in 11.86ms (9.38ms CPU time)",
  "trace": "Key -vvvv trace fragment (testRogueAdapterCannotHandCreditToNonWhitelisted):\n  IKRCompatibleEnterGate::admit(evil, MidnightAdapterFactory)\n    emit Admit(caller: MidnightAdapter(rogue), user: evil, adapterFactory: MidnightAdapterFactory)\n  Midnight::take(Offer{ maker: rogue, reduceOnly: true, tick: 0, ... }, units=1000e18, taker=evil)\n    <- [Revert] MakerCreditOrDebtIncreased()\n  <- [Revert] MakerCreditOrDebtIncreased()\nThe admit() succeeds (Admit event fires for the non-whitelisted evil), but the whole forceDeallocate\nreverts at the mandatory reduceOnly sale, rolling the admission back. evil ends with 0 credit.",
  "pocPath": "test/poc/RogueAdapterGateTest.sol",
  "pocSource": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport {MidnightAdapterIntegrationTest} from \"../integration/MidnightAdapterIntegrationTest.sol\";\nimport {IMidnightAdapter, IMidnightAdapterBase} from \"../../src/adapters/midnight/interfaces/IMidnightAdapter.sol\";\nimport {IMidnightAdapterFactory} from \"../../src/adapters/midnight/interfaces/IMidnightAdapterFactory.sol\";\nimport {IVaultV2} from \"../../src/interfaces/IVaultV2.sol\";\nimport {IIKRCompatibleEnterGate} from \"../../src/adapters/midnight/gates/interfaces/IIKRCompatibleEnterGate.sol\";\nimport {\n    IIKRCompatibleEnterGateFactory\n} from \"../../src/adapters/midnight/gates/interfaces/IIKRCompatibleEnterGateFactory.sol\";\nimport {IWhitelistEnterSubGate} from \"../../src/adapters/midnight/gates/interfaces/IWhitelistEnterSubGate.sol\";\nimport {\n    IWhitelistEnterSubGateFactory\n} from \"../../src/adapters/midnight/gates/interfaces/IWhitelistEnterSubGateFactory.sol\";\nimport {SafeERC20Lib} from \"../../src/libraries/SafeERC20Lib.sol\";\nimport {IMidnight, Market, Offer} from \"../../lib/midnight/src/interfaces/IMidnight.sol\";\nimport {IdLib} from \"../../lib/midnight/src/libraries/IdLib.sol\";\nimport {TickLib} from \"../../lib/midnight/src/libraries/TickLib.sol\";\nimport {HashLib} from \"../../lib/midnight/src/ratifiers/libraries/HashLib.sol\";\nimport {WAD} from \"../../src/libraries/ConstantsLib.sol\";\n\n/// @dev Minimal attacker-controlled \"vault\" used as a rogue MidnightAdapter's parentVault.\n/// @dev Pretends every account is a shareholder so the adapter's forceDeallocate taker check passes.\ncontract FakeVault {\n    address public immutable asset;\n\n    constructor(address _asset) {\n        asset = _asset;\n    }\n\n    function balanceOf(address) external pure returns (uint256) {\n        return 1;\n    }\n\n    /// @dev Drives the rogue adapter's deallocate() exactly as VaultV2.forceDeallocate would.\n    function callDeallocate(address adapter, bytes calldata data, uint256 assets, address sender) external {\n        IMidnightAdapter(adapter).deallocate(data, assets, IVaultV2.forceDeallocate.selector, sender);\n    }\n}\n\n/// @dev Review of the \"rogue adapter\" lead for the Midnight Adapter (Cantina competition, commit bbeda7b).\n/// @dev IKRCompatibleEnterGate.admit trusts factory.isCreated. Anyone can create an adapter with\n/// MidnightAdapterFactory for a parentVault of their own, so a rogue adapter deployed by a *registered*\n/// factory passes the gate's `factory.isCreated(msg.sender)` check and can invoke admit().\n/// @dev These tests set up a real, live gated market (honest whitelisted adapter holds credit) and prove\n/// whether the rogue adapter can weaponize admit() to push credit to a NON-whitelisted account.\ncontract RogueAdapterGateTest is MidnightAdapterIntegrationTest {\n    IMidnightAdapterFactory internal sharedFactory; // registered as admitter by the gate's roleSetter\n    IIKRCompatibleEnterGateFactory internal gateFactory;\n    IWhitelistEnterSubGate internal subGate;\n    IIKRCompatibleEnterGate internal gate;\n    IMidnightAdapter internal honestAdapter; // whitelisted, holds credit in the gated market\n    Market internal gatedMarket;\n    bytes32 internal gatedMarketId;\n\n    address internal evil = makeAddr(\"evil\"); // NOT credit-whitelisted\n    address internal gatedBorrower = makeAddr(\"gatedBorrower\");\n\n    /// @dev Builds a live gated market: an honest, whitelisted adapter buys credit from a borrower,\n    /// so the market exists on Midnight and the honest adapter holds real net credit.\n    function _setUpLiveGatedMarket() internal {\n        gateFactory =\n            IIKRCompatibleEnterGateFactory(deployCode(\"IKRCompatibleEnterGateFactory.sol:IKRCompatibleEnterGateFactory\"));\n        sharedFactory = IMidnightAdapterFactory(\n            deployCode(\"MidnightAdapterFactory.sol:MidnightAdapterFactory\", abi.encode(address(midnight)))\n        );\n        IWhitelistEnterSubGateFactory subGateFactory =\n            IWhitelistEnterSubGateFactory(deployCode(\"WhitelistEnterSubGateFactory.sol:WhitelistEnterSubGateFactory\"));\n        // Credit side CLOSED (compliance whitelist), debt side OPEN for demo simplicity.\n        subGate =\n            IWhitelistEnterSubGate(subGateFactory.createWhitelistEnterSubGate(owner, owner, false, true, bytes32(0)));\n\n        gatedMarket = _market(7 days);\n        gatedMarket.enterGate = gateFactory.createIKRCompatibleEnterGate(address(subGate), true, bytes32(0));\n        gate = IIKRCompatibleEnterGate(gatedMarket.enterGate);\n        gatedMarketId = IdLib.toId(gatedMarket);\n\n        uint256[] memory durations = new uint256[](1);\n        durations[0] = 7 days;\n        honestAdapter = IMidnightAdapter(\n            sharedFactory.createMidnightAdapter(address(vault), durations, address(gateFactory), bytes32(\"honest\"))\n        );\n\n        vm.startPrank(owner);\n        subGate.setIsWhitelister(true, owner, true);\n        subGate.setIsWhitelisted(true, address(honestAdapter), true); // honest adapter may hold credit\n        gate.addAdmitterFactory(address(sharedFactory)); // the shared factory is trusted\n        vm.stopPrank();\n\n        // Wire the honest adapter into the vault.\n        _submitAndSet(address(honestAdapter), abi.encodeCall(IMidnightAdapterBase.increaseMaxTtm, (90 days)));\n        _submitAndSet(address(vault), abi.encodeCall(IVaultV2.addAdapter, (address(honestAdapter))));\n        _submitAndSet(address(vault), abi.encodeCall(IVaultV2.setIsAllocator, (address(honestAdapter), true)));\n        _submitAndSet(\n            address(vault), abi.encodeCall(IVaultV2.setForceDeallocatePenalty, (address(honestAdapter), 0.02e18))\n        );\n        _increaseCaps(abi.encode(\"this\", address(honestAdapter)));\n        _increaseCaps(abi.encode(\"this/duration\", address(honestAdapter), 7 days));\n        _increaseCaps(\n            abi.encode(\n                \"collateralParams/rcfThreshold/liquidatorGate/enterGate\",\n                gatedMarket.collateralParams,\n                gatedMarket.rcfThreshold,\n                gatedMarket.liquidatorGate,\n                gatedMarket.enterGate\n            )\n        );\n        vm.prank(allocator);\n        honestAdapter.setIsSubRatifier(address(setterRatifier), true);\n\n        // Borrower makes a sell offer; honest adapter buys it, acquiring real credit in the gated market.\n        uint256 units = 10_000 * oneToken;\n        Offer memory borrowerOffer = Offer({\n            market: gatedMarket,\n            buy: false,\n            maker: gatedBorrower,\n            start: block.timestamp,\n            expiry: gatedMarket.maturity,\n            tick: TICK,\n            group: keccak256(abi.encode(\"gated buy\", gatedMarketId, units)),\n            callback: address(0),\n            callbackData: \"\",\n            receiverIfMakerIsSeller: gatedBorrower,\n            ratifier: address(setterRatifier),\n            reduceOnly: false,\n            maxUnits: uint128(units),\n            maxAssets: 0,\n            continuousFeeCap: type(uint256).max\n        });\n        uint256 collateral = units * 2e18 / oneToken;\n        deal(address(collateralToken), gatedBorrower, collateral);\n        bytes32 root = HashLib.hashOffer(borrowerOffer);\n        bytes32[] memory proof = new bytes32[](0);\n        bytes memory ratifierData = abi.encode(root, uint256(0), proof);\n        vm.startPrank(gatedBorrower);\n        collateralToken.approve(address(midnight), collateral);\n        midnight.supplyCollateral(gatedMarket, 0, collateral, gatedBorrower);\n        midnight.setIsAuthorized(address(setterRatifier), true, gatedBorrower);\n        setterRatifier.setIsRootRatified(gatedBorrower, root, true);\n        vm.stopPrank();\n\n        vm.prank(allocator);\n        honestAdapter.take(borrowerOffer, ratifierData, units, abi.encode(address(0), \"\"));\n\n        assertGt(midnight.credit(gatedMarketId, address(honestAdapter)), 0, \"honest adapter holds credit\");\n    }\n\n    /// @dev CORE: a rogue adapter from the registered factory, pointed at an attacker-owned fake vault,\n    /// tries to admit the non-whitelisted `evil` and hand it credit in the LIVE gated market through\n    /// forceDeallocate. admit() itself is reachable (factory.isCreated is true), but the hardcoded\n    /// reduceOnly sell offer makes the rogue adapter sell credit it does not own, so Midnight reverts\n    /// with MakerCreditOrDebtIncreased and `evil` receives nothing.\n    function testRogueAdapterCannotHandCreditToNonWhitelisted() public {\n        _setUpLiveGatedMarket();\n\n        FakeVault fakeVault = new FakeVault(address(underlyingToken));\n        uint256[] memory durations = new uint256[](1);\n        durations[0] = 7 days;\n        IMidnightAdapter rogue = IMidnightAdapter(\n            sharedFactory.createMidnightAdapter(address(fakeVault), durations, address(gateFactory), bytes32(\"rogue\"))\n        );\n        assertTrue(sharedFactory.isCreated(address(rogue)), \"rogue IS created by the trusted factory\");\n\n        uint256 assets = 1000 * oneToken;\n        deal(address(underlyingToken), evil, assets);\n        vm.startPrank(evil);\n        SafeERC20Lib.safeApprove(address(underlyingToken), address(rogue), assets);\n        // The adapter takes its own offer on behalf of the taker, so Midnight requires the taker to\n        // authorize the adapter. The attacker controls `evil`, so this is trivially satisfied.\n        midnight.setIsAuthorized(address(rogue), true, evil);\n        vm.stopPrank();\n\n        uint128 creditBefore = midnight.credit(gatedMarketId, evil);\n        assertEq(creditBefore, 0, \"evil starts with zero credit\");\n        assertFalse(subGate.isWhitelisted(true, evil), \"evil is not credit-whitelisted\");\n\n        // taker == sender == evil, so no extra Midnight authorization is needed.\n        bytes memory data = abi.encode(gatedMarket, evil);\n        vm.prank(evil);\n        vm.expectRevert(bytes4(0x98dce2bc)); // IMidnight.MakerCreditOrDebtIncreased()\n        fakeVault.callDeallocate(address(rogue), data, assets, evil);\n\n        uint128 creditAfter = midnight.credit(gatedMarketId, evil);\n        assertEq(creditAfter, 0, \"evil still has zero credit: admission could not be weaponized\");\n\n        emit log_named_uint(\"gated market exists; honest adapter credit\", midnight.credit(gatedMarketId, address(honestAdapter)));\n        emit log_named_uint(\"evil credit before (units)\", creditBefore);\n        emit log_named_uint(\"evil credit after  (units)\", creditAfter);\n    }\n\n    /// @dev Positive control: the admitted account under the hardcoded reduceOnly offer is the taker,\n    /// and the only credit that could ever move is the rogue adapter's OWN credit (which is zero in a\n    /// gated market, since it is never whitelisted and never admitted during a buy). Here we confirm a\n    /// legitimate in-kind redemption by a shareholder of the HONEST adapter's vault still succeeds in the\n    /// same gated market, i.e. the gate's admit path works as designed for honest adapters.\n    function testHonestInKindRedemptionStillWorks() public {\n        _setUpLiveGatedMarket();\n\n        uint256 units = 1000 * oneToken;\n        // depositor is a real shareholder of the honest adapter's vault (deposited in setUp).\n        assertGt(vault.balanceOf(depositor), 0, \"depositor holds vault shares\");\n        assertFalse(subGate.isWhitelisted(true, depositor), \"depositor is not credit-whitelisted\");\n\n        deal(address(underlyingToken), depositor, units);\n        uint128 before = midnight.credit(gatedMarketId, depositor);\n        vm.startPrank(depositor);\n        SafeERC20Lib.safeApprove(address(underlyingToken), address(honestAdapter), units);\n        midnight.setIsAuthorized(address(honestAdapter), true, depositor);\n        vault.forceDeallocate(address(honestAdapter), abi.encode(gatedMarket, depositor), units, depositor);\n        vm.stopPrank();\n        uint128 got = midnight.credit(gatedMarketId, depositor) - before;\n\n        assertGt(got, 0, \"honest shareholder received credit via admit path\");\n        emit log_named_uint(\"honest IKR: credit received by shareholder (units)\", got);\n    }\n}\n",
  "environment": "foundry forge 1.9.0, default profile via_ir (tests compiled with solc 0.8.28/0.8.34/0.8.37 as pinned by foundry.toml), evm osaka; XDG_DATA_HOME writable so solc installs succeeded."
 },
 "notCovered": [
  "Angles outside this one lead (accounting/rounding in buy/sell/hold/withdraw, PackedDurationsLib overflow, price/rate ratifier staleness, Vault V2 cap/allocation-id interaction, bad-debt/loss realisation, factory address prediction/duplicates) are handled by other items in the batch and were not re-examined here.",
  "The two team-held issues (shortfallAllowance consumed faster by a piecewise-filled sale; unused setIsWhitelistedWithSig re-admitting after a direct removal) were excluded per scope and not tested.",
  "No attempt was made to exploit misbehaviour requiring a trusted role (curator/allocator/sentinel/whitelister) or to re-raise findings already in the 2026-10-12 Cantina/TrustSec drafts."
 ]
}