netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iproute2: ip maddress: Check multiaddr length
@ 2020-08-14  8:46 Sascha Hauer
  2020-08-14 15:27 ` Stephen Hemminger
  0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2020-08-14  8:46 UTC (permalink / raw)
  To: netdev; +Cc: kernel, Sascha Hauer

ip maddress add|del takes a MAC address as argument, so insist on
getting a length of ETH_ALEN bytes. This makes sure the passed argument
is actually a MAC address and especially not an IPv4 address which
was previously accepted and silently taken as a MAC address.

While at it, do not print *argv in the error path as this has been
modified by ll_addr_a2n() and doesn't contain the full string anymore,
which can lead to misleading error messages.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 ip/ipmaddr.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
index 3400e055..9979ed58 100644
--- a/ip/ipmaddr.c
+++ b/ip/ipmaddr.c
@@ -291,7 +291,7 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
 {
 	struct ifreq ifr = {};
 	int family;
-	int fd;
+	int fd, len;
 
 	if (cmd == RTM_NEWADDR)
 		cmd = SIOCADDMULTI;
@@ -313,9 +313,12 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
 				usage();
 			if (ifr.ifr_hwaddr.sa_data[0])
 				duparg("address", *argv);
-			if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data,
-					14, *argv) < 0) {
-				fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv);
+			len = ll_addr_a2n(ifr.ifr_hwaddr.sa_data, 14, *argv);
+			if (len < 0)
+				exit(1);
+
+			if (len != ETH_ALEN) {
+				fprintf(stderr, "Error: Invalid address length %d - must be %d bytes\n", len, ETH_ALEN);
 				exit(1);
 			}
 		}
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iproute2: ip maddress: Check multiaddr length
  2020-08-14  8:46 [PATCH] iproute2: ip maddress: Check multiaddr length Sascha Hauer
@ 2020-08-14 15:27 ` Stephen Hemminger
  2020-08-17 11:25   ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Hemminger @ 2020-08-14 15:27 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: netdev, kernel

On Fri, 14 Aug 2020 10:46:26 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> ip maddress add|del takes a MAC address as argument, so insist on
> getting a length of ETH_ALEN bytes. This makes sure the passed argument
> is actually a MAC address and especially not an IPv4 address which
> was previously accepted and silently taken as a MAC address.
> 
> While at it, do not print *argv in the error path as this has been
> modified by ll_addr_a2n() and doesn't contain the full string anymore,
> which can lead to misleading error messages.
> 
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  ip/ipmaddr.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
> index 3400e055..9979ed58 100644
> --- a/ip/ipmaddr.c
> +++ b/ip/ipmaddr.c
> @@ -291,7 +291,7 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
>  {
>  	struct ifreq ifr = {};
>  	int family;
> -	int fd;
> +	int fd, len;
>  
>  	if (cmd == RTM_NEWADDR)
>  		cmd = SIOCADDMULTI;
> @@ -313,9 +313,12 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
>  				usage();
>  			if (ifr.ifr_hwaddr.sa_data[0])
>  				duparg("address", *argv);
> -			if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data,
> -					14, *argv) < 0) {
> -				fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv);
> +			len = ll_addr_a2n(ifr.ifr_hwaddr.sa_data, 14, *argv);

While you are at it, get rid of the hard code 14 here and use sizeof(ifr.ifr_hwaddr.sa_data)?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iproute2: ip maddress: Check multiaddr length
  2020-08-14 15:27 ` Stephen Hemminger
@ 2020-08-17 11:25   ` Sascha Hauer
  0 siblings, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2020-08-17 11:25 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev, kernel

On Fri, Aug 14, 2020 at 08:27:56AM -0700, Stephen Hemminger wrote:
> On Fri, 14 Aug 2020 10:46:26 +0200
> Sascha Hauer <s.hauer@pengutronix.de> wrote:
> 
> > ip maddress add|del takes a MAC address as argument, so insist on
> > getting a length of ETH_ALEN bytes. This makes sure the passed argument
> > is actually a MAC address and especially not an IPv4 address which
> > was previously accepted and silently taken as a MAC address.
> > 
> > While at it, do not print *argv in the error path as this has been
> > modified by ll_addr_a2n() and doesn't contain the full string anymore,
> > which can lead to misleading error messages.
> > 
> > Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> > ---
> >  ip/ipmaddr.c | 11 +++++++----
> >  1 file changed, 7 insertions(+), 4 deletions(-)
> > 
> > diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
> > index 3400e055..9979ed58 100644
> > --- a/ip/ipmaddr.c
> > +++ b/ip/ipmaddr.c
> > @@ -291,7 +291,7 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
> >  {
> >  	struct ifreq ifr = {};
> >  	int family;
> > -	int fd;
> > +	int fd, len;
> >  
> >  	if (cmd == RTM_NEWADDR)
> >  		cmd = SIOCADDMULTI;
> > @@ -313,9 +313,12 @@ static int multiaddr_modify(int cmd, int argc, char **argv)
> >  				usage();
> >  			if (ifr.ifr_hwaddr.sa_data[0])
> >  				duparg("address", *argv);
> > -			if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data,
> > -					14, *argv) < 0) {
> > -				fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv);
> > +			len = ll_addr_a2n(ifr.ifr_hwaddr.sa_data, 14, *argv);
> 
> While you are at it, get rid of the hard code 14 here and use sizeof(ifr.ifr_hwaddr.sa_data)?

Ok. I just sent out a v2.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-17 11:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-14  8:46 [PATCH] iproute2: ip maddress: Check multiaddr length Sascha Hauer
2020-08-14 15:27 ` Stephen Hemminger
2020-08-17 11:25   ` Sascha Hauer

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).