Skip to contentSkip to conversation

skbuff: Introduce slab_build_skb()

PATCH Applied Kees Cook 4 participants 5 messages Dec 8, 2022 – Dec 10, 2022

Subsystems (5)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v3 1/1 skbuff: Introduce slab_build_skb()

syzkaller reported:

  BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
  Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb().

When build_skb() is passed a frag_size of 0, it means the buffer came from kmalloc. In these cases, ksize() is used to find its actual size, but since the allocation may not have been made to that size, actually perform the krealloc() call so that all the associated buffer size checking will be correctly notified (and use the "new" pointer so that compiler hinting works correctly). Split this logic out into a new interface, slab_build_skb(), but leave the original 0 checking for now to catch any stragglers.

Reported-by [email protected]
Fixes 38931d8989b5 ("mm: Make ksize() a reporting-only function")
Cc Jakub Kicinski <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc pepsipu <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc kasan-dev <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Yonghong Song <[email protected]>

---
v3:
- make sure "resized" is passed back so compiler hints survive
- update kerndoc (kuba)
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ethernet/broadcom/bnx2.c      |  2 +-
 drivers/net/ethernet/qlogic/qed/qed_ll2.c |  2 +-
 include/linux/skbuff.h                    |  1 +
 net/bpf/test_run.c                        |  2 +-
 net/core/skbuff.c                         | 70 ++++++++++++++++++++---
 5 files changed, 66 insertions(+), 11 deletions(-)
drivers/net/ethernet/broadcom/bnx2.c +1 -1
index fec57f1982c8..b2230a4a2086 100644
@@ -3045,7 +3045,7 @@ bnx2_rx_skb(struct bnx2 *bp, struct bnx2_rx_ring_info *rxr, u8 *data,
 
 	dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
 			 DMA_FROM_DEVICE);
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		goto error;
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -1
index ed274f033626..e5116a86cfbc 100644
@@ -200,7 +200,7 @@ static void qed_ll2b_complete_rx_packet(void *cxt,
 	dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
 			 cdev->ll2->rx_size, DMA_FROM_DEVICE);
 
