Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2197518iof; Tue, 7 Jun 2022 22:56:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqImFzjONmfDPocuzv9p5sK+DhSCTLGokR0n5/NxN5jMxnz4XLLdUuPf6pKDyyEApdmKPs X-Received: by 2002:a65:6b8a:0:b0:3db:7dc5:fec2 with SMTP id d10-20020a656b8a000000b003db7dc5fec2mr27601314pgw.223.1654667804104; Tue, 07 Jun 2022 22:56:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667804; cv=none; d=google.com; s=arc-20160816; b=LgMmie79Hk8i+du4jqfGgRxedESaXXfwXJh0y+rclrh+kxz0qts28Yqd4LmDFZQnhx 2gC/qr3qrT0bifs9XD4cU+MtWEdyRUTikGj5pY8310Kx6ahwHfh49LEWC+uqj+k11Av9 /gbLNB9nE8KeBEArDux3JCeAUGwWUYWfDU6IazZ8pSiwe1NxBFenk4p7sS1ugTm7ZM2i R8+Ia9uEf1pKm/EbugkZy3qQI5LtADHBowoeHM4KEwE3jDwAD8AMZeH/gZNKnJJTw2SA pesgSCQ+x451jfbR9uJoOk1JY2dbyH4v8qE24NmtLh9elPGLXgs35ZEtrgTfaZa9kiEn EOjA== 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=HRMFB9peCfXGjZ/mFdZLIY3yn+BRQnjSuP3aQr6bPYU=; b=LKu1FFD4VOHNdpS0IBK2CqUtfAYvjotJ4IHf0j9lBRgWJ4H+rcKifsS3W/104i8AJy pe5FPBTXxVrb6+6Gq8MkvuNu5n7t6N6SwbDCXNTyNumCX2I9LVassiAjc2wwzylcWekY wVkpycORzjcfjBUqBCy8KDQxP69hi2G7KMFYEh8iM6Rbkn3RG0X7EcZs50aNlv1ETGQX fyXBRxERw1e/4uxFgo3CU0SYV6GW2v81rZaLfZSjddcmeY68usz8nmAsUccK4mzQZrt9 t+GICgjfL3IEFhCngZ//PwFzm4xodiUiKsfuLxWV1qljdTW6gEzgOOQNPtrXT5GGN4JD 2EkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=is3+rOrg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id i2-20020a17090aee8200b001df4ea7a026si28972105pjz.34.2022.06.07.22.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:56:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=is3+rOrg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9C34824EC71; Tue, 7 Jun 2022 22:22:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444284AbiFHBBi (ORCPT + 99 others); Tue, 7 Jun 2022 21:01:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382567AbiFGWCm (ORCPT ); Tue, 7 Jun 2022 18:02:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A04C24F952; Tue, 7 Jun 2022 12:14:45 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 8902F6192A; Tue, 7 Jun 2022 19:14:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94DB7C385A5; Tue, 7 Jun 2022 19:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629283; bh=Pm1sdPAeGW7S0pQyTrq8Rdh0/AYBc4BFWlu2G/4YV0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=is3+rOrgR588nGUdgXX1fPSRLixvc9BCELoafz9XQMIJi79UvvifGiZC4Nb8BBtBt 2xa/avQmF42blcCOyVpi94Mq2Rbp/aq4YsABqxqmKxLCqGNAUijodLzkDMZex7Zcrd nkJTb+GVtZF9Hg6yCQMsfSZ3PaztRfnT6+3+NIzE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jacky Li , Peter Gonda , Tom Lendacky , Herbert Xu , Sasha Levin Subject: [PATCH 5.18 594/879] crypto: ccp - Fix the INIT_EX data file open failure Date: Tue, 7 Jun 2022 19:01:52 +0200 Message-Id: <20220607165020.094510951@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Jacky Li [ Upstream commit 05def5cacfa0bd5ba380116046747da07ff5bd78 ] There are 2 common cases when INIT_EX data file might not be opened successfully and fail the sev initialization: 1. In user namespaces, normal user tasks (e.g. VMM) can change their current->fs->root to point to arbitrary directories. While init_ex_path is provided as a module param related to root file system. Solution: use the root directory of init_task to avoid accessing the wrong file. 2. Normal user tasks (e.g. VMM) don't have the privilege to access the INIT_EX data file. Solution: open the file as root and restore permissions immediately. Fixes: 3d725965f836 ("crypto: ccp - Add SEV_INIT_EX support") Signed-off-by: Jacky Li Reviewed-by: Peter Gonda Acked-by: Tom Lendacky Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- drivers/crypto/ccp/sev-dev.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 6ab93dfd478a..3aefb177715e 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -170,6 +171,31 @@ static void *sev_fw_alloc(unsigned long len) return page_address(page); } +static struct file *open_file_as_root(const char *filename, int flags, umode_t mode) +{ + struct file *fp; + struct path root; + struct cred *cred; + const struct cred *old_cred; + + task_lock(&init_task); + get_fs_root(init_task.fs, &root); + task_unlock(&init_task); + + cred = prepare_creds(); + if (!cred) + return ERR_PTR(-ENOMEM); + cred->fsuid = GLOBAL_ROOT_UID; + old_cred = override_creds(cred); + + fp = file_open_root(&root, filename, flags, mode); + path_put(&root); + + revert_creds(old_cred); + + return fp; +} + static int sev_read_init_ex_file(void) { struct sev_device *sev = psp_master->sev_data; @@ -181,7 +207,7 @@ static int sev_read_init_ex_file(void) if (!sev_init_ex_buffer) return -EOPNOTSUPP; - fp = filp_open(init_ex_path, O_RDONLY, 0); + fp = open_file_as_root(init_ex_path, O_RDONLY, 0); if (IS_ERR(fp)) { int ret = PTR_ERR(fp); @@ -217,7 +243,7 @@ static void sev_write_init_ex_file(void) if (!sev_init_ex_buffer) return; - fp = filp_open(init_ex_path, O_CREAT | O_WRONLY, 0600); + fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600); if (IS_ERR(fp)) { dev_err(sev->dev, "SEV: could not open file for write, error %ld\n", -- 2.35.1