[PATCH net] netlink: Fix off-by-one error in netlink_proto_init()

Jinjie Ruan posted 1 patch 3 weeks, 6 days ago
There is a newer version of this series
net/netlink/af_netlink.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH net] netlink: Fix off-by-one error in netlink_proto_init()
Posted by Jinjie Ruan 3 weeks, 6 days ago
In the error path of netlink_proto_init(), frees the already allocated
bucket table for new hash tables in a loop, but the loop condition
terminates when the index reaches zero, which fails to free the first
bucket table at index zero.

Check for >= 0 so that nl_table[0].hash is freed as well.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 net/netlink/af_netlink.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 0a9287fadb47..9601b85dda95 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2936,7 +2936,7 @@ static int __init netlink_proto_init(void)
 	for (i = 0; i < MAX_LINKS; i++) {
 		if (rhashtable_init(&nl_table[i].hash,
 				    &netlink_rhashtable_params) < 0) {
-			while (--i > 0)
+			while (--i >= 0)
 				rhashtable_destroy(&nl_table[i].hash);
 			kfree(nl_table);
 			goto panic;
-- 
2.34.1
Re: [PATCH net] netlink: Fix off-by-one error in netlink_proto_init()
Posted by Kuniyuki Iwashima 3 weeks, 6 days ago
From: Jinjie Ruan <ruanjinjie@huawei.com>
Date: Mon, 28 Oct 2024 16:05:15 +0800
> In the error path of netlink_proto_init(), frees the already allocated
> bucket table for new hash tables in a loop, but the loop condition
> terminates when the index reaches zero, which fails to free the first
> bucket table at index zero.
> 
> Check for >= 0 so that nl_table[0].hash is freed as well.
> 
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>  net/netlink/af_netlink.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index 0a9287fadb47..9601b85dda95 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -2936,7 +2936,7 @@ static int __init netlink_proto_init(void)
>  	for (i = 0; i < MAX_LINKS; i++) {
>  		if (rhashtable_init(&nl_table[i].hash,
>  				    &netlink_rhashtable_params) < 0) {
> -			while (--i > 0)
> +			while (--i >= 0)
>  				rhashtable_destroy(&nl_table[i].hash);
>  			kfree(nl_table);
>  			goto panic;

I remember the same question was posted in the past.
https://lore.kernel.org/netdev/ZfOalln%2FmyRNOkH6@cy-server/

As Eric alreday pointed out (and as mentioned in the thread above too),
it's going to panic, and we need not clean up resources here, so let's
remove rhashtable_destroy() and kfree() instead of adjusting the loop
condition.
Re: [PATCH net] netlink: Fix off-by-one error in netlink_proto_init()
Posted by Jinjie Ruan 3 weeks, 6 days ago

On 2024/10/29 2:24, Kuniyuki Iwashima wrote:
> From: Jinjie Ruan <ruanjinjie@huawei.com>
> Date: Mon, 28 Oct 2024 16:05:15 +0800
>> In the error path of netlink_proto_init(), frees the already allocated
>> bucket table for new hash tables in a loop, but the loop condition
>> terminates when the index reaches zero, which fails to free the first
>> bucket table at index zero.
>>
>> Check for >= 0 so that nl_table[0].hash is freed as well.
>>
>> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
>> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
>> ---
>>  net/netlink/af_netlink.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
>> index 0a9287fadb47..9601b85dda95 100644
>> --- a/net/netlink/af_netlink.c
>> +++ b/net/netlink/af_netlink.c
>> @@ -2936,7 +2936,7 @@ static int __init netlink_proto_init(void)
>>  	for (i = 0; i < MAX_LINKS; i++) {
>>  		if (rhashtable_init(&nl_table[i].hash,
>>  				    &netlink_rhashtable_params) < 0) {
>> -			while (--i > 0)
>> +			while (--i >= 0)
>>  				rhashtable_destroy(&nl_table[i].hash);
>>  			kfree(nl_table);
>>  			goto panic;
> 
> I remember the same question was posted in the past.
> https://lore.kernel.org/netdev/ZfOalln%2FmyRNOkH6@cy-server/
> 
> As Eric alreday pointed out (and as mentioned in the thread above too),
> it's going to panic, and we need not clean up resources here, so let's
> remove rhashtable_destroy() and kfree() instead of adjusting the loop
> condition.

Thank you very much, remove it is fine.

>
Re: [PATCH net] netlink: Fix off-by-one error in netlink_proto_init()
Posted by Eric Dumazet 3 weeks, 6 days ago
On Mon, Oct 28, 2024 at 9:05 AM Jinjie Ruan <ruanjinjie@huawei.com> wrote:
>
> In the error path of netlink_proto_init(), frees the already allocated
> bucket table for new hash tables in a loop, but the loop condition
> terminates when the index reaches zero, which fails to free the first
> bucket table at index zero.
>
> Check for >= 0 so that nl_table[0].hash is freed as well.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>  net/netlink/af_netlink.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
> index 0a9287fadb47..9601b85dda95 100644
> --- a/net/netlink/af_netlink.c
> +++ b/net/netlink/af_netlink.c
> @@ -2936,7 +2936,7 @@ static int __init netlink_proto_init(void)
>         for (i = 0; i < MAX_LINKS; i++) {
>                 if (rhashtable_init(&nl_table[i].hash,
>                                     &netlink_rhashtable_params) < 0) {
> -                       while (--i > 0)
> +                       while (--i >= 0)
>                                 rhashtable_destroy(&nl_table[i].hash);
>                         kfree(nl_table);
>                         goto panic;
> --

Note that the host is going to panic, many other pieces of memory are
left behind.

A Fixes: tag seems unnecessary.