Skip to content

Commit

Permalink
Fixes to potential buffer overflows on received data
Browse files Browse the repository at this point in the history
  • Loading branch information
debevv committed Feb 8, 2024
1 parent dde2653 commit 0234a56
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nanomodbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ nmbs_error recv_read_file_record_res(nmbs_t* nmbs, uint16_t* registers, uint16_t
return err;

uint8_t response_size = get_1(nmbs);
if (response_size > 245) {
if (response_size > 250) {
return NMBS_ERROR_INVALID_RESPONSE;
}

Expand Down Expand Up @@ -1798,6 +1798,7 @@ nmbs_error nmbs_read_file_record(nmbs_t* nmbs, uint16_t file_number, uint16_t re
if (record_number > 0x270F)
return NMBS_ERROR_INVALID_ARGUMENT;

// In expected response: max PDU length = 253, assuming a single file request, (253 - 1 - 1 - 1 - 1) / 2 = 124
if (count > 124)
return NMBS_ERROR_INVALID_ARGUMENT;

Expand Down
2 changes: 1 addition & 1 deletion tests/nanomodbus_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ void test_fc20(nmbs_transport transport) {
expect(registers[2] == 0xAA55);
expect(registers[3] == 0xFFFF);

check(nmbs_read_file_record(&CLIENT, 255, 9999, registers, 120));
check(nmbs_read_file_record(&CLIENT, 255, 9999, registers, 124));
expect(registers[123] == 42);

stop_client_and_server();
Expand Down

0 comments on commit 0234a56

Please sign in to comment.