]> git.ipfire.org Git - thirdparty/iproute2.git/commit
libnetlink: Fix memory leak in __rtnl_talk_iov()
authorLahav Schlesinger <lschlesinger@drivenets.com>
Mon, 5 Dec 2022 08:47:41 +0000 (10:47 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 9 Dec 2022 18:03:45 +0000 (10:03 -0800)
commit0faec4d050b607f7544b6cf9a4c2d57e191f981f
tree8f066fecd4f86b5349f6657870f88640f29c449b
parent4de59102f49ff9128378568cf967d6c7aabea6f2
libnetlink: Fix memory leak in __rtnl_talk_iov()

If `__rtnl_talk_iov` fails then callers are not expected to free `answer`.

Currently if `NLMSG_ERROR` was received with an error then the netlink
buffer was stored in `answer`, while still returning an error

This leak can be observed by running this snippet over time.
This triggers an `NLMSG_ERROR` because for each neighbour update, `ip`
will try to query for the name of interface 9999 in the wrong netns.
(which in itself is a separate bug)

 set -e

 ip netns del test-a || true
 ip netns add test-a
 ip netns del test-b || true
 ip netns add test-b

 ip -n test-a netns set test-b auto
 ip -n test-a link add veth_a index 9999 type veth \
  peer name veth_b netns test-b
 ip -n test-b link set veth_b up

 ip -n test-a monitor link address prefix neigh nsid label all-nsid \
  > /dev/null &
 monitor_pid=$!
 clean() {
  kill $monitor_pid
  ip netns del test-a
  ip netns del test-b
 }
 trap clean EXIT

 while true; do
  ip -n test-b neigh add dev veth_b 1.2.3.4 lladdr AA:AA:AA:AA:AA:AA
  ip -n test-b neigh del dev veth_b 1.2.3.4
 done

Fixes: 55870dfe7f8b ("Improve batch and dump times by caching link lookups")
Signed-off-by: Lahav Schlesinger <lschlesinger@drivenets.com>
Signed-off-by: Gilad Naaman <gnaaman@drivenets.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
lib/libnetlink.c