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 E4228C433EF for ; Fri, 15 Jul 2022 12:50:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 472536B010F; Fri, 15 Jul 2022 08:50:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FAF06B0110; Fri, 15 Jul 2022 08:50:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29BEC9401E5; Fri, 15 Jul 2022 08:50:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 166176B010F for ; Fri, 15 Jul 2022 08:50:28 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DD04820BDA for ; Fri, 15 Jul 2022 12:50:27 +0000 (UTC) X-FDA: 79689317694.09.0B22005 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 314881C0078 for ; Fri, 15 Jul 2022 12:50:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657889426; 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=yG6DZkuyy9DwOyDsoQVS06AJjtPTlfn1QRvO2HjX0rY=; b=In/8S+AjxYpzOedqIqaGS92Hs/I1TwBXGV3aK2WLyeMyBrZR2rSKyQ/S9+p6RU8YWSE/7+ hoCahxiRlggr6U0Xj9qdURBaPFX3zHQY3Y++2lVp/QDBs4Xv3wr3R1M7RR3od9NJ29jsKX q5tfBSlnNgXZtnCcAwYAbiofzCCX0Q8= 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-318-DWMnpf_aMruFoaMM0WX1bg-1; Fri, 15 Jul 2022 08:50:16 -0400 X-MC-Unique: DWMnpf_aMruFoaMM0WX1bg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A4BE101A58D; Fri, 15 Jul 2022 12:50:16 +0000 (UTC) Received: from raketa.redhat.com (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1FBF40CFD0A; Fri, 15 Jul 2022 12:50:14 +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 V3] mm: prevent page_frag_alloc() from corrupting the memory Date: Fri, 15 Jul 2022 14:50:13 +0200 Message-Id: <20220715125013.247085-1-mlombard@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657889427; 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=yG6DZkuyy9DwOyDsoQVS06AJjtPTlfn1QRvO2HjX0rY=; b=TCdkYpeJvcy8YjaiB+eb5pIieM1K4cVkuFp0bI8WJw1jkfmQretQ1c0AyfeVlmJqdCd3Wq 3uYS0Or+nGV+R6XBPOgRu+nLpnWszU2aR1c55svttPgv3ohkHSSReT8BlLSgL7Cj1M0Hb3 4UI3BW8ItLIoW1JXQB1tPHKCWu3QK80= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657889427; a=rsa-sha256; cv=none; b=3323Sg65vbToQE+Tzt1Kq2rLEb1Zjccawv8o27Jo7ilxPkHE5Wlhs0jGLA50WHyBJqeRDx 3wWXZr0zApW9BywJPzqbBcTbNuFgSnlMWuWaLnVKkXGejlfc2ZIJBVfnTmwnVhHdwuFmdN 6sjP0zG9HY50zUsbnUO9Vbd5hWUqcfg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="In/8S+Aj"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf18.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-Rspamd-Server: rspam05 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="In/8S+Aj"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf18.hostedemail.com: domain of mlombard@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=mlombard@redhat.com X-Stat-Signature: 8364fkfximwhwtzs7n6mrdayffkbcjgd X-Rspamd-Queue-Id: 314881C0078 X-HE-Tag: 1657889427-321214 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. V3: add a comment to explain why we return NULL. Signed-off-by: Maurizio Lombardi --- mm/page_alloc.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3df0485..59c4dddf379f 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5617,6 +5617,18 @@ 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)) { + /* + * The caller is trying to allocate a fragment + * with fragsz > PAGE_SIZE but the cache isn't big + * enough to satisfy the request, this may + * happen in low memory conditions. + * We don't release the cache page because + * it could make memory pressure worse + * so we simply return NULL here. + */ + return NULL; + } } nc->pagecnt_bias--; -- 2.31.1