* [patch -next] net: writes past the end of the struct
@ 2012-07-11 6:32 Dan Carpenter
2012-07-11 8:27 ` David Miller
0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2012-07-11 6:32 UTC (permalink / raw)
To: David S. Miller
Cc: Alexey Kuznetsov, James Morris, Hideaki YOSHIFUJI,
Patrick McHardy, netdev, kernel-janitors
There are a couple places that try to set part of the struct to 0 by
doing:
memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
It assumes that the first element is a dst_entry and the second element
is ->rt6_table. The problem is we changed the struct in 97cac0821a
('ipv6: Store route neighbour in rt6_info struct.') and we aren't
clearing rt->n but instead we're writing past the end of the array.
I've changed it to:
memset(&rt->n, 0, sizeof(*rt) - offsetof(struct rt6_info, n));
The memset in ip6_dst_alloc() was ok but I changed it to use offsetof()
as a cleanup.
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index 6e97855..c2186a7 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -1353,8 +1353,8 @@ static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
xdst = dst_alloc(dst_ops, NULL, 0, 0, 0);
if (likely(xdst)) {
- memset(&xdst->u.rt6.rt6i_table, 0,
- sizeof(*xdst) - sizeof(struct dst_entry));
+ memset(&xdst->u.rt6.n, 0,
+ sizeof(*xdst) - offsetof(struct rt6_info, n));
xdst->flo.ops = &xfrm_bundle_fc_ops;
} else
xdst = ERR_PTR(-ENOBUFS);
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 6cc6c88..41693f6 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -274,7 +274,7 @@ static inline struct rt6_info *ip6_dst_alloc(struct net *net,
if (rt) {
memset(&rt->n, 0,
- sizeof(*rt) - sizeof(struct dst_entry));
+ sizeof(*rt) - offsetof(struct rt6_info, n));
rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
}
return rt;
@@ -975,7 +975,7 @@ struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_ori
rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
if (rt) {
- memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
+ memset(&rt->n, 0, sizeof(*rt) - offsetof(struct rt6_info, n));
rt6_init_peer(rt, net->ipv6.peers);
new = &rt->dst;
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [patch -next] net: writes past the end of the struct
2012-07-11 6:32 [patch -next] net: writes past the end of the struct Dan Carpenter
@ 2012-07-11 8:27 ` David Miller
2012-07-11 8:37 ` Dan Carpenter
0 siblings, 1 reply; 3+ messages in thread
From: David Miller @ 2012-07-11 8:27 UTC (permalink / raw)
To: dan.carpenter; +Cc: kuznet, jmorris, yoshfuji, kaber, netdev, kernel-janitors
From: Dan Carpenter <dan.carpenter@oracle.com>
Date: Wed, 11 Jul 2012 09:32:14 +0300
> There are a couple places that try to set part of the struct to 0 by
> doing:
>
> memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
>
> It assumes that the first element is a dst_entry and the second element
> is ->rt6_table. The problem is we changed the struct in 97cac0821a
> ('ipv6: Store route neighbour in rt6_info struct.') and we aren't
> clearing rt->n but instead we're writing past the end of the array.
>
> I've changed it to:
> memset(&rt->n, 0, sizeof(*rt) - offsetof(struct rt6_info, n));
>
> The memset in ip6_dst_alloc() was ok but I changed it to use offsetof()
> as a cleanup.
>
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Steffen Klassert had patches to do this more nicely, using "dst + 1"
as the pointer calculation, which therefore works no matter what
elements we add to struct rt6_info after the dst_entry.
I asked him to fix some things, he did too much casting, but he never
respun his patch set. I definitely prefer this gets fixed his way.
http://patchwork.ozlabs.org/patch/169391/
http://patchwork.ozlabs.org/patch/169395/
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [patch -next] net: writes past the end of the struct
2012-07-11 8:27 ` David Miller
@ 2012-07-11 8:37 ` Dan Carpenter
0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2012-07-11 8:37 UTC (permalink / raw)
To: David Miller; +Cc: kuznet, jmorris, yoshfuji, kaber, netdev, kernel-janitors
On Wed, Jul 11, 2012 at 01:27:45AM -0700, David Miller wrote:
> I asked him to fix some things, he did too much casting, but he never
> respun his patch set. I definitely prefer this gets fixed his way.
>
Oooo. That's a clever trick. Nice.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-07-11 8:37 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-11 6:32 [patch -next] net: writes past the end of the struct Dan Carpenter
2012-07-11 8:27 ` David Miller
2012-07-11 8:37 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox