From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D2F1C433EF for ; Wed, 13 Jul 2022 15:03:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFD09940141; Wed, 13 Jul 2022 11:03:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BAEBC940134; Wed, 13 Jul 2022 11:03:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A755E940141; Wed, 13 Jul 2022 11:03:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9759A940134 for ; Wed, 13 Jul 2022 11:03:07 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6122634E64 for ; Wed, 13 Jul 2022 15:03:07 +0000 (UTC) X-FDA: 79682394414.22.4FCBC6F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 414EAC007F for ; Wed, 13 Jul 2022 15:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657724585; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=w0hQGbL9sUey3asO5//RVCwLG4Fv9JCcj/VgRL7bqrA=; b=d0qXP2ZVF8Veiyqwk0AJJ4vzOeqAhXTuMXZjLzVZSJ7U45oWIhGYUw+9robOhRTP6aMuQe yrHpK7Fq9/Dg4tTnU09UgdARVJlpgaVbFJu8lYLjF/Q5xG4NkiTHa91Cq+KetSlo/Z28Sk FMwasl9G/8AuG9anVJR88VNbY5iiTpc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-15-o_HSiA-VPWyfXP-L7SP2aA-1; Wed, 13 Jul 2022 11:01:45 -0400 X-MC-Unique: o_HSiA-VPWyfXP-L7SP2aA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 51AEF85A586; Wed, 13 Jul 2022 15:01:45 +0000 (UTC) Received: from raketa.redhat.com (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0C04141510F; Wed, 13 Jul 2022 15:01:43 +0000 (UTC) From: Maurizio Lombardi To: alexander.duyck@gmail.com Cc: kuba@kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, chen45464546@163.com Subject: [PATCH V2] mm: prevent page_frag_alloc() from corrupting the memory Date: Wed, 13 Jul 2022 17:01:43 +0200 Message-Id: <20220713150143.147537-1-mlombard@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=d0qXP2ZV; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf28.hostedemail.com: domain of mlombard@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=mlombard@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657724586; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=w0hQGbL9sUey3asO5//RVCwLG4Fv9JCcj/VgRL7bqrA=; b=c7pxlAyOvDsxbyBO8928YtcIN9t0QQe0R9VKzLUz6o4J2awY3N2Gsj6raEV0maI6mazowh EDtI1icGBod4cv0KNWTV1dkE2itfT9B3SNupio+eWR8/GcqVySTRD7shmXeRpQ2MTMiS97 kafI+bX6pu23f66NvcAwAkJgcvGttBg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657724586; a=rsa-sha256; cv=none; b=LHw7y3T8kKZsIVYnD6qP24ENXbxzYhl2TcrJL/6rppeHuFp8gn/njcftrUiygNHkYx5Mal EZRWIh7SjfW3Pl+deg3h4BBWssGNoiQAUh/8SN4+AaizDEdvTSBA7S+cmZoUGDyeJMBcmc mLYWT6h0z68y5co+n/HPTHzNyRmycbM= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 414EAC007F X-Stat-Signature: gxexn8c38fzpxcpzcrwbcok8kzdaiida Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=d0qXP2ZV; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf28.hostedemail.com: domain of mlombard@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=mlombard@redhat.com X-Rspam-User: X-HE-Tag: 1657724586-852253 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: A number of drivers call page_frag_alloc() with a fragment's size > PAGE_SIZE. In low memory conditions, __page_frag_cache_refill() may fail the order 3 cache allocation and fall back to order 0; In this case, the cache will be smaller than the fragment, causing memory corruptions. Prevent this from happening by checking if the newly allocated cache is large enough for the fragment; if not, the allocation will fail and page_frag_alloc() will return NULL. V2: do not free the cache page because this could make memory pressure even worse, just return NULL. Signed-off-by: Maurizio Lombardi --- mm/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3df0485..b1407254a826 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5617,6 +5617,8 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; offset = size - fragsz; + if (unlikely(offset < 0)) + return NULL; } nc->pagecnt_bias--; -- 2.31.1