Skip to contentSkip to conversation

Question about potential memleak in netlink_proto_init()

net/netlink Chenyuan Yang 4 participants 5 messages Mar 15, 2024 – Mar 16, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Dear Netlink Developers,

We are curious whether the function netlink_proto_init() might have a memory leak.

The function is https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
and the relevant code is

```
static int __init netlink_proto_init(void)
{
	int i;
  ...

	for (i = 0; i < MAX_LINKS; i++) {
		if (rhashtable_init(&nl_table[i].hash,
				    &netlink_rhashtable_params) < 0) {
			while (--i > 0)
				rhashtable_destroy(&nl_table[i].hash);
			kfree(nl_table);
			goto panic;
		}
	}
  ...
}
```

In the for loop, when rhashtable_init() fails, the function will free the allocated memory for nl_table[i].hash by checking while (--i > 0). However, the first element (i=1) of nl_table is not freed since i is decremented before the check.

Based on our understanding, a possible fix would be

```
-      while (--i > 0)
+      while (--i >= 0)
```

Please kindly correct us if we missed any key information. Looking forward to your response!

Best,
Chenyuan

#1 lore raw
Quoted 3 lines · Chenyuan Yang

Dear Netlink Developers,

We are curious whether the function netlink_proto_init() might have a memory leak.

Yes, but

Quoted 8 lines

The function is https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
and the relevant code is
```
static int __init netlink_proto_init(void)

{
	int i;

...

Quoted 7 lines
	for (i = 0; i < MAX_LINKS; i++) {
		if (rhashtable_init(&nl_table[i].hash,
				    &netlink_rhashtable_params) < 0) {
			while (--i > 0)
				rhashtable_destroy(&nl_table[i].hash);
			kfree(nl_table);
			goto panic;

... this calls panic(), kernel will crash intentionally.

Perhaps best patch would be to remove this error handling and panic straight away, this is pretty much dead code.

#2 lore raw
In reply to Chenyuan Yang

From: Chenyuan Yang <[email protected]>
Date: Thu, 14 Mar 2024 19:47:18 -0500

Quoted 12 lines

Dear Netlink Developers,

We are curious whether the function netlink_proto_init() might have a memory leak.

The function is https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
and the relevant code is
```
static int __init netlink_proto_init(void)

{
	int i;

...

Quoted 7 lines
	for (i = 0; i < MAX_LINKS; i++) {
		if (rhashtable_init(&nl_table[i].hash,
				    &netlink_rhashtable_params) < 0) {
			while (--i > 0)
				rhashtable_destroy(&nl_table[i].hash);
			kfree(nl_table);
			goto panic;

If rhashtable_init() fails, the kernel panic occurs, so there's no real memleak issue.

Quoted 15 lines
		}
	}
  ...
}
```

In the for loop, when `rhashtable_init()` fails, the function will free
the allocated memory for `nl_table[i].hash` by checking `while (--i > 0)`.
However, the first element (`i=1`) of `nl_table` is not freed since `i` is
decremented before the check.

Based on our understanding, a possible fix would be
```
(quoted diff) +1 -1
-      while (--i > 0)
+      while (--i >= 0)
Quoted 1 line

```

Change itself looks good, no need for cleanup in the first place though.

#3 lore raw
In reply to Chenyuan Yang
Quoted 36 lines · Chenyuan Yang

Dear Netlink Developers,

We are curious whether the function netlink_proto_init() might have a memory leak.

The function is
https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
and the relevant code is

```
static int __init netlink_proto_init(void)
{
	int i;
  ...

	for (i = 0; i < MAX_LINKS; i++) {
		if (rhashtable_init(&nl_table[i].hash,
				    &netlink_rhashtable_params) < 0) {
			while (--i > 0)
				rhashtable_destroy(&nl_table[i].hash);
			kfree(nl_table);
			goto panic;
		}
	}
  ...
}
```

In the for loop, when rhashtable_init() fails, the function will free the allocated memory for nl_table[i].hash by checking while (--i > 0). However, the first element (i=1) of nl_table is not freed since i is decremented before the check.

Based on our understanding, a possible fix would be ```

(quoted diff) +1 -1
-      while (--i > 0)
+      while (--i >= 0)
Quoted 1 line

```

The better pattern (and widely used in kernel) is

	while (i--)
Quoted 2 lines

Please kindly correct us if we missed any key information. Looking forward to your response!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Thank you all for sharing your insights on this issue.

I'm pondering over the best solution: should we follow Kuniyuki's suggestion to "eliminate the cleanup code," or would it be better to adopt Florian's approach of "eschewing error handling in favor of immediate panic"?

Best,
Chenyuan

Quoted 52 lines · Andy Shevchenko
Quoted 36 lines · Chenyuan Yang

Dear Netlink Developers,

We are curious whether the function netlink_proto_init() might have a memory leak.

The function is
https://elixir.bootlin.com/linux/v6.8/source/net/netlink/af_netlink.c#L2908
and the relevant code is

```
static int __init netlink_proto_init(void)
{
      int i;
  ...

      for (i = 0; i < MAX_LINKS; i++) {
              if (rhashtable_init(&nl_table[i].hash,
                                  &netlink_rhashtable_params) < 0) {
                      while (--i > 0)
                              rhashtable_destroy(&nl_table[i].hash);
                      kfree(nl_table);
                      goto panic;
              }
      }
  ...
}
```

In the for loop, when rhashtable_init() fails, the function will free the allocated memory for nl_table[i].hash by checking while (--i > 0). However, the first element (i=1) of nl_table is not freed since i is decremented before the check.

Based on our understanding, a possible fix would be ```

(quoted diff) +1 -1
-      while (--i > 0)
+      while (--i >= 0)
Quoted 1 line

```

The better pattern (and widely used in kernel) is

        while (i--)
Quoted 2 lines

Please kindly correct us if we missed any key information. Looking forward to your response!

--
With Best Regards,
Andy Shevchenko