Skip to content

Commit e5ad5a4

Browse files
tokangasrlubos
authored andcommitted
lib: nrf_modem_lib: Fix possible buffer overflow
Fixed buffer overflow when getsockopt() is called for SO_RCVTIMEO or SO_SNDTIMEO with a buffer which is too small for the timeval structure. Signed-off-by: Tommi Kangas <[email protected]>
1 parent 964bb4a commit e5ad5a4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/nrf_modem_lib/nrf9x_sockets.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,13 @@ static int nrf9x_socket_offload_getsockopt(void *obj, int level, int optname,
564564
}
565565
} else if ((optname == SO_RCVTIMEO) ||
566566
(optname == SO_SNDTIMEO)) {
567-
((struct timeval *)optval)->tv_sec =
568-
nrf_timeo.tv_sec;
569-
((struct timeval *)optval)->tv_usec =
570-
nrf_timeo.tv_usec;
571-
*optlen = sizeof(struct timeval);
567+
struct timeval tv;
568+
size_t tvlen = MIN(sizeof(struct timeval), *optlen);
569+
570+
tv.tv_sec = nrf_timeo.tv_sec;
571+
tv.tv_usec = nrf_timeo.tv_usec;
572+
memcpy(optval, &tv, tvlen);
573+
*optlen = tvlen;
572574
}
573575
}
574576
}

0 commit comments

Comments
 (0)