Skip to content

Commit 9edb8e2

Browse files
berendocarlescufi
authored andcommitted
mgmt: smp: Fix NULL pointer dereferences in UDP transport.
Guard call to k_thread_abort() to ensure it's not called with an uninitialized thread and fix improper use of inet_pton() with INADDR_ANY passed in place of a dotted-decimal string. Both of these would otherwise lead to NULL pointer dereferences. Signed-off-by: Berend Ozceri <[email protected]>
1 parent 07193f0 commit 9edb8e2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

subsys/mgmt/mcumgr/smp_udp.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ static struct configs configs = {
4444
#if CONFIG_MCUMGR_SMP_UDP_IPV4
4545
.ipv4 = {
4646
.proto = "IPv4",
47+
.sock = -1,
4748
},
4849
#endif
4950
#if CONFIG_MCUMGR_SMP_UDP_IPV6
5051
.ipv6 = {
5152
.proto = "IPv6",
53+
.sock = -1,
5254
},
5355
#endif
5456
};
@@ -204,7 +206,7 @@ int smp_udp_open(void)
204206
memset(&addr4, 0, sizeof(addr4));
205207
addr4.sin_family = AF_INET;
206208
addr4.sin_port = htons(CONFIG_MCUMGR_SMP_UDP_PORT);
207-
inet_pton(AF_INET, INADDR_ANY, &addr4.sin_addr);
209+
addr4.sin_addr.s_addr = htonl(INADDR_ANY);
208210

209211
conf = &configs.ipv4;
210212
conf->sock = create_socket((struct sockaddr *)&addr4, conf->proto);
@@ -240,13 +242,19 @@ int smp_udp_open(void)
240242
int smp_udp_close(void)
241243
{
242244
#if CONFIG_MCUMGR_SMP_UDP_IPV4
243-
k_thread_abort(&(configs.ipv4.thread));
244-
close(configs.ipv4.sock);
245+
if (configs.ipv4.sock >= 0) {
246+
k_thread_abort(&(configs.ipv4.thread));
247+
close(configs.ipv4.sock);
248+
configs.ipv4.sock = -1;
249+
}
245250
#endif
246251

247252
#if CONFIG_MCUMGR_SMP_UDP_IPV6
248-
k_thread_abort(&(configs.ipv6.thread));
249-
close(configs.ipv6.sock);
253+
if (configs.ipv6.sock >= 0) {
254+
k_thread_abort(&(configs.ipv6.thread));
255+
close(configs.ipv6.sock);
256+
configs.ipv6.sock = -1;
257+
}
250258
#endif
251259

252260
return MGMT_ERR_EOK;

0 commit comments

Comments
 (0)