From fc813ab0ba86c85d4b09c08ca0c273dd54b2a2fa Mon Sep 17 00:00:00 2001 From: Phil Day Date: Fri, 22 Nov 2024 17:24:36 +0000 Subject: [PATCH 1/4] Add support for publishing retained messages --- include/mqtt.h | 3 ++- lib/mqtt/mqtt.cc | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index 496da68..e01eefa 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -170,7 +170,8 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t, const char *topic, size_t topicLength, const void *payload, - size_t payloadLength); + size_t payloadLength, + bool retain = false); /** * Subscribe on a given MQTT connection. diff --git a/lib/mqtt/mqtt.cc b/lib/mqtt/mqtt.cc index 6392680..35f73c2 100644 --- a/lib/mqtt/mqtt.cc +++ b/lib/mqtt/mqtt.cc @@ -469,12 +469,15 @@ namespace "are not set."); // The payload and topic are only valid within the - // context of the callback: make them read-only and - // non-capturable. + // context of the callback: make them read-only, + // non-capturable, and limits to the length of the + // topic and payload. Capability topic{publishInfo->pTopicName}; Capability payload{publishInfo->pPayload}; topic.permissions() &= CHERI::Permission::Load; + topic.bounds() = publishInfo->topicNameLength; payload.permissions() &= CHERI::Permission::Load; + payload.bounds() = publishInfo->payloadLength; publishCallback(topic, publishInfo->topicNameLength, @@ -815,7 +818,8 @@ int mqtt_publish(Timeout *t, const char *topic, size_t topicLength, const void *payload, - size_t payloadLength) + size_t payloadLength, + bool retain) { if (!CHERI::check_pointer(topic, topicLength)) { @@ -872,6 +876,7 @@ int mqtt_publish(Timeout *t, publishInfo.topicNameLength = topicLength; publishInfo.pPayload = payload; publishInfo.payloadLength = payloadLength; + publishInfo.retain = retain; // Packet ID is needed for QoS > 0. int packetId = MQTT_GetPacketId(coreMQTTContext); From 2eaff1afe976653fff94834862c12f0d16b2734c Mon Sep 17 00:00:00 2001 From: Phil Day Date: Wed, 27 Nov 2024 19:03:17 +0000 Subject: [PATCH 2/4] add description of retain flag --- include/mqtt.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/mqtt.h b/include/mqtt.h index e01eefa..c8fb4ad 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -181,6 +181,14 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t, * include the zero terminator and must be at least one-character long). * * `qos` indicates the level of QoS (0, 1, or 2). + * + * `retain` indicates whether the message should be published as retained or + * not. The broker stores the last retained message and the corresponding QoS + * for that topic. Each client that subscribes to a topic pattern that matches + * the topic of the retained message receives the retained message immediately + * after they subscribe. The broker stores only one retained message per topic. + * Retained messages are cleared by publishing a zero length message with the + * retain flag set. * * The filter buffer must remain valid during the execution of this function. * If the caller frees it during the execution of this function, the subscribe From 0589d27a8a35b8bbb917aed9532ba56a7701d0c4 Mon Sep 17 00:00:00 2001 From: Phil Day Date: Thu, 28 Nov 2024 09:06:28 +0000 Subject: [PATCH 3/4] fix format error --- include/mqtt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mqtt.h b/include/mqtt.h index c8fb4ad..1fd949a 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -181,7 +181,7 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t, * include the zero terminator and must be at least one-character long). * * `qos` indicates the level of QoS (0, 1, or 2). - * + * * `retain` indicates whether the message should be published as retained or * not. The broker stores the last retained message and the corresponding QoS * for that topic. Each client that subscribes to a topic pattern that matches From 5d0b38b272a0ea30d1cf542bb3a709c9d2879cc5 Mon Sep 17 00:00:00 2001 From: Phil Day Date: Thu, 28 Nov 2024 21:00:10 +0000 Subject: [PATCH 4/4] Move comment to the right function --- include/mqtt.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index 1fd949a..218e882 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -137,6 +137,14 @@ int __cheri_compartment("MQTT") * * `qos` indicates the level of QoS (0, 1, or 2). * + * `retain` indicates whether the message should be published as retained or + * not. The broker stores the last retained message and the corresponding QoS + * for that topic. Each client that subscribes to a topic pattern that matches + * the topic of the retained message receives the retained message immediately + * after they subscribe. The broker stores only one retained message per topic. + * Retained messages are cleared by publishing a zero length message with the + * retain flag set. + * * Both the topic and payload buffers must remain valid during the execution of * this function. If the caller frees them during the execution of this * function, the publish may leak application data to the broker through the @@ -182,14 +190,6 @@ int __cheri_compartment("MQTT") mqtt_publish(Timeout *t, * * `qos` indicates the level of QoS (0, 1, or 2). * - * `retain` indicates whether the message should be published as retained or - * not. The broker stores the last retained message and the corresponding QoS - * for that topic. Each client that subscribes to a topic pattern that matches - * the topic of the retained message receives the retained message immediately - * after they subscribe. The broker stores only one retained message per topic. - * Retained messages are cleared by publishing a zero length message with the - * retain flag set. - * * The filter buffer must remain valid during the execution of this function. * If the caller frees it during the execution of this function, the subscribe * may leak application data to the broker through the filter.