Subject: Re: [PATCH v2 net-next] sockopt: Change getsockopt() of SO_BINDTODEVICE to return an interface name

From: Pavel Emelyanov <[email-masked]>
Date: Sat, 17 Nov 2012 09:06:24 +0400

> @@ -4165,6 +4180,8 @@ static int dev_ifname(struct net *net, struct ifreq __user
> *arg)
> 
>  	strcpy(ifr.ifr_name, dev->name);
>  	rcu_read_unlock();
> +	if (read_seqretry(&devnet_rename_seq, seq))
> +		goto retry;

I believe it makes sense to make the seqcount protection as a separate patch
with description of what may happen.

> 
>  	if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
>  		return -EFAULT;

> @@ -562,6 +563,59 @@ out:
>  	return ret;
>  }
> 
> +static int sock_getbindtodevice(struct sock *sk, char __user *optval,
> +				int __user *optlen, int len)
> +{
> +	int ret = -ENOPROTOOPT;
> +#ifdef CONFIG_NETDEVICES
> +	struct net *net = sock_net(sk);
> +	struct net_device *dev;
> +	char devname[IFNAMSIZ];
> +	unsigned seq;
> +
> +	if (sk->sk_bound_dev_if == 0) {
> +		len = 0;
> +		goto zero;
> +	}
> +
> +	ret = -EINVAL;
> +	if (len < IFNAMSIZ)
> +		goto out;
> +
> +retry:
> +	seq = read_seqbegin(&devnet_rename_seq);
> +	rcu_read_lock();
> +	dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);

The sk->sk_bound_dev_if might have changed to 0 while we did read_seqretry (or
did the len check above, but the race window is smaller) and this code will
report -ENODEV instead of zero lenght.

Other than this, the intention looks OK to me.

> +	ret = -ENODEV;
> +	if (!dev) {
> +		rcu_read_unlock();
> +		goto out;
> +	}
> +
> +	strcpy(devname, dev->name);
> +	rcu_read_unlock();
> +	if (read_seqretry(&devnet_rename_seq, seq))
> +		goto retry;
> +
> +	len = strlen(devname) + 1;
> +
> +	ret = -EFAULT;
> +	if (copy_to_user(optval, devname, len))
> +		goto out;
> +

Thanks,
Pavel