public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Oliver Hartkopp <socketcan@hartkopp.net>
To: "Dae R. Jeong" <threeearcat@gmail.com>,
	mkl@pengutronix.de, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, linux-can@vger.kernel.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: WARNING in isotp_tx_timer_handler and WARNING in print_tainted
Date: Sun, 26 Mar 2023 13:15:19 +0200	[thread overview]
Message-ID: <31c4a218-ee1b-4b64-59b6-ba5ef6ecce3c@hartkopp.net> (raw)
In-Reply-To: <ZB/93xJxq/BUqAgG@dragonet>

Hi,

On 26.03.23 10:10, Dae R. Jeong wrote:
> Hi,
> 
> I am curious about the error handling logic in isotp_sendmsg() which
> looks a bit unclear to me.
> 
> I was looking the `WARNING in isotp_tx_timer_handler` warning [1],
> which was firstly addressed by a commit [2] but reoccured even after
> the commit.
> [1]: https://syzkaller.appspot.com/bug?id=4f492d593461a5e44d76dd9322e179d13191a8ef
> [2]: c6adf659a8ba can: isotp: check CAN address family in isotp_bind()
> 
> I thought that the warning is caused by the concurrent execution of
> two isotp_sendmsg() as described below (I'm not 100% sure though).
> 
> CPU1                             CPU2
> isotp_sendmsg()                  isotp_sendmsg()
> -----                            -----
> old_state = so->tx.state; // ISOTP_IDLE
> 
>                                   cmpxchg(&so->tx.state, ISTOP_IDLE, ISOTP_SENDING) // success
> 							     ...
> 							     so->tx.state = ISTOP_WAIT_FIRST_FC;
> 							     hrtimer_start(&so->txtimer);
> 
> cmpxchg(&so->tx.state, ISTOP_IDLE, ISOTP_SENDING) // failed
> // if MSG_DONTWAIT is set in msg->msg_flags or
> // a signal is delivered during wait_event_interruptible()
> goto err_out;
> err_out:
>      so->tx.state = old_state; // ISTOP_IDLE
> 
>                                   isotp_tx_timer_handler()
> 								 -----
> 								 switch (so->tx.state) {
> 								 default:
> 								     WARN_ONCE();
> 								 }
> 
> Then, a commit [3] changed the logic of tx timer, and removed the
> WARN_ONCE() statement. So I thought that the issue is completely
> handled.
> [3]: 4f027cba8216 can: isotp: split tx timer into transmission and timeout
> 
> But even after [3] is applied, I found a warning that seems related
> occurred [4] (in the kernel commit: 478a351ce0d6).
> [4]: https://syzkaller.appspot.com/bug?id=11d0e5f6fef53a0ea486bbd07ddd3cba66132150
> 
> So I wonder whether the `err_out` logic in isotp_sendmsg() is safe.
> For me, it looks like isotp_sendmsg() can change so->tx.state to
> ISTOP_IDLE at any time. It may not be a problem if all other locations
> are aware of this. Is this an intended behavior?
> 
> Thank you in advance.

Thank you for picking this up!

In fact I was not aware of the possibility of a concurrent execution of 
isotp_sendmsg() and thought cmpxchg() would just make it ...

But looking at other *_sendmsg() implementations a lock_sock() seems to 
be a common pattern to handle concurrent syscalls, see:

git grep -p lock_sock net | grep sendmsg

What do you think about adopting this to isotp_sendmsg()? See patch below.

Best regards,
Oliver

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 9bc344851704..0b95c0df7a63 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -912,13 +912,12 @@ static enum hrtimer_restart 
isotp_txfr_timer_handler(struct hrtimer *hrtimer)
  		isotp_send_cframe(so);

  	return HRTIMER_NORESTART;
  }

-static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, 
size_t size)
+static int isotp_sendmsg_locked(struct sock *sk, struct msghdr *msg, 
size_t size)
  {
-	struct sock *sk = sock->sk;
  	struct isotp_sock *so = isotp_sk(sk);
  	u32 old_state = so->tx.state;
  	struct sk_buff *skb;
  	struct net_device *dev;
  	struct canfd_frame *cf;
@@ -1091,10 +1090,22 @@ static int isotp_sendmsg(struct socket *sock, 
struct msghdr *msg, size_t size)
  		wake_up_interruptible(&so->wait);

  	return err;
  }

+static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, 
size_t size)
+{
+	struct sock *sk = sock->sk;
+	int ret;
+
+	lock_sock(sk);
+	ret = isotp_sendmsg_locked(sk, msg, size);
+	release_sock(sk);
+
+	return ret;
+}
+
  static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, 
size_t size,
  			 int flags)
  {
  	struct sock *sk = sock->sk;
  	struct sk_buff *skb;




  reply	other threads:[~2023-03-26 11:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-26  8:10 WARNING in isotp_tx_timer_handler and WARNING in print_tainted Dae R. Jeong
2023-03-26 11:15 ` Oliver Hartkopp [this message]
2023-03-26 11:55   ` Dae R. Jeong
2023-03-26 16:17     ` Oliver Hartkopp
2023-03-27  1:58       ` Dae R. Jeong
     [not found]     ` <20230327014843.2431-1-hdanton@sina.com>
2023-03-31 10:25       ` Oliver Hartkopp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=31c4a218-ee1b-4b64-59b6-ba5ef6ecce3c@hartkopp.net \
    --to=socketcan@hartkopp.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=threeearcat@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox