提交 c34186ed 编写于 作者: J Julia Lawall 提交者: David S. Miller

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>
Signed-off-by: NJulia Lawall <julia@diku.dk>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 7e368739
...@@ -196,10 +196,10 @@ void tcp_get_allowed_congestion_control(char *buf, size_t maxlen) ...@@ -196,10 +196,10 @@ void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
int tcp_set_allowed_congestion_control(char *val) int tcp_set_allowed_congestion_control(char *val)
{ {
struct tcp_congestion_ops *ca; struct tcp_congestion_ops *ca;
char *clone, *name; char *saved_clone, *clone, *name;
int ret = 0; int ret = 0;
clone = kstrdup(val, GFP_USER); saved_clone = clone = kstrdup(val, GFP_USER);
if (!clone) if (!clone)
return -ENOMEM; return -ENOMEM;
...@@ -226,6 +226,7 @@ int tcp_set_allowed_congestion_control(char *val) ...@@ -226,6 +226,7 @@ int tcp_set_allowed_congestion_control(char *val)
} }
out: out:
spin_unlock(&tcp_cong_list_lock); spin_unlock(&tcp_cong_list_lock);
kfree(saved_clone);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册