-	skb = build_skb(buffer->data, 0);
+	skb = slab_build_skb(buffer->data);
 	if (!skb) {
 		DP_INFO(cdev, "Failed to build SKB\n");
 		kfree(buffer->data);
include/linux/skbuff.h +1 -0
index 7be5bb4c94b6..0b391b635430 100644
@@ -1253,6 +1253,7 @@ struct sk_buff *build_skb_around(struct sk_buff *skb,
 void skb_attempt_defer_free(struct sk_buff *skb);
 
 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
+struct sk_buff *slab_build_skb(void *data);
 
 /**
  * alloc_skb - allocate a network buffer
net/bpf/test_run.c +1 -1
index 13d578ce2a09..611b1f4082cf 100644
@@ -1130,7 +1130,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 	sock_init_data(NULL, sk);
 
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		kfree(ctx);
net/core/skbuff.c +62 -8

89 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 138 lines · Kees Cook

syzkaller reported:

  BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
  Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb().

When build_skb() is passed a frag_size of 0, it means the buffer came from kmalloc. In these cases, ksize() is used to find its actual size, but since the allocation may not have been made to that size, actually perform the krealloc() call so that all the associated buffer size checking will be correctly notified (and use the "new" pointer so that compiler hinting works correctly). Split this logic out into a new interface, slab_build_skb(), but leave the original 0 checking for now to catch any stragglers.

Reported-by [email protected]
Fixes 38931d8989b5 ("mm: Make ksize() a reporting-only function")
Cc Jakub Kicinski <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc pepsipu <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc kasan-dev <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Yonghong Song <[email protected]>

---
v3:
- make sure "resized" is passed back so compiler hints survive
- update kerndoc (kuba)
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ethernet/broadcom/bnx2.c      |  2 +-
 drivers/net/ethernet/qlogic/qed/qed_ll2.c |  2 +-
 include/linux/skbuff.h                    |  1 +
 net/bpf/test_run.c                        |  2 +-
 net/core/skbuff.c                         | 70 ++++++++++++++++++++---
 5 files changed, 66 insertions(+), 11 deletions(-)
drivers/net/ethernet/broadcom/bnx2.c +1 -1
index fec57f1982c8..b2230a4a2086 100644
@@ -3045,7 +3045,7 @@ bnx2_rx_skb(struct bnx2 *bp, struct bnx2_rx_ring_info *rxr, u8 *data,
 
 	dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
 			 DMA_FROM_DEVICE);
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		goto error;
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -1
index ed274f033626..e5116a86cfbc 100644
@@ -200,7 +200,7 @@ static void qed_ll2b_complete_rx_packet(void *cxt,
 	dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
 			 cdev->ll2->rx_size, DMA_FROM_DEVICE);
 
-	skb = build_skb(buffer->data, 0);
+	skb = slab_build_skb(buffer->data);
 	if (!skb) {
 		DP_INFO(cdev, "Failed to build SKB\n");
 		kfree(buffer->data);
include/linux/skbuff.h +1 -0
index 7be5bb4c94b6..0b391b635430 100644
@@ -1253,6 +1253,7 @@ struct sk_buff *build_skb_around(struct sk_buff *skb,
 void skb_attempt_defer_free(struct sk_buff *skb);
 
 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
+struct sk_buff *slab_build_skb(void *data);
 
 /**
  * alloc_skb - allocate a network buffer
net/bpf/test_run.c +1 -1
index 13d578ce2a09..611b1f4082cf 100644
@@ -1130,7 +1130,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 	sock_init_data(NULL, sk);
 
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		kfree(ctx);
net/core/skbuff.c +7 -4
index 1d9719e72f9d..ae5a6f7db37b 100644
@@ -269,12 +269,10 @@ static struct sk_buff *napi_skb_cache_get(void)
 	return skb;
 }
 
-/* Caller must provide SKB that is memset cleared */
-static void __build_skb_around(struct sk_buff *skb, void *data,
-			       unsigned int frag_size)
+static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
+					 unsigned int size)
 {
 	struct skb_shared_info *shinfo;
-	unsigned int size = frag_size ? : ksize(data);
 
 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 
@@ -296,15 +294,71 @@ static void __build_skb_around(struct sk_buff *skb, void *data,
 	skb_set_kcov_handle(skb, kcov_common_handle());
 }
 
+static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
+				     unsigned int *size)
+{
+	void *resized;
+

net/core/skbuff.c static void __build_skb_around(struct sk_buff *skb, void *data, · line 311

Quoted 10 lines
net/core/skbuff.c +10 -0
+	/* Must find the allocation size (and grow it to match). */
+	*size = ksize(data);
+	/* krealloc() will immediately return "data" when
+	 * "ksize(data)" is requested: it is the existing upper
+	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
+	 * that this "new" pointer needs to be passed back to the
+	 * caller for use so the __alloc_size hinting will be
+	 * tracked correctly.
+	 */
+	resized = krealloc(data, *size, GFP_ATOMIC);

Hmm, I just realized, this trick will probably break the new kmalloc size tracking from Feng Tang (CC'd)? We need to make krealloc() update the stored size, right? And even worse if slab_debug redzoning is enabled and after commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") where the lack of update will result in redzone check failures.

Quoted 56 lines
net/core/skbuff.c +44 -4
+	WARN_ON_ONCE(resized != data);
+	return resized;
+}
+
+/* build_skb() variant which can operate on slab buffers.
+ * Note that this should be used sparingly as slab buffers
+ * cannot be combined efficiently by GRO!
+ */
+struct sk_buff *slab_build_skb(void *data)
+{
+	struct sk_buff *skb;
+	unsigned int size;
+
+	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
+	if (unlikely(!skb))
+		return NULL;
+
+	memset(skb, 0, offsetof(struct sk_buff, tail));
+	data = __slab_build_skb(skb, data, &size);
+	__finalize_skb_around(skb, data, size);
+
+	return skb;
+}
+EXPORT_SYMBOL(slab_build_skb);
+
+/* Caller must provide SKB that is memset cleared */
+static void __build_skb_around(struct sk_buff *skb, void *data,
+			       unsigned int frag_size)
+{
+	unsigned int size = frag_size;
+
+	/* frag_size == 0 is considered deprecated now. Callers
+	 * using slab buffer should use slab_build_skb() instead.
+	 */
+	if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
+		data = __slab_build_skb(skb, data, &size);
+	__finalize_skb_around(skb, data, size);
+}
+
 /**
  * __build_skb - build a network buffer
  * @data: data buffer provided by caller
- * @frag_size: size of data, or 0 if head was kmalloced
+ * @frag_size: size of data (must not be 0)
  *
  * Allocate a new &sk_buff. Caller provides space holding head and
- * skb_shared_info. @data must have been allocated by kmalloc() only if
- * @frag_size is 0, otherwise data should come from the page allocator
- *  or vmalloc()
+ * skb_shared_info. @data must have been allocated from the page
+ * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
+ * allocation is deprecated, and callers should use slab_build_skb()
+ * instead.)
  * The return is the new skb buffer.
  * On a failure the return is %NULL, and @data is not freed.
  * Notes :
#2 lore raw
Quoted 148 lines · Vlastimil Babka
Quoted 147 lines · Kees Cook

syzkaller reported:

  BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
  Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb().

When build_skb() is passed a frag_size of 0, it means the buffer came from kmalloc. In these cases, ksize() is used to find its actual size, but since the allocation may not have been made to that size, actually perform the krealloc() call so that all the associated buffer size checking will be correctly notified (and use the "new" pointer so that compiler hinting works correctly). Split this logic out into a new interface, slab_build_skb(), but leave the original 0 checking for now to catch any stragglers.

Reported-by [email protected]
Fixes 38931d8989b5 ("mm: Make ksize() a reporting-only function")
Cc Jakub Kicinski <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc pepsipu <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc kasan-dev <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Yonghong Song <[email protected]>

---
v3:
- make sure "resized" is passed back so compiler hints survive
- update kerndoc (kuba)
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ethernet/broadcom/bnx2.c      |  2 +-
 drivers/net/ethernet/qlogic/qed/qed_ll2.c |  2 +-
 include/linux/skbuff.h                    |  1 +
 net/bpf/test_run.c                        |  2 +-
 net/core/skbuff.c                         | 70 ++++++++++++++++++++---
 5 files changed, 66 insertions(+), 11 deletions(-)
drivers/net/ethernet/broadcom/bnx2.c +1 -1
index fec57f1982c8..b2230a4a2086 100644
@@ -3045,7 +3045,7 @@ bnx2_rx_skb(struct bnx2 *bp, struct bnx2_rx_ring_info *rxr, u8 *data,
 
 	dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
 			 DMA_FROM_DEVICE);
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		goto error;
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -1
index ed274f033626..e5116a86cfbc 100644
@@ -200,7 +200,7 @@ static void qed_ll2b_complete_rx_packet(void *cxt,
 	dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
 			 cdev->ll2->rx_size, DMA_FROM_DEVICE);
 
-	skb = build_skb(buffer->data, 0);
+	skb = slab_build_skb(buffer->data);
 	if (!skb) {
 		DP_INFO(cdev, "Failed to build SKB\n");
 		kfree(buffer->data);
include/linux/skbuff.h +1 -0
index 7be5bb4c94b6..0b391b635430 100644
@@ -1253,6 +1253,7 @@ struct sk_buff *build_skb_around(struct sk_buff *skb,
 void skb_attempt_defer_free(struct sk_buff *skb);
 
 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
+struct sk_buff *slab_build_skb(void *data);
 
 /**
  * alloc_skb - allocate a network buffer
net/bpf/test_run.c +1 -1
index 13d578ce2a09..611b1f4082cf 100644
@@ -1130,7 +1130,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 	sock_init_data(NULL, sk);
 
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		kfree(ctx);
net/core/skbuff.c +16 -4
index 1d9719e72f9d..ae5a6f7db37b 100644
@@ -269,12 +269,10 @@ static struct sk_buff *napi_skb_cache_get(void)
 	return skb;
 }
 
-/* Caller must provide SKB that is memset cleared */
-static void __build_skb_around(struct sk_buff *skb, void *data,
-			       unsigned int frag_size)
+static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
+					 unsigned int size)
 {
 	struct skb_shared_info *shinfo;
-	unsigned int size = frag_size ? : ksize(data);
 
 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 
@@ -296,15 +294,71 @@ static void __build_skb_around(struct sk_buff *skb, void *data,
 	skb_set_kcov_handle(skb, kcov_common_handle());
 }
 
+static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
+				     unsigned int *size)
+{
+	void *resized;
+
+	/* Must find the allocation size (and grow it to match). */
+	*size = ksize(data);
+	/* krealloc() will immediately return "data" when
+	 * "ksize(data)" is requested: it is the existing upper
+	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
+	 * that this "new" pointer needs to be passed back to the
+	 * caller for use so the __alloc_size hinting will be
+	 * tracked correctly.
+	 */
Quoted 8 lines

net/core/skbuff.c static void __build_skb_around(struct sk_buff *skb, void *data, · line 311

Quoted 1 line
net/core/skbuff.c +1 -0
+	resized = krealloc(data, *size, GFP_ATOMIC);

Hmm, I just realized, this trick will probably break the new kmalloc size tracking from Feng Tang (CC'd)? We need to make krealloc() update the stored size, right? And even worse if slab_debug redzoning is enabled and after commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") where the lack of update will result in redzone check failures.

I think it's still safe, as currently we skip the kmalloc redzone check by calling skip_orig_size_check() inside __ksize(). But as we have plan to remove this skip_orig_size_check() after all ksize() usage has been sanitized, we need to cover this krealloc() case.

Thanks,
Feng

#3 lore raw
Quoted 158 lines · Feng Tang
Quoted 148 lines · Vlastimil Babka
Quoted 147 lines · Kees Cook

syzkaller reported:

  BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
  Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb().

When build_skb() is passed a frag_size of 0, it means the buffer came from kmalloc. In these cases, ksize() is used to find its actual size, but since the allocation may not have been made to that size, actually perform the krealloc() call so that all the associated buffer size checking will be correctly notified (and use the "new" pointer so that compiler hinting works correctly). Split this logic out into a new interface, slab_build_skb(), but leave the original 0 checking for now to catch any stragglers.

Reported-by [email protected]
Fixes 38931d8989b5 ("mm: Make ksize() a reporting-only function")
Cc Jakub Kicinski <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc pepsipu <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc kasan-dev <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Yonghong Song <[email protected]>

---
v3:
- make sure "resized" is passed back so compiler hints survive
- update kerndoc (kuba)
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]/

---
 drivers/net/ethernet/broadcom/bnx2.c      |  2 +-
 drivers/net/ethernet/qlogic/qed/qed_ll2.c |  2 +-
 include/linux/skbuff.h                    |  1 +
 net/bpf/test_run.c                        |  2 +-
 net/core/skbuff.c                         | 70 ++++++++++++++++++++---
 5 files changed, 66 insertions(+), 11 deletions(-)
drivers/net/ethernet/broadcom/bnx2.c +1 -1
index fec57f1982c8..b2230a4a2086 100644
@@ -3045,7 +3045,7 @@ bnx2_rx_skb(struct bnx2 *bp, struct bnx2_rx_ring_info *rxr, u8 *data,
 
 	dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
 			 DMA_FROM_DEVICE);
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		goto error;
drivers/net/ethernet/qlogic/qed/qed_ll2.c +1 -1
index ed274f033626..e5116a86cfbc 100644
@@ -200,7 +200,7 @@ static void qed_ll2b_complete_rx_packet(void *cxt,
 	dma_unmap_single(&cdev->pdev->dev, buffer->phys_addr,
 			 cdev->ll2->rx_size, DMA_FROM_DEVICE);
 
-	skb = build_skb(buffer->data, 0);
+	skb = slab_build_skb(buffer->data);
 	if (!skb) {
 		DP_INFO(cdev, "Failed to build SKB\n");
 		kfree(buffer->data);
include/linux/skbuff.h +1 -0
index 7be5bb4c94b6..0b391b635430 100644
@@ -1253,6 +1253,7 @@ struct sk_buff *build_skb_around(struct sk_buff *skb,
 void skb_attempt_defer_free(struct sk_buff *skb);
 
 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
+struct sk_buff *slab_build_skb(void *data);
 
 /**
  * alloc_skb - allocate a network buffer
net/bpf/test_run.c +1 -1
index 13d578ce2a09..611b1f4082cf 100644
@@ -1130,7 +1130,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 	sock_init_data(NULL, sk);
 
-	skb = build_skb(data, 0);
+	skb = slab_build_skb(data);
 	if (!skb) {
 		kfree(data);
 		kfree(ctx);
net/core/skbuff.c +16 -4
index 1d9719e72f9d..ae5a6f7db37b 100644
@@ -269,12 +269,10 @@ static struct sk_buff *napi_skb_cache_get(void)
 	return skb;
 }
 
-/* Caller must provide SKB that is memset cleared */
-static void __build_skb_around(struct sk_buff *skb, void *data,
-			       unsigned int frag_size)
+static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
+					 unsigned int size)
 {
 	struct skb_shared_info *shinfo;
-	unsigned int size = frag_size ? : ksize(data);
 
 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 
@@ -296,15 +294,71 @@ static void __build_skb_around(struct sk_buff *skb, void *data,
 	skb_set_kcov_handle(skb, kcov_common_handle());
 }
 
+static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
+				     unsigned int *size)
+{
+	void *resized;
+
+	/* Must find the allocation size (and grow it to match). */
+	*size = ksize(data);
+	/* krealloc() will immediately return "data" when
+	 * "ksize(data)" is requested: it is the existing upper
+	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
+	 * that this "new" pointer needs to be passed back to the
+	 * caller for use so the __alloc_size hinting will be
+	 * tracked correctly.
+	 */
Quoted 8 lines

net/core/skbuff.c static void __build_skb_around(struct sk_buff *skb, void *data, · line 311

Quoted 1 line
net/core/skbuff.c +1 -0
+	resized = krealloc(data, *size, GFP_ATOMIC);

Hmm, I just realized, this trick will probably break the new kmalloc size tracking from Feng Tang (CC'd)? We need to make krealloc() update the stored size, right? And even worse if slab_debug redzoning is enabled and after commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") where the lack of update will result in redzone check failures.

Quoted 2 lines

I think it's still safe, as currently we skip the kmalloc redzone check by calling skip_orig_size_check() inside __ksize(). But as we have plan

Ah, right, I forgot. So that's good.

Quoted 2 lines

to remove this skip_orig_size_check() after all ksize() usage has been sanitized, we need to cover this krealloc() case.

Yeah, can be done as part of the removal then, thanks.

Quoted 2 lines

Thanks,
Feng

#4 lore raw
In reply to Kees Cook
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Wed, 7 Dec 2022 22:02:59 -0800 you wrote:

syzkaller reported:

  BUG: KASAN: slab-out-of-bounds in __build_skb_around+0x235/0x340 net/core/skbuff.c:294
  Write of size 32 at addr ffff88802aa172c0 by task syz-executor413/5295

For bpf_prog_test_run_skb(), which uses a kmalloc()ed buffer passed to build_skb().

[...]

Here is the summary with links:
- [net-next,v3] skbuff: Introduce slab_build_skb()

    https://git.kernel.org/netdev/net-next/c/ce098da1497c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html