Branch data Line data Source code
1 : : // Copyright (c) 2023-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <addresstype.h>
6 : : #include <chainparams.h>
7 : : #include <coins.h>
8 : : #include <key.h>
9 : : #include <primitives/transaction.h>
10 : : #include <psbt.h>
11 : : #include <script/descriptor.h>
12 : : #include <script/interpreter.h>
13 : : #include <script/script.h>
14 : : #include <script/signingprovider.h>
15 : : #include <sync.h>
16 : : #include <test/fuzz/FuzzedDataProvider.h>
17 : : #include <test/fuzz/fuzz.h>
18 : : #include <test/fuzz/util.h>
19 : : #include <test/fuzz/util/descriptor.h>
20 : : #include <test/util/setup_common.h>
21 : : #include <util/check.h>
22 : : #include <util/time.h>
23 : : #include <util/translation.h>
24 : : #include <validation.h>
25 : : #include <wallet/scriptpubkeyman.h>
26 : : #include <wallet/test/util.h>
27 : : #include <wallet/types.h>
28 : : #include <wallet/wallet.h>
29 : : #include <wallet/walletutil.h>
30 : :
31 : : #include <map>
32 : : #include <memory>
33 : : #include <optional>
34 : : #include <string>
35 : : #include <utility>
36 : : #include <variant>
37 : :
38 : : namespace wallet {
39 : : namespace {
40 : : const TestingSetup* g_setup;
41 : :
42 : : //! The converter of mocked descriptors, needs to be initialized when the target is.
43 : : MockedDescriptorConverter MOCKED_DESC_CONVERTER;
44 : :
45 : 1 : void initialize_spkm()
46 : : {
47 [ + - + - : 1 : static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
+ - ]
48 : 1 : g_setup = testing_setup.get();
49 : 1 : SelectParams(ChainType::MAIN);
50 : 1 : MOCKED_DESC_CONVERTER.Init();
51 : 1 : }
52 : :
53 : : /**
54 : : * Key derivation is expensive. Deriving deep derivation paths take a lot of compute and we'd rather spend time
55 : : * elsewhere in this target, like on actually fuzzing the DescriptorScriptPubKeyMan. So rule out strings which could
56 : : * correspond to a descriptor containing a too large derivation path.
57 : : */
58 : 6576 : static bool TooDeepDerivPath(std::string_view desc)
59 : : {
60 : 6576 : const FuzzBufferType desc_buf{reinterpret_cast<const unsigned char *>(desc.data()), desc.size()};
61 : 6576 : return HasDeepDerivPath(desc_buf);
62 : : }
63 : :
64 : 6576 : static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
65 : : {
66 : 6576 : const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
67 [ - + + - : 6576 : if (TooDeepDerivPath(mocked_descriptor)) return {};
+ + ]
68 [ - + + - ]: 6573 : const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
69 [ + + ]: 6573 : if (!desc_str.has_value()) return std::nullopt;
70 : :
71 : 6497 : FlatSigningProvider keys;
72 [ + - ]: 6497 : std::string error;
73 [ + - + - ]: 6497 : std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
74 [ + + ]: 6497 : if (parsed_descs.empty()) return std::nullopt;
75 : :
76 [ + - + - : 5427 : WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
+ - ]
77 [ + - ]: 5427 : return std::make_pair(w_desc, keys);
78 : 19646 : }
79 : :
80 : 4496 : static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
81 : : {
82 : 4496 : LOCK(keystore.cs_wallet);
83 [ + - + - ]: 4496 : auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false);
84 [ + + ]: 4496 : if (!spk_manager_res) return nullptr;
85 : 4453 : return &spk_manager_res.value().get();
86 [ + - ]: 8992 : };
87 : :
88 [ + - ]: 5858 : FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
89 : : {
90 : 5402 : SeedRandomStateForTest(SeedRand::ZEROS);
91 : 5402 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
92 : 5402 : SetMockTime(ConsumeTime(fuzzed_data_provider));
93 : 5402 : const auto& node{g_setup->m_node};
94 : 5402 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
95 [ + - + - ]: 5402 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
96 [ + - ]: 5402 : CWallet& wallet{*wallet_ptr};
97 : 5402 : {
98 [ + - ]: 5402 : LOCK(wallet.cs_wallet);
99 [ + - ]: 5402 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
100 [ - + + - ]: 10804 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
101 [ + - ]: 5402 : wallet.m_keypool_size = 1;
102 : 0 : }
103 : :
104 [ + - ]: 5402 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
105 [ + + ]: 5402 : if (!wallet_desc.has_value()) return;
106 [ + - ]: 4410 : auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
107 [ + + ]: 4410 : if (spk_manager == nullptr) return;
108 : :
109 [ + + ]: 4367 : if (fuzzed_data_provider.ConsumeBool()) {
110 [ + - ]: 1174 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
111 [ + + ]: 1174 : if (!wallet_desc.has_value()) {
112 : 157 : return;
113 : : }
114 [ + - ]: 1017 : std::string error;
115 [ + - + + ]: 1017 : if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
116 [ + - ]: 86 : auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
117 [ + - ]: 86 : if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
118 : : }
119 [ + - ]: 2191 : }
120 : :
121 : 4210 : bool good_data{true};
122 [ + + + + : 57073 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + ]
123 [ + - ]: 24790 : CallOneOf(
124 : : fuzzed_data_provider,
125 : 800 : [&] {
126 : 800 : const CScript script{ConsumeScript(fuzzed_data_provider)};
127 [ + - + + ]: 800 : if (spk_manager->IsMine(script)) {
128 [ + - - + ]: 54 : assert(spk_manager->GetScriptPubKeys().count(script));
129 : : }
130 : 800 : },
131 : 5757 : [&] {
132 : 5757 : auto spks{spk_manager->GetScriptPubKeys()};
133 [ + + + - ]: 24365 : for (const CScript& spk : spks) {
134 [ - + + - ]: 18608 : assert(spk_manager->IsMine(spk));
135 : 18608 : CTxDestination dest;
136 [ + - ]: 18608 : bool extract_dest{ExtractDestination(spk, dest)};
137 [ + + ]: 18608 : if (extract_dest) {
138 [ + - ]: 14208 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
139 [ + + + + ]: 14208 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
140 : 4475 : *std::get_if<PKHash>(&dest) :
141 : 14208 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
142 [ + - ]: 14208 : std::string str_sig;
143 [ + - ]: 14208 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
144 [ + - ]: 14208 : (void)spk_manager->GetMetadata(dest);
145 : 14208 : }
146 : 18608 : }
147 : 5757 : },
148 : 1642 : [&] {
149 : 1642 : auto spks{spk_manager->GetScriptPubKeys()};
150 [ + - ]: 1642 : if (!spks.empty()) {
151 : 1642 : auto& spk{PickValue(fuzzed_data_provider, spks)};
152 [ + - ]: 1642 : (void)spk_manager->MarkUnusedAddresses(spk);
153 : : }
154 : 1642 : },
155 : 9248 : [&] {
156 : 9248 : LOCK(spk_manager->cs_desc_man);
157 [ + - ]: 9248 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
158 [ + - + + ]: 9248 : if (wallet_desc.descriptor->IsSingleType()) {
159 [ + - ]: 9125 : auto output_type{wallet_desc.descriptor->GetOutputType()};
160 [ + + ]: 9125 : if (output_type.has_value()) {
161 [ + - ]: 6724 : auto dest{spk_manager->GetNewDestination(*output_type)};
162 [ + + ]: 6724 : if (dest) {
163 [ + - - + ]: 5941 : assert(IsValidDestination(*dest));
164 [ + - - + ]: 5941 : assert(spk_manager->IsHDEnabled());
165 : : }
166 : 6724 : }
167 : : }
168 [ + - ]: 18496 : },
169 : 2206 : [&] {
170 : 2206 : CMutableTransaction tx_to;
171 : 2206 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
172 [ + + ]: 2206 : if (!opt_tx_to) {
173 : 167 : good_data = false;
174 [ - + ]: 167 : return;
175 : : }
176 [ + - ]: 2039 : tx_to = *opt_tx_to;
177 : :
178 : 2039 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
179 : 2039 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
180 [ + - ]: 2039 : std::map<int, bilingual_str> input_errors;
181 [ + - ]: 2039 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
182 [ + - ]: 6451 : },
183 : 5137 : [&] {
184 : 5137 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
185 [ + + ]: 5137 : if (!opt_psbt) {
186 : 760 : good_data = false;
187 : 760 : return;
188 : : }
189 [ + - ]: 4377 : auto psbt{*opt_psbt};
190 [ + - ]: 4377 : const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)};
191 [ + + ]: 4377 : std::optional<int> sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 151)};
192 [ + + ]: 4377 : if (sighash_type == 151) sighash_type = std::nullopt;
193 : 4377 : auto sign = fuzzed_data_provider.ConsumeBool();
194 : 4377 : auto bip32derivs = fuzzed_data_provider.ConsumeBool();
195 : 4377 : auto finalize = fuzzed_data_provider.ConsumeBool();
196 [ + - ]: 4377 : (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, sign, bip32derivs, nullptr, finalize);
197 : 5137 : }
198 : : );
199 : : }
200 : :
201 : 4210 : std::string descriptor;
202 [ + - ]: 4210 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
203 [ + - ]: 4210 : (void)spk_manager->GetEndRange();
204 [ + - ]: 4210 : (void)spk_manager->GetKeyPoolSize();
205 [ + - + - : 15014 : }
+ - ]
206 : :
207 : : } // namespace
208 : : } // namespace wallet
|