Skip to content

Commit 653c987

Browse files
rluboscarlescufi
authored andcommitted
net: lwm2m: Fix lwm2m_path_log_strdup buffer usage
Currently the lwm2m_path_log_strdup allocates a temporary buffer on a stack, and then passes it to the log_strdup function to create a copy of the string for the logger. log_strdup however will not copy the string if for instance immediate logging is used, therefore the logging function will still use the memory address of the already invalid buffer allocated within lwm2m_path_log_strdup. Fix this by passing an addittional `buf` parameter to the lwm2m_path_log_strdup function, therefore allowing the user to provide the buffer within a valid scope. CID: 220536 Fixes #34005 Signed-off-by: Robert Lubos <[email protected]>
1 parent 5cebdf5 commit 653c987

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
6868

6969
#define MAX_TOKEN_LEN 8
7070

71+
#define LWM2M_MAX_PATH_STR_LEN sizeof("65535/65535/65535/65535")
72+
7173
struct observe_node {
7274
sys_snode_t node;
7375
struct lwm2m_ctx *ctx;
@@ -556,9 +558,8 @@ static int engine_remove_observer(const uint8_t *token, uint8_t tkl)
556558
}
557559

558560
#if defined(CONFIG_LOG)
559-
char *lwm2m_path_log_strdup(struct lwm2m_obj_path *path)
561+
char *lwm2m_path_log_strdup(char *buf, struct lwm2m_obj_path *path)
560562
{
561-
char buf[sizeof("65535/65535/65535/65535")];
562563
size_t cur = sprintf(buf, "%u", path->obj_id);
563564

564565
if (path->level > 1) {
@@ -578,6 +579,7 @@ char *lwm2m_path_log_strdup(struct lwm2m_obj_path *path)
578579
#if defined(CONFIG_LWM2M_CANCEL_OBSERVE_BY_PATH)
579580
static int engine_remove_observer_by_path(struct lwm2m_obj_path *path)
580581
{
582+
char buf[LWM2M_MAX_PATH_STR_LEN];
581583
struct observe_node *obs, *found_obj = NULL;
582584
sys_snode_t *prev_node = NULL;
583585

@@ -595,7 +597,8 @@ static int engine_remove_observer_by_path(struct lwm2m_obj_path *path)
595597
return -ENOENT;
596598
}
597599

598-
LOG_INF("Removing observer for path %s", lwm2m_path_log_strdup(path));
600+
LOG_INF("Removing observer for path %s",
601+
lwm2m_path_log_strdup(buf, path));
599602
sys_slist_remove(&engine_observer_list, prev_node, &found_obj->node);
600603
(void)memset(found_obj, 0, sizeof(*found_obj));
601604

0 commit comments

Comments
 (0)