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/translation.h>
23 : : #include <validation.h>
24 : : #include <wallet/scriptpubkeyman.h>
25 : : #include <wallet/test/util.h>
26 : : #include <wallet/types.h>
27 : : #include <wallet/wallet.h>
28 : : #include <wallet/walletutil.h>
29 : :
30 : : #include <map>
31 : : #include <memory>
32 : : #include <optional>
33 : : #include <string>
34 : : #include <utility>
35 : : #include <variant>
36 : :
37 : : namespace wallet {
38 : : namespace {
39 : : const TestingSetup* g_setup;
40 : :
41 : : //! The converter of mocked descriptors, needs to be initialized when the target is.
42 : : MockedDescriptorConverter MOCKED_DESC_CONVERTER;
43 : :
44 : 0 : void initialize_spkm()
45 : : {
46 [ # # # # : 0 : static const auto testing_setup{MakeNoLogFileContext<const TestingSetup>()};
# # ]
47 : 0 : g_setup = testing_setup.get();
48 : 0 : SelectParams(ChainType::MAIN);
49 : 0 : MOCKED_DESC_CONVERTER.Init();
50 : 0 : }
51 : :
52 : : /**
53 : : * Key derivation is expensive. Deriving deep derivation paths take a lot of compute and we'd rather spend time
54 : : * elsewhere in this target, like on actually fuzzing the DescriptorScriptPubKeyMan. So rule out strings which could
55 : : * correspond to a descriptor containing a too large derivation path.
56 : : */
57 : 20540 : static bool TooDeepDerivPath(std::string_view desc)
58 : : {
59 : 20540 : const FuzzBufferType desc_buf{reinterpret_cast<const unsigned char *>(desc.data()), desc.size()};
60 : 41080 : return HasDeepDerivPath(desc_buf);
61 : 20540 : }
62 : :
63 : 20540 : static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
64 : : {
65 : 20540 : const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
66 [ + - + + ]: 20540 : if (TooDeepDerivPath(mocked_descriptor)) return {};
67 [ + - ]: 19970 : const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
68 [ + + ]: 19970 : if (!desc_str.has_value()) return std::nullopt;
69 : :
70 : 18363 : FlatSigningProvider keys;
71 : 18363 : std::string error;
72 [ + - + - ]: 18363 : std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
73 [ + + ]: 18363 : if (parsed_descs.empty()) return std::nullopt;
74 : :
75 [ + - + - : 11223 : WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
+ - ]
76 [ + - ]: 11223 : return std::make_pair(w_desc, keys);
77 : 20540 : }
78 : :
79 : 10518 : static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
80 : : {
81 : 10518 : LOCK(keystore.cs_wallet);
82 [ + - + - ]: 10518 : keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false);
83 [ + - ]: 10518 : return keystore.GetDescriptorScriptPubKeyMan(wallet_desc);
84 : 10518 : };
85 : :
86 [ + - ]: 11919 : FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
87 : : {
88 : 11917 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
89 : 11917 : const auto& node{g_setup->m_node};
90 : 11917 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
91 [ + - + - ]: 11917 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
92 : 11917 : CWallet& wallet{*wallet_ptr};
93 : : {
94 [ + - + - ]: 11917 : LOCK(wallet.cs_wallet);
95 [ + - ]: 11917 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
96 [ + - + - : 11917 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
+ - + - ]
97 : 11917 : wallet.m_keypool_size = 1;
98 : 11917 : }
99 : :
100 [ + - ]: 11917 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
101 [ + + ]: 11917 : if (!wallet_desc.has_value()) return;
102 [ + - ]: 9997 : auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
103 [ + - ]: 9997 : if (spk_manager == nullptr) return;
104 : :
105 [ + - + + ]: 9997 : if (fuzzed_data_provider.ConsumeBool()) {
106 [ + - ]: 8623 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
107 [ + + ]: 8623 : if (!wallet_desc.has_value()) {
108 : 7397 : return;
109 : : }
110 : 1226 : std::string error;
111 [ + - + + ]: 1226 : if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
112 [ + - ]: 521 : auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
113 [ - + ]: 521 : if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
114 : 521 : }
115 [ + + ]: 8623 : }
116 : :
117 : 2600 : bool good_data{true};
118 [ + + + - : 11893 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + + + ]
119 [ + - ]: 9293 : CallOneOf(
120 : : fuzzed_data_provider,
121 : 10010 : [&] {
122 : 717 : const CScript script{ConsumeScript(fuzzed_data_provider)};
123 [ + - ]: 717 : auto is_mine{spk_manager->IsMine(script)};
124 [ + + ]: 717 : if (is_mine == isminetype::ISMINE_SPENDABLE) {
125 [ + - + - : 3 : assert(spk_manager->GetScriptPubKeys().count(script));
+ - ]
126 : 3 : }
127 : 717 : },
128 : 11804 : [&] {
129 : 2511 : auto spks{spk_manager->GetScriptPubKeys()};
130 [ + + ]: 9437 : for (const CScript& spk : spks) {
131 [ + - - + ]: 6926 : assert(spk_manager->IsMine(spk) == ISMINE_SPENDABLE);
132 : 6926 : CTxDestination dest;
133 [ - + ]: 6926 : bool extract_dest{ExtractDestination(spk, dest)};
134 [ + + ]: 6926 : if (extract_dest) {
135 [ - + ]: 5312 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
136 [ + + + - : 8997 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
+ + ]
137 : 1627 : *std::get_if<PKHash>(&dest) :
138 [ + - ]: 3685 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
139 : 5312 : std::string str_sig;
140 [ + - ]: 5312 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
141 [ + - ]: 5312 : (void)spk_manager->GetMetadata(dest);
142 : 5312 : }
143 : 6926 : }
144 : 2511 : },
145 : 9716 : [&] {
146 : 423 : auto spks{spk_manager->GetScriptPubKeys()};
147 [ + + ]: 423 : if (!spks.empty()) {
148 [ + - ]: 395 : auto& spk{PickValue(fuzzed_data_provider, spks)};
149 [ + - ]: 395 : (void)spk_manager->MarkUnusedAddresses(spk);
150 : 395 : }
151 : 423 : },
152 : 12873 : [&] {
153 : 3580 : LOCK(spk_manager->cs_desc_man);
154 [ + - ]: 3580 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
155 [ + - + + ]: 3580 : if (wallet_desc.descriptor->IsSingleType()) {
156 [ + - ]: 3405 : auto output_type{wallet_desc.descriptor->GetOutputType()};
157 [ + + ]: 3405 : if (output_type.has_value()) {
158 [ + - ]: 2530 : auto dest{spk_manager->GetNewDestination(*output_type)};
159 [ + + ]: 2530 : if (dest) {
160 [ + - + - : 1464 : assert(IsValidDestination(*dest));
+ - ]
161 [ + - + - ]: 1464 : assert(spk_manager->IsHDEnabled());
162 : 1464 : }
163 : 2530 : }
164 : 3405 : }
165 : 3580 : },
166 : 10745 : [&] {
167 : 1452 : CMutableTransaction tx_to;
168 : 1452 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
169 [ + + ]: 1452 : if (!opt_tx_to) {
170 : 94 : good_data = false;
171 : 94 : return;
172 : : }
173 [ + - ]: 1358 : tx_to = *opt_tx_to;
174 : :
175 : 1358 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
176 [ + - ]: 1358 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
177 : 1358 : std::map<int, bilingual_str> input_errors;
178 [ + - ]: 1358 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
179 [ - + ]: 1452 : },
180 : 9903 : [&] {
181 : 610 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
182 [ + + ]: 610 : if (!opt_psbt) {
183 : 524 : good_data = false;
184 : 524 : return;
185 : : }
186 [ + - ]: 86 : auto psbt{*opt_psbt};
187 [ + - ]: 86 : const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)};
188 : 86 : const int sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 150)};
189 [ + - + - : 86 : (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, fuzzed_data_provider.ConsumeBool(), fuzzed_data_provider.ConsumeBool(), nullptr, fuzzed_data_provider.ConsumeBool());
+ - + - ]
190 [ - + ]: 610 : }
191 : : );
192 : 9293 : }
193 : :
194 : 2600 : std::string descriptor;
195 [ + - + - ]: 2600 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
196 [ + - ]: 2600 : (void)spk_manager->GetEndRange();
197 [ + - ]: 2600 : (void)spk_manager->GetKeyPoolSize();
198 [ - + ]: 11917 : }
199 : :
200 : : } // namespace
201 : : } // namespace wallet
|