Fix ops computation for l: and u: fragments
What changed, and why it matters
This commit fixes a bug in how the Ledger Bitcoin app counts Bitcoin script operations ('ops') for two specific Miniscript fragments, 'l:' and 'u:'. Because the count was too low, the app could accept wallet policies that look valid but are actually impossible to spend from under certain conditions. The fix makes the operation count more conservative so such unspendable policies are rejected. New tests are added to prevent the bug from returning.
Treat this as a security-relevant correctness fix. Review whether any already-registered wallet policies on deployed devices could have exploited the undercount, and consider whether a firmware update or policy re-validation is needed. Ensure the new regression tests are run in CI.
Security signals we found
Undercounting of script operation budget leading to acceptance of unspendable policies
Incorrect handling of Miniscript wrapper dissatisfaction semantics
Policy sanity check bypass via operation-count accounting flaw
Regression tests added for 'l:' and 'u:' wrappers and 201-op limit boundary
Evidence from the diff
In src/common/wallet.c, compute_miniscript_policy_ext_info() undercounted ops for the ‘l:’ (lift) and ‘u:’ (u) wrappers. These wrappers can always be dissatisfied via a constant-0 branch, meaning the child’s dissatisfaction op count should not be blindly propagated; instead the dissatisfaction path should be treated as costing 0 ops. The patch changes the dsat field for these wrappers from x.ops.dsat to maxcheck(0, x.ops.dsat), aligning with the Miniscript reference semantics. As a result, or_d and similar combinators now correctly account for the right branch’s ops when the left branch is dissatisfied via the constant-0 path. This prevents policies that exceed the 201-op limit only on a spend path from being classified as sane and accepted.
Changed components
src/common/wallet.ctests/test_register_wallet.pyunit-tests/test_wallet.cInspect captured patch +39 / −3
### src/common/wallet.c
@@ -2917,7 +2917,7 @@ int compute_miniscript_policy_ext_info(const policy_node_t *policy_node,
out->script_size = x.script_size + 4;
- out->ops = (miniscript_ops_t) {3 + x.ops.count, x.ops.sat, x.ops.dsat};
+ out->ops = (miniscript_ops_t) {3 + x.ops.count, x.ops.sat, maxcheck(0, x.ops.dsat)};
out->ss = (miniscript_stacksize_t) {sumcheck(x.ss.sat, 1),
maxcheck(1, sumcheck(x.ss.dsat, 1))};
### tests/test_register_wallet.py
@@ -497,8 +497,36 @@ def test_register_wallet_not_sane_policy(navigator: Navigator, firmware: Firmwar
assert error_code == EC_REGISTER_WALLET_POLICY_NOT_SANE
- # TODO: we can probably not trigger stack and ops limits with the current limits we have on the
- # miniscript policy size; otherwise it would be worth to add tests for them, too.
+ # Miniscript policy exceeding the 201 ops limit only when satisfying the multi in the second branch
+ # of the or_d. The ops of that branch are only accounted for if the dissatisfaction of the 'u:'
+ # wrapper (via its constant 0 branch) is.
+ keys = ",".join(f"@{i}/**" for i in range(10))
+ with pytest.raises(ExceptionRAPDU) as e:
+ client.register_wallet(WalletPolicy(
+ name="Too many ops",
+ descriptor_template=f"wsh(or_d(utv:or_i(jjjjjjjjjj:pk(@10/**),jjjjjjjjjj:pk(@11/**)),or_i(jjjjjjjjjjjjj:multi(1,{keys}),jjjjjjjjjjjjj:pk(@12/**))))",
+ keys_info=[
+ "[f5acc2fd/48'/1'/0'/2']tpubDFAqEGNyad35aBCKUAXbQGDjdVhNueno5ZZVEn3sQbW5ci457gLR7HyTmHBg93oourBssgUxuWz1jX5uhc1qaqFo9VsybY1J5FuedLfm4dK",
+ "tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF",
+ "tpubDDV6FDLcCieWUeN7R3vZK2Qs3KuQed3ScTY9EiwMXvyCkLjDbCb8RXaAgWDbkG4tW1BMKVF1zERHnyt78QKd4ZaAYGMJMpvHPwgSSU1AxZ3",
+ "tpubDF6JT5K4izwALMpFv7fQrpWr5bGUMEoWphkzTVJH8jTfgirNEgGZnxsWJDCCxhg2UnW5RcD9Tx8aVAdoM734X5bnRGmJUujz26uQ5gAC1nE",
+ "tpubDF4kujkh5dAhC1pFgBToZybXdvJFXXGX4BWdDxWqP7EUpG8gxkfMQeDjGPDnTr9e4NrkFmDM1ocav3Jz6x79CRZbxGr9dzFokJLuvDDnyRh",
+ "tpubDDcwGjxKph1xkiAnzvpgdnTeaEhVnH9d766yqvd16JN7EmPW9qSwnbco7kZwPd7UbyEwRojYGUaHT1UULbdqAjGQzeCy3qdZEwZLRmpzwZV",
+ "tpubDD7LLJNCVTKQiB41FH3NyJPzMUNroRtzzY3WFAzKZDikrMpw9PJTi6A2Yes5Tamin4wsgJ4JLsj2AVUSvQqP2T6q3bztu7obRuU3Lrh4eTw",
+ "tpubDCDraP1C24GGX6BHCewLBWbKQRNGACfz8JjyKXYoZjEJWeGV5Ng43FL31MryaiqeBjdC5dPUZD2zqnmMe6gqrYEstnu8pmJZYp3AQmhzQ6G",
+ "tpubDCczwGSwQAF9Z5gTqL3tjznCFC9De5kFBLGdJJuj3UogVyYXVG7HuFdNsvJ9oDtvn4waeawS8XvRpBfbAZaDv1pGRiZdc9qnQhLKTS8eWXH",
+ "tpubD6NzVbkrYhZ4YAPXpMw61GrdqXJJEiYhHo6wxVkfwZgUged5qXm6Df4NLf8ZTFXxW1UhxDKGeKdAVxZtmodC8KfR7SqmW6LGQfDGfnFLmQ6",
+ "tpubD6NzVbkrYhZ4WLczPJWReQycCJdd6YVWXubbVUFnJ5KgU5MDQrD998ZJLSmaB7GVcCnJSDWprxmrGkJ6SvgQC6QAffVpqSvonXmeizXcrkN",
+ "tpubDFY2NoEHyYsp4J98UCMAaRT5LzRYeXjWqh2txK2RsxPAR5YWKWyTeZBBncRJ7z5nL5RUQPEgycbgbbmywbeLaH9yWK6rnFAYQn28HyiYc1Y",
+ "tpubDEGquuorgFNb8bjh5kNZQMPtABJzoWwNm78FUmeoPkfRtoPF7JLrtoZeT3J3ybq1HmC3Rn1Q8wFQ8J5usanzups5rj7PJoQLNyvq8QbJruW",
+ ]),
+ navigator,
+ testname=test_name
+ )
+ assert DeviceException.exc.get(e.value.status) == NotSupportedError
+ assert len(e.value.data) == 2
+ error_code = int.from_bytes(e.value.data, 'big')
+ assert error_code == EC_REGISTER_WALLET_POLICY_NOT_SANE
def test_register_unusual_singlesig_accounts(navigator: Navigator, firmware: Firmware, client:
### unit-tests/test_wallet.c
@@ -794,6 +794,14 @@ static void test_miniscript_types(void **state) {
// or_b(sha256,a:sha256) has e=1 but m=0 (children lack 's'), so thresh must NOT be NONMAL.
Test("thresh(1,or_b(sha256(e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f),a:sha256(d1ec675902ef1633427ca360b290b0b3045a0d9058ddb5e648b4c3c3224c5c68)),a:0)", "?", TESTMODE_VALID, -1, -1);
+ // Regression tests: 'l:' and 'u:' can always be dissatisfied via the constant 0 branch, even if
+ // the child can't. The or_d must account for the ops of the multi executed after dissatisfying
+ // the first branch.
+ Test("or_d(utv:pk(@0/**),multi(1,@1/**,@2/**,@3/**))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 11, 4);
+ Test("or_d(ltv:pk(@0/**),multi(1,@1/**,@2/**,@3/**))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 11, 4);
+ // Only the right branch of the or_d exceeds the 201 ops limit
+ Test("or_d(utv:or_i(jjjjjjjjjj:pk(@10/**),jjjjjjjjjj:pk(@11/**)),or_i(jjjjjjjjjjjjj:multi(1,@0/**,@1/**,@2/**,@3/**,@4/**,@5/**,@6/**,@7/**,@8/**,@9/**),jjjjjjjjjjjjj:pk(@12/**)))", "?", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_TAPSCRIPT_INVALID, 211, -1);
+
// clang-format on
}
Why this scored 74/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.