Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp27607imm; Fri, 25 May 2018 15:20:02 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo/uqoJQcvaqaF9mBwV0dNi77MY6EoN1WIaNcGCFtd5OITPtYRxwoOPaKt3N+bHGyVbiBDg X-Received: by 2002:a62:ce4e:: with SMTP id y75-v6mr4176141pfg.175.1527286802134; Fri, 25 May 2018 15:20:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527286802; cv=none; d=google.com; s=arc-20160816; b=n0YWr9dikFWLtxfI79ltSjuf06IMWtts7P0VU1BwN5K5bHiGlyKlFhxMboEyTdRrem t2AJrPNPk2b8D43d0L2H1FOy8gv4Yx+3MQIUS8RW1p49baqhXwDc6MQUYd8wBgYNBb5B 8cycxWwlANhLpGyqBD0bdIQct6bXp6vLuJ4depa5KpPrkZJet1ZFeIMWJe2DEN6BPtUd o+wATDtrtbNKIY8FzdeEMu9dlB6d8346DWKwhOBtjKyLyOoavN/Zv1hc8694hJ3vq8nB NVn9QTgreqPzHy2SZzlgqhaXVhPkXwIKDZOt3bBlf8IChzg3BbYMa29bjSxQNH+KI/Xg KJ9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=2gG2YxlxdDzFnOhsrHNw/Y/Lv87Vr0/istGk7Sv4aWQ=; b=QC9AoOnmtG+G7A+k8U/pC9YuQ5zfkt1htrtH025BD7Xak7WRJgVV/TCNenTokmToun LWqBFCjc6cnMfD9kIujO8ery8QeI09lRWJyZA30Zk/bAuW6itq+3CmFxnV79uugmnVT9 Rviia4FLMfXckOnw12Fz8EmQ5jkcs1LQxza4XqWERy2KmkE8LfNN2cUBuMsX3Tufk4gi 8o5W1o7h6blR8pR+Wgy2qUmw9nWeMVTvkgoQrfBTDeyb9Ldhe2mZwwbhK+2+zuCkfdIK djHwfi26VEKEY3oquEx+IYDF+VX3TEjWeAaYUYuovg37j/UDslrDtQVvuvpoJpiNOBK4 xNBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CzAOcxWb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a17-v6si19447906pgf.15.2018.05.25.15.19.47; Fri, 25 May 2018 15:20:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CzAOcxWb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030639AbeEYWTi (ORCPT + 99 others); Fri, 25 May 2018 18:19:38 -0400 Received: from mail-it0-f65.google.com ([209.85.214.65]:39686 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030599AbeEYWTg (ORCPT ); Fri, 25 May 2018 18:19:36 -0400 Received: by mail-it0-f65.google.com with SMTP id c3-v6so8621165itj.4 for ; Fri, 25 May 2018 15:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=2gG2YxlxdDzFnOhsrHNw/Y/Lv87Vr0/istGk7Sv4aWQ=; b=CzAOcxWbyN4qnXB6jAJ3D+qdM75/qQwes+6q07TnUat11CC8hQTEiv7tILslogX37o oZ3T/+7qVsMmJGqhmqwVWzy2fYMveBa6tIgvPmP6FlGLKBMc8H6MGCIUrrgtHtw2mvkN YJYtf1FMjgYvtrsCPoyKAHYs9uxAK0x/avwf8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=2gG2YxlxdDzFnOhsrHNw/Y/Lv87Vr0/istGk7Sv4aWQ=; b=N0XWQakWqhbQ9ft1LhlOd0dvr8bTYCqZ3MD/3IyXXmrjjWXHljthcbLkA8EHPdltpL ftIwFL4vGUZqCNcTZa3O2bqKW4/9JWFYtbPQdcsKxM8puqpYE2j194ZZJfIV4VLcyobl EuoIDkYSlRA+lsboLMOExKJ/74GrSUH8uGsa8TX/3MaIfTl3LBMd8wh0/BdMyRTMFZGB /JS+hia3Z89wG9O8rupiA7Ba+T79ijgSFNHSm2Xy1GUNcgpS01T+eL+u3BsnKEnYHq0A X958R9u8mfogW6HH5SPbtvz/fToataD5UNaZi1mck1hStVhvVh9Mgf4hMu3t0UY+12fr mbDw== X-Gm-Message-State: ALKqPwc5KF79PXLF95w6gi7VCVrBDClPueEr5p4sg8Wwt5cST12Vx57M QBbDDccV2BvGiICOtVV3MLa4klfdLfsAYDRyrnKjCA== X-Received: by 2002:a24:af45:: with SMTP id l5-v6mr3792121iti.106.1527286775294; Fri, 25 May 2018 15:19:35 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a6b:bb86:0:0:0:0:0 with HTTP; Fri, 25 May 2018 15:19:34 -0700 (PDT) In-Reply-To: <1527285903-31799-1-git-send-email-sai.praneeth.prakhya@intel.com> References: <1527285903-31799-1-git-send-email-sai.praneeth.prakhya@intel.com> From: Ard Biesheuvel Date: Sat, 26 May 2018 00:19:34 +0200 Message-ID: Subject: Re: [PATCH V4 0/3] Use efi_rts_wq to invoke EFI Runtime Services To: Sai Praneeth Prakhya Cc: linux-efi@vger.kernel.org, Linux Kernel Mailing List , Lee Chun-Yi , Borislav Petkov , Tony Luck , Will Deacon , Dave Hansen , Mark Rutland , Bhupesh Sharma , Naresh Bhat , Ricardo Neri , Peter Zijlstra , Ravi Shankar , Matt Fleming , Dan Williams , Miguel Ojeda Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26 May 2018 at 00:05, Sai Praneeth Prakhya wrote: > From: Sai Praneeth > > Problem statement: > ------------------ > Presently, efi_runtime_services() silently switch %cr3 from swapper_pgd > to efi_pgd. As a consequence, kernel code that runs in efi_pgd (e.g., > perf code via an NMI) will have incorrect user space mappings[1]. This > could lead to otherwise unexpected access errors and, worse, unauthorized > access to firmware code and data. > > Detailed discussion of problem statement: > ----------------------------------------- > As this switch is not propagated to other kernel subsystems; they will > wrongly assume that swapper_pgd is still in use and it can lead to > following issues: > > 1. If kernel code tries to access user space addresses while in efi_pgd, > it could lead to unauthorized accesses to firmware code/data. > (e.g: <__>/copy_from_user_nmi()). > [This could also be disastrous if the frame pointer happens to point at > MMIO in the EFI runtime mappings] - Mark Rutland. > > An example of a subsystem that could touch user space while in efi_pgd is > perf. Assume that we are in efi_pgd, a user could use perf to profile > some user data and depending on the address the user is trying to > profile, two things could happen. > 1. If the mappings are absent, perf fails to profile. > 2. If efi_pgd does have mappings for the requested address (these > mappings are erroneous), perf profiles firmware code/data. If the > address is MMIO'ed, perf could have potentially changed some device state. > > The culprit in both the cases is, EFI subsystem swapping out pgd and not > perf. Because, EFI subsystem has broken the *general assumption* that > all other subsystems rely on - "user space might be valid and nobody has > switched %cr3". > > Solutions: > ---------- > There are two ways to fix this issue: > 1. Educate about pgd change to *all* the subsystems that could > potentially access user space while in efi_pgd. > On x86, AFAIK, it could happen only when some one touches user space > from the back of an NMI (a quick audit on <__>/copy_from_user_nmi, > showed perf and oprofile). On arm, it could happen from multiple > places as arm runs efi_runtime_services() interrupts enabled (ARM folks, > please comment on this as I might be wrong); whereas x86 runs > efi_runtime_services() interrupts disabled. > > I think, this solution isn't holistic because > a. Any other subsystem might well do the same, if not now, in future. > b. Also, this solution looks simpler on x86 but not true if it's the > same for arm (ARM folks, please comment on this as I might be wrong). > c. This solution looks like a work around rather than addressing the issue. > > 2. Running efi_runtime_services() in kthread context. > This makes sense because efi_pgd doesn't have user space and kthread > by definition means that user space is not valid. Any kernel code that > tries to touch user space while in kthread is buggy in itself. If so, > it should be an easy fix in the other subsystem. This also take us one > step closer to long awaiting proposal of Andy - Running EFI at CPL 3. > > What does this patch set do? > ---------------------------- > Introduce efi_rts_wq (EFI runtime services work queue). > When a user process requests the kernel to execute any efi_runtime_service(), > kernel queues the work to efi_rts_wq, a kthread comes along, switches to > efi_pgd and executes efi_runtime_service() in kthread context. IOW, this > patch set adds support to the EFI subsystem to handle all calls to > efi_runtime_services() using a work queue (which in turn uses kthread). > > How running efi_runtime_services() in kthread fixes above discussed issues? > --------------------------------------------------------------------------- > If we run efi_runtime_services() in kthread context and if perf > checks for it, we could get both the above scenarios correct by perf > aborting the profiling. Not only perf, but any subsystem that tries to > touch user space should first check for kthread context and if so, > should abort. > > Q. If we still need check for kthread context in other subsystems that > access user space, what does this patch set fix? > A. This patch set makes sure that EFI subsystem is not at fault. > Without this patch set the blame is upon EFI subsystem, because it's the > one that changed pgd and hasn't communicated this change to everyone and > hence broke the general assumption. Running efi_runtime_services() in > kthread means explicitly communicating that user space is invalid, now > it's the responsibility of other subsystem to make sure that it's > running in right context. > > Testing: > -------- > Tested using LUV (Linux UEFI Validation) for x86_64, x86_32 and arm64 > (qemu only). Will appreciate the effort if someone could test the > patches on real ARM/ARM64 machines. > LUV: https://01.org/linux-uefi-validation > > Credits: > -------- > Thanks to Ricardo, Dan, Miguel, Mark and Peter for reviews and suggestions. > Thanks to Boris and Andy for making me think through/help on what I am > addressing with this patch set. > > Please feel free to pour in your comments and concerns. > > Note: > ----- > Patches are based on Linus's kernel v4.17-rc6 > > [1] Backup: Detailing efi_pgd: > ------------------------------ > efi_pgd has mappings for EFI Runtime Code/Data (on x86, plus EFI Boot time > Code/Data) regions. Due to the nature of these mappings, they fall > in user space address ranges and they are not the same as swapper. > > [On arm64, the EFI mappings are in the VA range usually used for user > space. The two halves of the address space are managed by separate > tables, TTBR0 and TTBR1. We always map the kernel in TTBR1, and we map > user space or EFI runtime mappings in TTBR0.] - Mark Rutland > > Changes from V3 to V4: > ---------------------- > 1. As suggested by Peter, use completions instead of flush_work() as the > former is cheaper > 2. Call efi_delete_dummy_variable() from efisubsys_init(). Sorry! Ard, > wasn't able to find a better alternative to keep this change local to > arch/x86. > Two questions: - Should the non-blocking variants of the query and set_variable_store use the work queue? Doesn't that make them blocking? - If the non-blocking set_variable() does not use the work queue, can we just call it from efi_delete_dummy_variable(), and keep the calls where they are? > Changes from V2 to V3: > ---------------------- > 1. Rewrite the cover letter to clearly state the problem. What we are > fixing and what we are not fixing. > 2. Make efi_delete_dummy_variable() change local to x86. > 3. Avoid using BUG(), instead, print error message and exit gracefully. > 4. Move struct efi_runtime_work to runtime-wrappers.c file. > 5. Give enum a name (efi_rts_ids) and use it in efi_runtime_work. > 6. Add Naresh (maintainer of LUV for ARM) and Miguel to the CC list. > > Changes from V1 to V2: > ---------------------- > 1. Remove unnecessary include of asm/efi.h file - Fixes build error on > ia64, reported by 0-day > 2. Use enum to identify efi_runtime_services() > 3. Use alloc_ordered_workqueue() to create efi_rts_wq as > create_workqueue() is scheduled for depreciation. > 4. Make efi_call_rts() static, as it has no callers outside > runtime-wrappers.c > 5. Use BUG(), when we are unable to queue work or unable to identify > requested efi_runtime_service() - Because these two situations should > *never* happen. > > Sai Praneeth (3): > x86/efi: Call efi_delete_dummy_variable() during efi subsystem > initialization > efi: Create efi_rts_wq and efi_queue_work() to invoke all > efi_runtime_services() > efi: Use efi_rts_wq to invoke EFI Runtime Services > > arch/x86/include/asm/efi.h | 1 - > arch/x86/platform/efi/efi.c | 6 - > drivers/firmware/efi/efi.c | 20 +++ > drivers/firmware/efi/runtime-wrappers.c | 256 +++++++++++++++++++++++++++++--- > include/linux/efi.h | 6 + > 5 files changed, 262 insertions(+), 27 deletions(-) > > Signed-off-by: Sai Praneeth Prakhya > Suggested-by: Andy Lutomirski > Cc: Lee Chun-Yi > Cc: Borislav Petkov > Cc: Tony Luck > Cc: Will Deacon > Cc: Dave Hansen > Cc: Mark Rutland > Cc: Bhupesh Sharma > Cc: Naresh Bhat > Cc: Ricardo Neri > Cc: Peter Zijlstra > Cc: Ravi Shankar > Cc: Matt Fleming > Cc: Dan Williams > Cc: Ard Biesheuvel > Cc: Miguel Ojeda > > -- > 2.7.4 >