batman-adv: Make bat_priv->primary_if an rcu protected pointer
PATCH 1/1 batman-adv: Make bat_priv->primary_if an rcu protected pointer
The rcu protected macros rcu_dereference() and rcu_assign_pointer() for the bat_priv->primary_if need to be used, as well as spin/rcu locking.
Otherwise we might end up using a primary_if pointer pointing to already freed memory.
Signed-off-by Marek Lindner <[email protected]>
--- gateway_client.c | 33 ++++++++++------- hard-interface.c | 100 +++++++++++++++++++++++++++++++++++++------------- hard-interface.h | 1 + icmp_socket.c | 19 +++++++--- originator.c | 34 +++++++++++------- routing.c | 18 ++++++--- send.c | 17 +++++++-- soft-interface.c | 57 +++++++++++++++++++---------- translation-table.c | 57 ++++++++++++++++++++++------- types.h | 2 +- unicast.c | 16 ++++++--- vis.c | 37 +++++++++++++------ 12 files changed, 273 insertions(+), 118 deletions(-)
gateway_client.c +19 -14
@@ -435,30 +435,32 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset) {struct net_device *net_dev = (struct net_device *)seq->private;struct bat_priv *bat_priv =netdev_priv (net_dev); +struct hard_iface *primary_if;struct gw_node *gw_node;struct hlist_node *node; -int gw_count =0 ; +int gw_count =0 , ret =0 ; -if (!bat_priv->primary_if) { - -return seq_printf (seq,"BATMAN mesh %s disabled - please " -"specify interfaces to enable it\n" , - net_dev->name); + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) { + ret =seq_printf (seq,"BATMAN mesh %s disabled - please " +"specify interfaces to enable it\n" , + net_dev->name); + goto out; } -if (bat_priv->primary_if->if_status !=IF_ACTIVE ) { - -return seq_printf (seq,"BATMAN mesh %s disabled - " -"primary interface not active\n" , - net_dev->name); +if (primary_if->if_status !=IF_ACTIVE ) { + ret =seq_printf (seq,"BATMAN mesh %s disabled - " +"primary interface not active\n" , + net_dev->name); + goto out; }seq_printf (seq," %-12s (%s/%i) %17s [%10s]: gw_class ... " "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n" ,"Gateway" ,"#" ,TQ_MAX_VALUE ,"Nexthop" ,"outgoingIF" ,SOURCE_VERSION ,REVISION_VERSION_STR , - bat_priv->primary_if->net_dev->name, - bat_priv->primary_if->net_dev->dev_addr, net_dev->name); + primary_if->net_dev->name, + primary_if->net_dev->dev_addr, net_dev->name);rcu_read_lock ();hlist_for_each_entry_rcu (gw_node, node, &bat_priv->gw_list, list) {
@@ -476,7 +478,10 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)if (gw_count ==0 )seq_printf (seq,"No gateways in range ...\n" ); -return 0 ; +out: +if (primary_if) +hardif_free_ref (primary_if); +return ret; }int gw_is_target (struct bat_priv *bat_priv,struct sk_buff *skb)
hard-interface.c +74 -26
193 lines not on this page · raw message
hard-interface.h +1 -0
@@ -38,6 +38,7 @@ void hardif_remove_interfaces(void);int hardif_min_mtu (struct net_device *soft_iface);void update_min_mtu (struct net_device *soft_iface);void hardif_free_rcu (struct rcu_head *rcu); +struct hard_iface *primary_if_get_selected (struct bat_priv *bat_priv);static inline void hardif_free_ref (struct hard_iface *hard_iface) {
icmp_socket.c +14 -5
@@ -153,6 +153,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff, {struct socket_client *socket_client = file->private_data;struct bat_priv *bat_priv = socket_client->bat_priv; +struct hard_iface *primary_if =NULL ;struct sk_buff *skb;struct icmp_packet_rr *icmp_packet;
@@ -167,15 +168,21 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,return -EINVAL ; } -if (!bat_priv->primary_if) -return -EFAULT ; + primary_if =primary_if_get_selected (bat_priv); + +if (!primary_if) { + len = -EFAULT ; + goto out; + }if (len >=sizeof (struct icmp_packet_rr )) packet_len =sizeof (struct icmp_packet_rr ); skb =dev_alloc_skb (packet_len +sizeof (struct ethhdr )); -if (!skb) -return -ENOMEM ; +if (!skb) { + len = -ENOMEM ; + goto out; + }skb_reserve (skb,sizeof (struct ethhdr )); icmp_packet = (struct icmp_packet_rr *)skb_put (skb, packet_len);
@@ -233,7 +240,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff, goto dst_unreach;memcpy (icmp_packet->orig, - bat_priv->primary_if->net_dev->dev_addr,ETH_ALEN ); + primary_if->net_dev->dev_addr,ETH_ALEN );if (packet_len ==sizeof (struct icmp_packet_rr )) memcpy(icmp_packet->rr,
@@ -248,6 +255,8 @@ dst_unreach: free_skb:kfree_skb (skb); out: +if (primary_if) +hardif_free_ref (primary_if);if (neigh_node)neigh_node_free_ref (neigh_node);if (orig_node)
originator.c +21 -13
@@ -405,29 +405,34 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)struct hashtable_t *hash = bat_priv->orig_hash;struct hlist_node *node, *node_tmp;struct hlist_head *head; +struct hard_iface *primary_if;struct orig_node *orig_node;struct neigh_node *neigh_node, *neigh_node_tmp;int batman_count =0 ;int last_seen_secs;int last_seen_msecs; -int i; +int i, ret =0 ; -if ((!bat_priv->primary_if) || - (bat_priv->primary_if->if_status !=IF_ACTIVE )) { -if (!bat_priv->primary_if) -return seq_printf (seq,"BATMAN mesh %s disabled - " -"please specify interfaces to enable it\n" , - net_dev->name); + primary_if =primary_if_get_selected (bat_priv); -return seq_printf (seq,"BATMAN mesh %s " -"disabled - primary interface not active\n" , - net_dev->name); +if (!primary_if) { + ret =seq_printf (seq,"BATMAN mesh %s disabled - " +"please specify interfaces to enable it\n" , + net_dev->name); + goto out; + } + +if (primary_if->if_status !=IF_ACTIVE ) { + ret =seq_printf (seq,"BATMAN mesh %s " +"disabled - primary interface not active\n" , + net_dev->name); + goto out; }seq_printf (seq,"[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n" ,SOURCE_VERSION ,REVISION_VERSION_STR , - bat_priv->primary_if->net_dev->name, - bat_priv->primary_if->net_dev->dev_addr, net_dev->name); + primary_if->net_dev->name, + primary_if->net_dev->dev_addr, net_dev->name);seq_printf (seq," %-15s %s (%s/%i) %17s [%10s]: %20s ...\n" ,"Originator" ,"last-seen" ,"#" ,TQ_MAX_VALUE ,"Nexthop" ,"outgoingIF" ,"Potential nexthops" );
@@ -474,7 +479,10 @@ next:if (batman_count ==0 )seq_printf (seq,"No batman nodes in range ...\n" ); -return 0 ; +out: +if (primary_if) +hardif_free_ref (primary_if); +return ret; }static int orig_node_add_if (struct orig_node *orig_node,int max_if_num)
routing.c +12 -6
@@ -904,6 +904,7 @@ int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)static int recv_my_icmp_packet (struct bat_priv *bat_priv,struct sk_buff *skb,size_t icmp_len) { +struct hard_iface *primary_if =NULL ;struct orig_node *orig_node =NULL ;struct neigh_node *router =NULL ;struct icmp_packet_rr *icmp_packet;
@@ -917,7 +918,8 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv, goto out; } -if (!bat_priv->primary_if) + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) goto out;/* answer echo request (ping) */
@@ -937,8 +939,7 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv, icmp_packet = (struct icmp_packet_rr *)skb->data;memcpy (icmp_packet->dst, icmp_packet->orig,ETH_ALEN ); -memcpy (icmp_packet->orig, - bat_priv->primary_if->net_dev->dev_addr,ETH_ALEN ); +memcpy (icmp_packet->orig, primary_if->net_dev->dev_addr,ETH_ALEN ); icmp_packet->msg_type =ECHO_REPLY ; icmp_packet->ttl =TTL ;
@@ -946,6 +947,8 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv, ret =NET_RX_SUCCESS ; out: +if (primary_if) +hardif_free_ref (primary_if);if (router)neigh_node_free_ref (router);if (orig_node)
@@ -956,6 +959,7 @@ out:static int recv_icmp_ttl_exceeded (struct bat_priv *bat_priv,struct sk_buff *skb) { +struct hard_iface *primary_if =NULL ;struct orig_node *orig_node =NULL ;struct neigh_node *router =NULL ;struct icmp_packet *icmp_packet;
@@ -971,7 +975,8 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, goto out; } -if (!bat_priv->primary_if) + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) goto out;/* get routing information */
@@ -990,8 +995,7 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, icmp_packet = (struct icmp_packet *)skb->data;memcpy (icmp_packet->dst, icmp_packet->orig,ETH_ALEN ); -memcpy (icmp_packet->orig, - bat_priv->primary_if->net_dev->dev_addr,ETH_ALEN ); +memcpy (icmp_packet->orig, primary_if->net_dev->dev_addr,ETH_ALEN ); icmp_packet->msg_type =TTL_EXCEEDED ; icmp_packet->ttl =TTL ;
@@ -999,6 +1003,8 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, ret =NET_RX_SUCCESS ; out: +if (primary_if) +hardif_free_ref (primary_if);if (router)neigh_node_free_ref (router);if (orig_node)
send.c +13 -4
@@ -244,6 +244,7 @@ static void rebuild_batman_packet(struct bat_priv *bat_priv,void schedule_own_packet (struct hard_iface *hard_iface) {struct bat_priv *bat_priv =netdev_priv (hard_iface->soft_iface); +struct hard_iface *primary_if;unsigned long send_time;struct batman_packet *batman_packet;int vis_server;
@@ -253,6 +254,7 @@ void schedule_own_packet(struct hard_iface *hard_iface)return ; vis_server =atomic_read (&bat_priv->vis_mode); + primary_if =primary_if_get_selected (bat_priv);/** * the interface gets activated here to avoid race conditions between
@@ -266,7 +268,7 @@ void schedule_own_packet(struct hard_iface *hard_iface)/* if local hna has changed and interface is a primary interface */ if ((atomic_read (&bat_priv->hna_local_changed)) && - (hard_iface == bat_priv->primary_if)) + (hard_iface == primary_if))rebuild_batman_packet (bat_priv, hard_iface);/**
@@ -284,7 +286,7 @@ void schedule_own_packet(struct hard_iface *hard_iface)else batman_packet->flags &= ~VIS_SERVER ; -if ((hard_iface == bat_priv->primary_if) && +if ((hard_iface == primary_if) && (atomic_read (&bat_priv->gw_mode) ==GW_MODE_SERVER )) batman_packet->gw_flags = (uint8_t )atomic_read (&bat_priv->gw_bandwidth);
@@ -299,6 +301,9 @@ void schedule_own_packet(struct hard_iface *hard_iface) hard_iface->packet_buff, hard_iface->packet_len, hard_iface, 1, send_time); + +if (primary_if) +hardif_free_ref (primary_if); } void schedule_forward_packet(struct orig_node *orig_node,
@@ -403,6 +408,7 @@ static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,* skb is freed. */ int add_bcast_packet_to_list (struct bat_priv *bat_priv,struct sk_buff *skb) { +struct hard_iface *primary_if =NULL ;struct forw_packet *forw_packet;struct bcast_packet *bcast_packet;
@@ -411,7 +417,8 @@ int add_bcast_packet_to_list(struct bat_priv *bat_priv, struct sk_buff *skb) goto out; } -if (!bat_priv->primary_if) + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) goto out; forw_packet =kmalloc (sizeof (struct forw_packet ),GFP_ATOMIC );
@@ -430,7 +437,7 @@ int add_bcast_packet_to_list(struct bat_priv *bat_priv, struct sk_buff *skb)skb_reset_mac_header (skb); forw_packet->skb = skb; - forw_packet->if_incoming = bat_priv->primary_if; + forw_packet->if_incoming = primary_if;/* how often did we send the bcast packet ? */ forw_packet->num_packets =0 ;
@@ -443,6 +450,8 @@ packet_free: out_and_inc:atomic_inc (&bat_priv->bcast_queue_left); out: +if (primary_if) +hardif_free_ref (primary_if);return NETDEV_TX_BUSY ; }
soft-interface.c +38 -19
138 lines not on this page · raw message
translation-table.c +43 -14
109 lines not on this page · raw message
types.h +1 -1
@@ -149,7 +149,7 @@ struct bat_priv {struct hlist_head softif_neigh_list;struct softif_neigh __rcu *softif_neigh;struct debug_log *debug_log; -struct hard_iface *primary_if; +struct hard_iface __rcu *primary_if;struct kobject *mesh_obj;struct dentry *debug_dir;struct hlist_head forw_bat_list;
unicast.c +11 -5
@@ -221,15 +221,17 @@ int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv, struct hard_iface *hard_iface, uint8_t dstaddr[]) { struct unicast_packet tmp_uc, *unicast_packet; +struct hard_iface *primary_if;struct sk_buff *frag_skb;struct unicast_frag_packet *frag1, *frag2;int uc_hdr_len =sizeof (struct unicast_packet );int ucf_hdr_len =sizeof (struct unicast_frag_packet );int data_len = skb->len - uc_hdr_len; -int large_tail =0 ; +int large_tail =0 , ret =NET_RX_DROP ;uint16_t seqno; -if (!bat_priv->primary_if) + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) goto dropped; frag_skb =dev_alloc_skb (data_len - (data_len /2 ) + ucf_hdr_len);
@@ -254,7 +256,7 @@ int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv, frag1->version =COMPAT_VERSION ; frag1->packet_type =BAT_UNICAST_FRAG ; -memcpy (frag1->orig, bat_priv->primary_if->net_dev->dev_addr,ETH_ALEN ); +memcpy (frag1->orig, primary_if->net_dev->dev_addr,ETH_ALEN );memcpy (frag2, frag1,sizeof (struct unicast_frag_packet ));if (data_len &1 )
@@ -269,13 +271,17 @@ int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,send_skb_packet (skb, hard_iface, dstaddr);send_skb_packet (frag_skb, hard_iface, dstaddr); -return NET_RX_SUCCESS ; + ret =NET_RX_SUCCESS ; + goto out; drop_frag:kfree_skb (frag_skb); dropped:kfree_skb (skb); -return NET_RX_DROP ; +out: +if (primary_if) +hardif_free_ref (primary_if); +return ret; }int unicast_send_skb (struct sk_buff *skb,struct bat_priv *bat_priv)
vis.c +26 -11
@@ -204,6 +204,7 @@ static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,int vis_seq_print_text (struct seq_file *seq,void *offset) { +struct hard_iface *primary_if;struct hlist_node *node;struct hlist_head *head;struct vis_info *info;
@@ -215,15 +216,18 @@ int vis_seq_print_text(struct seq_file *seq, void *offset)HLIST_HEAD (vis_if_list);struct if_list_entry *entry;struct hlist_node *pos, *n; -int i, j; +int i, j, ret =0 ;int vis_server =atomic_read (&bat_priv->vis_mode);size_t buff_pos, buf_size;char *buff;int compare; -if ((!bat_priv->primary_if) || - (vis_server ==VIS_TYPE_CLIENT_UPDATE )) -return 0 ; + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) + goto out; + +if (vis_server ==VIS_TYPE_CLIENT_UPDATE ) + goto out; buf_size =1 ;/* Estimate length */
@@ -270,7 +274,8 @@ int vis_seq_print_text(struct seq_file *seq, void *offset) buff =kmalloc (buf_size,GFP_ATOMIC );if (!buff) {spin_unlock_bh (&bat_priv->vis_hash_lock); -return -ENOMEM ; + ret = -ENOMEM ; + goto out; } buff[0 ] ='\0' ; buff_pos =0 ;
@@ -328,7 +333,10 @@ int vis_seq_print_text(struct seq_file *seq, void *offset)seq_printf (seq,"%s" , buff);kfree (buff); -return 0 ; +out: +if (primary_if) +hardif_free_ref (primary_if); +return ret; }/* add the info packet to the send list, if it was not
@@ -815,16 +823,20 @@ out:/* only send one vis packet. called from send_vis_packets() */ static void send_vis_packet (struct bat_priv *bat_priv,struct vis_info *info) { +struct hard_iface *primary_if;struct vis_packet *packet; + primary_if =primary_if_get_selected (bat_priv); +if (!primary_if) + goto out; + packet = (struct vis_packet *)info->skb_packet->data;if (packet->ttl <2 ) {pr_debug ("Error - can't send vis packet: ttl exceeded\n" ); -return ; + goto out; } -memcpy (packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr, -ETH_ALEN ); +memcpy (packet->sender_orig, primary_if->net_dev->dev_addr,ETH_ALEN ); packet->ttl--;if (is_broadcast_ether_addr (packet->target_orig))
@@ -832,6 +844,10 @@ static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)else unicast_vis_packet (bat_priv, info); packet->ttl++;/* restore TTL */ + +out: +if (primary_if) +hardif_free_ref (primary_if); }/* called from timer; send (and maybe generate) vis packet. */
@@ -858,8 +874,7 @@ static void send_vis_packets(struct work_struct *work)kref_get (&info->refcount);spin_unlock_bh (&bat_priv->vis_hash_lock); -if (bat_priv->primary_if) -send_vis_packet (bat_priv, info); +send_vis_packet (bat_priv, info);spin_lock_bh (&bat_priv->vis_hash_lock);send_list_del (info);
Signature · 1 line
1.7.2.3
Marek Lindner
AppliedQuoted 5 lines · On Wednesday 20 April 2011 15:43:34 Marek Lindner wrote:
The rcu protected macros
rcu_dereference()andrcu_assign_pointer()for thebat_priv->primary_ifneed to be used, as well as spin/rcu locking.Otherwise we might end up using a
primary_ifpointer pointing to already freed memory.
Applied in revision 89079ae.
Regards,
Marek