Commit 8ed001c9 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'gtp-fix-several-bugs-in-gtp-module'

Taehee Yoo says:

====================
gtp: fix several bugs in gtp module

This patchset fixes several bugs in the GTP module.

1. Do not allow adding duplicate TID and ms_addr pdp context.
In the current code, duplicate TID and ms_addr pdp context could be added.
So, RX and TX path could find correct pdp context.

2. Fix wrong condition in ->dumpit() callback.
->dumpit() callback is re-called if dump packet size is too big.
So, before return, it saves last position and then restart from
last dump position.
TID value is used to find last dump position.
GTP module allows adding zero TID value. But ->dumpit() callback ignores
zero TID value.
So, dump would not work correctly if dump packet size too big.

3. Fix use-after-free in ipv4_pdp_find().
RX and TX patch always uses gtp->tid_hash and gtp->addr_hash.
but while packet processing, these hash pointer would be freed.
So, use-after-free would occur.

4. Fix panic because of zero size hashtable
GTP hashtable size could be set by user-space.
If hashsize is set to 0, hashtable will not work and panic will occur.
====================
Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
parents 5d485ed8 6a902c0f
...@@ -38,7 +38,6 @@ struct pdp_ctx { ...@@ -38,7 +38,6 @@ struct pdp_ctx {
struct hlist_node hlist_addr; struct hlist_node hlist_addr;
union { union {
u64 tid;
struct { struct {
u64 tid; u64 tid;
u16 flow; u16 flow;
...@@ -641,9 +640,16 @@ static void gtp_link_setup(struct net_device *dev) ...@@ -641,9 +640,16 @@ static void gtp_link_setup(struct net_device *dev)
} }
static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize); static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
static void gtp_hashtable_free(struct gtp_dev *gtp);
static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]); static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
static void gtp_destructor(struct net_device *dev)
{
struct gtp_dev *gtp = netdev_priv(dev);
kfree(gtp->addr_hash);
kfree(gtp->tid_hash);
}
static int gtp_newlink(struct net *src_net, struct net_device *dev, static int gtp_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[], struct nlattr *tb[], struct nlattr *data[],
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
...@@ -661,10 +667,13 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, ...@@ -661,10 +667,13 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev,
if (err < 0) if (err < 0)
return err; return err;
if (!data[IFLA_GTP_PDP_HASHSIZE]) if (!data[IFLA_GTP_PDP_HASHSIZE]) {
hashsize = 1024; hashsize = 1024;
else } else {
hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]); hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
if (!hashsize)
hashsize = 1024;
}
err = gtp_hashtable_new(gtp, hashsize); err = gtp_hashtable_new(gtp, hashsize);
if (err < 0) if (err < 0)
...@@ -678,13 +687,15 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, ...@@ -678,13 +687,15 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev,
gn = net_generic(dev_net(dev), gtp_net_id); gn = net_generic(dev_net(dev), gtp_net_id);
list_add_rcu(&gtp->list, &gn->gtp_dev_list); list_add_rcu(&gtp->list, &gn->gtp_dev_list);
dev->priv_destructor = gtp_destructor;
netdev_dbg(dev, "registered new GTP interface\n"); netdev_dbg(dev, "registered new GTP interface\n");
return 0; return 0;
out_hashtable: out_hashtable:
gtp_hashtable_free(gtp); kfree(gtp->addr_hash);
kfree(gtp->tid_hash);
out_encap: out_encap:
gtp_encap_disable(gtp); gtp_encap_disable(gtp);
return err; return err;
...@@ -693,8 +704,13 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, ...@@ -693,8 +704,13 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev,
static void gtp_dellink(struct net_device *dev, struct list_head *head) static void gtp_dellink(struct net_device *dev, struct list_head *head)
{ {
struct gtp_dev *gtp = netdev_priv(dev); struct gtp_dev *gtp = netdev_priv(dev);
struct pdp_ctx *pctx;
int i;
for (i = 0; i < gtp->hash_size; i++)
hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
pdp_context_delete(pctx);
gtp_hashtable_free(gtp);
list_del_rcu(&gtp->list); list_del_rcu(&gtp->list);
unregister_netdevice_queue(dev, head); unregister_netdevice_queue(dev, head);
} }
...@@ -772,20 +788,6 @@ static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize) ...@@ -772,20 +788,6 @@ static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
return -ENOMEM; return -ENOMEM;
} }
static void gtp_hashtable_free(struct gtp_dev *gtp)
{
struct pdp_ctx *pctx;
int i;
for (i = 0; i < gtp->hash_size; i++)
hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
pdp_context_delete(pctx);
synchronize_rcu();
kfree(gtp->addr_hash);
kfree(gtp->tid_hash);
}
static struct sock *gtp_encap_enable_socket(int fd, int type, static struct sock *gtp_encap_enable_socket(int fd, int type,
struct gtp_dev *gtp) struct gtp_dev *gtp)
{ {
...@@ -926,24 +928,31 @@ static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info) ...@@ -926,24 +928,31 @@ static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
} }
} }
static int ipv4_pdp_add(struct gtp_dev *gtp, struct sock *sk, static int gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
struct genl_info *info) struct genl_info *info)
{ {
struct pdp_ctx *pctx, *pctx_tid = NULL;
struct net_device *dev = gtp->dev; struct net_device *dev = gtp->dev;
u32 hash_ms, hash_tid = 0; u32 hash_ms, hash_tid = 0;
struct pdp_ctx *pctx; unsigned int version;
bool found = false; bool found = false;
__be32 ms_addr; __be32 ms_addr;
ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]); ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size; hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
version = nla_get_u32(info->attrs[GTPA_VERSION]);
hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) { pctx = ipv4_pdp_find(gtp, ms_addr);
if (pctx->ms_addr_ip4.s_addr == ms_addr) { if (pctx)
found = true;
if (version == GTP_V0)
pctx_tid = gtp0_pdp_find(gtp,
nla_get_u64(info->attrs[GTPA_TID]));
else if (version == GTP_V1)
pctx_tid = gtp1_pdp_find(gtp,
nla_get_u32(info->attrs[GTPA_I_TEI]));
if (pctx_tid)
found = true; found = true;
break;
}
}
if (found) { if (found) {
if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
...@@ -951,6 +960,11 @@ static int ipv4_pdp_add(struct gtp_dev *gtp, struct sock *sk, ...@@ -951,6 +960,11 @@ static int ipv4_pdp_add(struct gtp_dev *gtp, struct sock *sk,
if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE) if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (pctx && pctx_tid)
return -EEXIST;
if (!pctx)
pctx = pctx_tid;
ipv4_pdp_fill(pctx, info); ipv4_pdp_fill(pctx, info);
if (pctx->gtp_version == GTP_V0) if (pctx->gtp_version == GTP_V0)
...@@ -1074,7 +1088,7 @@ static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info) ...@@ -1074,7 +1088,7 @@ static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
goto out_unlock; goto out_unlock;
} }
err = ipv4_pdp_add(gtp, sk, info); err = gtp_pdp_add(gtp, sk, info);
out_unlock: out_unlock:
rcu_read_unlock(); rcu_read_unlock();
...@@ -1232,43 +1246,46 @@ static int gtp_genl_dump_pdp(struct sk_buff *skb, ...@@ -1232,43 +1246,46 @@ static int gtp_genl_dump_pdp(struct sk_buff *skb,
struct netlink_callback *cb) struct netlink_callback *cb)
{ {
struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp; struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
int i, j, bucket = cb->args[0], skip = cb->args[1];
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct gtp_net *gn = net_generic(net, gtp_net_id);
unsigned long tid = cb->args[1];
int i, k = cb->args[0], ret;
struct pdp_ctx *pctx; struct pdp_ctx *pctx;
struct gtp_net *gn;
gn = net_generic(net, gtp_net_id);
if (cb->args[4]) if (cb->args[4])
return 0; return 0;
rcu_read_lock();
list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) { list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
if (last_gtp && last_gtp != gtp) if (last_gtp && last_gtp != gtp)
continue; continue;
else else
last_gtp = NULL; last_gtp = NULL;
for (i = k; i < gtp->hash_size; i++) { for (i = bucket; i < gtp->hash_size; i++) {
hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) { j = 0;
if (tid && tid != pctx->u.tid) hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
continue; hlist_tid) {
else if (j >= skip &&
tid = 0; gtp_genl_fill_info(skb,
ret = gtp_genl_fill_info(skb,
NETLINK_CB(cb->skb).portid, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, cb->nlh->nlmsg_seq,
cb->nlh->nlmsg_type, pctx); cb->nlh->nlmsg_type, pctx)) {
if (ret < 0) {
cb->args[0] = i; cb->args[0] = i;
cb->args[1] = pctx->u.tid; cb->args[1] = j;
cb->args[2] = (unsigned long)gtp; cb->args[2] = (unsigned long)gtp;
goto out; goto out;
} }
j++;
} }
skip = 0;
} }
bucket = 0;
} }
cb->args[4] = 1; cb->args[4] = 1;
out: out:
rcu_read_unlock();
return skb->len; return skb->len;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment