Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2519041rwb; Mon, 3 Oct 2022 01:49:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Js7ukSPr8fqx/1FGLKG9QLhTV5VipkBQnmIW2xXacWLWgZPZUSUcvTGzSmwQumImGTAdU X-Received: by 2002:a17:902:e84c:b0:178:2a08:501c with SMTP id t12-20020a170902e84c00b001782a08501cmr21226610plg.110.1664786966090; Mon, 03 Oct 2022 01:49:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664786966; cv=none; d=google.com; s=arc-20160816; b=yKvr4F0vbBbjZ3x2NZz2he1SMPx4NcCSfGTZxQG5Uot13HMoTz2O31i7w0v95h5+Zt kDMeJ+6S+063HYtOYRMKX8dKeqgS83WmaxQWiNe9vdCTeUUjKAJQY/6B96snrMMt4T5a YQ0uNzTGpr4bOmVhIwVVmAI+dOaPV2M7HveWy5drVF54zSa+FNw9v8rBjiF+d9kALE2E TYGimYsiWws1KFmgmXTy4iOvJFWlC+50PWDgVOaNOENfBXrxABWd4lj+IMhV69GxMR2i 3j/ljw912nKOk7DQhoRtM1X2NTB0+3gE/AO1pfcTqcMocZSyevZs6tkOHxer20aMstvw N8Vw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0XHwsnizSr06hX3oSvbn4nd8+4Zvlw8SUT+8Lkxykg8=; b=OXagIQtppMd0ZQ1SzlZkTbmxXqppNhgcbs8sN+V7487F8sGPgpzEZJ27MBY/cXab+V k1aKKeQ39DxNdkeQZ8WYjrFGh+1jTmckB615cGCVki8YzZoR81dXDNlPmuacRHcm+YrB PkJ+3SmeINjJvZR4+/uV1hQipiXsM9jsqjHaDSEpZZTfWinKcjLtU/Y8z2znxfpd8ucI Xdwr6yb2URxXTyz5wgWAKmKRSf8+69mA/zvtJ5gnHeEtPBbclt1VN4xhMrCKim0SZnt6 I/Fx2Eq8oUr4bKpyP4WivTOquGfdJ6yPGbGEvny4elM5RxSX389gUo0yc7iOsqAyNAv3 j01Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Lczw+nVt; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mu6-20020a17090b388600b00205593e76e8si14937254pjb.81.2022.10.03.01.49.14; Mon, 03 Oct 2022 01:49:26 -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=@linuxfoundation.org header.s=korg header.b=Lczw+nVt; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231996AbiJCHmA (ORCPT + 99 others); Mon, 3 Oct 2022 03:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231976AbiJCHlU (ORCPT ); Mon, 3 Oct 2022 03:41:20 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E43EB491FB; Mon, 3 Oct 2022 00:24:29 -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 9407060F82; Mon, 3 Oct 2022 07:24:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5261C433D7; Mon, 3 Oct 2022 07:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664781869; bh=ivNjXrfrIXCEFG5ZUWRbebZnTyy8wgJJVSWxFA6JxRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lczw+nVtqJDfydECUD0maTGX97Cg1uk2XrrE1rKAkGMgPriJJPCsWQhCaUMFlSlZY e7t9AlYWUnYZS0xyQeb2HghMrSsfp7vncSKkhJZg1l1Sw9ljYhJGeZ2gTo1XYU6vGO +pHUn53denh8Gtp5dTUpFVrOcVMUVEDxcbTeQ4eQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maurizio Lombardi , Alexander Duyck , Chen Lin , Jakub Kicinski , Andrew Morton Subject: [PATCH 4.19 10/25] mm: prevent page_frag_alloc() from corrupting the memory Date: Mon, 3 Oct 2022 09:12:13 +0200 Message-Id: <20221003070715.711618269@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221003070715.406550966@linuxfoundation.org> References: <20221003070715.406550966@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 From: Maurizio Lombardi commit dac22531bbd4af2426c4e29e05594415ccfa365d upstream. 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. Link: https://lkml.kernel.org/r/20220715125013.247085-1-mlombard@redhat.com Fixes: b63ae8ca096d ("mm/net: Rename and move page fragment handling from net/ to mm/") Signed-off-by: Maurizio Lombardi Reviewed-by: Alexander Duyck Cc: Chen Lin Cc: Jakub Kicinski Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/page_alloc.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4603,6 +4603,18 @@ refill: /* 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--;