|
14 | 14 | #include "mgmt/mgmt.h" |
15 | 15 | #include "smp/smp.h" |
16 | 16 |
|
17 | | -static int |
18 | | -smp_align4(int x) |
19 | | -{ |
20 | | - int rem; |
21 | | - |
22 | | - rem = x % 4; |
23 | | - if (rem == 0) { |
24 | | - return x; |
25 | | - } else { |
26 | | - return x - rem + 4; |
27 | | - } |
28 | | -} |
29 | | - |
30 | 17 | /** |
31 | 18 | * Converts a request opcode to its corresponding response opcode. |
32 | 19 | */ |
@@ -289,7 +276,8 @@ smp_on_err(struct smp_streamer *streamer, const struct mgmt_hdr *req_hdr, |
289 | 276 | * @param streamer The streamer to use for reading, writing, and transmitting. |
290 | 277 | * @param req A buffer containing the request packet. |
291 | 278 | * |
292 | | - * @return 0 on success, MGMT_ERR_ECORRUPT if buffer starts with non SMP data header, |
| 279 | + * @return 0 on success, MGMT_ERR_ECORRUPT if buffer starts with non SMP data header |
| 280 | + * or there is not enough bytes to process header, |
293 | 281 | * or other MGMT_ERR_[...] code on failure. |
294 | 282 | */ |
295 | 283 | int |
@@ -318,6 +306,12 @@ smp_process_request_packet(struct smp_streamer *streamer, void *req) |
318 | 306 | valid_hdr = true; |
319 | 307 | } |
320 | 308 | mgmt_ntoh_hdr(&req_hdr); |
| 309 | + /* Does buffer contain whole message? */ |
| 310 | + if (streamer->mgmt_stmr.reader->message_size < (req_hdr.nh_len + MGMT_HDR_SIZE)) { |
| 311 | + rc = MGMT_ERR_ECORRUPT; |
| 312 | + break; |
| 313 | + } |
| 314 | + |
321 | 315 | mgmt_streamer_trim_front(&streamer->mgmt_stmr, req, MGMT_HDR_SIZE); |
322 | 316 |
|
323 | 317 | rsp = mgmt_streamer_alloc_rsp(&streamer->mgmt_stmr, req); |
@@ -345,7 +339,7 @@ smp_process_request_packet(struct smp_streamer *streamer, void *req) |
345 | 339 | } |
346 | 340 |
|
347 | 341 | /* Trim processed request to free up space for subsequent responses. */ |
348 | | - mgmt_streamer_trim_front(&streamer->mgmt_stmr, req, smp_align4(req_hdr.nh_len)); |
| 342 | + mgmt_streamer_trim_front(&streamer->mgmt_stmr, req, req_hdr.nh_len); |
349 | 343 |
|
350 | 344 | cmd_done_arg.err = MGMT_ERR_EOK; |
351 | 345 | mgmt_evt(MGMT_EVT_OP_CMD_DONE, req_hdr.nh_group, req_hdr.nh_id, |
|
0 commit comments