From d7274ac5f0f5429a5d937365c41cdda3484770c4 Mon Sep 17 00:00:00 2001 From: Nate Weibley Date: Mon, 12 Oct 2015 13:36:37 -0400 Subject: [PATCH 1/2] Enable hash check of used parameters to verify integrity of GCS local copy --- src/modules/mavlink/mavlink_parameters.cpp | 30 +++++++++++++++++----- src/modules/systemlib/param/param.c | 24 +++++++++++++++++ src/modules/systemlib/param/param.h | 7 +++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/modules/mavlink/mavlink_parameters.cpp b/src/modules/mavlink/mavlink_parameters.cpp index 414b7ee96f..b005f3d80d 100644 --- a/src/modules/mavlink/mavlink_parameters.cpp +++ b/src/modules/mavlink/mavlink_parameters.cpp @@ -44,6 +44,8 @@ #include "mavlink_parameters.h" #include "mavlink_main.h" +#define HASH_PARAM "_HASH_CHECK" + MavlinkParametersManager::MavlinkParametersManager(Mavlink *mavlink) : MavlinkStream(mavlink), _send_all_index(-1), _rc_param_map_pub(nullptr), @@ -121,13 +123,27 @@ MavlinkParametersManager::handle_message(const mavlink_message_t *msg) /* when no index is given, loop through string ids and compare them */ if (req_read.param_index < 0) { - /* local name buffer to enforce null-terminated string */ - char name[MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN + 1]; - strncpy(name, req_read.param_id, MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN); - /* enforce null termination */ - name[MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN] = '\0'; - /* attempt to find parameter and send it */ - send_param(param_find_no_notification(name)); + if (strncmp(req_read.param_id, HASH_PARAM, MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN) == 0) { + /* return hash check for cached params */ + uint32_t hash = param_hash_check(); + + /* build the one-off response message */ + mavlink_param_value_t msg; + msg.param_count = param_count_used(); + msg.param_index = -1; + strncpy(msg.param_id, HASH_PARAM, MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN); + msg.param_type = MAV_PARAM_TYPE_UINT32; + memcpy(&msg.param_value, &hash, sizeof(hash)); + _mavlink->send_message(MAVLINK_MSG_ID_PARAM_VALUE, &msg); + } else { + /* local name buffer to enforce null-terminated string */ + char name[MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN + 1]; + strncpy(name, req_read.param_id, MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN); + /* enforce null termination */ + name[MAVLINK_MSG_PARAM_VALUE_FIELD_PARAM_ID_LEN] = '\0'; + /* attempt to find parameter and send it */ + send_param(param_find_no_notification(name)); + } } else { /* when index is >= 0, send this parameter again */ diff --git a/src/modules/systemlib/param/param.c b/src/modules/systemlib/param/param.c index 097e61e350..d9976ccdaa 100644 --- a/src/modules/systemlib/param/param.c +++ b/src/modules/systemlib/param/param.c @@ -65,6 +65,8 @@ #include "uORB/topics/parameter_update.h" #include "px4_parameters.h" +#include + #if 0 # define debug(fmt, args...) do { warnx(fmt, ##args); } while(0) #else @@ -1035,3 +1037,25 @@ param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_chang func(arg, param); } } + +uint32_t param_hash_check(void) +{ + uint32_t param_hash = 0; + + param_lock(); + + /* compute the CRC32 over all string param names and 4 byte values */ + for (param_t param = 0; handle_in_range(param); param++) { + if (!param_used(param)) { + continue; + } + const char *name = param_name(param); + const void *val = param_get_value_ptr(param); + param_hash = crc32part((const uint8_t*)name, strlen(name), param_hash); + param_hash = crc32part(val, sizeof(union param_value_u), param_hash); + } + + param_unlock(); + + return param_hash; +} diff --git a/src/modules/systemlib/param/param.h b/src/modules/systemlib/param/param.h index d7cf666a71..f8334361f1 100644 --- a/src/modules/systemlib/param/param.h +++ b/src/modules/systemlib/param/param.h @@ -333,6 +333,13 @@ __EXPORT int param_save_default(void); */ __EXPORT int param_load_default(void); +/** + * Generate the hash of all parameters and their values + * + * @return CRC32 hash of all param_ids and values + */ +__EXPORT uint32_t param_hash_check(void); + /* * Macros creating static parameter definitions. * From 8a4699c65641388c7dee5aeb3adc6e90b33869ff Mon Sep 17 00:00:00 2001 From: Nate Weibley Date: Mon, 12 Oct 2015 15:26:11 -0400 Subject: [PATCH 2/2] Fix style --- src/modules/systemlib/param/param.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/systemlib/param/param.c b/src/modules/systemlib/param/param.c index d9976ccdaa..f606f20702 100644 --- a/src/modules/systemlib/param/param.c +++ b/src/modules/systemlib/param/param.c @@ -1049,9 +1049,10 @@ uint32_t param_hash_check(void) if (!param_used(param)) { continue; } + const char *name = param_name(param); const void *val = param_get_value_ptr(param); - param_hash = crc32part((const uint8_t*)name, strlen(name), param_hash); + param_hash = crc32part((const uint8_t *)name, strlen(name), param_hash); param_hash = crc32part(val, sizeof(union param_value_u), param_hash); }