From patchwork Mon Nov 26 10:51:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 10697973 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6B23217D5 for ; Mon, 26 Nov 2018 11:00:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C131297FF for ; Mon, 26 Nov 2018 11:00:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F25629801; Mon, 26 Nov 2018 11:00:46 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3A91297FF for ; Mon, 26 Nov 2018 11:00:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68D5B6B41CB; Mon, 26 Nov 2018 06:00:43 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6181A6B41CE; Mon, 26 Nov 2018 06:00:43 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41B3F6B41CD; Mon, 26 Nov 2018 06:00:43 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by kanga.kvack.org (Postfix) with ESMTP id F1A476B41CB for ; Mon, 26 Nov 2018 06:00:42 -0500 (EST) Received: by mail-pg1-f200.google.com with SMTP id p4so7592474pgj.21 for ; Mon, 26 Nov 2018 03:00:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=yeV91r2Dlr4x72pQKzdxcWraAdl0Hspep5bSiU4rFJI=; b=gtrVk5R2nOdIpwRxz4grumDqoSUbKySewQQqWp9cjlavDEb02u+mGEB6c4pu69LitO WS+g11U11bUxxVXnr1ts0byMqa50G5Z9ksSVy7NXYKyh+zFRkTKUTIR9Oi2epYeLqurS 55WcQvDWgjk2+WxO6x+g/w202cGmjditMB7d+9U7+3peOwwmDbJ314TsZiDl29ANmKdF YqrkBvFRHjemOQhUlXx/6L89TklI8DwyNQyTrXpn0rY7TG8BkanLgMSjQId09SidsicI HV601u7veB7/qtsZDyfCaHRZ4IACbaMPjyV9GpL6ZeBQKQWpcIloqfRnccFqoNVkDeSQ tRgw== X-Gm-Message-State: AA+aEWbZoefQz4T4aS6TYn33tDkVtR2FWw1sMeGOdtHAyG008pM7RdIK MzPs1V4h3b7O37OidaGfBlv1vCqCm7usx0pl5h0HcTYVMG61Yu5Q4pZ0ZEtvs/SJF4DFmY3pxzc WBihX6NUX3QHGzINruvtr3pycFVPUcTiOylMxrYyO8i68hDnn0pTb7GYnxFNsvFA= X-Received: by 2002:a17:902:6f09:: with SMTP id w9mr27826484plk.309.1543230042451; Mon, 26 Nov 2018 03:00:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/X+9NdOlaTuCalCud75ELxeMeQGp9pHbLSL4WVAnFn00afGL7vXIC83AWC1xqsf8va3ltfp X-Received: by 2002:a17:902:6f09:: with SMTP id w9mr27826397plk.309.1543230041548; Mon, 26 Nov 2018 03:00:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543230041; cv=none; d=google.com; s=arc-20160816; b=Dr7Z22g09VflEEChSaDsRlJWiAgF2OH/raG7m5ANARI5RWrcYPduaH5TuxcHj3MGAl x9v/5UfO5gtR2d+NOZEEMLpeI+0R+GMljmPRMqJU1F3rg2pPBcQRQE5Rbb2mfdI+TjkO sjWPSB/wcvqkO39JJ0FYPoZ/cXzJJE6ODNXna8qKmaLKOBzPzMnc93BB/huo5cV0F0D4 QaIitRpHjAaXU7P8zkoJHd9XW4apU8HuKt/JJDbXzjvQzU19wtXIgUxrilqjtLZdft/Y 121SE0W/UdsSNA6SaVgC4c7IkzslXbwd8KmD6dKdOlCCOR8Ydj6IcwY+v7WRrcpGCgkO UQYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=yeV91r2Dlr4x72pQKzdxcWraAdl0Hspep5bSiU4rFJI=; b=mrd2AKukEqc7MQrY1MbnMm8sRIJdsyefa6ke2Iz2VWu5+HP4OaF7csCUO9Bi5P2pbK IIDII+P0zA4OMUueGk+vL9nNtBXXyuBzdhMG8hjtwB19RkPDnLsXslF2qsCu7Qt8dH6/ PZR1luRnXC3fjbNM4mf89aqGptnprL6//I0oIDulolpRbnBfrUQp8ZEDRClBYTHbXlXd mjuTib/9iTxwKsa/1yoBPHMMExWVLzaHGU94NCi5S/B8q1JUmIQtsjV4Cf1Q0o5Pwj3O 9VSSrxAwwgoa4PKsVowqP77zTHqSO+NxOo6BwS2SvEHOAxOo1Djd0h8vNT/71H5wTwbg HGDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1TDatplh; spf=pass (google.com: domain of srs0=uhmk=of=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom="SRS0=UhMK=OF=linuxfoundation.org=gregkh@kernel.org" Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id w11si7087313plz.327.2018.11.26.03.00.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Nov 2018 03:00:41 -0800 (PST) Received-SPF: pass (google.com: domain of srs0=uhmk=of=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1TDatplh; spf=pass (google.com: domain of srs0=uhmk=of=linuxfoundation.org=gregkh@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom="SRS0=UhMK=OF=linuxfoundation.org=gregkh@kernel.org" Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9830020817; Mon, 26 Nov 2018 11:00:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543230041; bh=LaPX98p5/0BH2YUXG3GhbBtnEJxJtz+LXuSWxuS5utA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1TDatplh135vEVjeNXDAlWwnvG48TTh7lpafocdWzVdqqBHSpkId2Vfh/8pwf2sbb 7VzdelQu4ggjUzPF7Ra5wPkLk50CxfnKq1jgDMyVdQzV2uvQA2GqaklXDEQiCxHu1w mAAX8j36pP5CzCGcfw2Ok0eX+YUvwqRJ1+h6kyUQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Kirill A. Shutemov" , Thomas Gleixner , bp@alien8.de, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, boris.ostrovsky@oracle.com, jgross@suse.com, bhe@redhat.com, willy@infradead.org, linux-mm@kvack.org, Sasha Levin Subject: [PATCH 4.14 49/62] x86/ldt: Unmap PTEs for the slot before freeing LDT pages Date: Mon, 26 Nov 2018 11:51:30 +0100 Message-Id: <20181126105054.440503950@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181126105050.592727680@linuxfoundation.org> References: <20181126105050.592727680@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ commit a0e6e0831c516860fc7f9be1db6c081fe902ebcf upstream 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: f55f0501cbf6 ("x86/pti: Put the LDT in its own PGD if PTI is on") Signed-off-by: Kirill A. Shutemov Signed-off-by: Thomas Gleixner 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 Signed-off-by: Sasha Levin --- arch/x86/kernel/ldt.c | 49 +++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c index 26d713ecad34..65df298d4e9e 100644 --- a/arch/x86/kernel/ldt.c +++ b/arch/x86/kernel/ldt.c @@ -103,14 +103,6 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) /* * If PTI is enabled, this maps the LDT into the kernelmode and * usermode tables for the given mm. - * - * There is no corresponding unmap function. Even if the LDT is freed, we - * leave the PTEs around until the slot is reused or the mm is destroyed. - * This is harmless: the LDT is always in ordinary memory, and no one will - * access the freed slot. - * - * If we wanted to unmap freed LDTs, we'd also need to do a flush to make - * it useful, and the flush would slow down modify_ldt(). */ static int map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) @@ -119,8 +111,8 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) bool is_vmalloc, had_top_level_entry; unsigned long va; spinlock_t *ptl; + int i, nr_pages; pgd_t *pgd; - int i; if (!static_cpu_has(X86_FEATURE_PTI)) return 0; @@ -141,7 +133,9 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) is_vmalloc = is_vmalloc_addr(ldt->entries); - for (i = 0; i * PAGE_SIZE < ldt->nr_entries * LDT_ENTRY_SIZE; i++) { + nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE); + + for (i = 0; i < nr_pages; i++) { unsigned long offset = i << PAGE_SHIFT; const void *src = (char *)ldt->entries + offset; unsigned long pfn; @@ -189,14 +183,42 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) } } - va = (unsigned long)ldt_slot_va(slot); - flush_tlb_mm_range(mm, va, va + LDT_SLOT_STRIDE, 0); - ldt->slot = slot; #endif return 0; } +static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt) +{ +#ifdef CONFIG_PAGE_TABLE_ISOLATION + unsigned long va; + int i, nr_pages; + + if (!ldt) + return; + + /* LDT map/unmap is only required for PTI */ + if (!static_cpu_has(X86_FEATURE_PTI)) + return; + + nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE); + + for (i = 0; i < nr_pages; i++) { + unsigned long offset = i << PAGE_SHIFT; + spinlock_t *ptl; + pte_t *ptep; + + va = (unsigned long)ldt_slot_va(ldt->slot) + offset; + ptep = get_locked_pte(mm, va, &ptl); + pte_clear(mm, va, ptep); + pte_unmap_unlock(ptep, ptl); + } + + va = (unsigned long)ldt_slot_va(ldt->slot); + flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, 0); +#endif /* CONFIG_PAGE_TABLE_ISOLATION */ +} + static void free_ldt_pgtables(struct mm_struct *mm) { #ifdef CONFIG_PAGE_TABLE_ISOLATION @@ -433,6 +455,7 @@ static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode) } install_ldt(mm, new_ldt); + unmap_ldt_struct(mm, old_ldt); free_ldt_struct(old_ldt); error = 0;