Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp449969imw; Fri, 15 Jul 2022 06:28:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s+QeUnLzhUqL6hZLN1j7Tkr8Ot42yYf3AlDlXA+fY/r/WRiUyQ/+3w3iMu5ijhGMGvfb0F X-Received: by 2002:a05:6870:6195:b0:100:ee8a:ce6d with SMTP id a21-20020a056870619500b00100ee8ace6dmr7525238oah.155.1657891725247; Fri, 15 Jul 2022 06:28:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657891725; cv=none; d=google.com; s=arc-20160816; b=DwkVucFIODS9bO+h/Y/q6nQpqAIpMZl07hsK4xPi16GbQKXDNTR6doJYbc7AwmiXVX EoCvRmQoxLKzaPpQ16sH4/C7mNVy/78MI/Bg7iWK+BzeeM7Ji6ATemCDFNBDiLSokRwX uANalnhR71DXB6LNPUXZU+XH0f6GGOlGbYzS6ECh2Xjd74fFh9eciF2UiMBA9UT5+CuX 8uYCux0L10kO4MAW0+ydgM16CafFuqsEAQ3szvlLRLZ+ypYPkZ5XUCvK4GV2x+OGRL2M QMQgFlp4O/6C+fgs9bkmRvvf4szN/61V6FXsS9OxOrlkV7OCXF18XUyCU00poAs74K1M iDPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=yG6DZkuyy9DwOyDsoQVS06AJjtPTlfn1QRvO2HjX0rY=; b=F2zYjVjmV1SVVf7m+7yfTfhWImtrwhKbQkdcjp5TdrTlh+THxNhsrCZDGvzsrtsU0m XVeMGdSe2wP/ULYz6Tn1XOIWvOrl9LYqbz1k8/k73H5MQaZQO9oOjt1leD9G0w0ImaSF WRySpfVqalCQQhp+zNVeEkv2K6vaArEp3LjC6+ppG6n7dk9vG/5Jf+FEy+75pXlizAme lPaGmh24/ZZ4zMNeokAXOvrcSSlubKJ7lkGXiHGez0CyCo4zmwecIuYyHOmQte+RM0Db /mrH8g0bmVV/BV9maDUxq9kb//79EA3+vRI3qLDIcwHpoQB8DplMRu2vqqXht5jufDIL 1eDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="In/8S+Aj"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q31-20020a056830441f00b0061c8273e0absi1083423otv.207.2022.07.15.06.28.31; Fri, 15 Jul 2022 06:28:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="In/8S+Aj"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232296AbiGOMub (ORCPT + 99 others); Fri, 15 Jul 2022 08:50:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230129AbiGOMu2 (ORCPT ); Fri, 15 Jul 2022 08:50:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5DED255B8 for ; Fri, 15 Jul 2022 05:50:27 -0700 (PDT) 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 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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