Skip to content

Commit dc6de33

Browse files
author
David S. Miller
committed
[NET]: Add skb->truesize assertion checking.
Add some sanity checking. truesize should be at least sizeof(struct sk_buff) plus the current packet length. If not, then truesize is seriously mangled and deserves a kernel log message. Currently we'll do the check for release of stream socket buffers. But we can add checks to more spots over time. Incorporating ideas from Herbert Xu. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b60b49e commit dc6de33

4 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ extern void skb_over_panic(struct sk_buff *skb, int len,
344344
void *here);
345345
extern void skb_under_panic(struct sk_buff *skb, int len,
346346
void *here);
347+
extern void skb_truesize_bug(struct sk_buff *skb);
348+
349+
static inline void skb_truesize_check(struct sk_buff *skb)
350+
{
351+
if (unlikely((int)skb->truesize < sizeof(struct sk_buff) + skb->len))
352+
skb_truesize_bug(skb);
353+
}
347354

348355
extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
349356
int getfrag(void *from, char *to, int offset,

‎include/net/sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ static inline void sk_stream_set_owner_r(struct sk_buff *skb, struct sock *sk)
454454

455455
static inline void sk_stream_free_skb(struct sock *sk, struct sk_buff *skb)
456456
{
457+
skb_truesize_check(skb);
457458
sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
458459
sk->sk_wmem_queued -= skb->truesize;
459460
sk->sk_forward_alloc += skb->truesize;

‎net/core/skbuff.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ void skb_under_panic(struct sk_buff *skb, int sz, void *here)
112112
BUG();
113113
}
114114

115+
void skb_truesize_bug(struct sk_buff *skb)
116+
{
117+
printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118+
"len=%u, sizeof(sk_buff)=%Zd\n",
119+
skb->truesize, skb->len, sizeof(struct sk_buff));
120+
}
121+
EXPORT_SYMBOL(skb_truesize_bug);
122+
115123
/* Allocate a new skbuff. We do this ourselves so we can fill in a few
116124
* 'private' fields and also do memory statistics to find all the
117125
* [BEEP] leaks.

‎net/core/stream.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void sk_stream_rfree(struct sk_buff *skb)
176176
{
177177
struct sock *sk = skb->sk;
178178

179+
skb_truesize_check(skb);
179180
atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
180181
sk->sk_forward_alloc += skb->truesize;
181182
}

0 commit comments

Comments
 (0)