Skip to content

Commit

Permalink
udp:add tls cleanup protection to protect udp_callback info in psock_…
Browse files Browse the repository at this point in the history
…udp_recvfrom

Signed-off-by: wangchen <[email protected]>
  • Loading branch information
wangchen61698 committed Aug 20, 2024
1 parent ffd45ef commit 61230e9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions net/udp/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ struct udp_wrbuffer_s
};
#endif

struct udp_callback_s
{
FAR struct net_driver_s *dev;
FAR struct udp_conn_s *conn;
FAR struct devif_callback_s *udp_cb;
sem_t *sem;
};

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -719,6 +727,19 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn,
uint16_t udp_callback(FAR struct net_driver_s *dev,
FAR struct udp_conn_s *conn, uint16_t flags);

/****************************************************************************
* Name: udp_callback_cleanup
*
* Description:
* Cleanup data and cb when thread is canceled.
*
* Input Parameters:
* arg - A pointer with conn and callback struct.
*
****************************************************************************/

void udp_callback_cleanup(FAR void *arg);

/****************************************************************************
* Name: psock_udp_recvfrom
*
Expand Down
24 changes: 24 additions & 0 deletions net/udp/udp_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,28 @@ uint16_t udp_callback(FAR struct net_driver_s *dev,
return flags;
}

/****************************************************************************
* Name: udp_callback_cleanup
*
* Description:
* Cleanup data and cb when thread is canceled.
*
* Input Parameters:
* arg - A pointer with conn and callback struct.
*
****************************************************************************/

void udp_callback_cleanup(FAR void *arg)
{
FAR struct udp_callback_s *cb = (FAR struct udp_callback_s *)arg;

nerr("ERROR: pthread is being canceled, need to cleanup cb\n");

udp_callback_free(cb->dev, cb->conn, cb->udp_cb);
if (cb->sem)
{
nxsem_destroy(cb->sem);
}
}

#endif /* CONFIG_NET && CONFIG_NET_UDP */
13 changes: 13 additions & 0 deletions net/udp/udp_recvfrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <nuttx/net/netdev.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/udp.h>
#include <nuttx/tls.h>
#include <netinet/in.h>

#include "netdev/netdev.h"
Expand Down Expand Up @@ -625,6 +626,7 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg,
{
FAR struct udp_conn_s *conn = psock->s_conn;
FAR struct net_driver_s *dev;
struct udp_callback_s info;
struct udp_recvfrom_s state;
int ret;

Expand Down Expand Up @@ -693,13 +695,24 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg,
state.ir_cb->priv = (FAR void *)&state;
state.ir_cb->event = udp_eventhandler;

/* Push a cancellation point onto the stack. This will be
* called if the thread is canceled.
*/

info.dev = dev;
info.conn = conn;
info.udp_cb = state.ir_cb;
info.sem = &state.ir_sem;
tls_cleanup_push(tls_get_info(), udp_callback_cleanup, &info);

/* Wait for either the receive to complete or for an error/timeout
* to occur. net_sem_timedwait will also terminate if a signal is
* received.
*/

ret = net_sem_timedwait(&state.ir_sem,
_SO_TIMEOUT(conn->sconn.s_rcvtimeo));
tls_cleanup_pop(tls_get_info(), 0);
if (ret == -ETIMEDOUT)
{
ret = -EAGAIN;
Expand Down

0 comments on commit 61230e9

Please sign in to comment.