netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Kuniyuki Iwashima <kuniyu@amazon.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	Patrick McHardy <kaber@trash.net>,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	Christophe Ricard <christophe-h.ricard@st.com>,
	Johannes Berg <johannes.berg@intel.com>,
	David Ahern <dsahern@gmail.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>,
	Brad Spencer <bspencer@blackberry.com>
Subject: Re: [PATCH v2 net] netlink: Use copy_to_user() for optval in netlink_getsockopt().
Date: Thu, 20 Apr 2023 20:33:24 -0700	[thread overview]
Message-ID: <20230420203324.04c50e8d@kernel.org> (raw)
In-Reply-To: <20230420233351.77166-1-kuniyu@amazon.com>

On Thu, 20 Apr 2023 16:33:51 -0700 Kuniyuki Iwashima wrote:
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index f365dfdd672d..5c0d17b3984c 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -1742,7 +1742,7 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
>  {
>  	struct sock *sk = sock->sk;
>  	struct netlink_sock *nlk = nlk_sk(sk);
> -	int len, val, err;
> +	int len, val;
>  
>  	if (level != SOL_NETLINK)
>  		return -ENOPROTOOPT;
> @@ -1753,40 +1753,27 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
>  		return -EINVAL;
>  
>  	switch (optname) {
> -	case NETLINK_PKTINFO:
> +	case NETLINK_LIST_MEMBERSHIPS:
> +		break;
> +	default:
>  		if (len < sizeof(int))
>  			return -EINVAL;
>  		len = sizeof(int);
> +	}


> -		err = 0;
>  		break;
>  	default:
> -		err = -ENOPROTOOPT;
> +		return -ENOPROTOOPT;
>  	}
> -	return err;
> +
> +	if (put_user(len, optlen) ||
> +	    copy_to_user(optval, &val, len))
> +		return -EFAULT;

Maybe this is a nit pick but we'd unnecessarily change the return value
to unknown opts when len < 4, from -ENOPROTOOPT to -EINVAL, right?

How about we do:

	unsigned int flag;

	flag = 0;
	switch (optname) {
	case NETLINK_PKTINFO:
		flag = NETLINK_F_RECV_PKTINFO;
 		break;
 	case NETLINK_BROADCAST_ERROR:
		flag = NETLINK_F_BROADCAST_SEND_ERROR;
		break;
	...
	case NETLINK_LIST_MEMBERSHIPS: {
	...
	default:
		return -ENOPROTOOPT;
	}

	if (flag) {
		if (len < sizeof(int))
			return -EINVAL;
		len = sizeof(int);
 		val = nlk->flags & flag ? 1 : 0;
		if (put_user(len, optlen) || 
		    copy_to_user(optval, &val, len))
			return -EFAULT;
		...
	}

  reply	other threads:[~2023-04-21  3:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-20 23:33 [PATCH v2 net] netlink: Use copy_to_user() for optval in netlink_getsockopt() Kuniyuki Iwashima
2023-04-21  3:33 ` Jakub Kicinski [this message]
2023-04-21 17:50   ` Kuniyuki Iwashima
2023-04-21  7:56 ` Johannes Berg
2023-04-21 17:52   ` Kuniyuki Iwashima

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230420203324.04c50e8d@kernel.org \
    --to=kuba@kernel.org \
    --cc=bspencer@blackberry.com \
    --cc=christophe-h.ricard@st.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=edumazet@google.com \
    --cc=johannes.berg@intel.com \
    --cc=kaber@trash.net \
    --cc=kuni1840@gmail.com \
    --cc=kuniyu@amazon.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).