Skip to content

Commit 1f30fb9

Browse files
igsilyadavem330
authored andcommitted
net: openvswitch: fix leak of nested actions
While parsing user-provided actions, openvswitch module may dynamically allocate memory and store pointers in the internal copy of the actions. So this memory has to be freed while destroying the actions. Currently there are only two such actions: ct() and set(). However, there are many actions that can hold nested lists of actions and ovs_nla_free_flow_actions() just jumps over them leaking the memory. For example, removal of the flow with the following actions will lead to a leak of the memory allocated by nf_ct_tmpl_alloc(): actions:clone(ct(commit),0) Non-freed set() action may also leak the 'dst' structure for the tunnel info including device references. Under certain conditions with a high rate of flow rotation that may cause significant memory leak problem (2MB per second in reporter's case). The problem is also hard to mitigate, because the user doesn't have direct control over the datapath flows generated by OVS. Fix that by iterating over all the nested actions and freeing everything that needs to be freed recursively. New build time assertion should protect us from this problem if new actions will be added in the future. Unfortunately, openvswitch module doesn't use NLA_F_NESTED, so all attributes has to be explicitly checked. sample() and clone() actions are mixing extra attributes into the user-provided action list. That prevents some code generalization too. Fixes: 34ae932 ("openvswitch: Make tunnel set action attach a metadata dst") Link: https://mail.openvswitch.org/pipermail/ovs-dev/2022-March/392922.html Reported-by: Stéphane Graber <stgraber@ubuntu.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org> Acked-by: Aaron Conole <aconole@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 11f8e7c commit 1f30fb9

1 file changed

Lines changed: 90 additions & 5 deletions

File tree

net/openvswitch/flow_netlink.c

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,62 @@ static struct sw_flow_actions *nla_alloc_flow_actions(int size)
23172317
return sfa;
23182318
}
23192319

2320+
static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len);
2321+
2322+
static void ovs_nla_free_check_pkt_len_action(const struct nlattr *action)
2323+
{
2324+
const struct nlattr *a;
2325+
int rem;
2326+
2327+
nla_for_each_nested(a, action, rem) {
2328+
switch (nla_type(a)) {
2329+
case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL:
2330+
case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER:
2331+
ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2332+
break;
2333+
}
2334+
}
2335+
}
2336+
2337+
static void ovs_nla_free_clone_action(const struct nlattr *action)
2338+
{
2339+
const struct nlattr *a = nla_data(action);
2340+
int rem = nla_len(action);
2341+
2342+
switch (nla_type(a)) {
2343+
case OVS_CLONE_ATTR_EXEC:
2344+
/* The real list of actions follows this attribute. */
2345+
a = nla_next(a, &rem);
2346+
ovs_nla_free_nested_actions(a, rem);
2347+
break;
2348+
}
2349+
}
2350+
2351+
static void ovs_nla_free_dec_ttl_action(const struct nlattr *action)
2352+
{
2353+
const struct nlattr *a = nla_data(action);
2354+
2355+
switch (nla_type(a)) {
2356+
case OVS_DEC_TTL_ATTR_ACTION:
2357+
ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2358+
break;
2359+
}
2360+
}
2361+
2362+
static void ovs_nla_free_sample_action(const struct nlattr *action)
2363+
{
2364+
const struct nlattr *a = nla_data(action);
2365+
int rem = nla_len(action);
2366+
2367+
switch (nla_type(a)) {
2368+
case OVS_SAMPLE_ATTR_ARG:
2369+
/* The real list of actions follows this attribute. */
2370+
a = nla_next(a, &rem);
2371+
ovs_nla_free_nested_actions(a, rem);
2372+
break;
2373+
}
2374+
}
2375+
23202376
static void ovs_nla_free_set_action(const struct nlattr *a)
23212377
{
23222378
const struct nlattr *ovs_key = nla_data(a);
@@ -2330,25 +2386,54 @@ static void ovs_nla_free_set_action(const struct nlattr *a)
23302386
}
23312387
}
23322388

2333-
void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2389+
static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len)
23342390
{
23352391
const struct nlattr *a;
23362392
int rem;
23372393

2338-
if (!sf_acts)
2394+
/* Whenever new actions are added, the need to update this
2395+
* function should be considered.
2396+
*/
2397+
BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 23);
2398+
2399+
if (!actions)
23392400
return;
23402401

2341-
nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
2402+
nla_for_each_attr(a, actions, len, rem) {
23422403
switch (nla_type(a)) {
2343-
case OVS_ACTION_ATTR_SET:
2344-
ovs_nla_free_set_action(a);
2404+
case OVS_ACTION_ATTR_CHECK_PKT_LEN:
2405+
ovs_nla_free_check_pkt_len_action(a);
2406+
break;
2407+
2408+
case OVS_ACTION_ATTR_CLONE:
2409+
ovs_nla_free_clone_action(a);
23452410
break;
2411+
23462412
case OVS_ACTION_ATTR_CT:
23472413
ovs_ct_free_action(a);
23482414
break;
2415+
2416+
case OVS_ACTION_ATTR_DEC_TTL:
2417+
ovs_nla_free_dec_ttl_action(a);
2418+
break;
2419+
2420+
case OVS_ACTION_ATTR_SAMPLE:
2421+
ovs_nla_free_sample_action(a);
2422+
break;
2423+
2424+
case OVS_ACTION_ATTR_SET:
2425+
ovs_nla_free_set_action(a);
2426+
break;
23492427
}
23502428
}
2429+
}
2430+
2431+
void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2432+
{
2433+
if (!sf_acts)
2434+
return;
23512435

2436+
ovs_nla_free_nested_actions(sf_acts->actions, sf_acts->actions_len);
23522437
kfree(sf_acts);
23532438
}
23542439

0 commit comments

Comments
 (0)