From 5fab97c9d0b1fa40e5860ea83aabf9908b27f3c4 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 27 Feb 2017 20:57:51 -0800 Subject: [PATCH] net/mqtt: fix race condition in mqtt_init() We need to set the mqtt context recv function prior to calling net_context_recv which installs the mqtt_recv callback. If not, we risk a race condition where an unprotected reference to mqtt->rcv(mqtt, buf) is made in mqtt_recv(). Change-Id: If90ee58f4ea6f7879ef7c12b969ba27647426acc Signed-off-by: Marti Bolivar --- subsys/net/lib/mqtt/mqtt.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c index 8d8e1d1a576..2fa9b850189 100644 --- a/subsys/net/lib/mqtt/mqtt.c +++ b/subsys/net/lib/mqtt/mqtt.c @@ -837,11 +837,6 @@ int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type) ctx->clean_session = 1; ctx->connected = 0; - /* Install the receiver callback, timeout is set to K_NO_WAIT. - * In this case, no return code is evaluated. - */ - (void)net_context_recv(ctx->net_ctx, mqtt_recv, K_NO_WAIT, ctx); - ctx->app_type = app_type; switch (ctx->app_type) { @@ -855,5 +850,10 @@ int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type) return -EINVAL; } + /* Install the receiver callback, timeout is set to K_NO_WAIT. + * In this case, no return code is evaluated. + */ + (void)net_context_recv(ctx->net_ctx, mqtt_recv, K_NO_WAIT, ctx); + return 0; }