iopsys-feed/obuspa/patches/0001-validate-mtp-topic.patch
2022-09-22 16:32:42 +05:30

140 lines
6.1 KiB
Diff

diff --git a/src/core/device.h b/src/core/device.h
index adf8fa6..072f953 100644
--- a/src/core/device.h
+++ b/src/core/device.h
@@ -111,6 +111,7 @@ typedef struct
// Following member variables only set if USP message was received over MQTT
int mqtt_instance;
char *mqtt_topic; // only set if reply_to was specified in the received MQTT packet
+ char *mqtt_topic_recv;
// Following member variables only set if USP message was received over CoAP
@@ -252,7 +253,7 @@ void DEVICE_MTP_NotifyMqttConnDeleted(int mqtt_instance);
int DEVICE_MTP_ValidateMqttReference(dm_req_t *req, char *value);
void DEVICE_CONTROLLER_SetRolesFromMqtt(int mqtt_instance, ctrust_role_t role);
char *DEVICE_CONTROLLER_GetControllerTopic(int mqtt_instance);
-
+int validate_controller_topic_by_endpointid(char *endpointid, mtp_protocol_t proto, char *recv_topic);
//------------------------------------------------------------------------------
// Tables used to convert to/from an enumeration to/from a string
extern const enum_entry_t mtp_protocols[kMtpProtocol_Max];
diff --git a/src/core/device_controller.c b/src/core/device_controller.c
index 62c803f..103388d 100755
--- a/src/core/device_controller.c
+++ b/src/core/device_controller.c
@@ -239,6 +239,41 @@ int Async_E2ESessionReset(dm_req_t *req, kv_vector_t *input_args, int request);
extern const enum_entry_t e2e_session_modes[kE2EMode_Max];
#endif
+
+int validate_controller_topic_by_endpointid(char *endpointid, mtp_protocol_t proto, char *recv_topic)
+{
+ controller_t *cont = FindEnabledControllerByEndpointId(endpointid);
+ if (cont == NULL)
+ {
+ USP_LOG_Error("not able to find the controller from endpointid [%s]", endpointid);
+ return USP_ERR_PERMISSION_DENIED;
+ }
+#ifdef ENABLE_MQTT
+ if (proto == kMtpProtocol_MQTT)
+ {
+ controller_mtp_t *mtp = FindFirstEnabledMtp(cont, proto);
+
+ if (mtp == NULL) {
+ USP_LOG_Error("Not able to find mtp[%d] for endpointid [%s]", proto, endpointid);
+ return USP_ERR_REQUEST_DENIED;
+ }
+
+ if (mtp->protocol != proto) {
+ USP_LOG_Error("No matching mtp[%d] for endpointid [%s]", proto, endpointid);
+ return USP_ERR_REQUEST_DENIED;
+ }
+
+ char *response_topic = DEVICE_MTP_GetAgentMqttResponseTopic(mtp->mqtt_connection_instance);
+ if (response_topic && strcmp(response_topic, recv_topic) != 0 ) {
+ USP_LOG_Error("Controller response topic[%s] and recv topic[%s] mismatch, probably a spoof message", response_topic, recv_topic);
+ return USP_ERR_PERMISSION_DENIED;
+ }
+ }
+#endif
+
+ return USP_ERR_OK;
+}
+
/*********************************************************************//**
**
** DEVICE_CONTROLLER_Init
diff --git a/src/core/dm_exec.c b/src/core/dm_exec.c
index c0b95d8..6eb51e2 100755
--- a/src/core/dm_exec.c
+++ b/src/core/dm_exec.c
@@ -624,6 +624,7 @@ void DM_EXEC_PostUspRecord(unsigned char *pbuf, int pbuf_len, ctrust_role_t role
pur->mtp_reply_to.coap_encryption = mrt->coap_encryption;
pur->mtp_reply_to.coap_reset_session_hint = mrt->coap_reset_session_hint;
pur->mtp_reply_to.mqtt_topic = USP_STRDUP(mrt->mqtt_topic);
+ pur->mtp_reply_to.mqtt_topic_recv = USP_STRDUP(mrt->mqtt_topic_recv);
pur->mtp_reply_to.mqtt_instance = mrt->mqtt_instance;
pur->mtp_reply_to.wsclient_cont_instance = mrt->wsclient_cont_instance;
pur->mtp_reply_to.wsclient_mtp_instance = mrt->wsclient_mtp_instance;
@@ -1144,6 +1145,7 @@ void ProcessMessageQueueSocketActivity(socket_set_t *set)
USP_SAFE_FREE(mrt->coap_resource);
USP_SAFE_FREE(mrt->mqtt_topic);
USP_SAFE_FREE(mrt->cont_endpoint_id);
+ USP_SAFE_FREE(mrt->mqtt_topic_recv);
break;
#ifndef DISABLE_STOMP
diff --git a/src/core/mqtt.c b/src/core/mqtt.c
index 7dba9f4..3ee9c97 100644
--- a/src/core/mqtt.c
+++ b/src/core/mqtt.c
@@ -3141,6 +3141,7 @@ void ReceiveMqttMessage(mqtt_client_t *client, const struct mosquitto_message *m
mrt.mqtt_topic = response_topic;
}
+ mrt.mqtt_topic_recv = message->topic;
// Message may not be valid USP
DM_EXEC_PostUspRecord(message->payload, message->payloadlen, client->role, &mrt);
}
diff --git a/src/core/msg_handler.c b/src/core/msg_handler.c
index ce67626..4af9ade 100644
--- a/src/core/msg_handler.c
+++ b/src/core/msg_handler.c
@@ -118,7 +118,7 @@ static enum_entry_t mtp_content_types[] = {
//------------------------------------------------------------------------------
// Forward declarations. Note these are not static, because we need them in the symbol table for USP_LOG_Callstack() to show them
int HandleUspMessage(Usp__Msg *usp, char *controller_endpoint, mtp_reply_to_t *mrt);
-int ValidateUspRecord(UspRecord__Record *rec);
+int ValidateUspRecord(UspRecord__Record *rec, mtp_reply_to_t *mrt);
char *MtpSendItemToString(mtp_send_item_t *msi);
void CacheControllerRoleForCurMsg(char *endpoint_id, ctrust_role_t role, mtp_protocol_t protocol);
int QueueUspNoSessionRecord(usp_send_item_t *usi, char *endpoint_id, char *usp_msg_id, mtp_reply_to_t *mrt, time_t expiry_time);
@@ -172,7 +172,7 @@ int MSG_HANDLER_HandleBinaryRecord(unsigned char *pbuf, int pbuf_len, ctrust_rol
#endif
// Exit if USP record failed validation
- err = ValidateUspRecord(rec);
+ err = ValidateUspRecord(rec, mrt);
if (err != USP_ERR_OK)
{
goto exit;
@@ -725,7 +725,7 @@ exit:
** \return USP_ERR_OK if record is valid
**
**************************************************************************/
-int ValidateUspRecord(UspRecord__Record *rec)
+int ValidateUspRecord(UspRecord__Record *rec, mtp_reply_to_t *mrt)
{
char *endpoint_id;
@@ -806,6 +806,9 @@ int ValidateUspRecord(UspRecord__Record *rec)
return USP_ERR_REQUEST_DENIED;
}
+#if OBUSPA_CONTROLLER_MTP_VERIFY
+ return validate_controller_topic_by_endpointid(rec->from_id, mrt->protocol, mrt->mqtt_topic_recv);
+#endif
// If the code gets here, then the USP record passed validation, and the encapsulated USP message may be processed
return USP_ERR_OK;
}