Skip to contentSkip to conversation

ipv4: fix error path in fou_create()

PATCH In review Kangmin Park 2 participants 2 messages Aug 8, 2021

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

PATCH v3 1/1 ipv4: fix error path in fou_create()

sock is always NULL when udp_sock_create() is failed and fou is always NULL when kzalloc() is failed in error label.

So, add error_sock and error_alloc label and fix the error path in those cases.

---
v3:
- change commit message
- fix error path

---
 net/ipv4/fou.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/fou.c +4 -3
index e5f69b0bf3df..f1d99e776bb8 100644
@@ -572,13 +572,13 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
 	/* Open UDP socket */
 	err = udp_sock_create(net, &cfg->udp_config, &sock);
 	if (err < 0)
-		goto error;
+		goto error_sock;
 
 	/* Allocate FOU port structure */
 	fou = kzalloc(sizeof(*fou), GFP_KERNEL);
 	if (!fou) {
 		err = -ENOMEM;
-		goto error;
+		goto error_alloc;
 	}
 
 	sk = sock->sk;
@@ -627,9 +627,10 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
 
 error:
 	kfree(fou);
+error_alloc:
 	if (sock)
 		udp_tunnel_sock_release(sock);
-
+error_sock:
 	return err;
 }
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 38 lines · Kangmin Park

sock is always NULL when udp_sock_create() is failed and fou is always NULL when kzalloc() is failed in error label.

So, add error_sock and error_alloc label and fix the error path in those cases.

---
v3:
- change commit message
- fix error path

---
 net/ipv4/fou.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/ipv4/fou.c +2 -2
index e5f69b0bf3df..f1d99e776bb8 100644
@@ -572,13 +572,13 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
 	/* Open UDP socket */
 	err = udp_sock_create(net, &cfg->udp_config, &sock);
 	if (err < 0)
-		goto error;
+		goto error_sock;
 
 	/* Allocate FOU port structure */
 	fou = kzalloc(sizeof(*fou), GFP_KERNEL);
 	if (!fou) {
 		err = -ENOMEM;
-		goto error;
+		goto error_alloc;
 	}
 
 	sk = sock->sk;
@@ -627,9 +627,10 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
 

net/ipv4/fou.c static int fou_create(struct net *net, struct fou_cfg *cfg, · line 636

Quoted 10 lines
net/ipv4/fou.c +2 -1
 error:
 	kfree(fou);
+error_alloc:
 	if (sock)
 		udp_tunnel_sock_release(sock);
-
+error_sock:
 	return err;
 }
 

since sock and fou are initialized to NULL, kfree(NULL) is allowed and there is an 'if (sock)' check before the release, no fix is really needed.