Fix [bug#80] npdu_decode via deprecation (#447)

* Fix [bug#80] npdu_decode via deprecation

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-07-10 17:27:10 -05:00
committed by GitHub
parent 13c85db4cb
commit 4efaf097c0
23 changed files with 81 additions and 67 deletions
+1
View File
@@ -16,6 +16,7 @@ The git repositories are hosted at the following sites:
### Security
- Fix [bug#80] npdu_decode via deprecation (#447)
- Fix [bug#79] out of bounds jump in h_apdu.c:apdu_handler (#446)
### Fixed
+1 -1
View File
@@ -185,7 +185,7 @@ static void My_NPDU_Handler(BACNET_ADDRESS *src, /* source address */
BACNET_ADDRESS dest = { 0 };
BACNET_NPDU_DATA npdu_data = { 0 };
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (npdu_data.network_layer_message) {
if (apdu_offset <= pdu_len) {
My_Router_Handler(src, &npdu_data, &pdu[apdu_offset],
+1 -1
View File
@@ -162,7 +162,7 @@ static void mstp_monitor_i_am(uint8_t mac, uint8_t *pdu, uint16_t pdu_len)
if (pdu[0] == BACNET_PROTOCOL_VERSION) {
MSTP_Fill_BACnet_Address(&src, mac);
apdu_offset = npdu_decode(&pdu[0], &dest, &src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, &src, &npdu_data);
if ((!npdu_data.network_layer_message) && (apdu_offset > 0) &&
(apdu_offset < pdu_len) && (src.net == 0)) {
apdu_len = pdu_len - apdu_offset;
+1 -1
View File
@@ -149,7 +149,7 @@ static void My_NPDU_Handler(BACNET_ADDRESS *src, /* source address */
BACNET_ADDRESS dest = { 0 };
BACNET_NPDU_DATA npdu_data = { 0 };
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (npdu_data.network_layer_message) {
My_Router_Handler(src, &npdu_data, &pdu[apdu_offset],
(uint16_t)(pdu_len - apdu_offset));
+1 -1
View File
@@ -945,7 +945,7 @@ static void my_routing_npdu_handler(
if (!pdu) {
/* no packet */
} else if (pdu[0] == BACNET_PROTOCOL_VERSION) {
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (apdu_offset <= 0) {
fprintf(stderr, "NPDU: Decoding failed; Discarded!\n");
} else if (npdu_data.network_layer_message) {
+1 -1
View File
@@ -943,7 +943,7 @@ static void my_routing_npdu_handler(
if (!pdu) {
/* no packet */
} else if (pdu[0] == BACNET_PROTOCOL_VERSION) {
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (apdu_offset <= 0) {
fprintf(stderr, "NPDU: Decoding failed; Discarded!\n");
} else if (npdu_data.network_layer_message) {
+2 -1
View File
@@ -766,7 +766,8 @@ uint16_t process_msg(BACMSG *msg, MSG_DATA *data, uint8_t **buff)
memmove(data, msg->data, sizeof(MSG_DATA));
apdu_offset = npdu_decode(data->pdu, &data->dest, &addr, &npdu_data);
apdu_offset = bacnet_npdu_decode(data->pdu, data->pdu_len, &data->dest,
&addr, &npdu_data);
apdu_len = data->pdu_len - apdu_offset;
srcport = find_snet(msg->origin);
+2 -1
View File
@@ -45,7 +45,8 @@ uint16_t process_network_message(BACMSG *msg, MSG_DATA *data, uint8_t **buff)
memmove(data, msg->data, sizeof(MSG_DATA));
apdu_offset = npdu_decode(data->pdu, &data->dest, NULL, &npdu_data);
apdu_offset = bacnet_npdu_decode(data->pdu, data->pdu_len, &data->dest,
NULL, &npdu_data);
apdu_len = data->pdu_len - apdu_offset;
srcport = find_snet(msg->origin);
+1 -1
View File
@@ -134,7 +134,7 @@ static void My_NPDU_Handler(BACNET_ADDRESS *src, /* source address */
BACNET_ADDRESS dest = { 0 };
BACNET_NPDU_DATA npdu_data = { 0 };
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (npdu_data.network_layer_message) {
My_Router_Handler(src, &npdu_data, &pdu[apdu_offset],
(uint16_t)(pdu_len - apdu_offset));
+1 -1
View File
@@ -132,7 +132,7 @@ static void My_NPDU_Handler(BACNET_ADDRESS *src, /* source address */
BACNET_ADDRESS dest = { 0 };
BACNET_NPDU_DATA npdu_data = { 0 };
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (npdu_data.network_layer_message) {
My_Router_Handler(src, &npdu_data, &pdu[apdu_offset],
(uint16_t)(pdu_len - apdu_offset));
+4 -3
View File
@@ -245,8 +245,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -263,7 +263,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+4 -3
View File
@@ -246,8 +246,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -264,7 +264,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+6 -5
View File
@@ -311,7 +311,8 @@ static void *dlmstp_master_fsm_task(void *pArg)
/* do nothing while immediate transitioning */
run_loop = MSTP_Master_Node_FSM(&MSTP_Port);
pthread_mutex_lock(&Thread_Mutex);
if (!run_thread) run_loop = false;
if (!run_thread)
run_loop = false;
pthread_mutex_unlock(&Thread_Mutex);
}
} else if (MSTP_Port.This_Station < 255) {
@@ -438,8 +439,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
#if PRINT_ENABLED
fprintf(stderr,
@@ -467,7 +468,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
#if PRINT_ENABLED
fprintf(stderr,
@@ -825,4 +827,3 @@ int main(int argc, char *argv[])
return 0;
}
#endif
+4 -3
View File
@@ -411,8 +411,8 @@ bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
#if PRINT_ENABLED
fprintf(stderr,
@@ -440,7 +440,8 @@ bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
#if PRINT_ENABLED
fprintf(stderr,
+4 -3
View File
@@ -567,8 +567,8 @@ static bool mstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -585,7 +585,8 @@ static bool mstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+4 -3
View File
@@ -567,8 +567,8 @@ static bool mstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -585,7 +585,8 @@ static bool mstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+8 -7
View File
@@ -247,8 +247,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -265,7 +265,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
@@ -582,8 +583,8 @@ static void MSTP_Receive_Frame_FSM(void)
source = SourceAddress;
destination = DestinationAddress;
frame = FrameType;
Frame_Rx_Callback(source, destination,
frame, InputBuffer, DataLength);
Frame_Rx_Callback(source, destination, frame,
InputBuffer, DataLength);
}
/* wait for the start of the next frame. */
Receive_State = MSTP_RECEIVE_STATE_IDLE;
@@ -674,8 +675,8 @@ static void MSTP_Receive_Frame_FSM(void)
source = SourceAddress;
destination = DestinationAddress;
frame = FrameType;
Frame_Rx_Callback(source, destination,
frame, InputBuffer, DataLength);
Frame_Rx_Callback(source, destination, frame,
InputBuffer, DataLength);
}
} else {
+4 -3
View File
@@ -323,8 +323,8 @@ bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -340,7 +340,8 @@ bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
request.service_choice = request_pdu[offset + 3];
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+4 -4
View File
@@ -308,8 +308,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = (uint16_t)npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = (uint16_t)bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -325,8 +325,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
request.service_choice = request_pdu[offset + 3];
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = (uint16_t)npdu_decode(
&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = (uint16_t)bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+4 -3
View File
@@ -239,8 +239,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = npdu_decode(
&request_pdu[0], NULL, &request.address, &request.npdu_data);
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -257,7 +257,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t *request_pdu,
/* decode the reply data */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
+1 -1
View File
@@ -281,7 +281,7 @@ void routing_npdu_handler(
/* only handle the version that we know how to handle */
if (pdu[0] == BACNET_PROTOCOL_VERSION) {
apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data);
apdu_offset = bacnet_npdu_decode(pdu, pdu_len, &dest, src, &npdu_data);
if (apdu_offset <= 0) {
debug_printf("NPDU: Decoding failed; Discarded!\n");
} else if (npdu_data.network_layer_message) {
+2
View File
@@ -27,6 +27,7 @@
#include <stdbool.h>
#include <stdint.h>
#include "bacnet/bacnet_stack_exports.h"
#include "bacnet/basic/sys/platform.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacenum.h"
@@ -98,6 +99,7 @@ extern "C" {
BACNET_NPDU_DATA * dest,
BACNET_NPDU_DATA * src);
BACNET_STACK_DEPRECATED("Use bacnet_npdu_decode() instead")
BACNET_STACK_EXPORT
int npdu_decode(
uint8_t * npdu,
+3 -3
View File
@@ -42,7 +42,7 @@ static void test_NPDU_Network(void)
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data);
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
zassert_not_equal(npdu_len, 0, NULL);
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);
@@ -95,7 +95,7 @@ static void testNPDU2(void)
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data);
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
zassert_not_equal(npdu_len, 0, NULL);
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);
@@ -160,7 +160,7 @@ static void testNPDU1(void)
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data);
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
zassert_not_equal(npdu_len, 0, NULL);
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);