* [PATCH] ifstat: Add NULL pointer check for argument of strdup()
@ 2024-01-10 20:52 Maks Mishin
2024-01-11 9:49 ` Denis Kirjanov
2024-01-13 1:46 ` Stephen Hemminger
0 siblings, 2 replies; 3+ messages in thread
From: Maks Mishin @ 2024-01-10 20:52 UTC (permalink / raw)
To: Stephen Hemminger; +Cc: Maks Mishin, netdev
When calling a strdup() function its argument do not being checked
for NULL pointer.
Added NULL pointer checks in body of get_nlmsg_extended(), get_nlmsg() and
load_raw_data() functions.
Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
---
misc/ifstat.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/misc/ifstat.c b/misc/ifstat.c
index f6f9ba50..ee301799 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -129,7 +129,12 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
abort();
n->ifindex = ifsm->ifindex;
- n->name = strdup(ll_index_to_name(ifsm->ifindex));
+ const char *name = ll_index_to_name(ifsm->ifindex);
+
+ if (name == NULL)
+ return -1;
+
+ n->name = strdup(name);
if (sub_type == NO_SUB_TYPE) {
memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));
@@ -175,7 +180,12 @@ static int get_nlmsg(struct nlmsghdr *m, void *arg)
if (!n)
abort();
n->ifindex = ifi->ifi_index;
- n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
+ const char *name = RTA_DATA(tb[IFLA_IFNAME]);
+
+ if (name == NULL)
+ return -1;
+
+ n->name = strdup(name);
memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
memset(&n->rate, 0, sizeof(n->rate));
for (i = 0; i < MAXS; i++)
@@ -263,6 +273,9 @@ static void load_raw_table(FILE *fp)
abort();
*next++ = 0;
+ if (p == NULL)
+ abort();
+
n->name = strdup(p);
p = next;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] ifstat: Add NULL pointer check for argument of strdup()
2024-01-10 20:52 [PATCH] ifstat: Add NULL pointer check for argument of strdup() Maks Mishin
@ 2024-01-11 9:49 ` Denis Kirjanov
2024-01-13 1:46 ` Stephen Hemminger
1 sibling, 0 replies; 3+ messages in thread
From: Denis Kirjanov @ 2024-01-11 9:49 UTC (permalink / raw)
To: Maks Mishin, Stephen Hemminger; +Cc: netdev
On 1/10/24 23:52, Maks Mishin wrote:
> When calling a strdup() function its argument do not being checked
> for NULL pointer.
> Added NULL pointer checks in body of get_nlmsg_extended(), get_nlmsg() and
> load_raw_data() functions.
>
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
> ---
> misc/ifstat.c | 17 +++++++++++++++--
> 1 file changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/misc/ifstat.c b/misc/ifstat.c
> index f6f9ba50..ee301799 100644
> --- a/misc/ifstat.c
> +++ b/misc/ifstat.c
> @@ -129,7 +129,12 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
> abort();
>
> n->ifindex = ifsm->ifindex;
> - n->name = strdup(ll_index_to_name(ifsm->ifindex));
> + const char *name = ll_index_to_name(ifsm->ifindex);
> +
> + if (name == NULL)
> + return -1;
> +
> + n->name = strdup(name);
>
> if (sub_type == NO_SUB_TYPE) {
> memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));
> @@ -175,7 +180,12 @@ static int get_nlmsg(struct nlmsghdr *m, void *arg)
> if (!n)
> abort();
> n->ifindex = ifi->ifi_index;
> - n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
> + const char *name = RTA_DATA(tb[IFLA_IFNAME]);
> +
> + if (name == NULL)
> + return -1;
> +
> + n->name = strdup(name);
> memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
> memset(&n->rate, 0, sizeof(n->rate));
> for (i = 0; i < MAXS; i++)
In the above cases it makes more sense to adjust errno to EINVAL in the case of the invalid argument
for strdup.
> @@ -263,6 +273,9 @@ static void load_raw_table(FILE *fp)
> abort();
> *next++ = 0;
>
> + if (p == NULL)
> + abort();
> +
> n->name = strdup(p);
> p = next;
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] ifstat: Add NULL pointer check for argument of strdup()
2024-01-10 20:52 [PATCH] ifstat: Add NULL pointer check for argument of strdup() Maks Mishin
2024-01-11 9:49 ` Denis Kirjanov
@ 2024-01-13 1:46 ` Stephen Hemminger
1 sibling, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2024-01-13 1:46 UTC (permalink / raw)
To: Maks Mishin; +Cc: netdev
On Wed, 10 Jan 2024 23:52:52 +0300
Maks Mishin <maks.mishinfz@gmail.com> wrote:
> When calling a strdup() function its argument do not being checked
> for NULL pointer.
> Added NULL pointer checks in body of get_nlmsg_extended(), get_nlmsg() and
> load_raw_data() functions.
>
> Signed-off-by: Maks Mishin <maks.mishinFZ@gmail.com>
Yes, ifstat has lots of poor memory management, but this patch
isn't addressing it.
> ---
> misc/ifstat.c | 17 +++++++++++++++--
> 1 file changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/misc/ifstat.c b/misc/ifstat.c
> index f6f9ba50..ee301799 100644
> --- a/misc/ifstat.c
> +++ b/misc/ifstat.c
> @@ -129,7 +129,12 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
> abort();
>
> n->ifindex = ifsm->ifindex;
> - n->name = strdup(ll_index_to_name(ifsm->ifindex));
> + const char *name = ll_index_to_name(ifsm->ifindex);
> +
> + if (name == NULL)
> + return -1;
> +
> + n->name = strdup(name);
>
What if strdup() returns NULL?
The error handling in ifstat is frankly aweful.
It just calls abort() everywhere.
> if (sub_type == NO_SUB_TYPE) {
> memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));
> @@ -175,7 +180,12 @@ static int get_nlmsg(struct nlmsghdr *m, void *arg)
> if (!n)
> abort();
> n->ifindex = ifi->ifi_index;
> - n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
> + const char *name = RTA_DATA(tb[IFLA_IFNAME]);
> +
> + if (name == NULL)
> + return -1;
This NULL check can't happen. A few lines above, there is check for tb[IFLA_IFNAME]
being NULL, and RTA_DATA() is macro that just offsets that netlink attribute
to find the data.
> +
> + n->name = strdup(name);
> memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
> memset(&n->rate, 0, sizeof(n->rate));
> for (i = 0; i < MAXS; i++)
> @@ -263,6 +273,9 @@ static void load_raw_table(FILE *fp)
> abort();
> *next++ = 0;
>
> + if (p == NULL)
> + abort();
> +
> n->name = strdup(p);
> p = next;
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-13 1:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-10 20:52 [PATCH] ifstat: Add NULL pointer check for argument of strdup() Maks Mishin
2024-01-11 9:49 ` Denis Kirjanov
2024-01-13 1:46 ` Stephen Hemminger
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).