Skip to content

Commit a0e6e08

Browse files
kirylKAGA-KOKO
authored andcommitted
x86/ldt: Unmap PTEs for the slot before freeing LDT pages
modify_ldt(2) leaves the old LDT mapped after switching over to the new one. The old LDT gets freed and the pages can be re-used. Leaving the mapping in place can have security implications. The mapping is present in the userspace page tables and Meltdown-like attacks can read these freed and possibly reused pages. It's relatively simple to fix: unmap the old LDT and flush TLB before freeing the old LDT memory. This further allows to avoid flushing the TLB in map_ldt_struct() as the slot is unmapped and flushed by unmap_ldt_struct() or has never been mapped at all. [ tglx: Massaged changelog and removed the needless line breaks ] Fixes: f55f050 ("x86/pti: Put the LDT in its own PGD if PTI is on") Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: bp@alien8.de Cc: hpa@zytor.com Cc: dave.hansen@linux.intel.com Cc: luto@kernel.org Cc: peterz@infradead.org Cc: boris.ostrovsky@oracle.com Cc: jgross@suse.com Cc: bhe@redhat.com Cc: willy@infradead.org Cc: linux-mm@kvack.org Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20181026122856.66224-3-kirill.shutemov@linux.intel.com
1 parent d52888a commit a0e6e08

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

arch/x86/kernel/ldt.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,15 @@ static void sanity_check_ldt_mapping(struct mm_struct *mm)
199199
/*
200200
* If PTI is enabled, this maps the LDT into the kernelmode and
201201
* usermode tables for the given mm.
202-
*
203-
* There is no corresponding unmap function. Even if the LDT is freed, we
204-
* leave the PTEs around until the slot is reused or the mm is destroyed.
205-
* This is harmless: the LDT is always in ordinary memory, and no one will
206-
* access the freed slot.
207-
*
208-
* If we wanted to unmap freed LDTs, we'd also need to do a flush to make
209-
* it useful, and the flush would slow down modify_ldt().
210202
*/
211203
static int
212204
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
213205
{
214206
unsigned long va;
215207
bool is_vmalloc;
216208
spinlock_t *ptl;
209+
int i, nr_pages;
217210
pgd_t *pgd;
218-
int i;
219211

220212
if (!static_cpu_has(X86_FEATURE_PTI))
221213
return 0;
@@ -238,7 +230,9 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
238230

239231
is_vmalloc = is_vmalloc_addr(ldt->entries);
240232

241-
for (i = 0; i * PAGE_SIZE < ldt->nr_entries * LDT_ENTRY_SIZE; i++) {
233+
nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
234+
235+
for (i = 0; i < nr_pages; i++) {
242236
unsigned long offset = i << PAGE_SHIFT;
243237
const void *src = (char *)ldt->entries + offset;
244238
unsigned long pfn;
@@ -272,20 +266,50 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
272266
/* Propagate LDT mapping to the user page-table */
273267
map_ldt_struct_to_user(mm);
274268

275-
va = (unsigned long)ldt_slot_va(slot);
276-
flush_tlb_mm_range(mm, va, va + LDT_SLOT_STRIDE, PAGE_SHIFT, false);
277-
278269
ldt->slot = slot;
279270
return 0;
280271
}
281272

273+
static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
274+
{
275+
unsigned long va;
276+
int i, nr_pages;
277+
278+
if (!ldt)
279+
return;
280+
281+
/* LDT map/unmap is only required for PTI */
282+
if (!static_cpu_has(X86_FEATURE_PTI))
283+
return;
284+
285+
nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
286+
287+
for (i = 0; i < nr_pages; i++) {
288+
unsigned long offset = i << PAGE_SHIFT;
289+
spinlock_t *ptl;
290+
pte_t *ptep;
291+
292+
va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
293+
ptep = get_locked_pte(mm, va, &ptl);
294+
pte_clear(mm, va, ptep);
295+
pte_unmap_unlock(ptep, ptl);
296+
}
297+
298+
va = (unsigned long)ldt_slot_va(ldt->slot);
299+
flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, PAGE_SHIFT, false);
300+
}
301+
282302
#else /* !CONFIG_PAGE_TABLE_ISOLATION */
283303

284304
static int
285305
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
286306
{
287307
return 0;
288308
}
309+
310+
static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
311+
{
312+
}
289313
#endif /* CONFIG_PAGE_TABLE_ISOLATION */
290314

291315
static void free_ldt_pgtables(struct mm_struct *mm)
@@ -524,6 +548,7 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
524548
}
525549

526550
install_ldt(mm, new_ldt);
551+
unmap_ldt_struct(mm, old_ldt);
527552
free_ldt_struct(old_ldt);
528553
error = 0;
529554

0 commit comments

Comments
 (0)