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 1606EC43334 for ; Mon, 11 Jul 2022 07:52:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F11594000C; Mon, 11 Jul 2022 03:52:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A04A940007; Mon, 11 Jul 2022 03:52:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0902294000C; Mon, 11 Jul 2022 03:52:36 -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 EADC9940007 for ; Mon, 11 Jul 2022 03:52:35 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B4EC920BCD for ; Mon, 11 Jul 2022 07:52:35 +0000 (UTC) X-FDA: 79674051870.01.DAA0208 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 5EE5B12006A for ; Mon, 11 Jul 2022 07:52:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657525954; 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=0fVL/6h+ioYmSvIw9J0PUDh//b3naVw7P8qpW/RZmLE=; b=iykVixE/J96fdpGSLuIxZvJlvvlzHaYWkZ6R5cDNcmnHI0xfPDE5q3Ec+ztKvZWIV0FqW0 gMJ6G3giZ4uHzN9uNwor812nzP4XErbOMe8vl403E+RXcsctrhfKVHPijDKLRo4o5UTEeW ib0TcgTGKTli6bckHbCePYEd3aqQqv4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-70-mz4UEfffNoaNIqStyUEDOg-1; Mon, 11 Jul 2022 03:52:28 -0400 X-MC-Unique: mz4UEfffNoaNIqStyUEDOg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 405982999B38; Mon, 11 Jul 2022 07:52:28 +0000 (UTC) Received: from raketa.redhat.com (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6CEF40D296D; Mon, 11 Jul 2022 07:52:26 +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] mm: prevent page_frag_alloc() from corrupting the memory Date: Mon, 11 Jul 2022 09:52:25 +0200 Message-Id: <20220711075225.15687-1-mlombard@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657525955; a=rsa-sha256; cv=none; b=LsBqiEbQ2ufVGdAltx8Q0JvVM1u4zfPoqFOB7I83KMnQgPUUj9EIh688h47ybzoBsbr8d4 yBeRZ4bPwu6BszkrhMSSV1W9t5D2/Ap2ivWjagw/WJ1pWIn0WDRc1Tavw9DFpg5abQndPN oyHCPsY7MXiaUEObr7cwAixZJMzpKRU= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="iykVixE/"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf29.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=1657525955; 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=0fVL/6h+ioYmSvIw9J0PUDh//b3naVw7P8qpW/RZmLE=; b=xTGVnSflXBUtXsecGhL13z55JLpHYnlcghPa5fuFhQdaH5GDOCd3mhLUpY0P+QvSWzVH+9 pBtxKUWpVdDiTLzyfFT+QHhkK6G5RwbZ+f9ftujWXKyy1KaqLYPgC1m9GfFd/wXGYin4QO tQa5DI0FSMA/TTkNsOw84hQTsMaMJJQ= Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="iykVixE/"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf29.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: x1aheq7swm6itekrx7h9nw336mk5f714 X-Rspamd-Queue-Id: 5EE5B12006A X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1657525955-467229 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; If this happens, 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. Signed-off-by: Maurizio Lombardi --- mm/page_alloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3df0485..7fb000d7e90c 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5611,12 +5611,17 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, /* if size can vary use size else just use PAGE_SIZE */ size = nc->size; #endif - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); - /* 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)) { + free_the_page(page, compound_order(page)); + nc->va = NULL; + return NULL; + } + + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); } nc->pagecnt_bias--; -- 2.31.1