Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp862156imw; Wed, 13 Jul 2022 09:16:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v0I0Eh9V5P43VDnVa/NQi9rVjOa0WeUNZFwrSCFGzxziWN99z4HSbg7REm5rlBBLwz74Ea X-Received: by 2002:a05:6402:2b8b:b0:43a:5475:f1ae with SMTP id fj11-20020a0564022b8b00b0043a5475f1aemr6057407edb.363.1657728971068; Wed, 13 Jul 2022 09:16:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657728971; cv=none; d=google.com; s=arc-20160816; b=IIOwhRCFBnYEwD+HZPsLiKeU1asULluw55YOWYQOvpf2mz2GEyrcjKKdJ8tktVpIlS ULcJmHOUBQpOzbFN4q2dnP1h4z2awLziuhuMHFFHauU8jTocL9aZc2JH8pVxGRGKw+F6 LbusFpANoOQabf/tmPs69V+U0fc16kJn6P2jPoEf5xfiDD+4B8YD9AL/bT8G/5Y3xwTT lwBW5TsNAn0G1nbL8R4fLFo9CMkz7IfoVOGpj0dDtN/psh6qcxRT2Snrt8JoAHK3ZpEA BYBsr5loTRDlLU7uyd6ig5uz2w/j3ViRPMy5qp4CaHkIlou8L7ADH2i5W0yqgA49QnLZ PVfQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=tXf1/GwN5VRQHjpjwxB/v4i3CCyRId2B1xpC6Jr7gXM=; b=sEptcwM+I+Xvo99pq9QAumwlPdUGw27EhCjomXoIAi9IZRK3SFaCEoyDBr8Gs7/RVz +nuj3xiyt+JnHMl0dzOYUjvumn27vBWmHnyAmJuVu30uyfQ0odmBNGh2Yo3ERL1B8hIE rtJIf6IzJ094LPsm55mtOC9qgXZyLz5CHUM8O/znFJE57gnvicij3ooxsirVGKYyqCv+ Y81uHVgdnRO3oXqFh0kKdtADOGceSf5kwj5bGmpQmb1tj2+W29yLG2WvMBxl/ru2o6A4 QJB5a6fd7umE9TgsdZlnzNASRDhVzto75VV59jHssEiatHNq9s5wfJU6EPePX3Azj9A/ TvxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=Qf191j2Q; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs18-20020a170906dc9200b0072aed50f0ffsi2514909ejc.663.2022.07.13.09.15.46; Wed, 13 Jul 2022 09:16:11 -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=@linux-foundation.org header.s=korg header.b=Qf191j2Q; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236952AbiGMPpp (ORCPT + 99 others); Wed, 13 Jul 2022 11:45:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236958AbiGMPpl (ORCPT ); Wed, 13 Jul 2022 11:45:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B9114E84A; Wed, 13 Jul 2022 08:45:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8621E6195E; Wed, 13 Jul 2022 15:45:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 847BCC3411E; Wed, 13 Jul 2022 15:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1657727137; bh=GZo3SLj/oGl49Xss42bjHxtoHp0PqzsJ7wTiSiRsKu8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Qf191j2Qu9cUwxfj8D/zsyS2B4cUskeOsFriYkXt/W+kcMl4o9Mk5WdXS/GcPWXOq 5Q4lPI07mFjOLy7o9uJApchupsOxpFIxEisxbyRPjmmWX3odJE+mPQO599lI3f1Jl2 Rm/ZJrHp7XHl3HEpQSVc30Mxt8bictGaERSkKA64= Date: Wed, 13 Jul 2022 08:45:36 -0700 From: Andrew Morton To: Maurizio Lombardi Cc: alexander.duyck@gmail.com, kuba@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, chen45464546@163.com Subject: Re: [PATCH V2] mm: prevent page_frag_alloc() from corrupting the memory Message-Id: <20220713084536.1af461b016a16c58baad7db2@linux-foundation.org> In-Reply-To: <20220713150143.147537-1-mlombard@redhat.com> References: <20220713150143.147537-1-mlombard@redhat.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Wed, 13 Jul 2022 17:01:43 +0200 Maurizio Lombardi wrote: > 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. > > ... > > --- 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--; I think we should have a comment here explaining (at least) why we'd bale after a successful allocation and explaining why we don't call free_the_page().