linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pavel Skripkin <paskripkin@gmail.com>
To: davem@davemloft.net, kuba@kernel.org, kaber@trash.net,
	david.ward@ll.mit.edu
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Pavel Skripkin <paskripkin@gmail.com>,
	syzbot+5cfab121b54dff775399@syzkaller.appspotmail.com
Subject: [PATCH] net: 802: fix memory leak in mrp_uninit_applicant
Date: Mon, 19 Jul 2021 00:01:04 +0300	[thread overview]
Message-ID: <20210718210104.30285-1-paskripkin@gmail.com> (raw)

Syzbot reported memory leak in mrp_uninit_applicant(). The problem was
in missing clean up function in mrp_uninit_applicant().

The reproducer provided by syzbot doing following things in order:

	1. mrp_request_join()
	     mrp_attr_event(app, attr, MRP_EVENT_JOIN);
		/* attr->state == MRP_APPLICANT_VP */

	2. mrp_request_leave()
	     mrp_attr_event(app, attr, MRP_EVENT_LV);
		/* attr->state == MRP_APPLICANT_VO */

	3. mrp_uninit_applicant()
  	     mrp_mad_event(app, MRP_EVENT_TX);
		/* attr is not freed */

Why attr won't be freed? Since last event == MRP_EVENT_TX let's refer
to mrp_tx_action_table:

static const u8
mrp_tx_action_table[MRP_APPLICANT_MAX + 1] = {
	[MRP_APPLICANT_VO] = MRP_TX_ACTION_S_IN_OPTIONAL,
	[MRP_APPLICANT_VP] = MRP_TX_ACTION_S_JOIN_IN,
	[MRP_APPLICANT_VN] = MRP_TX_ACTION_S_NEW,
	[MRP_APPLICANT_AN] = MRP_TX_ACTION_S_NEW,
	[MRP_APPLICANT_AA] = MRP_TX_ACTION_S_JOIN_IN,
	[MRP_APPLICANT_QA] = MRP_TX_ACTION_S_JOIN_IN_OPTIONAL,
	[MRP_APPLICANT_LA] = MRP_TX_ACTION_S_LV,
	[MRP_APPLICANT_AO] = MRP_TX_ACTION_S_IN_OPTIONAL,
	[MRP_APPLICANT_QO] = MRP_TX_ACTION_S_IN_OPTIONAL,
	[MRP_APPLICANT_AP] = MRP_TX_ACTION_S_JOIN_IN,
	[MRP_APPLICANT_QP] = MRP_TX_ACTION_S_IN_OPTIONAL,
};

[MRP_APPLICANT_VO] member has MRP_TX_ACTION_S_IN_OPTIONAL action and
mrp_attr_event() just returns in case of this action.
Since mrp_uninit_applicant() is destroy function for applicant we need
to free remaining attrs to avoid memory leaks.

Reported-and-tested-by: syzbot+5cfab121b54dff775399@syzkaller.appspotmail.com
Fixes: febf018d2234 ("net/802: Implement Multiple Registration Protocol (MRP)")
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
---
 net/802/mrp.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/net/802/mrp.c b/net/802/mrp.c
index bea6e43d45a0..bf319f3f2094 100644
--- a/net/802/mrp.c
+++ b/net/802/mrp.c
@@ -834,6 +834,16 @@ static void mrp_release_port(struct net_device *dev)
 	kfree_rcu(port, rcu);
 }
 
+static void mrp_destroy_remaining_attrs(struct mrp_applicant *app)
+{
+	while (!RB_EMPTY_ROOT(&app->mad)) {
+		struct mrp_attr *attr =
+			rb_entry(rb_first(&app->mad),
+				 struct mrp_attr, node);
+		mrp_attr_destroy(app, attr);
+	}
+}
+
 int mrp_init_applicant(struct net_device *dev, struct mrp_application *appl)
 {
 	struct mrp_applicant *app;
@@ -896,6 +906,13 @@ void mrp_uninit_applicant(struct net_device *dev, struct mrp_application *appl)
 	spin_lock_bh(&app->lock);
 	mrp_mad_event(app, MRP_EVENT_TX);
 	mrp_pdu_queue(app);
+
+	/* We need to free remaining attrs since this scenario is possible:
+	 *	mrp_request_join()
+	 *	mrp_request_leave()
+	 *	mrp_uninit_applicant()
+	 */
+	mrp_destroy_remaining_attrs(app);
 	spin_unlock_bh(&app->lock);
 
 	mrp_queue_xmit(app);
-- 
2.32.0


                 reply	other threads:[~2021-07-18 21:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210718210104.30285-1-paskripkin@gmail.com \
    --to=paskripkin@gmail.com \
    --cc=davem@davemloft.net \
    --cc=david.ward@ll.mit.edu \
    --cc=kaber@trash.net \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=syzbot+5cfab121b54dff775399@syzkaller.appspotmail.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;
as well as URLs for NNTP newsgroup(s).