netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <kuba@kernel.org>
Cc: <bspencer@blackberry.com>, <christophe-h.ricard@st.com>,
	<davem@davemloft.net>, <dsahern@gmail.com>, <edumazet@google.com>,
	<johannes.berg@intel.com>, <kaber@trash.net>,
	<kuni1840@gmail.com>, <kuniyu@amazon.com>,
	<netdev@vger.kernel.org>, <pabeni@redhat.com>,
	<pablo@netfilter.org>
Subject: Re: [PATCH v2 net] netlink: Use copy_to_user() for optval in netlink_getsockopt().
Date: Fri, 21 Apr 2023 10:50:52 -0700	[thread overview]
Message-ID: <20230421175052.75494-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20230420203324.04c50e8d@kernel.org>

From:   Jakub Kicinski <kuba@kernel.org>
Date:   Thu, 20 Apr 2023 20:33:24 -0700
> 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?

Exactly, we should not change it.

> 
> How about we do:

This is cleaner, will do in v3.

Thanks!

> 
> 	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 17:51 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
2023-04-21 17:50   ` Kuniyuki Iwashima [this message]
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=20230421175052.75494-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --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=kuba@kernel.org \
    --cc=kuni1840@gmail.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).