From: Stephen Hemminger <stephen@networkplumber.org>
To: Serhey Popovych <serhe.popovych@gmail.com>
Cc: netdev@vger.kernel.org
Subject: Re: [PATCH iproute2 3/3] utils: ll_map: Make network device name fixed size array of char
Date: Thu, 28 Dec 2017 09:45:32 -0800 [thread overview]
Message-ID: <20171228094532.4a653cc0@xeon-e3> (raw)
In-Reply-To: <1513755451-9800-4-git-send-email-serhe.popovych@gmail.com>
On Wed, 20 Dec 2017 09:37:31 +0200
Serhey Popovych <serhe.popovych@gmail.com> wrote:
> Network device names are fixed in size and never exceed
> IFNAMSIZ (16 bytes).
>
> Make name fixed size array to always malloc() same size chunk
> of memory and use memcpy()/memcmp() with constant IFNAMSIZ
> to benefit from possible compiler optimizations replacing
> call to a function with two/four load/store instructions
> on 64/32 bit systems.
>
> Check if IFLA_IFNAME attribute present in netlink message
> (should always) and use strncpy() to pad name with zeros.
>
> Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
> ---
> lib/ll_map.c | 20 ++++++++++++--------
> 1 file changed, 12 insertions(+), 8 deletions(-)
>
> diff --git a/lib/ll_map.c b/lib/ll_map.c
> index abe7bdc..fcbf0fb 100644
> --- a/lib/ll_map.c
> +++ b/lib/ll_map.c
> @@ -30,7 +30,7 @@ struct ll_cache {
> unsigned flags;
> unsigned index;
> unsigned short type;
> - char name[];
> + char name[IFNAMSIZ];
> };
>
> #define IDXMAP_SIZE 1024
> @@ -71,7 +71,7 @@ static struct ll_cache *ll_get_by_name(const char *name)
> struct ll_cache *im
> = container_of(n, struct ll_cache, name_hash);
>
> - if (strncmp(im->name, name, IFNAMSIZ) == 0)
> + if (!strcmp(im->name, name))
> return im;
> }
>
> @@ -82,7 +82,7 @@ int ll_remember_index(const struct sockaddr_nl *who,
> struct nlmsghdr *n, void *arg)
> {
> unsigned int h;
> - const char *ifname;
> + char ifname[IFNAMSIZ];
> struct ifinfomsg *ifi = NLMSG_DATA(n);
> struct ll_cache *im;
> struct rtattr *tb[IFLA_MAX+1];
> @@ -105,17 +105,21 @@ int ll_remember_index(const struct sockaddr_nl *who,
> }
>
> parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
> - ifname = rta_getattr_str(tb[IFLA_IFNAME]);
> - if (ifname == NULL)
> +
> + if (!tb[IFLA_IFNAME])
> + return 0;
> + strncpy(ifname, rta_getattr_str(tb[IFLA_IFNAME]), IFNAMSIZ);
> + if (!ifname[0])
> return 0;
> + ifname[IFNAMSIZ - 1] = '\0';
>
> if (im) {
> /* change to existing entry */
> - rehash = strcmp(im->name, ifname);
> + rehash = memcmp(im->name, ifname, IFNAMSIZ);
This is not safe. There is not guarantee that bytes after end of string are zero.
And in your code, strncpy() will overwrite characters from the beginning to null,
it will not overwrite after that. Then comparison with entry may not work because
of the data after that.
I really doubt this is critical path on anything. Probably just having a better
hash table implementation would solve that.
next prev parent reply other threads:[~2017-12-28 18:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-20 7:37 [PATCH iproute2 0/3] Forbid "type" for peer, update ifname and make it array in ll_cache Serhey Popovych
2017-12-20 7:37 ` [PATCH iproute2 1/3] vxcan,veth: Forbid "type" for peer device Serhey Popovych
2017-12-26 17:05 ` Stephen Hemminger
2017-12-28 10:54 ` Serhey Popovych
2017-12-28 11:01 ` Serhey Popovych
2017-12-28 17:40 ` Stephen Hemminger
2017-12-20 7:37 ` [PATCH iproute2 2/3] utils: ll_map: Update name and type for existing entry Serhey Popovych
2017-12-28 17:46 ` Stephen Hemminger
2017-12-28 18:22 ` Serhey Popovych
2017-12-20 7:37 ` [PATCH iproute2 3/3] utils: ll_map: Make network device name fixed size array of char Serhey Popovych
2017-12-28 17:45 ` Stephen Hemminger [this message]
2017-12-28 18:17 ` Serhey Popovych
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=20171228094532.4a653cc0@xeon-e3 \
--to=stephen@networkplumber.org \
--cc=netdev@vger.kernel.org \
--cc=serhe.popovych@gmail.com \
/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).