|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of rippled: https://github.com/ripple/rippled |
| 4 | + Copyright (c) 2024 XRPL-Labs |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and/or distribute this software for any |
| 7 | + purpose with or without fee is hereby granted, provided that the above |
| 8 | + copyright notice and this permission notice appear in all copies. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +*/ |
| 18 | +//============================================================================== |
| 19 | + |
| 20 | +#include <ripple/app/hook/GasValidator.h> |
| 21 | +#include <ripple/app/hook/Guard.h> |
| 22 | +#include <ripple/app/hook/Macro.h> |
| 23 | +#include <ripple/basics/Log.h> |
| 24 | +#include <ripple/protocol/Feature.h> |
| 25 | +#include <wasmedge/wasmedge.h> |
| 26 | + |
| 27 | +namespace hook { |
| 28 | + |
| 29 | +std::optional<std::string> |
| 30 | +validateWasmHostFunctionsForGas( |
| 31 | + std::vector<uint8_t> const& wasm, |
| 32 | + Rules const& rules, |
| 33 | + beast::Journal const& j) |
| 34 | +{ |
| 35 | + // Create WasmEdge Loader |
| 36 | + WasmEdge_LoaderContext* loader = WasmEdge_LoaderCreate(NULL); |
| 37 | + if (!loader) |
| 38 | + { |
| 39 | + return "Failed to create WasmEdge Loader"; |
| 40 | + } |
| 41 | + |
| 42 | + // Parse WASM binary |
| 43 | + WasmEdge_ASTModuleContext* astModule = NULL; |
| 44 | + WasmEdge_Result res = WasmEdge_LoaderParseFromBuffer( |
| 45 | + loader, &astModule, wasm.data(), wasm.size()); |
| 46 | + |
| 47 | + if (!WasmEdge_ResultOK(res)) |
| 48 | + { |
| 49 | + WasmEdge_LoaderDelete(loader); |
| 50 | + const char* msg = WasmEdge_ResultGetMessage(res); |
| 51 | + return std::string("Failed to parse WASM: ") + |
| 52 | + (msg ? msg : "unknown error"); |
| 53 | + } |
| 54 | + |
| 55 | + // Get import count |
| 56 | + uint32_t importCount = WasmEdge_ASTModuleListImportsLength(astModule); |
| 57 | + |
| 58 | + if (importCount == 0) |
| 59 | + { |
| 60 | + WasmEdge_ASTModuleDelete(astModule); |
| 61 | + WasmEdge_LoaderDelete(loader); |
| 62 | + JLOG(j.trace()) << "HookSet(" << hook::log::IMPORTS_MISSING |
| 63 | + << "): WASM must import at least hook API functions"; |
| 64 | + return "WASM must import at least hook API functions"; |
| 65 | + } |
| 66 | + |
| 67 | + // Get imports (max 256) |
| 68 | + const WasmEdge_ImportTypeContext* imports[256]; |
| 69 | + uint32_t actualImportCount = std::min(importCount, 256u); |
| 70 | + actualImportCount = |
| 71 | + WasmEdge_ASTModuleListImports(astModule, imports, actualImportCount); |
| 72 | + |
| 73 | + std::optional<std::string> error; |
| 74 | + |
| 75 | + // Check each import |
| 76 | + for (uint32_t i = 0; i < actualImportCount; i++) |
| 77 | + { |
| 78 | + WasmEdge_String moduleName = |
| 79 | + WasmEdge_ImportTypeGetModuleName(imports[i]); |
| 80 | + WasmEdge_String externalName = |
| 81 | + WasmEdge_ImportTypeGetExternalName(imports[i]); |
| 82 | + WasmEdge_ExternalType extType = |
| 83 | + WasmEdge_ImportTypeGetExternalType(imports[i]); |
| 84 | + |
| 85 | + // Only check function imports |
| 86 | + if (extType != WasmEdge_ExternalType_Function) |
| 87 | + continue; |
| 88 | + |
| 89 | + // Convert WasmEdge_String to std::string for comparison |
| 90 | + std::string modName(moduleName.Buf, moduleName.Length); |
| 91 | + std::string extName(externalName.Buf, externalName.Length); |
| 92 | + |
| 93 | + // Check module name is "env" |
| 94 | + if (modName != "env") |
| 95 | + { |
| 96 | + JLOG(j.trace()) |
| 97 | + << "HookSet(" << hook::log::IMPORT_MODULE_ENV |
| 98 | + << "): Import module must be 'env', found: " << modName; |
| 99 | + error = "Import module must be 'env', found: " + modName; |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + // Check for forbidden _g function (guard function) |
| 104 | + if (extName == "_g") |
| 105 | + { |
| 106 | + JLOG(j.trace()) |
| 107 | + << "HookSet(" << hook::log::IMPORT_ILLEGAL |
| 108 | + << "): Gas-type hooks cannot import _g (guard) function"; |
| 109 | + error = "Gas-type hooks cannot import _g (guard) function"; |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + // Check external name length |
| 114 | + if (extName.length() < 1 || extName.length() > 64) |
| 115 | + { |
| 116 | + JLOG(j.trace()) << "HookSet(" << hook::log::IMPORT_NAME_BAD |
| 117 | + << "): Import name length invalid: " << extName; |
| 118 | + error = "Import name length invalid: " + extName; |
| 119 | + break; |
| 120 | + } |
| 121 | + |
| 122 | + // Check against whitelist using find() |
| 123 | + bool found = false; |
| 124 | + |
| 125 | + // Check base whitelist (import_whitelist) |
| 126 | + if (hook_api::import_whitelist.find(extName) != |
| 127 | + hook_api::import_whitelist.end()) |
| 128 | + { |
| 129 | + found = true; |
| 130 | + } |
| 131 | + |
| 132 | + // Check extended whitelist (import_whitelist_1) |
| 133 | + if (!found && rules.enabled(featureHooksUpdate1) && |
| 134 | + hook_api::import_whitelist_1.find(extName) != |
| 135 | + hook_api::import_whitelist_1.end()) |
| 136 | + { |
| 137 | + found = true; |
| 138 | + } |
| 139 | + |
| 140 | + if (!found) |
| 141 | + { |
| 142 | + JLOG(j.trace()) << "HookSet(" << hook::log::IMPORT_ILLEGAL |
| 143 | + << "): Import not in whitelist: " << extName; |
| 144 | + error = "Import not in whitelist: " + extName; |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Cleanup |
| 150 | + WasmEdge_ASTModuleDelete(astModule); |
| 151 | + WasmEdge_LoaderDelete(loader); |
| 152 | + |
| 153 | + return error; |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace hook |
0 commit comments