From mboxrd@z Thu Jan 1 00:00:00 1970 Date: Tue, 19 Jun 2018 16:20:35 +0100 From: Will Deacon Subject: Re: [PATCH] arm64: mm: mark tramp_pg_dir read-only Message-ID: <20180619152035.GM13984@arm.com> References: <20180530044806.18449-1-yaojun8558363@gmail.com> <20180530091457.GB2452@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: To: Ard Biesheuvel Cc: YaoJun , Kernel Hardening , Catalin Marinas , linux-arm-kernel , Linux Kernel Mailing List List-ID: Hi Ard, Sorry, I forgot to reply to this. On Wed, May 30, 2018 at 11:53:20AM +0200, Ard Biesheuvel wrote: > On 30 May 2018 at 11:14, Will Deacon wrote: > > On Wed, May 30, 2018 at 12:48:06PM +0800, YaoJun wrote: > >> To protect against KSMA(Kernel Space Mirroring Attack), make > >> tramp_pg_dir read-only. The principle of KSMA is to insert a > >> carefully constructed PGD entry into the translation table. > >> The type of this entry is block, which maps the kernel text > >> and its access permissions bits are 01. The user process can > >> then modify kernel text directly through this mapping. In this > >> way, an arbitrary write can be converted to multiple arbitrary > >> writes. > >> > >> Signed-off-by: YaoJun > >> --- > >> arch/arm64/mm/mmu.c | 4 ++++ > >> 1 file changed, 4 insertions(+) > >> > >> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c > >> index 2dbb2c9f1ec1..ac4b22c7e435 100644 > >> --- a/arch/arm64/mm/mmu.c > >> +++ b/arch/arm64/mm/mmu.c > >> @@ -551,6 +551,10 @@ static int __init map_entry_trampoline(void) > >> __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE, > >> prot, pgd_pgtable_alloc, 0); > >> > >> + update_mapping_prot(__pa_symbol(tramp_pg_dir), > >> + (unsigned long)tramp_pg_dir, > >> + PGD_SIZE, PAGE_KERNEL_RO); > > > > Hmm, I like the idea but is there a risk that the page table has been mapped > > as part of a block entry, which we can't safely split at this point (i.e. > > we'll run into one of the BUG_ONs in the mapping code)? > > > > We'd need to create a separate segment for it initially so the mapping > is already at the right granularity. Why do you think that's the case? I can't see anything that guarantees this for the page table itself. Will From mboxrd@z Thu Jan 1 00:00:00 1970 From: will.deacon@arm.com (Will Deacon) Date: Tue, 19 Jun 2018 16:20:35 +0100 Subject: [PATCH] arm64: mm: mark tramp_pg_dir read-only In-Reply-To: References: <20180530044806.18449-1-yaojun8558363@gmail.com> <20180530091457.GB2452@arm.com> Message-ID: <20180619152035.GM13984@arm.com> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org Hi Ard, Sorry, I forgot to reply to this. On Wed, May 30, 2018 at 11:53:20AM +0200, Ard Biesheuvel wrote: > On 30 May 2018 at 11:14, Will Deacon wrote: > > On Wed, May 30, 2018 at 12:48:06PM +0800, YaoJun wrote: > >> To protect against KSMA(Kernel Space Mirroring Attack), make > >> tramp_pg_dir read-only. The principle of KSMA is to insert a > >> carefully constructed PGD entry into the translation table. > >> The type of this entry is block, which maps the kernel text > >> and its access permissions bits are 01. The user process can > >> then modify kernel text directly through this mapping. In this > >> way, an arbitrary write can be converted to multiple arbitrary > >> writes. > >> > >> Signed-off-by: YaoJun > >> --- > >> arch/arm64/mm/mmu.c | 4 ++++ > >> 1 file changed, 4 insertions(+) > >> > >> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c > >> index 2dbb2c9f1ec1..ac4b22c7e435 100644 > >> --- a/arch/arm64/mm/mmu.c > >> +++ b/arch/arm64/mm/mmu.c > >> @@ -551,6 +551,10 @@ static int __init map_entry_trampoline(void) > >> __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE, > >> prot, pgd_pgtable_alloc, 0); > >> > >> + update_mapping_prot(__pa_symbol(tramp_pg_dir), > >> + (unsigned long)tramp_pg_dir, > >> + PGD_SIZE, PAGE_KERNEL_RO); > > > > Hmm, I like the idea but is there a risk that the page table has been mapped > > as part of a block entry, which we can't safely split at this point (i.e. > > we'll run into one of the BUG_ONs in the mapping code)? > > > > We'd need to create a separate segment for it initially so the mapping > is already at the right granularity. Why do you think that's the case? I can't see anything that guarantees this for the page table itself. Will