From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: Re: [PATCH iproute2 v2] ipaddress: strengthen check on 'label' input Date: Fri, 1 Jun 2018 15:56:41 -0400 Message-ID: <20180601155641.76616597@shemminger-XPS-13-9360> References: <1527605827-25516-1-git-send-email-ptalbert@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: netdev@vger.kernel.org To: Patrick Talbert Return-path: Received: from mail-qt0-f193.google.com ([209.85.216.193]:39244 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752105AbeFAT4p (ORCPT ); Fri, 1 Jun 2018 15:56:45 -0400 Received: by mail-qt0-f193.google.com with SMTP id p23-v6so8764309qtn.6 for ; Fri, 01 Jun 2018 12:56:45 -0700 (PDT) In-Reply-To: <1527605827-25516-1-git-send-email-ptalbert@redhat.com> Sender: netdev-owner@vger.kernel.org List-ID: On Tue, 29 May 2018 16:57:07 +0200 Patrick Talbert wrote: > As mentioned in the ip-address man page, an address label must > be equal to the device name or prefixed by the device name > followed by a colon. Currently the only check on this input is > to see if the device name appears at the beginning of the label > string. > > This commit adds an additional check to ensure label == dev or > continues with a colon. > > Signed-off-by: Patrick Talbert > Suggested-by: Stephen Hemminger Yes, this looks better but still have some feedback. > --- > ip/ipaddress.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/ip/ipaddress.c b/ip/ipaddress.c > index 00da14c..fce2008 100644 > --- a/ip/ipaddress.c > +++ b/ip/ipaddress.c > @@ -2040,6 +2040,22 @@ static bool ipaddr_is_multicast(inet_prefix *a) > return false; > } > > +static bool is_valid_label(const char *dev, const char *label) > +{ > + char alias[strlen(dev) + 1]; > + > + if (strlen(label) < strlen(dev)) > + return false; > + > + strcpy(alias, dev); > + strcat(alias, ":"); > + if (strncmp(label, dev, strlen(dev)) == 0 || > + strncmp(label, alias, strlen(alias)) == 0) > + return true; > + else > + return false; > +} This string copying and comparison still is much more overhead than it needs to be. The following tests out to be equivalent with a single strncmp and strlen. Why not just: diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 00da14c6f97c..eac489e94fe4 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -2040,6 +2040,16 @@ static bool ipaddr_is_multicast(inet_prefix *a) return false; } +static bool is_valid_label(const char *label, const char *dev) +{ + size_t len = strlen(dev); + + if (strncmp(label, dev, len) != 0) + return false; + + return label[len] == '\0' || label[len] == ':'; +} + Doesn't matter much now, but code seems to get copied.