Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp185325iob; Mon, 2 May 2022 16:35:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwWTnEGMvrYC6NK5yWDj+GRhbzZvvcs/cm0ykltrkmpt8zc64HIY3K9dylo1fwbDBgeo30x X-Received: by 2002:a63:83c2:0:b0:3c2:1c58:5779 with SMTP id h185-20020a6383c2000000b003c21c585779mr5756821pge.287.1651534503260; Mon, 02 May 2022 16:35:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651534503; cv=none; d=google.com; s=arc-20160816; b=fjbykgUc7bx7ef+RPSle75QwU923GlJagA7ItVIRUsJPzDNBH9eSZWlRs9IcTEt/qB l4enFJAHGKrxL1tm8oZe2o4jdMxxY+UAmC7ho5W/dzkjor/tBVY15iphCwGKzOxvKjzs Be9hFYDrX0sxEq8+HoH9QKAMWghjodY6w+qGvW9nKgzR0u2hLAATfT6WpmgjiM91rdmK qCZANluF8VfaLKK4QC47kdKAngpO5WFIU1O2KefrBoJYY61fixXSHmMiErTR8UfXjxE2 zmIt7ZxWD+aOVmRjYLPeSTUCPcPKu7+y7/bWaYoTmiOcLLiIra4kjVrLIucviTMVKcP7 k3Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=cIjpgsiLsw3XcYdc/Hr1MoGaRlP4zUefffh+YhKfGsI=; b=bKVDGdeBxWB12s0+AXnWIZi5QCq7rUvv3NZmcNOmYkchdDDqdvw1fjnlWDDyMbsFCW NAHGiQfVg8fpkrf/srSPy4B1zhCjgG+JgMH8uxdG9psmeAEPMVaGPoQbNPS6fDz7tFQx 7GQl5Nubw0PQ9GMBNI+9W0NnsILW6VWoqQSFVf74/G1hE1Ax9Evb/gCLh9vpmWlDALti 4uSDh2/n7aM0ojr/JKsYs2XHThzn0G0G6C9HQIrjwoH6Ug2ScGPgLyc/MhivL2g+5fVF h4YZGccHMDoU4NGsY4bLejmAvCuEsICl6VU6BuLMOVa5yUBGkIrRhAi46+J3kk7TDppT QS4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J8UU11ZJ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y14-20020a17090322ce00b00158bfb59d03si5516265plg.382.2022.05.02.16.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 16:35:03 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J8UU11ZJ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8482131DF7; Mon, 2 May 2022 16:34:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350665AbiD1SCO (ORCPT + 99 others); Thu, 28 Apr 2022 14:02:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345831AbiD1SCM (ORCPT ); Thu, 28 Apr 2022 14:02:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BAB9EBB088 for ; Thu, 28 Apr 2022 10:58:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651168736; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=cIjpgsiLsw3XcYdc/Hr1MoGaRlP4zUefffh+YhKfGsI=; b=J8UU11ZJokZAullUlrMg+p8n2ZOMjTpmGHcXA/ShfBT9wyn+ZTR6ydgrUwf/eBszbXavip 2ZRn2KAZkXEZbmzyeaxx/WrzKbgjSysTqNnZoyrDTH45y+gm0vmPThO+X+dTMxwY6B91X8 QxjAivVzIIt/V5IJ216BfdyxsXRDPKo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-468-ZiLh68HvNo-ThRqG6eU8rQ-1; Thu, 28 Apr 2022 13:58:46 -0400 X-MC-Unique: ZiLh68HvNo-ThRqG6eU8rQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 88B263C0E182; Thu, 28 Apr 2022 17:58:45 +0000 (UTC) Received: from fedora (unknown [10.22.33.56]) by smtp.corp.redhat.com (Postfix) with SMTP id 26BA2574B5B; Thu, 28 Apr 2022 17:58:43 +0000 (UTC) Date: Thu, 28 Apr 2022 14:58:42 -0300 From: Wander Lairson Costa To: Kuppuswamy Sathyanarayanan Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , "Kirill A . Shutemov" , Tony Luck , Andi Kleen , Kai Huang , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 3/3] x86/tdx: Add Quote generation support Message-ID: References: <20220422233418.1203092-1-sathyanarayanan.kuppuswamy@linux.intel.com> <20220422233418.1203092-4-sathyanarayanan.kuppuswamy@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220422233418.1203092-4-sathyanarayanan.kuppuswamy@linux.intel.com> X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Spam-Status: No, score=-2.6 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 On Fri, Apr 22, 2022 at 04:34:18PM -0700, Kuppuswamy Sathyanarayanan wrote: [snip] > +static long tdx_get_tdquote(void __user *argp) > +{ > + struct tdx_quote_hdr *quote_hdr; > + struct tdx_quote_req quote_req; > + void *quote_buf = NULL; > + dma_addr_t handle; > + long ret = 0, err; > + u64 quote_buf_len; > + > + mutex_lock("e_lock); > + > + reinit_completion(&req_compl); > + > + /* Copy Quote request struct from user buffer */ > + if (copy_from_user("e_req, argp, sizeof(struct tdx_quote_req))) > + return -EFAULT; > + > + /* Make sure the length & timeout is valid */ > + if (quote_req.len <= 0 || quote_req.timeout <= 0) len and timeout are unsigned values, so they will never be negative. > + return -EINVAL; > + > + /* Align with page size to meet 4K alignment */ > + quote_buf_len = PAGE_ALIGN(quote_req.len); > + > + /* > + * Allocate DMA buffer to get TDQUOTE data from the VMM. > + * dma_alloc_coherent() API internally marks allocated > + * memory as shared with VMM. So explicit shared mapping is > + * not required. > + */ > + quote_buf = dma_alloc_coherent(&pdev->dev, quote_buf_len, &handle, > + GFP_KERNEL | __GFP_ZERO); > + if (!quote_buf) { > + ret = -ENOMEM; > + goto quote_failed; > + } > + > + /* Copy TDREPORT from user Quote data buffer to kernel Quote buffer */ > + if (copy_from_user(quote_buf, (void __user *)quote_req.buf, quote_req.len)) { > + ret = -EFAULT; > + goto quote_failed; > + } > + > + /* Submit GetQuote Request */ > + err = tdx_hcall_get_quote(quote_buf, quote_buf_len); > + if (err) { > + /* if failed, copy hypercall error code to user buffer */ > + ret = put_user(err, (long __user *)argp); The assigment to ret is unused. > + ret = -EIO; > + goto quote_failed; > + } > + > + /* Wait for attestation completion */ > + ret = wait_for_completion_interruptible_timeout( > + &req_compl, > + msecs_to_jiffies(quote_req.timeout)); > + if (ret <= 0) { > + ret = -EIO; > + goto quote_failed; > + } > + > + /* Copy generated Quote data back to user buffer */ > + if (copy_to_user((void __user *)quote_req.buf, quote_buf, quote_buf_len)) { Shouldn't we use quote_req.len instead of quote_buf_len here? > + ret = -EFAULT; > + goto quote_failed; > + } > + > + quote_hdr = (struct tdx_quote_hdr *)quote_buf; > + > + /* Make sure quote generation is successful */ > + if (!quote_hdr->status) > + ret = 0; > + else > + ret = -EIO; > + Shouldn't copy_to_user be called after checking the status? > +quote_failed: > + if (quote_buf) > + dma_free_coherent(&pdev->dev, quote_buf_len, quote_buf, handle); > + > + mutex_unlock("e_lock); > + > + return ret; > +} > + > +static void attestation_callback_handler(void) > +{ > + complete(&req_compl); > +} > + > static long tdx_attest_ioctl(struct file *file, unsigned int cmd, > unsigned long arg) > { > @@ -89,6 +183,9 @@ static long tdx_attest_ioctl(struct file *file, unsigned int cmd, > case TDX_CMD_GET_TDREPORT: > ret = tdx_get_tdreport(argp); > break; > + case TDX_CMD_GEN_QUOTE: > + ret = tdx_get_tdquote(argp); > + break; > default: > pr_err("cmd %d not supported\n", cmd); > break; > @@ -103,6 +200,14 @@ static const struct file_operations tdx_attest_fops = { > .llseek = no_llseek, > }; > > +/* Helper function to cleanup attestation related allocations */ > +static void _tdx_attest_remove(void) > +{ > + misc_deregister(&miscdev); Won't misc_deregister be called even if misc_register fails? > + > + tdx_remove_ev_notify_handler(); > +} > +