Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4003853pxb; Tue, 26 Jan 2021 09:51:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvEBIxWdJ9QpoVyhQevPTUfifIQd90cEgJ9xTECMyaqJSw4cXGMPRl1tNQmhgnfnr7TPrj X-Received: by 2002:a05:6402:70a:: with SMTP id w10mr5578061edx.184.1611683510838; Tue, 26 Jan 2021 09:51:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611683510; cv=none; d=google.com; s=arc-20160816; b=dE21AauTBVFVQ7hzwOuNGaH+33inYSyvuJ3kQd0P9wSxo3jPSzpBwC/T4YK/dOfyT+ o25lEjMtvAF3H+ACErK/auJWudN6h/uAT/UEgU2YAi6EUCXWgKinHa/qecdgzDkc4r3U 7sOMYAp8rRw1stGciPixwBI86QfelTM6lKLwBt4VqvLJrcvGdZHtLV8qww4nG3Y0nMjg Ve95Q1kWsb9XRPPWqzHMrTHtdC3Inv+klfokWW9I6I99DKIY1jnbpUo/LR3j7CDFa8Oq Z+tVOOXc2HSE+YSk/vxpMoatklWw6BvzpT8ORqbBB793b6YkeoEnvDWza9vgWMozaSbk Nzxw== 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=+kADzMX37IC0SNZ2OcxrT8BUfNQoydwzzBPieGkGRNo=; b=GMdbTBZMe+Wq1H+DN5dnKXX6bwS7UjE7wx0IMW7s5Aw83AvFhEfT53EgfiQPROiX4w M+f+Eyzi0x0tS13iQjp3Pt9KvuY9kNJ3E8jiR7qQUmiEQZLkJx4AuL4YFchR/8PkAnsa PClLgOQ8KN018I65BeAxaweWQAjKxVn9lsOoylcd6tqyQcFeUPCdbFnRcYi29Bm97h31 XLe9WcMbjb5hGXMqEY7quuAF67412xD1StRn1K5jZ9KaBVrQEWAAKWrMrScumsppK51J O/C0HhD4CDI5S2SB3z+Uni3tLcfzDy+9ewse/sbrNm25oXvt3qBmchZbuQVPlD5SoQr7 K2tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=hElHLJuy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q3si9080304edn.37.2021.01.26.09.51.25; Tue, 26 Jan 2021 09:51:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=hElHLJuy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S2390158AbhAZIcw (ORCPT + 99 others); Tue, 26 Jan 2021 03:32:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:58162 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728247AbhAYSnU (ORCPT ); Mon, 25 Jan 2021 13:43:20 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 59721221E7; Mon, 25 Jan 2021 18:42:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1611600147; bh=IvC8Y5xINbI2xrQg1uy9+MeeYbIZw42VCzYgpTN/ilA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hElHLJuyo8MXE6ilLddW64ZLhraYu23E0Jy1Eo9pqsqK4kNGYITjIRSmgjynQMJ4+ ov9yzwuxpfGX9L+GA6LHQdTQ1AdP0HWujEvWlxOSXkXvslQomnjQrEo8Jv8ZQ91k2G /f+D4vwk2K8++QS5oZhA77JMzdaVtaCZmUo/Q6bE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lecopzer Chen , Andrey Ryabinin , Dan Williams , Dmitry Vyukov , Alexander Potapenko , YJ Chiang , Andrey Konovalov , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 49/58] kasan: fix unaligned address is unhandled in kasan_remove_zero_shadow Date: Mon, 25 Jan 2021 19:39:50 +0100 Message-Id: <20210125183158.814212724@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210125183156.702907356@linuxfoundation.org> References: <20210125183156.702907356@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lecopzer Chen commit a11a496ee6e2ab6ed850233c96b94caf042af0b9 upstream. During testing kasan_populate_early_shadow and kasan_remove_zero_shadow, if the shadow start and end address in kasan_remove_zero_shadow() is not aligned to PMD_SIZE, the remain unaligned PTE won't be removed. In the test case for kasan_remove_zero_shadow(): shadow_start: 0xffffffb802000000, shadow end: 0xffffffbfbe000000 3-level page table: PUD_SIZE: 0x40000000 PMD_SIZE: 0x200000 PAGE_SIZE: 4K 0xffffffbf80000000 ~ 0xffffffbfbdf80000 will not be removed because in kasan_remove_pud_table(), kasan_pmd_table(*pud) is true but the next address is 0xffffffbfbdf80000 which is not aligned to PUD_SIZE. In the correct condition, this should fallback to the next level kasan_remove_pmd_table() but the condition flow always continue to skip the unaligned part. Fix by correcting the condition when next and addr are neither aligned. Link: https://lkml.kernel.org/r/20210103135621.83129-1-lecopzer@gmail.com Fixes: 0207df4fa1a86 ("kernel/memremap, kasan: make ZONE_DEVICE with work with KASAN") Signed-off-by: Lecopzer Chen Cc: Andrey Ryabinin Cc: Dan Williams Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: YJ Chiang Cc: Andrey Konovalov Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/kasan/kasan_init.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) --- a/mm/kasan/kasan_init.c +++ b/mm/kasan/kasan_init.c @@ -372,9 +372,10 @@ static void kasan_remove_pmd_table(pmd_t if (kasan_pte_table(*pmd)) { if (IS_ALIGNED(addr, PMD_SIZE) && - IS_ALIGNED(next, PMD_SIZE)) + IS_ALIGNED(next, PMD_SIZE)) { pmd_clear(pmd); - continue; + continue; + } } pte = pte_offset_kernel(pmd, addr); kasan_remove_pte_table(pte, addr, next); @@ -397,9 +398,10 @@ static void kasan_remove_pud_table(pud_t if (kasan_pmd_table(*pud)) { if (IS_ALIGNED(addr, PUD_SIZE) && - IS_ALIGNED(next, PUD_SIZE)) + IS_ALIGNED(next, PUD_SIZE)) { pud_clear(pud); - continue; + continue; + } } pmd = pmd_offset(pud, addr); pmd_base = pmd_offset(pud, 0); @@ -423,9 +425,10 @@ static void kasan_remove_p4d_table(p4d_t if (kasan_pud_table(*p4d)) { if (IS_ALIGNED(addr, P4D_SIZE) && - IS_ALIGNED(next, P4D_SIZE)) + IS_ALIGNED(next, P4D_SIZE)) { p4d_clear(p4d); - continue; + continue; + } } pud = pud_offset(p4d, addr); kasan_remove_pud_table(pud, addr, next); @@ -457,9 +460,10 @@ void kasan_remove_zero_shadow(void *star if (kasan_p4d_table(*pgd)) { if (IS_ALIGNED(addr, PGDIR_SIZE) && - IS_ALIGNED(next, PGDIR_SIZE)) + IS_ALIGNED(next, PGDIR_SIZE)) { pgd_clear(pgd); - continue; + continue; + } } p4d = p4d_offset(pgd, addr);