From patchwork Sun Jan 24 08:22:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 12042149 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E29B9C433E0 for ; Sun, 24 Jan 2021 08:23:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 88AE422582 for ; Sun, 24 Jan 2021 08:23:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 88AE422582 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2698B6B000A; Sun, 24 Jan 2021 03:23:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F25B6B000C; Sun, 24 Jan 2021 03:23:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E1616B000D; Sun, 24 Jan 2021 03:23:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0243.hostedemail.com [216.40.44.243]) by kanga.kvack.org (Postfix) with ESMTP id E8B916B000A for ; Sun, 24 Jan 2021 03:23:05 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id ABD808249980 for ; Sun, 24 Jan 2021 08:23:05 +0000 (UTC) X-FDA: 77739978330.19.peace40_2b062522757b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 91B1B1ACEA4 for ; Sun, 24 Jan 2021 08:23:05 +0000 (UTC) X-HE-Tag: peace40_2b062522757b X-Filterd-Recvd-Size: 6639 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Sun, 24 Jan 2021 08:23:05 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id b8so5764666plx.0 for ; Sun, 24 Jan 2021 00:23:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/x6cdpjaOH33+ELSrGwY3sQ3qRgzggzyFztG5hnhdsk=; b=aO4gEjmWuAuh0gZMGkfqR1QuVFpQRIH3UxpYZqGzPJi9zwsdd163c6MNj6YBKia+ZQ xaV2O1fDHJGBjZkyX1Xfps2OADbM1Seeotu2I7jMQkobgb5Dt2hquvM6Uci/8mrklLGi E3vNbCOlcObgpoQV0H1VtQUDw0Z9L4hzJPcTQ/g+XwFU5xpQdzmuAu8yuEkwkVfij7Vb 1v8AQi8CTlNtZZXthdGd+fm3tikUnrkoadMRBh/RdkuULLxDkVQRN3tU2QDzq1ZjegOY /Ctv4/l98syX528uaxkOcfQ2sCkUrorQ7xGxJ5LJRFAIenRQfUDOpKe2hOeJ6UUP9r5s 444w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/x6cdpjaOH33+ELSrGwY3sQ3qRgzggzyFztG5hnhdsk=; b=ZtYjnhhX6D+RPdPn46XLcFmoV9ewXfiV2DojehE6AM04TiX3qriAuMb0zLiknR//7a E24lMvRtOswZs4i1d7ewrgUp40XrKCfhreZIuZ2tz2gKR8zSNvIMrK0ZYBc4R1oo76NO NNLaRDEREtEFVpPVSTOQlU38t0KLFib1Y2NRJbP5QETPa8D62/jBhoB6YwCyNzKpeCaS e0AQdxxB2tITQuPOB6ba9ePcfqfMlG64QkP117ldZklTWQbRia1Z0IirGpOd3ZtftJHB ZnPbWb7zwuqyry2CMLHJpRSNujght2+QgAVXLBDwMmaUDnY+Og3lX6xBlAQfX1NeQfeO 35/A== X-Gm-Message-State: AOAM530yOWd/CP5SItm+UtFAktvCAit8Z5qN0ZMAU3k/nJIozAlN3Fk5 l8rdkIfLna9cyn4ig6ee1GCy2sm1qQs= X-Google-Smtp-Source: ABdhPJyrsytE/NMqNg0eGkiocOJPB9OIhVhalaGRXuMoTtXPLltyqIr5T+D2zqmrI8yVhUBnUVlz2Q== X-Received: by 2002:a17:902:e80c:b029:de:a20b:7a9c with SMTP id u12-20020a170902e80cb02900dea20b7a9cmr13712689plg.12.1611476584165; Sun, 24 Jan 2021 00:23:04 -0800 (PST) Received: from bobo.ozlabs.ibm.com ([124.170.13.62]) by smtp.gmail.com with ESMTPSA id gb12sm11799757pjb.51.2021.01.24.00.22.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Jan 2021 00:23:03 -0800 (PST) From: Nicholas Piggin To: linux-mm@kvack.org, Andrew Morton Cc: Nicholas Piggin , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Zefan Li , Jonathan Cameron , Christoph Hellwig , Christophe Leroy , Rick Edgecombe , Ding Tianhong Subject: [PATCH v10 02/12] mm: apply_to_pte_range warn and fail if a large pte is encountered Date: Sun, 24 Jan 2021 18:22:20 +1000 Message-Id: <20210124082230.2118861-3-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20210124082230.2118861-1-npiggin@gmail.com> References: <20210124082230.2118861-1-npiggin@gmail.com> MIME-Version: 1.0 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: apply_to_pte_range might mistake a large pte for bad, or treat it as a page table, resulting in a crash or corruption. Add a test to warn and return error if large entries are found. Signed-off-by: Nicholas Piggin Reviewed-by: Christoph Hellwig --- mm/memory.c | 66 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index feff48e1465a..672e39a72788 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2440,13 +2440,21 @@ static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud, } do { next = pmd_addr_end(addr, end); - if (create || !pmd_none_or_clear_bad(pmd)) { - err = apply_to_pte_range(mm, pmd, addr, next, fn, data, - create, mask); - if (err) - break; + if (pmd_none(*pmd) && !create) + continue; + if (WARN_ON_ONCE(pmd_leaf(*pmd))) + return -EINVAL; + if (!pmd_none(*pmd) && WARN_ON_ONCE(pmd_bad(*pmd))) { + if (!create) + continue; + pmd_clear_bad(pmd); } + err = apply_to_pte_range(mm, pmd, addr, next, + fn, data, create, mask); + if (err) + break; } while (pmd++, addr = next, addr != end); + return err; } @@ -2468,13 +2476,21 @@ static int apply_to_pud_range(struct mm_struct *mm, p4d_t *p4d, } do { next = pud_addr_end(addr, end); - if (create || !pud_none_or_clear_bad(pud)) { - err = apply_to_pmd_range(mm, pud, addr, next, fn, data, - create, mask); - if (err) - break; + if (pud_none(*pud) && !create) + continue; + if (WARN_ON_ONCE(pud_leaf(*pud))) + return -EINVAL; + if (!pud_none(*pud) && WARN_ON_ONCE(pud_bad(*pud))) { + if (!create) + continue; + pud_clear_bad(pud); } + err = apply_to_pmd_range(mm, pud, addr, next, + fn, data, create, mask); + if (err) + break; } while (pud++, addr = next, addr != end); + return err; } @@ -2496,13 +2512,21 @@ static int apply_to_p4d_range(struct mm_struct *mm, pgd_t *pgd, } do { next = p4d_addr_end(addr, end); - if (create || !p4d_none_or_clear_bad(p4d)) { - err = apply_to_pud_range(mm, p4d, addr, next, fn, data, - create, mask); - if (err) - break; + if (p4d_none(*p4d) && !create) + continue; + if (WARN_ON_ONCE(p4d_leaf(*p4d))) + return -EINVAL; + if (!p4d_none(*p4d) && WARN_ON_ONCE(p4d_bad(*p4d))) { + if (!create) + continue; + p4d_clear_bad(p4d); } + err = apply_to_pud_range(mm, p4d, addr, next, + fn, data, create, mask); + if (err) + break; } while (p4d++, addr = next, addr != end); + return err; } @@ -2522,9 +2546,17 @@ static int __apply_to_page_range(struct mm_struct *mm, unsigned long addr, pgd = pgd_offset(mm, addr); do { next = pgd_addr_end(addr, end); - if (!create && pgd_none_or_clear_bad(pgd)) + if (pgd_none(*pgd) && !create) continue; - err = apply_to_p4d_range(mm, pgd, addr, next, fn, data, create, &mask); + if (WARN_ON_ONCE(pgd_leaf(*pgd))) + return -EINVAL; + if (!pgd_none(*pgd) && WARN_ON_ONCE(pgd_bad(*pgd))) { + if (!create) + continue; + pgd_clear_bad(pgd); + } + err = apply_to_p4d_range(mm, pgd, addr, next, + fn, data, create, &mask); if (err) break; } while (pgd++, addr = next, addr != end);