From patchwork Wed Jun 20 08:57:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Yao X-Patchwork-Id: 10476597 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D758604D3 for ; Wed, 20 Jun 2018 08:58:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 42E7028567 for ; Wed, 20 Jun 2018 08:58:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 372822858B; Wed, 20 Jun 2018 08:58:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 2842528567 for ; Wed, 20 Jun 2018 08:58:36 +0000 (UTC) Received: (qmail 3309 invoked by uid 550); 20 Jun 2018 08:58:33 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 3160 invoked from network); 20 Jun 2018 08:58:32 -0000 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; bh=TQMpU0Ug4DYvWW8+W0pAUfsBe+XZS6vBuJb34NysVCg=; b=IH5eozonxgGO5K+5XRQaC3IDyWMKcP74vKoNvWU+v7KjvdMFa8MOM7h20YxTiuBjEy ImCxT3f+La3kqicWVNev6YgHxlyZ5ysOeZMqu18ORiGoTYKEOUXglRVZvpwPLVDlaP0O 5Y+LV1X8Z7n42RcBFeQVqNyHwCGaR42wXAXtcKX4wmgJTaXSbFGplJMFvUHxFuO+3e5C 6HvM1zqN2AbiCp6XBw9k+fxNgw+BsRu5CwDWWY/7hwskNzM4dbVAWbfvPQ4dxoSCYlFB eEQ6Io5c3up+D0+KYCNjSg5xdbCilnqZTh6NktifjDwx+xusbfNx838OxddkoDHAyMrH RMfQ== 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; bh=TQMpU0Ug4DYvWW8+W0pAUfsBe+XZS6vBuJb34NysVCg=; b=Nye9Fov3yAllhPtruxd/QIxePiTMOMCCeSghWb+7KELa/DdI+zQLLln/v4VbEVquPA 22zq8Ou9n3bf34Gf2o0JblODZldKjPrNAIrfAPOFtp3E9qC6WevMe6XfNyZqf3nqLXzc alNHXNwAXW2c95gp7x4VKFqJqCSJdFR2nQPkk+tenRkKsOKbpowPZw7r96saHOIbxYwo 68oURWb97PBLAh5f9qq3CLKFxcrRKRySI4AM2KvD1Gv86OWDbE6Tjea6SQoYQqmOdA4G ZdgPO0mhaMSnx3P/yRGZQPajSuaT6NfpwnvEg2tB/85nYao/XeSnkdMSBA+5Ar2RoR8f zgCw== X-Gm-Message-State: APt69E22Oxa5G11MHuW6MVYYr6jS0Vz/MsR33FGld8o1duPWGxSP2qQl 5ZnYjzQQTL312JIW9thMWek= X-Google-Smtp-Source: ADUXVKL0v3RJbIGyxvfsEkVVtqLPuOoO0+19aShfUhe1WsTmtbk6j1MEdm7WIcDUZUUKmmndVt2xdA== X-Received: by 2002:a63:2c9:: with SMTP id 192-v6mr2273029pgc.354.1529485099957; Wed, 20 Jun 2018 01:58:19 -0700 (PDT) From: Jun Yao To: linux-arm-kernel@lists.infradead.org Cc: catalin.marinas@arm.com, will.deacon@arm.com, ard.biesheuvel@linaro.org, james.morse@arm.com, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH 1/1] arm64/mm: move {idmap_pg_dir, tramp_pg_dir, swapper_pg_dir} to .rodata section Date: Wed, 20 Jun 2018 16:57:55 +0800 Message-Id: <20180620085755.20045-2-yaojun8558363@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180620085755.20045-1-yaojun8558363@gmail.com> References: <20180620085755.20045-1-yaojun8558363@gmail.com> X-Virus-Scanned: ClamAV using ClamSMTP Move {idmap_pg_dir,tramp_pg_dir,swapper_pg_dir} to .rodata section. And update the swapper_pg_dir by fixmap. Signed-off-by: Jun Yao --- arch/arm64/include/asm/pgalloc.h | 19 +++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 32 ++++++++++++++++++-------------- arch/arm64/mm/mmu.c | 23 +++++++++++++++++++---- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/arch/arm64/include/asm/pgalloc.h b/arch/arm64/include/asm/pgalloc.h index 2e05bcd944c8..cc96a7e6957d 100644 --- a/arch/arm64/include/asm/pgalloc.h +++ b/arch/arm64/include/asm/pgalloc.h @@ -29,6 +29,10 @@ #define PGALLOC_GFP (GFP_KERNEL | __GFP_ZERO) #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t)) +#if CONFIG_STRICT_KERNEL_RWX +extern spinlock_t pgdir_lock; +#endif + #if CONFIG_PGTABLE_LEVELS > 2 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr) @@ -78,6 +82,21 @@ static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t pudp, pgdval_t prot) static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, pud_t *pudp) { +#if CONFIG_STRICT_KERNEL_RWX + if (mm == &init_mm) { + pgd_t *pgd; + + spin_lock(&pgdir_lock); + pgd = pgd_set_fixmap(__pa_symbol(swapper_pg_dir)); + + pgd = (pgd_t *)((unsigned long)pgd + pgdp - swapper_pg_dir); + __pgd_populate(pgdp, __pa(pudp), PUD_TYPE_TABLE); + + pgd_clear_fixmap(); + spin_unlock(&pgdir_lock); + return; + } +#endif __pgd_populate(pgdp, __pa(pudp), PUD_TYPE_TABLE); } #else diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 605d1b60469c..86532c57206a 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -216,21 +216,25 @@ SECTIONS BSS_SECTION(0, 0, 0) . = ALIGN(PAGE_SIZE); - idmap_pg_dir = .; - . += IDMAP_DIR_SIZE; -#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 - tramp_pg_dir = .; - . += PAGE_SIZE; -#endif - -#ifdef CONFIG_ARM64_SW_TTBR0_PAN - reserved_ttbr0 = .; - . += RESERVED_TTBR0_SIZE; -#endif - swapper_pg_dir = .; - . += SWAPPER_DIR_SIZE; - swapper_pg_end = .; + .rodata : { + . = ALIGN(PAGE_SIZE); + idmap_pg_dir = .; + . += IDMAP_DIR_SIZE; + + #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 + tramp_pg_dir = .; + . += PAGE_SIZE; + #endif + + #ifdef CONFIG_ARM64_SW_TTBR0_PAN + reserved_ttbr0 = .; + . += RESERVED_TTBR0_SIZE; + #endif + swapper_pg_dir = .; + . += SWAPPER_DIR_SIZE; + swapper_pg_end = .; + } __pecoff_data_size = ABSOLUTE(. - __initdata_begin); _end = .; diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 2dbb2c9f1ec1..c1aa85a6ada5 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -66,6 +66,10 @@ static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; +#ifdef CONFIG_STRICT_KERNEL_RWX +DEFINE_SPINLOCK(pgdir_lock); +#endif + pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size, pgprot_t vma_prot) { @@ -417,12 +421,22 @@ static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, void __init mark_linear_text_alias_ro(void) { + unsigned long size; + /* * Remove the write permissions from the linear alias of .text/.rodata + * + * We free some pages in .rodata at paging_init(), which generates a + * hole. And the hole splits .rodata into two pieces. */ + size = (unsigned long)swapper_pg_dir + PAGE_SIZE - (unsigned long)_text; update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text), - (unsigned long)__init_begin - (unsigned long)_text, - PAGE_KERNEL_RO); + size, PAGE_KERNEL_RO); + + size = (unsigned long)__init_begin - (unsigned long)swapper_pg_end; + update_mapping_prot(__pa_symbol(swapper_pg_end), + (unsigned long)lm_alias(swapper_pg_end), + size, PAGE_KERNEL_RO); } static void __init map_mem(pgd_t *pgdp) @@ -587,8 +601,9 @@ static void __init map_kernel(pgd_t *pgdp) */ map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0, VM_NO_GUARD); - map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, - &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD); + map_kernel_segment(pgdp, __start_rodata, __inittext_begin, + PAGE_KERNEL, &vmlinux_rodata, + NO_CONT_MAPPINGS | NO_BLOCK_MAPPINGS, VM_NO_GUARD); map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot, &vmlinux_inittext, 0, VM_NO_GUARD); map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,