Skip to content

Commit

Permalink
net/hsr: Fix NULL pointer dereference and refcnt bugs when deleting a…
Browse files Browse the repository at this point in the history
… HSR interface.

To repeat:

$ sudo ip link del hsr0
BUG: unable to handle kernel NULL pointer dereference at 0000000000000018
IP: [<ffffffff8187f495>] hsr_del_port+0x15/0xa0
etc...

Bug description:

As part of the hsr master device destruction, hsr_del_port() is called for each of
the hsr ports. At each such call, the master device is updated regarding features
and mtu. When the master device is freed before the slave interfaces, master will
be NULL in hsr_del_port(), which led to a NULL pointer dereference.

Additionally, dev_put() was called on the master device itself in hsr_del_port(),
causing a refcnt error.

A third bug in the same code path was that the rtnl lock was not taken before
hsr_del_port() was called as part of hsr_dev_destroy().

The reporter (Nicolas Dichtel) also said: "hsr_netdev_notify() supposes that the
port will always be available when the notification is for an hsr interface. It's
wrong. For example, netdev_wait_allrefs() may resend NETDEV_UNREGISTER.". As a
precaution against this, a check for port == NULL was added in hsr_dev_notify().

Reported-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Fixes: 51f3c60 ("net/hsr: Move slave init to hsr_slave.c.")
Signed-off-by: Arvid Brodin <arvid.brodin@alten.se>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Arvid Brodin authored and David S. Miller committed Mar 1, 2015
1 parent 187d678 commit 56b08fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions net/hsr/hsr_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,11 @@ static void hsr_dev_destroy(struct net_device *hsr_dev)
struct hsr_port *port;

hsr = netdev_priv(hsr_dev);

rtnl_lock();
hsr_for_each_port(hsr, port)
hsr_del_port(port);
rtnl_unlock();

del_timer_sync(&hsr->prune_timer);
del_timer_sync(&hsr->announce_timer);
Expand Down
4 changes: 4 additions & 0 deletions net/hsr/hsr_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
return NOTIFY_DONE; /* Not an HSR device */
hsr = netdev_priv(dev);
port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
if (port == NULL) {
/* Resend of notification concerning removed device? */
return NOTIFY_DONE;
}
} else {
hsr = port->hsr;
}
Expand Down
10 changes: 7 additions & 3 deletions net/hsr/hsr_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ void hsr_del_port(struct hsr_port *port)
list_del_rcu(&port->port_list);

if (port != master) {
netdev_update_features(master->dev);
dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
if (master != NULL) {
netdev_update_features(master->dev);
dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
}
netdev_rx_handler_unregister(port->dev);
dev_set_promiscuity(port->dev, -1);
}
Expand All @@ -192,5 +194,7 @@ void hsr_del_port(struct hsr_port *port)
*/

synchronize_rcu();
dev_put(port->dev);

if (port != master)
dev_put(port->dev);
}

0 comments on commit 56b08fd

Please sign in to comment.