Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp815227imw; Wed, 13 Jul 2022 08:26:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t34GvDDdiJms5vjyI+tuNLbfUOKr3kbCvx5jtuYnqVC0UbiTvMb/V61G44iZVGMreEzWvd X-Received: by 2002:a63:2a8d:0:b0:412:3286:6bfe with SMTP id q135-20020a632a8d000000b0041232866bfemr3419624pgq.239.1657725996740; Wed, 13 Jul 2022 08:26:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657725996; cv=none; d=google.com; s=arc-20160816; b=hu0zdJ5Phg+KNcb0X+6Xww2c0k9CrzuPdcwxo6w4YDeZnGbTnGrPsRfx0mmq8FytXZ UFdYJJzN1iikgcM/qnixrsoA1d5y+yK+E7XhVI6NIUTdaUpMnYBvOuqLt+dsHPbhk9Tg hYG+a6Bj28DSLzURZSwd9h/8pon1ksugNOwBQ+RBQ2t5sr72ILcbquYFCniY+L6g8X12 4AAfAFGw+YdT5lANDdxOi2JL3+9Tx2QSd77sUO6UkDcM1JlT9OvP6L6lNphUeQWrzDeP hL+kFNajxE/I7Sdtg4Tmhw7DtFhIqHAPxqUlzB31cM9yZy9usgsgfMou2Br4nO99a17Y DvpA== 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=w0hQGbL9sUey3asO5//RVCwLG4Fv9JCcj/VgRL7bqrA=; b=HlPWiQ60nsXw555/wjdMnUAiIZJ1I8PYStmbbAFRGsE2Es+eQPNgx7KaeZ7fo9Tae3 d7xofSI4d1AafYLy0FvosBV3zu4POGvpVpK5mgCnXyQEyCyXnuQ+BNhP0i5EYnrKnIEY wIq3c4rUQdQHRpf2jekkrJRE7FohUYRwmxbKz39/A4Nnb2Vd/3bTiUpY5ZFSghX4AD+5 BywhKBf4B9KHLDfJjaN1E+dSpdnaN1ZJRgIuXHVQ77YnOpiWfRuBaCDGWL7iLGjgVOkD WhQJJH9mjEyATZvCxV9VjUbeUPA0xLLL0EMiTVg08zVCdZY3NV4W4jm5hTrRFTZGhELm Iv1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J1vRRnWY; 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 a8-20020a1709027d8800b0015d1f88d9b9si16241208plm.458.2022.07.13.08.26.24; Wed, 13 Jul 2022 08:26:36 -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=J1vRRnWY; 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 S236659AbiGMO7T (ORCPT + 99 others); Wed, 13 Jul 2022 10:59:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236671AbiGMO7E (ORCPT ); Wed, 13 Jul 2022 10:59:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 196FC186CF for ; Wed, 13 Jul 2022 07:59:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657724342; 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=w0hQGbL9sUey3asO5//RVCwLG4Fv9JCcj/VgRL7bqrA=; b=J1vRRnWY//5U4zehULQDsB4hOYYgQNh4pdnZHi46kGnfefYSZEcoONw+Ajbpclw+3ywaYM Qf9PEIhfdCgV8Nu1yjge1/izuOy21R5Pb41vsOr4mt4qv3dgo96I1s1j4LvQO9dmXeI8of l6D8nvfJe84bk3tYhA5eNFxioHT13eo= 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-497-z1xXtB4MP9-llpg5yvzC_A-1; Wed, 13 Jul 2022 10:58:57 -0400 X-MC-Unique: z1xXtB4MP9-llpg5yvzC_A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EC9DC3C2F762; Wed, 13 Jul 2022 14:58:56 +0000 (UTC) Received: from raketa.redhat.com (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84B2B2026D07; Wed, 13 Jul 2022 14:58:55 +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: Wed, 13 Jul 2022 16:58:54 +0200 Message-Id: <20220713145854.147356-1-mlombard@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,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 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. Signed-off-by: Maurizio Lombardi --- mm/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e008a3df0485..b1407254a826 100644 --- 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--; -- 2.31.1