linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: page_frag: Warn_on when frag_alloc size is bigger than PAGE_SIZE
@ 2022-05-28 15:39 Chen Lin
  2022-05-29 23:30 ` Andrew Morton
  0 siblings, 1 reply; 17+ messages in thread
From: Chen Lin @ 2022-05-28 15:39 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Chen Lin

netdev_alloc_frag->page_frag_alloc may cause memory corruption in 
the following process:

1. A netdev_alloc_frag function call need alloc 200 Bytes to build a skb.

2. Insufficient memory to alloc PAGE_FRAG_CACHE_MAX_ORDER(32K) in 
__page_frag_cache_refill to fill frag cache, then one page(eg:4K) 
is allocated, now current frag cache is 4K, alloc is success, 
nc->pagecnt_bias--.

3. Then this 200 bytes skb in step 1 is freed, page->_refcount--.

4. Another netdev_alloc_frag function call need alloc 5k, page->_refcount 
is equal to nc->pagecnt_bias, reset page count bias and offset to 
start of new frag. page_frag_alloc will return the 4K memory for a 
5K memory request.

5. The caller write on the extra 1k memory which is not actual allocated 
will cause memory corruption.

page_frag_alloc is for fragmented allocation. We should warn the caller 
to avoid memory corruption.

Signed-off-by: Chen Lin <chen45464546@163.com>
---
 mm/page_alloc.c |    5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index e008a3d..6c0db52 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -5574,6 +5574,11 @@ void *page_frag_alloc_align(struct page_frag_cache *nc,
 	struct page *page;
 	int offset;
 
+	/* frag_alloc is not suitable for memory alloc which fragsz
+	 * is bigger than PAGE_SIZE, use kmalloc or alloc_pages instead.
+	 */
+	WARN_ON(fragsz > PAGE_SIZE);
+
 	if (unlikely(!nc->va)) {
 refill:
 		page = __page_frag_cache_refill(nc, gfp_mask);
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-07-08  8:07 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-28 15:39 [PATCH] mm: page_frag: Warn_on when frag_alloc size is bigger than PAGE_SIZE Chen Lin
2022-05-29 23:30 ` Andrew Morton
2022-05-30 13:39   ` [PATCH v2] " Chen Lin
2022-05-30 19:27     ` Jakub Kicinski
2022-05-30 19:29       ` Jakub Kicinski
2022-05-31 14:41         ` Chen Lin
2022-05-31 15:14           ` Jakub Kicinski
2022-05-31 15:36             ` Chen Lin
2022-05-31 15:47               ` Jakub Kicinski
2022-05-31 18:28                 ` Alexander Duyck
2022-06-01 12:32                   ` 愚树
2022-06-01 15:04                     ` Alexander Duyck
2022-07-06 15:21                       ` Maurizio Lombardi
2022-07-08  8:06                     ` Maurizio Lombardi
2022-05-30 20:07     ` Andrew Morton
2022-05-31 14:43       ` [PATCH v3] " Chen Lin
2022-05-31 23:45         ` Eric Dumazet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).