Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2319666pxb; Mon, 20 Sep 2021 18:42:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNVQKbspDtn3LZOg1AzhWMOCt0YrTX/hN+7DwHI+ngC82UCXrBbIIFAh/RNMBNVl2d9IgI X-Received: by 2002:a05:6e02:dd3:: with SMTP id l19mr19599393ilj.144.1632188579339; Mon, 20 Sep 2021 18:42:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188579; cv=none; d=google.com; s=arc-20160816; b=cGowuPIIGg42yImUS11fvIObScQe+mxey36RxyUzKp6QukGa2rG/pVTGweW+rEV/ml B8VOVHcI5m4FGjPlYo8t2reRMMIwlYhfrmtfo8tk55ngbt8wpoKHMEEjVoF7RYEjI3gr 7w0UHTrjXnYLERmfgFOGsbE/EwaBT1Qhq3PdmnwiHXhTMiEuJXG6Y6L6YBGoNuYPGpCm +YlhgVTLhEKKOPx6DzCh4q/vHUxrXE3jfvNklmsfAln+iRtH2xqcqSKAPT+Q1ZOZAWhc yhVvmXGVHEEcKZY+1wNUr3YA+ueg7Y+GIhwSK8jLjAoHz1TekeNK12/TJzB4CuF8YiSr MByg== 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=K355r9tPZUgaoI5QvxC9uahGjDVh1RGBkvHBxjifXL8=; b=L6ofMikGpLDkbFdSiQPBhK5+xfJQOCS0PR3GwF320hOJLnNsakjb5McvlQyyytr6GG JQ1P0fUzjp3mIdxuMGqZFbq5Y/6oDGLKF+0s/mw6bGwoGCZzS3wqQVnB/I8heZ/z8MJa 8KCZN5n8jZocZDTScDr50vtZBM/N60Ec7rsGyvTQ7vg+rdUZLQEKiChaxw/ChY83cbkV vIRpmioVierbwWmLjHgnfvUQvZOlxuKobfskxYY2MJnAWLFW/DDjxQeH8MQVkkRVe/ay SzbHDfQJZLXBc1OnZcl7n0Yj+zGthSlTwu/1khQm/k9pwDjPRZ3x3Fh/4HUE7vs240Cu 13Mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HhQqJHq8; 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 k15si13079048ilo.140.2021.09.20.18.42.48; Mon, 20 Sep 2021 18:42:59 -0700 (PDT) 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=HhQqJHq8; 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 S237605AbhITQ40 (ORCPT + 99 others); Mon, 20 Sep 2021 12:56:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:36476 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231450AbhITQwl (ORCPT ); Mon, 20 Sep 2021 12:52:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 96BAD61283; Mon, 20 Sep 2021 16:49:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632156600; bh=45SKdGiHkmRp6wIZWsLXTn4XbutAhJ20hsO9+fLMeTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HhQqJHq8VzXc3EtX0I8i9OmYU/VTHsP1S8I3cE2r9F4Tr36pP8TlDM3xr2cBZk7Io GRJvyXtDAVB3qrb/e65QNggAcdcF2v206w7zwwIjbDQ/IZ1QhKhBr50V5QSRB1HkjF YazHuZXxtvD9IFSO+bcsu0S6RVFigyYCTFE0bkGg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Olsa , Mike Rapoport , Borislav Petkov , David Hildenbrand , Dave Hansen Subject: [PATCH 4.4 126/133] x86/mm: Fix kern_addr_valid() to cope with existing but not present entries Date: Mon, 20 Sep 2021 18:43:24 +0200 Message-Id: <20210920163916.748703240@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163912.603434365@linuxfoundation.org> References: <20210920163912.603434365@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: Mike Rapoport commit 34b1999da935a33be6239226bfa6cd4f704c5c88 upstream. Jiri Olsa reported a fault when running: # cat /proc/kallsyms | grep ksys_read ffffffff8136d580 T ksys_read # objdump -d --start-address=0xffffffff8136d580 --stop-address=0xffffffff8136d590 /proc/kcore /proc/kcore: file format elf64-x86-64 Segmentation fault general protection fault, probably for non-canonical address 0xf887ffcbff000: 0000 [#1] SMP PTI CPU: 12 PID: 1079 Comm: objdump Not tainted 5.14.0-rc5qemu+ #508 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-4.fc34 04/01/2014 RIP: 0010:kern_addr_valid Call Trace: read_kcore ? rcu_read_lock_sched_held ? rcu_read_lock_sched_held ? rcu_read_lock_sched_held ? trace_hardirqs_on ? rcu_read_lock_sched_held ? lock_acquire ? lock_acquire ? rcu_read_lock_sched_held ? lock_acquire ? rcu_read_lock_sched_held ? rcu_read_lock_sched_held ? rcu_read_lock_sched_held ? lock_release ? _raw_spin_unlock ? __handle_mm_fault ? rcu_read_lock_sched_held ? lock_acquire ? rcu_read_lock_sched_held ? lock_release proc_reg_read ? vfs_read vfs_read ksys_read do_syscall_64 entry_SYSCALL_64_after_hwframe The fault happens because kern_addr_valid() dereferences existent but not present PMD in the high kernel mappings. Such PMDs are created when free_kernel_image_pages() frees regions larger than 2Mb. In this case, a part of the freed memory is mapped with PMDs and the set_memory_np_noalias() -> ... -> __change_page_attr() sequence will mark the PMD as not present rather than wipe it completely. Have kern_addr_valid() check whether higher level page table entries are present before trying to dereference them to fix this issue and to avoid similar issues in the future. Stable backporting note: ------------------------ Note that the stable marking is for all active stable branches because there could be cases where pagetable entries exist but are not valid - see 9a14aefc1d28 ("x86: cpa, fix lookup_address"), for example. So make sure to be on the safe side here and use pXY_present() accessors rather than pXY_none() which could #GP when accessing pages in the direct map. Also see: c40a56a7818c ("x86/mm/init: Remove freed kernel image areas from alias mapping") for more info. Reported-by: Jiri Olsa Signed-off-by: Mike Rapoport Signed-off-by: Borislav Petkov Reviewed-by: David Hildenbrand Acked-by: Dave Hansen Tested-by: Jiri Olsa Cc: # 4.4+ Link: https://lkml.kernel.org/r/20210819132717.19358-1-rppt@kernel.org Signed-off-by: Greg Kroah-Hartman --- arch/x86/mm/init_64.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1182,21 +1182,21 @@ int kern_addr_valid(unsigned long addr) return 0; pud = pud_offset(pgd, addr); - if (pud_none(*pud)) + if (!pud_present(*pud)) return 0; if (pud_large(*pud)) return pfn_valid(pud_pfn(*pud)); pmd = pmd_offset(pud, addr); - if (pmd_none(*pmd)) + if (!pmd_present(*pmd)) return 0; if (pmd_large(*pmd)) return pfn_valid(pmd_pfn(*pmd)); pte = pte_offset_kernel(pmd, addr); - if (pte_none(*pte)) + if (!pte_present(*pte)) return 0; return pfn_valid(pte_pfn(*pte));