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 [ + - + - ]: 2 : 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 [ + - ]: 2 : }
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 : 6990 : static bool TooDeepDerivPath(std::string_view desc)
59 : : {
60 : 6990 : const FuzzBufferType desc_buf{reinterpret_cast<const unsigned char *>(desc.data()), desc.size()};
61 : 6990 : return HasDeepDerivPath(desc_buf);
62 : : }
63 : :
64 : 6990 : static std::optional<std::pair<WalletDescriptor, FlatSigningProvider>> CreateWalletDescriptor(FuzzedDataProvider& fuzzed_data_provider)
65 : : {
66 : 6990 : const std::string mocked_descriptor{fuzzed_data_provider.ConsumeRandomLengthString()};
67 [ + - + + ]: 6990 : if (TooDeepDerivPath(mocked_descriptor)) return {};
68 [ + - ]: 6984 : const auto desc_str{MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)};
69 [ + + ]: 6984 : if (!desc_str.has_value()) return std::nullopt;
70 : :
71 : 6874 : FlatSigningProvider keys;
72 [ + - ]: 6874 : std::string error;
73 [ + - + - ]: 6874 : std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str.value(), keys, error, false);
74 [ + + ]: 6874 : if (parsed_descs.empty()) return std::nullopt;
75 : :
76 [ + - + - : 5666 : WalletDescriptor w_desc{std::move(parsed_descs.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1};
+ - ]
77 [ + - ]: 5666 : return std::make_pair(w_desc, keys);
78 : 20848 : }
79 : :
80 : 4773 : static DescriptorScriptPubKeyMan* CreateDescriptor(WalletDescriptor& wallet_desc, FlatSigningProvider& keys, CWallet& keystore)
81 : : {
82 : 4773 : LOCK(keystore.cs_wallet);
83 [ + - + - ]: 4773 : auto spk_manager_res = keystore.AddWalletDescriptor(wallet_desc, keys, /*label=*/"", /*internal=*/false);
84 [ + + ]: 4773 : if (!spk_manager_res) return nullptr;
85 : 3713 : return &spk_manager_res.value().get();
86 [ + - ]: 9546 : };
87 : :
88 [ + - ]: 6186 : FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
89 : : {
90 : 5744 : SeedRandomStateForTest(SeedRand::ZEROS);
91 : 5744 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
92 : 5744 : SetMockTime(ConsumeTime(fuzzed_data_provider));
93 : 5744 : const auto& node{g_setup->m_node};
94 : 5744 : Chainstate& chainstate{node.chainman->ActiveChainstate()};
95 [ + - + - ]: 5744 : std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
96 [ + - ]: 5744 : CWallet& wallet{*wallet_ptr};
97 : 5744 : {
98 [ + - ]: 5744 : LOCK(wallet.cs_wallet);
99 [ + - ]: 5744 : wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
100 [ + - + - ]: 11488 : wallet.SetLastBlockProcessed(chainstate.m_chain.Height(), chainstate.m_chain.Tip()->GetBlockHash());
101 [ + - ]: 5744 : wallet.m_keypool_size = 1;
102 : 0 : }
103 : :
104 [ + - ]: 5744 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
105 [ + + ]: 5744 : if (!wallet_desc.has_value()) return;
106 [ + - ]: 4658 : auto spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
107 [ + + ]: 4658 : if (spk_manager == nullptr) return;
108 : :
109 [ + + ]: 3598 : if (fuzzed_data_provider.ConsumeBool()) {
110 [ + - ]: 1246 : auto wallet_desc{CreateWalletDescriptor(fuzzed_data_provider)};
111 [ + + ]: 1246 : if (!wallet_desc.has_value()) {
112 : 238 : return;
113 : : }
114 [ + - ]: 1008 : std::string error;
115 [ + - + + ]: 1008 : if (spk_manager->CanUpdateToWalletDescriptor(wallet_desc->first, error)) {
116 [ + - ]: 115 : auto new_spk_manager{CreateDescriptor(wallet_desc->first, wallet_desc->second, wallet)};
117 [ + - ]: 115 : if (new_spk_manager != nullptr) spk_manager = new_spk_manager;
118 : : }
119 [ + - ]: 2254 : }
120 : :
121 : 3360 : bool good_data{true};
122 [ + + + + : 47774 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 20) {
+ + ]
123 [ + - ]: 20969 : CallOneOf(
124 : : fuzzed_data_provider,
125 : 709 : [&] {
126 : 709 : const CScript script{ConsumeScript(fuzzed_data_provider)};
127 [ + - ]: 709 : auto is_mine{spk_manager->IsMine(script)};
128 [ + + ]: 709 : if (is_mine == isminetype::ISMINE_SPENDABLE) {
129 [ + - - + ]: 30 : assert(spk_manager->GetScriptPubKeys().count(script));
130 : : }
131 : 709 : },
132 : 6520 : [&] {
133 : 6520 : auto spks{spk_manager->GetScriptPubKeys()};
134 [ + + + - ]: 32691 : for (const CScript& spk : spks) {
135 [ - + + - ]: 26171 : assert(spk_manager->IsMine(spk) == ISMINE_SPENDABLE);
136 : 26171 : CTxDestination dest;
137 [ + - ]: 26171 : bool extract_dest{ExtractDestination(spk, dest)};
138 [ + + ]: 26171 : if (extract_dest) {
139 [ + - ]: 20080 : const std::string msg{fuzzed_data_provider.ConsumeRandomLengthString()};
140 [ + + + + ]: 20080 : PKHash pk_hash{std::get_if<PKHash>(&dest) && fuzzed_data_provider.ConsumeBool() ?
141 : 7811 : *std::get_if<PKHash>(&dest) :
142 : 20080 : PKHash{ConsumeUInt160(fuzzed_data_provider)}};
143 [ + - ]: 20080 : std::string str_sig;
144 [ + - ]: 20080 : (void)spk_manager->SignMessage(msg, pk_hash, str_sig);
145 [ + - ]: 20080 : (void)spk_manager->GetMetadata(dest);
146 : 20080 : }
147 : 26171 : }
148 : 6520 : },
149 : 1338 : [&] {
150 : 1338 : auto spks{spk_manager->GetScriptPubKeys()};
151 [ + - ]: 1338 : if (!spks.empty()) {
152 : 1338 : auto& spk{PickValue(fuzzed_data_provider, spks)};
153 [ + - ]: 1338 : (void)spk_manager->MarkUnusedAddresses(spk);
154 : : }
155 : 1338 : },
156 : 7803 : [&] {
157 : 7803 : LOCK(spk_manager->cs_desc_man);
158 [ + - ]: 7803 : auto wallet_desc{spk_manager->GetWalletDescriptor()};
159 [ + - + + ]: 7803 : if (wallet_desc.descriptor->IsSingleType()) {
160 [ + - ]: 7576 : auto output_type{wallet_desc.descriptor->GetOutputType()};
161 [ + + ]: 7576 : if (output_type.has_value()) {
162 [ + - ]: 5482 : auto dest{spk_manager->GetNewDestination(*output_type)};
163 [ + + ]: 5482 : if (dest) {
164 [ + - - + ]: 4700 : assert(IsValidDestination(*dest));
165 [ + - - + ]: 4700 : assert(spk_manager->IsHDEnabled());
166 : : }
167 : 5482 : }
168 : : }
169 [ + - ]: 15606 : },
170 : 1229 : [&] {
171 : 1229 : CMutableTransaction tx_to;
172 : 1229 : const std::optional<CMutableTransaction> opt_tx_to{ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS)};
173 [ + + ]: 1229 : if (!opt_tx_to) {
174 : 131 : good_data = false;
175 [ - + ]: 131 : return;
176 : : }
177 [ + - ]: 1098 : tx_to = *opt_tx_to;
178 : :
179 : 1098 : std::map<COutPoint, Coin> coins{ConsumeCoins(fuzzed_data_provider)};
180 : 1098 : const int sighash{fuzzed_data_provider.ConsumeIntegral<int>()};
181 [ + - ]: 1098 : std::map<int, bilingual_str> input_errors;
182 [ + - ]: 1098 : (void)spk_manager->SignTransaction(tx_to, coins, sighash, input_errors);
183 [ + - ]: 3556 : },
184 : 3370 : [&] {
185 : 3370 : std::optional<PartiallySignedTransaction> opt_psbt{ConsumeDeserializable<PartiallySignedTransaction>(fuzzed_data_provider)};
186 [ + + ]: 3370 : if (!opt_psbt) {
187 : 753 : good_data = false;
188 : 753 : return;
189 : : }
190 [ + - ]: 2617 : auto psbt{*opt_psbt};
191 [ + - ]: 2617 : const PrecomputedTransactionData txdata{PrecomputePSBTData(psbt)};
192 [ - + ]: 2617 : std::optional<int> sighash_type{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 151)};
193 [ - + ]: 2617 : if (sighash_type == 151) sighash_type = std::nullopt;
194 : 2617 : auto sign = fuzzed_data_provider.ConsumeBool();
195 : 2617 : auto bip32derivs = fuzzed_data_provider.ConsumeBool();
196 : 2617 : auto finalize = fuzzed_data_provider.ConsumeBool();
197 [ + - ]: 2617 : (void)spk_manager->FillPSBT(psbt, txdata, sighash_type, sign, bip32derivs, nullptr, finalize);
198 : 3370 : }
199 : : );
200 : : }
201 : :
202 : 3360 : std::string descriptor;
203 [ + - ]: 3360 : (void)spk_manager->GetDescriptorString(descriptor, /*priv=*/fuzzed_data_provider.ConsumeBool());
204 [ + - ]: 3360 : (void)spk_manager->GetEndRange();
205 [ + - ]: 3360 : (void)spk_manager->GetKeyPoolSize();
206 [ + - + - : 14848 : }
+ - ]
207 : :
208 : : } // namespace
209 : : } // namespace wallet
|