The following commit has been merged in the next branch:
commit b52cc619bc0056c06b3ad93b1e4efbc35729120e
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:42 2010 +0000
batman-adv: Use refcnt to track usage count of batman_if
get_batman_if_by_netdev and get_active_batman_if may leak data from the
rcu protected list of interfaces. The rcu protected list of all gateway
nodes leaks the actual data outside the read-side critical area. This is
not valid as we may free the data using a call_rcu created callback
after we unlock using rcu_read_unlock. A workaround is to provide a
reference count to be sure that the memory isn't freed to early.
It is currently only to implement the already existing functionality and
doesn't provide the full tracking of all usage cases.
Additionally, we must hardif_hold inside the
rcu_read_lock()..rcu_read_unlock() before we attach to the structure
which "leaks" it. When another function now removed it from its usage
context (primary_if, usage on stack, ...) then we must hardif_put it. If
it is decremented to zero then we can issue the call_rcu to the freeing
function. So "put" is not allowed inside an rcu_read_lock.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/hard-interface.c b/hard-interface.c
index 0b3ee6b..445498c 100644
--- a/hard-interface.c
+++ b/hard-interface.c
@@ -403,6 +403,8 @@ static struct batman_if *hardif_add_interface(struct net_device *net_dev)
batman_if->soft_iface = NULL;
batman_if->if_status = IF_NOT_IN_USE;
INIT_LIST_HEAD(&batman_if->list);
+ atomic_set(&batman_if->refcnt, 0);
+ hardif_hold(batman_if);
check_known_mac_addr(batman_if->net_dev->dev_addr);
@@ -435,8 +437,7 @@ static void hardif_remove_interface(struct batman_if *batman_if)
list_del_rcu(&batman_if->list);
synchronize_rcu();
sysfs_del_hardif(&batman_if->hardif_obj);
- dev_put(batman_if->net_dev);
- kfree(batman_if);
+ hardif_put(batman_if);
}
void hardif_remove_interfaces(void)
diff --git a/hard-interface.h b/hard-interface.h
index 4b49527..d550889 100644
--- a/hard-interface.h
+++ b/hard-interface.h
@@ -42,4 +42,17 @@ int batman_skb_recv(struct sk_buff *skb,
int hardif_min_mtu(struct net_device *soft_iface);
void update_min_mtu(struct net_device *soft_iface);
+static inline void hardif_hold(struct batman_if *batman_if)
+{
+ atomic_inc(&batman_if->refcnt);
+}
+
+static inline void hardif_put(struct batman_if *batman_if)
+{
+ if (atomic_dec_and_test(&batman_if->refcnt)) {
+ dev_put(batman_if->net_dev);
+ kfree(batman_if);
+ }
+}
+
#endif /* _NET_BATMAN_ADV_HARD_INTERFACE_H_ */
diff --git a/types.h b/types.h
index b162644..bb5827f 100644
--- a/types.h
+++ b/types.h
@@ -44,6 +44,7 @@ struct batman_if {
unsigned char *packet_buff;
int packet_len;
struct kobject *hardif_obj;
+ atomic_t refcnt;
struct packet_type batman_adv_ptype;
struct net_device *soft_iface;
};
--
batman-adv
The following commit has been merged in the next branch:
commit 4dd9bc01f2cf13135cf15d41dbf6479446fe4a06
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:31 2010 +0000
batman-adv: Introduce if_list_lock to protect if_list
The update critical sections of if_list must be protected by a locking
primitive other than RCU. The iterator must also be protected by the
chosen locking mechanism.
The rtnl_lock in hardif_remove_interfaces must also be moved outside the
iterator primitive to ensure that we don't deadlock the kernel due to
differently nested locks in hardif_remove_interfaces and hard_if_event.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/hard-interface.c b/hard-interface.c
index edbfddf..3cd7cb1 100644
--- a/hard-interface.c
+++ b/hard-interface.c
@@ -35,6 +35,9 @@
#define MIN(x, y) ((x) < (y) ? (x) : (y))
+/* protect update critical side of if_list - but not the content */
+static DEFINE_SPINLOCK(if_list_lock);
+
struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
{
struct batman_if *batman_if;
@@ -402,7 +405,11 @@ static struct batman_if *hardif_add_interface(struct net_device *net_dev)
INIT_LIST_HEAD(&batman_if->list);
check_known_mac_addr(batman_if->net_dev->dev_addr);
+
+ spin_lock(&if_list_lock);
list_add_tail_rcu(&batman_if->list, &if_list);
+ spin_unlock(&if_list_lock);
+
return batman_if;
free_if:
@@ -430,6 +437,8 @@ static void hardif_remove_interface(struct batman_if *batman_if)
return;
batman_if->if_status = IF_TO_BE_REMOVED;
+
+ /* caller must take if_list_lock */
list_del_rcu(&batman_if->list);
sysfs_del_hardif(&batman_if->hardif_obj);
dev_put(batman_if->net_dev);
@@ -440,11 +449,13 @@ void hardif_remove_interfaces(void)
{
struct batman_if *batman_if, *batman_if_tmp;
+ rtnl_lock();
+ spin_lock(&if_list_lock);
list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
- rtnl_lock();
hardif_remove_interface(batman_if);
- rtnl_unlock();
}
+ spin_unlock(&if_list_lock);
+ rtnl_unlock();
}
static int hard_if_event(struct notifier_block *this,
@@ -469,7 +480,9 @@ static int hard_if_event(struct notifier_block *this,
hardif_deactivate_interface(batman_if);
break;
case NETDEV_UNREGISTER:
+ spin_lock(&if_list_lock);
hardif_remove_interface(batman_if);
+ spin_unlock(&if_list_lock);
break;
case NETDEV_CHANGEMTU:
if (batman_if->soft_iface)
--
batman-adv
The following commit has been merged in the next branch:
commit 265d3feae9803b10fc7137f7f40b643c82557940
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:35 2010 +0000
batman-adv: Always protect list_for_each_entry_rcu with RCU
receive_bat_packet is not called with rcu_read_lock so we must ensure by
ourself that we protect list_for_each_entry_rcu using the correct RCU
locks.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/routing.c b/routing.c
index a07e0e0..5dbff51 100644
--- a/routing.c
+++ b/routing.c
@@ -564,6 +564,7 @@ void receive_bat_packet(struct ethhdr *ethhdr,
batman_packet->tq, batman_packet->ttl, batman_packet->version,
has_directlink_flag);
+ rcu_read_lock();
list_for_each_entry_rcu(batman_if, &if_list, list) {
if (batman_if->if_status != IF_ACTIVE)
continue;
@@ -586,6 +587,7 @@ void receive_bat_packet(struct ethhdr *ethhdr,
if (compare_orig(ethhdr->h_source, broadcast_addr))
is_broadcast = 1;
}
+ rcu_read_unlock();
if (batman_packet->version != COMPAT_VERSION) {
bat_dbg(DBG_BATMAN, bat_priv,
--
batman-adv
The following commit has been merged in the next branch:
commit 7905069edf9d126949e2d24a31aa98ac7be7f5a9
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:35 2010 +0000
batman-adv: Remove unneeded rcu_read_lock
Regions which do not use rcu functions don't need to protected by
rcu_read_lock. If we want to protect data from being freed than it must
be covered by the same read-side critical section or otherwise the grace
period may already ended and freed the memory before we called
rcu_read_lock again.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/originator.c b/originator.c
index 41e4818..471fd90 100644
--- a/originator.c
+++ b/originator.c
@@ -330,7 +330,6 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
net_dev->name);
}
- rcu_read_lock();
seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
SOURCE_VERSION, REVISION_VERSION_STR,
bat_priv->primary_if->net_dev->name,
@@ -338,7 +337,6 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
"Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
"outgoingIF", "Potential nexthops");
- rcu_read_unlock();
spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
--
batman-adv
The following commit has been merged in the master branch:
commit beb5157f3eb704480451bdd2e5d65ad1f71e987a
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:45 2010 +0000
batman-adv: Track references of batman_if in set_primary_if
set_primary_if exchanges the current primary interfaces with a new one.
This is a new reference and thus we have to count it and decrease the
count of the old primary interface.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/hard-interface.c b/hard-interface.c
index f519b4b..942a44a 100644
--- a/hard-interface.c
+++ b/hard-interface.c
@@ -113,9 +113,17 @@ static void set_primary_if(struct bat_priv *bat_priv,
{
struct batman_packet *batman_packet;
struct vis_packet *vis_packet;
+ struct batman_if *old_if;
+ if (batman_if)
+ hardif_hold(batman_if);
+
+ old_if = bat_priv->primary_if;
bat_priv->primary_if = batman_if;
+ if (old_if)
+ hardif_put(old_if);
+
if (!bat_priv->primary_if)
return;
--
batman-adv
The following commit has been merged in the master branch:
commit 5d1d5b413ead68d71687c1baaff93e2a130d03ed
Author: Sven Eckelmann <sven.eckelmann(a)gmx.de>
Date: Sat Sep 18 15:35:42 2010 +0000
batman-adv: Use refcnt to track usage count of batman_if
get_batman_if_by_netdev and get_active_batman_if may leak data from the
rcu protected list of interfaces. The rcu protected list of all gateway
nodes leaks the actual data outside the read-side critical area. This is
not valid as we may free the data using a call_rcu created callback
after we unlock using rcu_read_unlock. A workaround is to provide a
reference count to be sure that the memory isn't freed to early.
It is currently only to implement the already existing functionality and
doesn't provide the full tracking of all usage cases.
Additionally, we must hardif_hold inside the
rcu_read_lock()..rcu_read_unlock() before we attach to the structure
which "leaks" it. When another function now removed it from its usage
context (primary_if, usage on stack, ...) then we must hardif_put it. If
it is decremented to zero then we can issue the call_rcu to the freeing
function. So "put" is not allowed inside an rcu_read_lock.
Signed-off-by: Sven Eckelmann <sven.eckelmann(a)gmx.de>
diff --git a/hard-interface.c b/hard-interface.c
index 0b3ee6b..445498c 100644
--- a/hard-interface.c
+++ b/hard-interface.c
@@ -403,6 +403,8 @@ static struct batman_if *hardif_add_interface(struct net_device *net_dev)
batman_if->soft_iface = NULL;
batman_if->if_status = IF_NOT_IN_USE;
INIT_LIST_HEAD(&batman_if->list);
+ atomic_set(&batman_if->refcnt, 0);
+ hardif_hold(batman_if);
check_known_mac_addr(batman_if->net_dev->dev_addr);
@@ -435,8 +437,7 @@ static void hardif_remove_interface(struct batman_if *batman_if)
list_del_rcu(&batman_if->list);
synchronize_rcu();
sysfs_del_hardif(&batman_if->hardif_obj);
- dev_put(batman_if->net_dev);
- kfree(batman_if);
+ hardif_put(batman_if);
}
void hardif_remove_interfaces(void)
diff --git a/hard-interface.h b/hard-interface.h
index 4b49527..d550889 100644
--- a/hard-interface.h
+++ b/hard-interface.h
@@ -42,4 +42,17 @@ int batman_skb_recv(struct sk_buff *skb,
int hardif_min_mtu(struct net_device *soft_iface);
void update_min_mtu(struct net_device *soft_iface);
+static inline void hardif_hold(struct batman_if *batman_if)
+{
+ atomic_inc(&batman_if->refcnt);
+}
+
+static inline void hardif_put(struct batman_if *batman_if)
+{
+ if (atomic_dec_and_test(&batman_if->refcnt)) {
+ dev_put(batman_if->net_dev);
+ kfree(batman_if);
+ }
+}
+
#endif /* _NET_BATMAN_ADV_HARD_INTERFACE_H_ */
diff --git a/types.h b/types.h
index ecc4365..a609100 100644
--- a/types.h
+++ b/types.h
@@ -44,6 +44,7 @@ struct batman_if {
unsigned char *packet_buff;
int packet_len;
struct kobject *hardif_obj;
+ atomic_t refcnt;
struct packet_type batman_adv_ptype;
struct net_device *soft_iface;
};
--
batman-adv