modem: ppp: fix crash when attaching to a pipe

modem_pipe_attach() can send events before returning, which could
provoke a crash as ppp->pipe, still NULL at that time, could be
used either in receiving (if the pipe had some data pending) or
in sending (if the PPP module had already been attached and had
some data to send in its transmit buffer).

ppp->pipe is now set before modem_pipe_attach().
Also, the ATTACHED_BIT is now set only after having actually attached.
And finally, the send_work is now scheduled on PIPE_EVENT_OPENED
so that data is flushed when the (closed) attached pipe is opened.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
This commit is contained in:
Tomi Fontanilles 2024-02-06 15:42:11 +02:00 committed by Fabio Baltieri
parent 5fc02e3501
commit 6aef9d4d9a

View file

@ -319,6 +319,7 @@ static void modem_ppp_pipe_callback(struct modem_pipe *pipe, enum modem_pipe_eve
k_work_submit(&ppp->process_work);
break;
case MODEM_PIPE_EVENT_OPENED:
case MODEM_PIPE_EVENT_TRANSMIT_IDLE:
k_work_submit(&ppp->send_work);
break;
@ -467,12 +468,14 @@ const struct ppp_api modem_ppp_ppp_api = {
int modem_ppp_attach(struct modem_ppp *ppp, struct modem_pipe *pipe)
{
if (atomic_test_and_set_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT) == true) {
if (atomic_test_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT) == true) {
return 0;
}
modem_pipe_attach(pipe, modem_ppp_pipe_callback, ppp);
ppp->pipe = pipe;
modem_pipe_attach(pipe, modem_ppp_pipe_callback, ppp);
atomic_set_bit(&ppp->state, MODEM_PPP_STATE_ATTACHED_BIT);
return 0;
}