Skip to content

Commit ae5f2fb

Browse files
jessegrossdavem330
authored andcommitted
openvswitch: Zero flows on allocation.
When support for megaflows was introduced, OVS needed to start installing flows with a mask applied to them. Since masking is an expensive operation, OVS also had an optimization that would only take the parts of the flow keys that were covered by a non-zero mask. The values stored in the remaining pieces should not matter because they are masked out. While this works fine for the purposes of matching (which must always look at the mask), serialization to netlink can be problematic. Since the flow and the mask are serialized separately, the uninitialized portions of the flow can be encoded with whatever values happen to be present. In terms of functionality, this has little effect since these fields will be masked out by definition. However, it leaks kernel memory to userspace, which is a potential security vulnerability. It is also possible that other code paths could look at the masked key and get uninitialized data, although this does not currently appear to be an issue in practice. This removes the mask optimization for flows that are being installed. This was always intended to be the case as the mask optimizations were really targetting per-packet flow operations. Fixes: 03f0d91 ("openvswitch: Mega flow implementation") Signed-off-by: Jesse Gross <jesse@nicira.com> Acked-by: Pravin B Shelar <pshelar@nicira.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 53adc9e commit ae5f2fb

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

‎net/openvswitch/datapath.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
952952
if (error)
953953
goto err_kfree_flow;
954954

955-
ovs_flow_mask_key(&new_flow->key, &key, &mask);
955+
ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
956956

957957
/* Extract flow identifier. */
958958
error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
@@ -1080,7 +1080,7 @@ static struct sw_flow_actions *get_flow_actions(struct net *net,
10801080
struct sw_flow_key masked_key;
10811081
int error;
10821082

1083-
ovs_flow_mask_key(&masked_key, key, mask);
1083+
ovs_flow_mask_key(&masked_key, key, true, mask);
10841084
error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
10851085
if (error) {
10861086
OVS_NLERR(log,

‎net/openvswitch/flow_table.c‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@ static u16 range_n_bytes(const struct sw_flow_key_range *range)
5757
}
5858

5959
void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
60-
const struct sw_flow_mask *mask)
60+
bool full, const struct sw_flow_mask *mask)
6161
{
62-
const long *m = (const long *)((const u8 *)&mask->key +
63-
mask->range.start);
64-
const long *s = (const long *)((const u8 *)src +
65-
mask->range.start);
66-
long *d = (long *)((u8 *)dst + mask->range.start);
62+
int start = full ? 0 : mask->range.start;
63+
int len = full ? sizeof *dst : range_n_bytes(&mask->range);
64+
const long *m = (const long *)((const u8 *)&mask->key + start);
65+
const long *s = (const long *)((const u8 *)src + start);
66+
long *d = (long *)((u8 *)dst + start);
6767
int i;
6868

69-
/* The memory outside of the 'mask->range' are not set since
70-
* further operations on 'dst' only uses contents within
71-
* 'mask->range'.
69+
/* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
70+
* if 'full' is false the memory outside of the 'mask->range' is left
71+
* uninitialized. This can be used as an optimization when further
72+
* operations on 'dst' only use contents within 'mask->range'.
7273
*/
73-
for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
74+
for (i = 0; i < len; i += sizeof(long))
7475
*d++ = *s++ & *m++;
7576
}
7677

@@ -475,7 +476,7 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
475476
u32 hash;
476477
struct sw_flow_key masked_key;
477478

478-
ovs_flow_mask_key(&masked_key, unmasked, mask);
479+
ovs_flow_mask_key(&masked_key, unmasked, false, mask);
479480
hash = flow_hash(&masked_key, &mask->range);
480481
head = find_bucket(ti, hash);
481482
hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {

‎net/openvswitch/flow_table.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *,
8686
bool ovs_flow_cmp(const struct sw_flow *, const struct sw_flow_match *);
8787

8888
void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
89-
const struct sw_flow_mask *mask);
89+
bool full, const struct sw_flow_mask *mask);
9090
#endif /* flow_table.h */

0 commit comments

Comments
 (0)