From d529c20674131b9ce36853b92784e901a1bc86f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 9 Aug 2023 13:19:18 +1000 Subject: [PATCH] extmod/modssl_mbedtls: Fix ioctl of a socket in closed/error state. Signed-off-by: Damien George --- extmod/modssl_mbedtls.c | 13 +++++++++---- tests/extmod/ssl_ioctl.py.exp | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/extmod/modssl_mbedtls.c b/extmod/modssl_mbedtls.c index 8974ff65dd..b02b77b76f 100644 --- a/extmod/modssl_mbedtls.c +++ b/extmod/modssl_mbedtls.c @@ -486,15 +486,20 @@ STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i mp_uint_t ret = 0; uintptr_t saved_arg = 0; mp_obj_t sock = self->sock; - if (sock == MP_OBJ_NULL || (request != MP_STREAM_CLOSE && self->last_error != 0)) { - // Closed or error socket: - return MP_STREAM_POLL_NVAL; - } if (request == MP_STREAM_CLOSE) { + if (sock == MP_OBJ_NULL) { + // Already closed socket, do nothing. + return 0; + } self->sock = MP_OBJ_NULL; mbedtls_ssl_free(&self->ssl); } else if (request == MP_STREAM_POLL) { + if (sock == MP_OBJ_NULL || self->last_error != 0) { + // Closed or error socket, return NVAL flag. + return MP_STREAM_POLL_NVAL; + } + // If the library signaled us that it needs reading or writing, only check that direction, // but save what the caller asked because we need to restore it later if (self->poll_mask && (arg & MP_STREAM_POLL_RDWR)) { diff --git a/tests/extmod/ssl_ioctl.py.exp b/tests/extmod/ssl_ioctl.py.exp index 72de7ed06d..22208b00cc 100644 --- a/tests/extmod/ssl_ioctl.py.exp +++ b/tests/extmod/ssl_ioctl.py.exp @@ -3,4 +3,4 @@ 10 OSError 4 0 3 32 -4 32 +4 0