Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3285737iob; Mon, 16 May 2022 18:09:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxK+gI882rb2wvg7XRf03wDDlfQ8ri29UmqaSPkBtuxPTPwMFx227h2QIt24FMvL1/p4AoY X-Received: by 2002:a50:d0d5:0:b0:42a:ab4d:5635 with SMTP id g21-20020a50d0d5000000b0042aab4d5635mr10592682edf.271.1652749767470; Mon, 16 May 2022 18:09:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652749767; cv=none; d=google.com; s=arc-20160816; b=YRvmrSnh8921HuLsMpk+mqfeiP8MCXDmA9J3gGxP4O4zFEpT5DfrK6cjWj0Q3Grk98 W+FPe+lXsMI0D7PnLD/JdAO1evLC+Txh69SaOzn8B/Ncmj74WLkVjZ68+OP81GPUDngi B/D3T+Hvh1Cd13+QPuVJ+1C5saSp5agLtym0vjUlMzCW6vd6nxdPIj1U/3WVAY0mK/mw 6vdizUjQ4rnfOJ6qKXDGaG1lhNIb2XXMzB1Db07HYRNVeQ1imme3HVcMty0vMgL8ozBf EhAN8JClIWX6gWjwogF5/5y+un7zLy/YOJoCi5IJRgmsbGVTmt/8dUSOdmhpmpqjm+Ph 2MRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qxG9FH1c9COMPksjB1EYiBfq/ym/cfnuL/4JlfpjyYc=; b=PC6DhwUJ0+xUOCR8f01GRn1zlgwh26ZXywAZ6qwfBbFKrmCckHwPb4VO0oaKDuRWU7 UnjX6TM6uXeBppgJKDn/TmGR+HVT8nEMTSCqhE78PxC799QKtWkZAayuc6fete/O8cE+ z84/d2PwMzY32td8KfwFkGqWvK2cL4bKc5rdFBL1AzdTxVKUs4hPThlkp+ivF6ZCjuPh 895fGp11YUL8fGXL3DWDD2K3UO00ADpIqW2vMzIXWtbV4b2XAQmn3k1pu8/BDWhGJOul OQpJv40c2BSq/sfpfMrtr9G8m9xoZncHTSs58jzaSVeFx3x6O3Q8uGV3P+RVuwm0k0Hb lOqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wO8UhFEt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i3-20020a170906444300b006df76385e67si920872ejp.775.2022.05.16.18.09.02; Mon, 16 May 2022 18:09:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wO8UhFEt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241783AbiEPUQ7 (ORCPT + 99 others); Mon, 16 May 2022 16:16:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348441AbiEPT6k (ORCPT ); Mon, 16 May 2022 15:58:40 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64A7343AEB; Mon, 16 May 2022 12:50:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 09665B81612; Mon, 16 May 2022 19:50:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 705A1C385AA; Mon, 16 May 2022 19:50:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652730637; bh=Vac/Rm/MDFZxlgiWjKxf+XFwWFW0KieGzwYBnwj+aXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wO8UhFEtozfu/FGe+btJz5vAsfKxAF51WelY1KewYYYTSRNk3UlHbT7uSk+PNSc9a 6OZnZNkPsBQjFz3M/NXdE+8J6wosaLpgjhrsANUFS8s67eDjWd2XRtQGQTHWnXDLE7 KRyoR2wZR8ezcenUkdprWZAZDq1H2lO0p7gBqhFU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= , Paul Moore , Luis Chamberlain Subject: [PATCH 5.15 063/102] firmware_loader: use kernel credentials when reading firmware Date: Mon, 16 May 2022 21:36:37 +0200 Message-Id: <20220516193625.805119386@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220516193623.989270214@linuxfoundation.org> References: <20220516193623.989270214@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: ThiƩbaud Weksteen commit 581dd69830341d299b0c097fc366097ab497d679 upstream. Device drivers may decide to not load firmware when probed to avoid slowing down the boot process should the firmware filesystem not be available yet. In this case, the firmware loading request may be done when a device file associated with the driver is first accessed. The credentials of the userspace process accessing the device file may be used to validate access to the firmware files requested by the driver. Ensure that the kernel assumes the responsibility of reading the firmware. This was observed on Android for a graphic driver loading their firmware when the device file (e.g. /dev/mali0) was first opened by userspace (i.e. surfaceflinger). The security context of surfaceflinger was used to validate the access to the firmware file (e.g. /vendor/firmware/mali.bin). Previously, Android configurations were not setting up the firmware_class.path command line argument and were relying on the userspace fallback mechanism. In this case, the security context of the userspace daemon (i.e. ueventd) was consistently used to read firmware files. More Android devices are now found to set firmware_class.path which gives the kernel the opportunity to read the firmware directly (via kernel_read_file_from_path_initns). In this scenario, the current process credentials were used, even if unrelated to the loading of the firmware file. Signed-off-by: ThiƩbaud Weksteen Cc: # 5.10 Reviewed-by: Paul Moore Acked-by: Luis Chamberlain Link: https://lore.kernel.org/r/20220502004952.3970800-1-tweek@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/firmware_loader/main.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -795,6 +795,8 @@ _request_firmware(const struct firmware size_t offset, u32 opt_flags) { struct firmware *fw = NULL; + struct cred *kern_cred = NULL; + const struct cred *old_cred; bool nondirect = false; int ret; @@ -811,6 +813,18 @@ _request_firmware(const struct firmware if (ret <= 0) /* error or already assigned */ goto out; + /* + * We are about to try to access the firmware file. Because we may have been + * called by a driver when serving an unrelated request from userland, we use + * the kernel credentials to read the file. + */ + kern_cred = prepare_kernel_cred(NULL); + if (!kern_cred) { + ret = -ENOMEM; + goto out; + } + old_cred = override_creds(kern_cred); + ret = fw_get_filesystem_firmware(device, fw->priv, "", NULL); /* Only full reads can support decompression, platform, and sysfs. */ @@ -836,6 +850,9 @@ _request_firmware(const struct firmware } else ret = assign_fw(fw, device); + revert_creds(old_cred); + put_cred(kern_cred); + out: if (ret < 0) { fw_abort_batch_reqs(fw);