|
| 1 | +#include "checks.h" |
| 2 | +#include "config.h" |
| 3 | +#include "log.h" |
| 4 | +#include "memzero.h" |
| 5 | +#include "nanopb_stream.h" |
| 6 | +#include "print.h" |
| 7 | +#include "rand.h" |
| 8 | +#include "rpc.h" |
| 9 | + |
| 10 | +#include <assert.h> |
| 11 | +#include <pb_decode.h> |
| 12 | +#include <pb_encode.h> |
| 13 | +#include <squareup/subzero/internal.pb.h> |
| 14 | + |
| 15 | +// Helper which returns the size of a buffer that would be needed to hold the serialized version of the given |
| 16 | +// protobuf structure, assuming that pb_encode_delimited() serialization will be used. |
| 17 | +static size_t get_serialized_proto_struct_size(const pb_field_t fields[], const void* const proto_struct) { |
| 18 | + pb_ostream_t stream = PB_OSTREAM_SIZING; |
| 19 | + if (!pb_encode_delimited(&stream, fields, proto_struct)) { |
| 20 | + ERROR("%s: pb_encode_delimited() failed: %s", __func__, PB_GET_ERROR(&stream)); |
| 21 | + return 0; |
| 22 | + } |
| 23 | + return stream.bytes_written; |
| 24 | +} |
| 25 | + |
| 26 | +// Serializes the given protobuf structure to the given buffer of the given size, using pb_encode_delimited(). |
| 27 | +// Returns true on success or false on failure. |
| 28 | +// Caller brings their own buffer memory, this function does not allocate. |
| 29 | +static bool serialize_proto_struct_to_buffer( |
| 30 | + pb_byte_t* const buffer, |
| 31 | + const size_t buffer_size, |
| 32 | + const pb_field_t fields[], |
| 33 | + const void* const proto_struct) { |
| 34 | + pb_ostream_t ostream = pb_ostream_from_buffer(buffer, buffer_size); |
| 35 | + if (!pb_encode_delimited(&ostream, fields, proto_struct)) { |
| 36 | + ERROR("%s: pb_encode_delimited() failed: %s", __func__, PB_GET_ERROR(&ostream)); |
| 37 | + return false; |
| 38 | + } |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +// Deserializes the given buffer into the given protobuf structure, using pb_decode_delimited(). |
| 43 | +// Returns true on success or false on failure. |
| 44 | +// Caller brings their own protobuf structure, this function does not allocate. |
| 45 | +static bool deserialize_proto_struct_from_buffer( |
| 46 | + const pb_byte_t* const buffer, |
| 47 | + const size_t buffer_size, |
| 48 | + const pb_field_t fields[], |
| 49 | + void* const proto_struct) { |
| 50 | + pb_istream_t istream = pb_istream_from_buffer(buffer, buffer_size); |
| 51 | + if (!pb_decode_delimited(&istream, fields, proto_struct)) { |
| 52 | + ERROR("%s: pb_decode_delimited() failed: %s", __func__, PB_GET_ERROR(&istream)); |
| 53 | + return false; |
| 54 | + } |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Helper for checking assumptions in the gnarly protobuf mangling code in verify_rpc_oversized_message_rejected(). |
| 60 | + */ |
| 61 | +static bool check_byte_equals(const char* parent_func, const pb_byte_t* const buf, size_t idx, pb_byte_t expected_val) { |
| 62 | + const pb_byte_t actual_val = buf[idx]; |
| 63 | + if (actual_val != expected_val) { |
| 64 | + ERROR( |
| 65 | + "%s: buf[%zu] contains an unexpected value: %hhu, expected: %hhu", parent_func, idx, actual_val, expected_val); |
| 66 | + return false; |
| 67 | + } |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +static pb_byte_t request_buffer[256] = { 0 }; |
| 72 | +static pb_byte_t response_buffer[256] = { 0 }; |
| 73 | + |
| 74 | +int verify_rpc_oversized_message_rejected(void) { |
| 75 | + int result = 0; |
| 76 | + |
| 77 | + // Construct an initial InternalCommandRequest which holds an InitWallet command |
| 78 | + // with a maximum-allowed-length random_bytes field. |
| 79 | + InternalCommandRequest cmd = InternalCommandRequest_init_default; |
| 80 | + cmd.version = VERSION; |
| 81 | + cmd.wallet_id = 1; // dummy value |
| 82 | + cmd.which_command = InternalCommandRequest_InitWallet_tag; |
| 83 | + static_assert( |
| 84 | + sizeof(cmd.command.InitWallet.random_bytes.bytes) == MASTER_SEED_SIZE, |
| 85 | + "MASTER_SEED_SIZE must equal sizeof(cmd.command.InitWallet.random_bytes.bytes)"); |
| 86 | + cmd.command.InitWallet.random_bytes.size = MASTER_SEED_SIZE; |
| 87 | + random_buffer(cmd.command.InitWallet.random_bytes.bytes, MASTER_SEED_SIZE); |
| 88 | + |
| 89 | + // Compute the size of the serialized struct. |
| 90 | + size_t serialized_size = get_serialized_proto_struct_size(InternalCommandRequest_fields, &cmd); |
| 91 | + if (serialized_size == 0) { |
| 92 | + ERROR("%s: error computing serialized request size", __func__); |
| 93 | + result = -1; |
| 94 | + goto out; |
| 95 | + } |
| 96 | + |
| 97 | + if (serialized_size + 1 > sizeof(request_buffer)) { |
| 98 | + ERROR( |
| 99 | + "%s: sizeof(request_buffer) == %zu but needs to be at least %zu. Modify the code and rebuild.", |
| 100 | + __func__, |
| 101 | + sizeof(request_buffer), |
| 102 | + serialized_size + 1); |
| 103 | + result = -1; |
| 104 | + goto out; |
| 105 | + } |
| 106 | + |
| 107 | + // Serialize the request struct into request_buffer. |
| 108 | + if (!serialize_proto_struct_to_buffer(request_buffer, sizeof(request_buffer), InternalCommandRequest_fields, &cmd)) { |
| 109 | + ERROR("%s: serialize_proto_struct_to_buffer() failed", __func__); |
| 110 | + result = -1; |
| 111 | + goto out; |
| 112 | + } |
| 113 | + |
| 114 | + // Corrupt the message by making the random_bytes field 1 byte longer than the max allowed size. |
| 115 | + // Note that this is a bit fragile and could break if the protobuf definitions inside |
| 116 | + // internal.proto are changed. But if that happens, hopefully this test breaks immediately |
| 117 | + // and can be fixed. Understanding of low-level protobuf serialization is recommended, see |
| 118 | + // https://protobuf.dev/programming-guides/encoding/ for the details (it's not that bad). |
| 119 | + // Basically: |
| 120 | + // serialized_request[0] - varint-encoded leading LEN byte. This is not normally there for binary |
| 121 | + // encoded protobufs, but it's added by nanopb because we are using |
| 122 | + // pb_encode_delimited(). If the message is longer than 127 bytes, this |
| 123 | + // length will actually take more than 1 byte, shifting everything after |
| 124 | + // it by a byte. |
| 125 | + // *** NOTE: WE NEED TO INCREMENT THIS BY 1. *** |
| 126 | + // serialized_request[1] - field id (1 << 3) + tag (0) for field 1 (version). Should equal 8. |
| 127 | + // serialized_request[2..3] - varint-encoded value for field 1. Leave this alone, it's the |
| 128 | + // contents of the 'version' field (210 at the time of writing). If |
| 129 | + // version ever exceeds 16383, this will start taking up an extra byte |
| 130 | + // and shift everything after it by a byte. |
| 131 | + // serialized_request[4] - field id (2 << 3) + tag (0) for field 2 (wallet_id). Should equal 16. |
| 132 | + // serialized_request[5] - varint-encoded value for field 2. Leave this alone, it's the dummy |
| 133 | + // 'wallet' field which we set to 1 above. Should equal 1. |
| 134 | + // serialized_request[6] - field id (5 << 3) + tag (2, for 'LEN') for field 5 (command.InitWallet). |
| 135 | + // Should equal 42. |
| 136 | + // serialized_request[7] - varint-encoded LEN of the InitWalletRequest submessage. |
| 137 | + // Should equal 66. |
| 138 | + // *** NOTE: WE NEED TO INCREMENT THIS BY 1. *** |
| 139 | + // serialized_request[8] - field id (1 << 3) + tag (2, for 'LEN') for field 1 of sub-message. |
| 140 | + // Should equal 10. |
| 141 | + // serialized_request[9] - varint-encoded LEN of field 1 (random_bytes) of sub-message. |
| 142 | + // Should equal 64. |
| 143 | + // *** NOTE: WE NEED TO INCREMENT THIS BY 1. *** |
| 144 | + // serialized_request[10..73] - the contents of the random_bytes field. Should be 64 bytes in length. |
| 145 | + // serialized_request[74] - doesn't exist in the original message. We add an extra data byte here. |
| 146 | + // It can be any value, we arbitrarily choose 0xaa. |
| 147 | + // |
| 148 | + // Let's check the above assumptions to make sure they are correct before proceeding: |
| 149 | + if (!check_byte_equals(__func__, request_buffer, 0, (pb_byte_t) 73)) { |
| 150 | + result = -1; |
| 151 | + goto out; |
| 152 | + } |
| 153 | + if (!check_byte_equals(__func__, request_buffer, 0, (pb_byte_t) (serialized_size - 1))) { |
| 154 | + result = -1; |
| 155 | + goto out; |
| 156 | + } |
| 157 | + if (!check_byte_equals(__func__, request_buffer, 1, (pb_byte_t) ((1 << 3) + 0))) { |
| 158 | + result = -1; |
| 159 | + goto out; |
| 160 | + } |
| 161 | + // The 'cmd.version' field is varint-encoded into 2 little-endian bytes: |
| 162 | + // ... First byte contains least-significant 7 bits + highest bit set to indicate that there's more data. |
| 163 | + if (!check_byte_equals(__func__, request_buffer, 2, (pb_byte_t) ((cmd.version & 0x7f) | 0x80))) { |
| 164 | + result = -1; |
| 165 | + goto out; |
| 166 | + } |
| 167 | + // ... Second byte contains the next 1-7 bits + highest bit unset to indicate that there's no more data. |
| 168 | + if (!check_byte_equals(__func__, request_buffer, 3, (pb_byte_t) (cmd.version >> 7))) { |
| 169 | + result = -1; |
| 170 | + goto out; |
| 171 | + } |
| 172 | + if (!check_byte_equals(__func__, request_buffer, 4, (pb_byte_t) ((2 << 3) + 0))) { |
| 173 | + result = -1; |
| 174 | + goto out; |
| 175 | + } |
| 176 | + if (!check_byte_equals(__func__, request_buffer, 5, (pb_byte_t) cmd.wallet_id)) { |
| 177 | + result = -1; |
| 178 | + goto out; |
| 179 | + } |
| 180 | + if (!check_byte_equals(__func__, request_buffer, 6, (pb_byte_t) ((5 << 3) + 2))) { |
| 181 | + result = -1; |
| 182 | + goto out; |
| 183 | + } |
| 184 | + if (!check_byte_equals(__func__, request_buffer, 7, (pb_byte_t) (MASTER_SEED_SIZE + 2))) { |
| 185 | + result = -1; |
| 186 | + goto out; |
| 187 | + } |
| 188 | + if (!check_byte_equals(__func__, request_buffer, 8, (pb_byte_t) ((1 << 3) + 2))) { |
| 189 | + result = -1; |
| 190 | + goto out; |
| 191 | + } |
| 192 | + if (!check_byte_equals(__func__, request_buffer, 9, (pb_byte_t) MASTER_SEED_SIZE)) { |
| 193 | + result = -1; |
| 194 | + goto out; |
| 195 | + } |
| 196 | + |
| 197 | + request_buffer[0]++; // increment leading LEN byte |
| 198 | + request_buffer[7]++; // increment LEN byte for top-level field 5 |
| 199 | + request_buffer[9]++; // increment LEN byte for nested field 1 |
| 200 | + request_buffer[serialized_size] = 0xaa; // set the last byte to an arbitrary value |
| 201 | + serialized_size++; // increment serialized_size since we added a byte of data |
| 202 | + |
| 203 | + // Create a stream which will read from the corrupted serialized buffer. |
| 204 | + pb_istream_t istream = pb_istream_from_buffer(request_buffer, serialized_size); |
| 205 | + // Create a stream which will write to the response buffer. |
| 206 | + pb_ostream_t ostream = pb_ostream_from_buffer(response_buffer, sizeof(response_buffer)); |
| 207 | + |
| 208 | + // Now that we have a serialized buffer, try to pass it to handle_incoming_message(). |
| 209 | + // This should fail because the InitWallet.random_bytes field has a length of 65 bytes, |
| 210 | + // but nanopb options set the limit for this field at 64 bytes. |
| 211 | + // |
| 212 | + // NOTE: when building for nCipher, there are command hooks that would reject the command |
| 213 | + // because it's missing the tickets for key use authorization. But this doesn't matter for |
| 214 | + // this test case, because the protobuf parsing happens before that and fails first. |
| 215 | + ERROR("(next line is expected to show red text...)"); |
| 216 | + |
| 217 | + handle_incoming_message(&istream, &ostream); // <---- this is the actual function under test |
| 218 | + |
| 219 | + // Extract the response structure from the serialized_response buffer. It should be an error. |
| 220 | + const size_t response_size = ostream.bytes_written; |
| 221 | + if (response_size == 0) { |
| 222 | + ERROR("%s: no response received from handle_incoming_message(): %s", __func__, PB_GET_ERROR(&ostream)); |
| 223 | + result = -1; |
| 224 | + goto out; |
| 225 | + } |
| 226 | + |
| 227 | + // note: no need to initialize the response, static bool deserialize_proto_struct_from_buffer() does it via |
| 228 | + // pb_decode_delimited(). |
| 229 | + InternalCommandResponse response; |
| 230 | + if (!deserialize_proto_struct_from_buffer( |
| 231 | + response_buffer, response_size, InternalCommandResponse_fields, &response)) { |
| 232 | + ERROR("%s: deserialize_proto_struct_from_buffer() failed", __func__); |
| 233 | + result = -1; |
| 234 | + goto out; |
| 235 | + } |
| 236 | + |
| 237 | + // Check that the response contains an error. |
| 238 | + if (response.which_response != InternalCommandResponse_Error_tag) { |
| 239 | + ERROR( |
| 240 | + "%s: wrong response tag: %d, expected: %d", |
| 241 | + __func__, |
| 242 | + (int) response.which_response, |
| 243 | + (int) InternalCommandResponse_Error_tag); |
| 244 | + result = -1; |
| 245 | + goto out; |
| 246 | + } |
| 247 | + |
| 248 | + // Check that the error response contains the expected error code. |
| 249 | + if (response.response.Error.code != Result_COMMAND_DECODE_FAILED) { |
| 250 | + ERROR( |
| 251 | + "%s: wrong response error code: %d, expected: %d", |
| 252 | + __func__, |
| 253 | + (int) response.response.Error.code, |
| 254 | + (int) Result_COMMAND_DECODE_FAILED); |
| 255 | + result = -1; |
| 256 | + goto out; |
| 257 | + } |
| 258 | + |
| 259 | + // Check that the error response contains some message. |
| 260 | + if (!response.response.Error.has_message) { |
| 261 | + ERROR("%s: error response does not contain a 'message' field", __func__); |
| 262 | + result = -1; |
| 263 | + goto out; |
| 264 | + } |
| 265 | + |
| 266 | + // Check that the error response contains the expected message. |
| 267 | + if (0 != strcmp("Decode Input failed: bytes overflow", response.response.Error.message)) { |
| 268 | + ERROR("%s: error response contains unexpected message: %s", __func__, response.response.Error.message); |
| 269 | + result = -1; |
| 270 | + goto out; |
| 271 | + } |
| 272 | + |
| 273 | +out: |
| 274 | + memzero(request_buffer, sizeof(request_buffer)); |
| 275 | + memzero(response_buffer, sizeof(response_buffer)); |
| 276 | + |
| 277 | + if (result == 0) { |
| 278 | + INFO("%s: ok", __func__); |
| 279 | + } |
| 280 | + return result; |
| 281 | +} |
0 commit comments