Skip to contentSkip to conversation

netns: use a spin_lock to protect nsid management

PATCH Applied Dan Carpenter 4 participants 6 messages May 12, 2015 – May 15, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hello Nicolas Dichtel,

The patch 95f38411df05: "netns: use a spin_lock to protect nsid management" from May 7, 2015, leads to the following static checker warning:

	net/core/net_namespace.c:580 rtnl_net_newid()
	warn: inconsistent returns 'spin_lock:&nsid_lock'.

net/core/net_namespace.c
565
566 spin_lock_irqsave(&nsid_lock, flags);
567 if (__peernet2id(net, peer) >= 0) {
568 err = -EEXIST;
569 goto out;

I don't know if __peernet2id() unlocks on error but it can't possibly restore flags so this isn't right.

   570          }
   571  
   572          err = alloc_netid(net, peer, nsid);
   573          spin_unlock_irqrestore(&nsid_lock, flags);
   574          if (err >= 0) {
   575                  rtnl_net_notifyid(net, RTM_NEWNSID, err);
   576                  err = 0;
   577          }
   578  out:
   579          put_net(peer);
   580          return err;
   581  }

regards,
dan carpenter

#1 lore raw

(Please always Cc netdev for networking bugs)

Quoted 11 lines · Dan Carpenter

Hello Nicolas Dichtel,

The patch 95f38411df05: "netns: use a spin_lock to protect nsid management" from May 7, 2015, leads to the following static checker warning:

net/core/net_namespace.c:580 rtnl_net_newid()
warn: inconsistent returns 'spin_lock:&nsid_lock'.

net/core/net_namespace.c
565

Quoted 8 lines
   566          spin_lock_irqsave(&nsid_lock, flags);
   567          if (__peernet2id(net, peer) >= 0) {
   568                  err = -EEXIST;
   569                  goto out;

I don't know if __peernet2id() unlocks on error but it can't possibly restore flags so this isn't right.

Yes it is a bug.

Also why do we have to disable IRQ when holding the spinlock? IOW, which case takes the spin lock in IRQ context?

Thanks.

#2 lore raw

Le 12/05/2015 21:16, Cong Wang a écrit :
[snip]

Quoted 2 lines

Also why do we have to disable IRQ when holding the spinlock? IOW, which case takes the spin lock in IRQ context?

peernet2id() is called by do_one_broadcast() (net/netlink/af_netlink.c). So every callers of netlink_broadcast[_filtered]()/nlmsg_unicast() /nlmsg_multicast() may needs this lock.
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Dan Carpenter
Quoted 10 lines · Dan Carpenter

Hello Nicolas Dichtel,

The patch 95f38411df05: "netns: use a spin_lock to protect nsid management" from May 7, 2015, leads to the following static checker warning:

	net/core/net_namespace.c:580 rtnl_net_newid()
	warn: inconsistent returns 'spin_lock:&nsid_lock'.

net/core/net_namespace.c

Quoted 8 lines
    565
    566          spin_lock_irqsave(&nsid_lock, flags);
    567          if (__peernet2id(net, peer) >= 0) {
    568                  err = -EEXIST;
    569                  goto out;

I don't know if __peernet2id() unlocks on error but it can't possibly restore flags so this isn't right.

It's a bug. I will send a patch.

Thank you for the report.

Nicolas
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

PATCH 1/1 netns: fix unbalanced spin_lock on error

Unlock was missing on error path.

Fixes 95f38411df05 ("netns: use a spin_lock to protect nsid management")
---
 net/core/net_namespace.c | 1 +
 1 file changed, 1 insertion(+)
net/core/net_namespace.c +1 -0
index a665bf490c88..cb32c0378c10 100644
@@ -565,6 +565,7 @@ static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh)
 
 	spin_lock_irqsave(&nsid_lock, flags);
 	if (__peernet2id(net, peer) >= 0) {
+		spin_unlock_irqrestore(&nsid_lock, flags);
 		err = -EEXIST;
 		goto out;
 	}
Signature · 1 line
2.2.2