netfilter: ipset: harden payload calculation in call_ad()
Subsystems (3)
PATCH 1/1 netfilter: ipset: harden payload calculation in call_ad()
call_ad() computes the netlink error payload size with min(SIZE_MAX, sizeof(*errmsg) + nlmsg_len(nlh)), but min(SIZE_MAX, x) is always x, so the guard is a no-op.
Replace it with an explicit negative-length check and check_add_overflow() so the addition is validated before being passed to nlmsg_new().
--- net/netfilter/ipset/ip_set_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
net/netfilter/ipset/ip_set_core.c +8 -2
@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/ip.h> +#include <linux/overflow.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/rculist.h>
@@ -1763,13 +1764,18 @@ call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,struct nlmsghdr *rep, *nlh =nlmsg_hdr (skb);struct sk_buff *skb2;struct nlmsgerr *errmsg; -size_t payload =min (SIZE_MAX , -sizeof (*errmsg) +nlmsg_len (nlh)); +int nlmsg_payload_len =nlmsg_len (nlh); +size_t payload;int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nlattr *cda[IPSET_ATTR_CMD_MAX +1 ];struct nlattr *cmdattr;u32 *errline; +if (nlmsg_payload_len <0 || +check_add_overflow (sizeof (*errmsg), + (size_t )nlmsg_payload_len, &payload)) +return -ENOMEM ; + skb2 =nlmsg_new (payload,GFP_KERNEL );if (!skb2)return -ENOMEM ;
Signature · 1 line
2.50.1 (Apple Git-155)
Florian Westphal
maintainerQuoted 7 lines · David Baum
call_ad()computes the netlink error payload size withmin(SIZE_MAX, sizeof(*errmsg) + nlmsg_len(nlh)), butmin(SIZE_MAX, x)is always x, so the guard is a no-op.Replace it with an explicit negative-length check and
check_add_overflow()so the addition is validated before being passed tonlmsg_new().
Jozsef, are you ok with this patch?
Full quote below.
Quoted 38 lines
Signed-off-by David Baum <[email protected]>--- net/netfilter/ipset/ip_set_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)net/netfilter/ipset/ip_set_core.c +8
-2@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/ip.h> +#include <linux/overflow.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/rculist.h> @@ -1763,13 +1764,18 @@ call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,struct nlmsghdr *rep, *nlh =nlmsg_hdr (skb);struct sk_buff *skb2;struct nlmsgerr *errmsg; -size_t payload =min (SIZE_MAX , -sizeof (*errmsg) +nlmsg_len (nlh)); +int nlmsg_payload_len =nlmsg_len (nlh); +size_t payload;int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nlattr *cda[IPSET_ATTR_CMD_MAX +1 ];struct nlattr *cmdattr;u32 *errline; +if (nlmsg_payload_len <0 || +check_add_overflow (sizeof (*errmsg), + (size_t )nlmsg_payload_len, &payload)) +return -ENOMEM ; + skb2 =nlmsg_new (payload,GFP_KERNEL );if (!skb2)return -ENOMEM ;
Jozsef Kadlecsik
Acked-byHi Florian,
Quoted 11 lines · Florian Westphal
Quoted 7 lines · David Baum
call_ad()computes the netlink error payload size withmin(SIZE_MAX, sizeof(*errmsg) + nlmsg_len(nlh)), butmin(SIZE_MAX, x)is always x, so the guard is a no-op.Replace it with an explicit negative-length check and
check_add_overflow()so the addition is validated before being passed tonlmsg_new().Jozsef, are you ok with this patch?
Full quote below.
Yes, the patch is fine. It seems I missed it :-(.
Best regards,
Jozsef
Quoted 40 lines
Quoted 38 lines
Signed-off-by David Baum <[email protected]>--- net/netfilter/ipset/ip_set_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)net/netfilter/ipset/ip_set_core.c +8
-2@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/ip.h> +#include <linux/overflow.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/rculist.h> @@ -1763,13 +1764,18 @@ call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,struct nlmsghdr *rep, *nlh =nlmsg_hdr (skb);struct sk_buff *skb2;struct nlmsgerr *errmsg; -size_t payload =min (SIZE_MAX , -sizeof (*errmsg) +nlmsg_len (nlh)); +int nlmsg_payload_len =nlmsg_len (nlh); +size_t payload;int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nlattr *cda[IPSET_ATTR_CMD_MAX +1 ];struct nlattr *cmdattr;u32 *errline; +if (nlmsg_payload_len <0 || +check_add_overflow (sizeof (*errmsg), + (size_t )nlmsg_payload_len, &payload)) +return -ENOMEM ; + skb2 =nlmsg_new (payload,GFP_KERNEL );if (!skb2)return -ENOMEM ;
Pablo Neira Ayuso
maintainerQuoted 29 lines · David Baum
call_ad()computes the netlink error payload size withmin(SIZE_MAX, sizeof(*errmsg) + nlmsg_len(nlh)), butmin(SIZE_MAX, x)is always x, so the guard is a no-op.Replace it with an explicit negative-length check and
check_add_overflow()so the addition is validated before being passed tonlmsg_new().Signed-off-by David Baum <[email protected]>--- net/netfilter/ipset/ip_set_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)net/netfilter/ipset/ip_set_core.c +1
-0@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/ip.h> +#include <linux/overflow.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/rculist.h> @@ -1763,13 +1764,18 @@ call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,struct nlmsghdr *rep, *nlh =nlmsg_hdr (skb);struct sk_buff *skb2;struct nlmsgerr *errmsg;
net/netfilter/ipset/ip_set_core.c call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb, · line 1774
Quoted 10 lines
net/netfilter/ipset/ip_set_core.c +3
-2-size_t payload =min (SIZE_MAX , -sizeof (*errmsg) +nlmsg_len (nlh)); +int nlmsg_payload_len =nlmsg_len (nlh); +size_t payload;int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nlattr *cda[IPSET_ATTR_CMD_MAX +1 ];struct nlattr *cmdattr;u32 *errline; + if (nlmsg_payload_len < 0 ||
Hm, nlh was already sanitized by nfnetlink?
net/netfilter/ipset/ip_set_core.c call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb, · line 1776
Quoted 2 lines
net/netfilter/ipset/ip_set_core.c +2
-0+check_add_overflow (sizeof (*errmsg), + (size_t)nlmsg_payload_len, &payload))
Maybe cap this to int:
int payload = sizeof(struct nlmsgerr);
...
if (check_add_overflow(payload, nlmsg_payload_len, &payload))Quoted 5 lines
net/netfilter/ipset/ip_set_core.c +2
-0+return -ENOMEM ; + skb2 =nlmsg_new (payload,GFP_KERNEL );if (!skb2)return -ENOMEM ;
Pablo Neira Ayuso
maintainerQuoted 48 lines · Pablo Neira Ayuso
Quoted 29 lines · David Baum
call_ad()computes the netlink error payload size withmin(SIZE_MAX, sizeof(*errmsg) + nlmsg_len(nlh)), butmin(SIZE_MAX, x)is always x, so the guard is a no-op.Replace it with an explicit negative-length check and
check_add_overflow()so the addition is validated before being passed tonlmsg_new().Signed-off-by David Baum <[email protected]>--- net/netfilter/ipset/ip_set_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)net/netfilter/ipset/ip_set_core.c +1
-0@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/ip.h> +#include <linux/overflow.h> #include <linux/skbuff.h> #include <linux/spinlock.h> #include <linux/rculist.h> @@ -1763,13 +1764,18 @@ call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,struct nlmsghdr *rep, *nlh =nlmsg_hdr (skb);struct sk_buff *skb2;struct nlmsgerr *errmsg;net/netfilter/ipset/ip_set_core.c call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb, · line 1774
Quoted 10 lines
net/netfilter/ipset/ip_set_core.c +3
-2-size_t payload =min (SIZE_MAX , -sizeof (*errmsg) +nlmsg_len (nlh)); +int nlmsg_payload_len =nlmsg_len (nlh); +size_t payload;int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nlattr *cda[IPSET_ATTR_CMD_MAX +1 ];struct nlattr *cmdattr;u32 *errline; + if (nlmsg_payload_len < 0 ||Hm, nlh was already sanitized by nfnetlink?
net/netfilter/ipset/ip_set_core.c call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb, · line 1776
Quoted 2 lines
net/netfilter/ipset/ip_set_core.c +2
-0+check_add_overflow (sizeof (*errmsg), + (size_t)nlmsg_payload_len, &payload))Maybe cap this to int:
Quoted 4 lines
int payload = sizeof(struct nlmsgerr);
...
if (check_add_overflow(payload, nlmsg_payload_len, &payload))Wait, then payload and nlmsg_payload_len should be __u32, not int.
Quoted 7 lines
Quoted 6 lines
net/netfilter/ipset/ip_set_core.c +2
-0+return -ENOMEM ; + skb2 =nlmsg_new (payload,GFP_KERNEL );if (!skb2)return -ENOMEM ;