Skip to contentSkip to conversation

net/ipv4: Eliminate kstrdup memory leak

PATCH Applied Julia Lawall 2 participants 2 messages Aug 27, 2010 – Aug 28, 2010

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

PATCH 1/1 net/ipv4: Eliminate kstrdup memory leak

The string clone is only used as a temporary copy of the argument val within the while loop, and so it should be freed before leaving the function. The call to strsep, however, modifies clone, so a pointer to the front of the string is kept in saved_clone, to make it possible to free it.

The sematic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r exists@
local idexpression x;
expression E;
identifier l;
statement S;
@@

*x= \(kasprintf\|kstrdup\)(...);
...
if (x = NULL) S
... when != kfree(x)

    when != E = x

if (...) {
<... when != kfree(x)
* goto l;
...>
* return ...;
}
// </smpl>

---
 net/ipv4/tcp_cong.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv4/tcp_cong.c +3 -2
index 0ec9bd0..850c737 100644
@@ -196,10 +196,10 @@ void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
 int tcp_set_allowed_congestion_control(char *val)
 {
 	struct tcp_congestion_ops *ca;
-	char *clone, *name;
+	char *saved_clone, *clone, *name;
 	int ret = 0;
 
-	clone = kstrdup(val, GFP_USER);
+	saved_clone = clone = kstrdup(val, GFP_USER);
 	if (!clone)
 		return -ENOMEM;
 
@@ -226,6 +226,7 @@ int tcp_set_allowed_congestion_control(char *val)
 	}
 out:
 	spin_unlock(&tcp_cong_list_lock);
+	kfree(saved_clone);
 
 	return ret;
 }
maintainer Applied
#1 lore raw

From: Julia Lawall <[email protected]>
Date: Fri, 27 Aug 2010 21:47:43 +0200

Quoted 7 lines

The string clone is only used as a temporary copy of the argument val within the while loop, and so it should be freed before leaving the function. The call to strsep, however, modifies clone, so a pointer to the front of the string is kept in saved_clone, to make it possible to free it.

The sematic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

...

Quoted 1 line

Applied, thanks!