public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Simon Wunderlich <sw@simonwunderlich.de>
To: Sven Eckelmann <sven@narfation.org>
Cc: b.a.t.m.a.n@lists.open-mesh.org
Subject: Re: [B.A.T.M.A.N.] [PATCH v2 3/3] batman-adv: Fix reference leak in batadv_find_router
Date: Fri, 01 Jul 2016 15:30:43 +0200	[thread overview]
Message-ID: <4909174.zVFi6JY9rb@prime> (raw)
In-Reply-To: <1467310294-5892-2-git-send-email-sven@narfation.org>

[-- Attachment #1: Type: text/plain, Size: 4556 bytes --]

Acked-by: Simon Wunderlich <sw@simonwunderlich.de>

Thanks, this also looks much cleaner now!
     Simon

On Thursday 30 June 2016 20:11:34 Sven Eckelmann wrote:
> The replacement of last_bonding_candidate in batadv_orig_node has to be an
> atomic operation. Otherwise it is possible that the reference counter of a
> batadv_orig_ifinfo is reduced which was no longer the
> last_bonding_candidate when the new candidate is added. This can either
> lead to an invalid memory access or to reference leaks which make it
> impossible to an interface which was added to batman-adv.
> 
> Fixes: 797edd9e87ac ("batman-adv: add bonding again")
> Signed-off-by: Sven Eckelmann <sven@narfation.org>
> ---
> v2:
>  - get refcnt for new selected router before assigning it to returned
>    variable
>  - move refcnt cleanup of all remembered candidates/routers to central place
> ---
>  net/batman-adv/routing.c | 52
> ++++++++++++++++++++++++++++++++++++------------ net/batman-adv/types.h   |
>  4 +++-
>  2 files changed, 42 insertions(+), 14 deletions(-)
> 
> diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
> index bba9276..a4253d4 100644
> --- a/net/batman-adv/routing.c
> +++ b/net/batman-adv/routing.c
> @@ -462,6 +462,29 @@ static int batadv_check_unicast_packet(struct
> batadv_priv *bat_priv, }
> 
>  /**
> + * batadv_last_bonding_replace - Replace last_bonding_candidate of
> orig_node + * @orig_node: originator node whose bonding candidates should
> be replaced + * @new_candidate: new bonding candidate or NULL
> + */
> +static void
> +batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
> +			    struct batadv_orig_ifinfo *new_candidate)
> +{
> +	struct batadv_orig_ifinfo *old_candidate;
> +
> +	spin_lock_bh(&orig_node->neigh_list_lock);
> +	old_candidate = orig_node->last_bonding_candidate;
> +
> +	if (new_candidate)
> +		kref_get(&new_candidate->refcount);
> +	orig_node->last_bonding_candidate = new_candidate;
> +	spin_unlock_bh(&orig_node->neigh_list_lock);
> +
> +	if (old_candidate)
> +		batadv_orig_ifinfo_put(old_candidate);
> +}
> +
> +/**
>   * batadv_find_router - find a suitable router for this originator
>   * @bat_priv: the bat priv with all the soft interface information
>   * @orig_node: the destination node
> @@ -568,10 +591,6 @@ next:
>  	}
>  	rcu_read_unlock();
> 
> -	/* last_bonding_candidate is reset below, remove the old reference. */
> -	if (orig_node->last_bonding_candidate)
> -		batadv_orig_ifinfo_put(orig_node->last_bonding_candidate);
> -
>  	/* After finding candidates, handle the three cases:
>  	 * 1) there is a next candidate, use that
>  	 * 2) there is no next candidate, use the first of the list
> @@ -580,21 +599,28 @@ next:
>  	if (next_candidate) {
>  		batadv_neigh_node_put(router);
> 
> -		/* remove references to first candidate, we don't need it. */
> -		if (first_candidate) {
> -			batadv_neigh_node_put(first_candidate_router);
> -			batadv_orig_ifinfo_put(first_candidate);
> -		}
> +		kref_get(&next_candidate_router->refcount);
>  		router = next_candidate_router;
> -		orig_node->last_bonding_candidate = next_candidate;
> +		batadv_last_bonding_replace(orig_node, next_candidate);
>  	} else if (first_candidate) {
>  		batadv_neigh_node_put(router);
> 
> -		/* refcounting has already been done in the loop above. */
> +		kref_get(&first_candidate_router->refcount);
>  		router = first_candidate_router;
> -		orig_node->last_bonding_candidate = first_candidate;
> +		batadv_last_bonding_replace(orig_node, first_candidate);
>  	} else {
> -		orig_node->last_bonding_candidate = NULL;
> +		batadv_last_bonding_replace(orig_node, NULL);
> +	}
> +
> +	/* cleanup of candidates */
> +	if (first_candidate) {
> +		batadv_neigh_node_put(first_candidate_router);
> +		batadv_orig_ifinfo_put(first_candidate);
> +	}
> +
> +	if (next_candidate) {
> +		batadv_neigh_node_put(next_candidate_router);
> +		batadv_orig_ifinfo_put(next_candidate);
>  	}
> 
>  	return router;
> diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
> index d82f6b4..96af6da 100644
> --- a/net/batman-adv/types.h
> +++ b/net/batman-adv/types.h
> @@ -329,7 +329,9 @@ struct batadv_orig_node {
>  	DECLARE_BITMAP(bcast_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
>  	u32 last_bcast_seqno;
>  	struct hlist_head neigh_list;
> -	/* neigh_list_lock protects: neigh_list and router */
> +	/* neigh_list_lock protects: neigh_list, ifinfo_list,
> +	 * last_bonding_candidate and router
> +	 */
>  	spinlock_t neigh_list_lock;
>  	struct hlist_node hash_entry;
>  	struct batadv_priv *bat_priv;

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2016-07-01 13:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-30 18:10 [B.A.T.M.A.N.] [PATCH v2 1/3] batman-adv: Fix orig_node_vlan leak on orig_node_release Sven Eckelmann
2016-06-30 18:11 ` [B.A.T.M.A.N.] [PATCH v2 2/3] batman-adv: Fix non-atomic bla_claim::backbone_gw access Sven Eckelmann
2016-07-01 13:34   ` Simon Wunderlich
2016-07-01 13:49   ` [B.A.T.M.A.N.] [PATCH v3 2/4] " Sven Eckelmann
2016-07-01 13:54     ` Simon Wunderlich
2016-07-05  9:26       ` Marek Lindner
2016-06-30 18:11 ` [B.A.T.M.A.N.] [PATCH v2 3/3] batman-adv: Fix reference leak in batadv_find_router Sven Eckelmann
2016-07-01 13:30   ` Simon Wunderlich [this message]
2016-07-05  9:31   ` Marek Lindner
2016-06-30 19:41 ` [B.A.T.M.A.N.] [PATCH maint v2 4/4] batman-adv: Free last_bonding_candidate on release of orig_node Sven Eckelmann
2016-07-05  9:34   ` Marek Lindner
2016-07-05  9:18 ` [B.A.T.M.A.N.] [PATCH v2 1/3] batman-adv: Fix orig_node_vlan leak on orig_node_release Marek Lindner

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=4909174.zVFi6JY9rb@prime \
    --to=sw@simonwunderlich.de \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    --cc=sven@narfation.org \
    /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