mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 21:11:12 +02:00
extmod/modlwip: Fix crash when calling recv on listening socket.
Add check to prevent calling recv on a socket in the listening state. This prevents a crash/hard fault when user code mistakenly tries to recv on the listening socket instead of on the accepted connection. Add corresponding test case to demonstrate the bug. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This commit is contained in:
committed by
Damien George
parent
2a46759fe8
commit
2f04381aeb
@@ -825,6 +825,12 @@ static mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
|
|||||||
// Check for any pending errors
|
// Check for any pending errors
|
||||||
STREAM_ERROR_CHECK(socket);
|
STREAM_ERROR_CHECK(socket);
|
||||||
|
|
||||||
|
if (socket->state == STATE_LISTENING) {
|
||||||
|
// original socket in listening state, not the accepted connection.
|
||||||
|
*_errno = MP_ENOTCONN;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (socket->incoming.tcp.pbuf == NULL) {
|
if (socket->incoming.tcp.pbuf == NULL) {
|
||||||
|
|
||||||
// Non-blocking socket or flag
|
// Non-blocking socket or flag
|
||||||
|
@@ -1,30 +1,73 @@
|
|||||||
# Test recv on socket that just accepted a connection
|
# Test recv on listening socket after accept(), with various listen() arguments
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
PORT = 8000
|
PORT = 8000
|
||||||
|
|
||||||
|
# Test cases for listen() function
|
||||||
|
LISTEN_ARGS = [None, 0, 1, 2] # None means no argument
|
||||||
|
|
||||||
|
|
||||||
# Server
|
# Server
|
||||||
def instance0():
|
def instance0():
|
||||||
multitest.globals(IP=multitest.get_network_ip())
|
multitest.globals(IP=multitest.get_network_ip())
|
||||||
s = socket.socket()
|
|
||||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
|
|
||||||
s.listen(1)
|
|
||||||
multitest.next()
|
multitest.next()
|
||||||
s.accept()
|
|
||||||
try:
|
test_num = 0
|
||||||
print("recv", s.recv(10)) # should raise Errno 107 ENOTCONN
|
for blocking_mode in [True, False]:
|
||||||
except OSError as er:
|
for listen_arg in LISTEN_ARGS:
|
||||||
print(er.errno in (107, 128))
|
test_num += 1
|
||||||
s.close()
|
s = socket.socket()
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
|
||||||
|
|
||||||
|
# Call listen with or without argument based on test case
|
||||||
|
if listen_arg is None:
|
||||||
|
print(f"Test case {test_num}/8: listen() blocking={blocking_mode}")
|
||||||
|
s.listen()
|
||||||
|
else:
|
||||||
|
print(f"Test case {test_num}/8: listen({listen_arg}) blocking={blocking_mode}")
|
||||||
|
s.listen(listen_arg)
|
||||||
|
|
||||||
|
# Signal client that server is ready
|
||||||
|
multitest.broadcast(f"server_ready_{test_num}")
|
||||||
|
|
||||||
|
# Wait for client connection
|
||||||
|
c, _ = s.accept()
|
||||||
|
|
||||||
|
# Set blocking mode after accept
|
||||||
|
s.setblocking(blocking_mode)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("recv", s.recv(10)) # should raise Errno 107 ENOTCONN
|
||||||
|
except OSError as er:
|
||||||
|
# Verify the error code is either 107 (ENOTCONN) or 128 (ENOTCONN on Windows)
|
||||||
|
print(er.errno in (107, 128))
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
c.close()
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
# Signal client we're done with this test case
|
||||||
|
multitest.broadcast(f"server_done_{test_num}")
|
||||||
|
|
||||||
|
|
||||||
# Client
|
# Client
|
||||||
def instance1():
|
def instance1():
|
||||||
multitest.next()
|
multitest.next()
|
||||||
s = socket.socket()
|
|
||||||
s.connect(socket.getaddrinfo(IP, PORT)[0][-1])
|
test_num = 0
|
||||||
s.send(b"GET / HTTP/1.0\r\n\r\n")
|
for blocking_mode in [True, False]:
|
||||||
s.close()
|
for _ in LISTEN_ARGS:
|
||||||
|
test_num += 1
|
||||||
|
# Wait for server to be ready
|
||||||
|
multitest.wait(f"server_ready_{test_num}")
|
||||||
|
|
||||||
|
# Connect to server
|
||||||
|
s = socket.socket()
|
||||||
|
s.connect(socket.getaddrinfo(IP, PORT)[0][-1])
|
||||||
|
s.send(b"GET / HTTP/1.0\r\n\r\n")
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
# Wait for server to finish this test case
|
||||||
|
multitest.wait(f"server_done_{test_num}")
|
||||||
|
Reference in New Issue
Block a user