Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp1095758rwl; Thu, 5 Jan 2023 08:36:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXuFJ348lWYL/lTLmu7WHecUI4EBo+3kKCWmTU58cM3vLkhoUaVnlSw9P2EVBX9IIsBht3vf X-Received: by 2002:a17:907:cb85:b0:7c0:f216:cc14 with SMTP id un5-20020a170907cb8500b007c0f216cc14mr45073792ejc.11.1672936583254; Thu, 05 Jan 2023 08:36:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672936583; cv=none; d=google.com; s=arc-20160816; b=O9mexTfyTIbR6Kh7DTc52pDlf2JjHmhRHwjteqaGQaYCkFagQbY5uNNa4WyO36400o XXhkoEKqehqt+zXN1hqVKDRT0Oq/Hk35SnUbubOT8yai9CSpX7JPvCAz+M7mcPD/eD1q CQGalyEANDF4ZTIwXGd5LFqauOqaOS2JZwuu4X1bOl8Mie7Jk9DgLsbMxpvUnrLGFM8V l3SYqcJU6z1Ds9hv0F15HE2UDF84YEuijZHG4SQfILQWug///SdOVDWfc6Pxd5RIi4Ea MBf2xSXuiTFYgEaHxs2547FA9u0A6Na5kVpNBaxseLxz9h5v+LUPCIp/w4K3+YGJu1m8 9crw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=XOUSBHCbt3L6baU67NYbMwQeZSqNhjqW9pLmj+l1fas=; b=T532UIb4hAIQvDcl6YwqXvueQQ+rsNfKgxJCKIyAgNMT5iYJpbZGl7NU9T1o5Jqnke Xrff4m2w1BEqbO3uhrSllYN0D58rf4V8ncJ1ZqyMJkB6aRDa7AhSjpSL/OYWq9Rxkn3R ijJSwXrB6Ahp/Da87TorRUIETieTN6Ehj2+yb8lTHz1/CQ/oCvCoZ/4QtbkPe4bzvJ4n GfzVQ7KB8Xumf3pKDpSUJqJZGRzom0beW5cpvuKda4hV6Pz5AREOWaJN6M4wKb/qPtyF r4EmHBxME/AOEOPLbd/VODbGfM7Pv0DlggOejeUxaO5NuNaFqYEPTHMSENySQvUITRRK nQlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MUqVqK0F; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds11-20020a170907724b00b007ba49ec6d3bsi35126358ejc.390.2023.01.05.08.36.10; Thu, 05 Jan 2023 08:36:23 -0800 (PST) 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=@intel.com header.s=Intel header.b=MUqVqK0F; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234967AbjAEQAU (ORCPT + 55 others); Thu, 5 Jan 2023 11:00:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234692AbjAEQAS (ORCPT ); Thu, 5 Jan 2023 11:00:18 -0500 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4A65F66 for ; Thu, 5 Jan 2023 08:00:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1672934417; x=1704470417; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=8/WgwRhiv0/FiYFKv4n/XrzjJxXuz3JbFtRzxczISeg=; b=MUqVqK0FjIEejx+MmUHiB6/aY/24CTccgrxyf76grkU0LOtVNAkzf1GJ kIhPeYSW3IvrAaye2XVg80SiHRV0wGGTMrbN01vN3WMy7iXyNcrrspv49 P0RVhd5+lAYPCPI+Mj+ach+ZbHotSx1F8HKWd1on+svfXiVFekNAwkewf FdHzPJ7Kkl7Yiz6OwXtDHG1dr2jogOJmwhBFqeUfceCpu1MQjietfpIvq cBSt8sS4E4QF8f5StUOzzed+ZZ2Yoxf/b+muV8h2MNLhWlYJPTB2XmsPb KyoqyB+HBuKgig7QDcyjtVKP74Xb/I+wzfhlQXBGw9dVlkLX6Nth5sjEZ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10580"; a="310028634" X-IronPort-AV: E=Sophos;i="5.96,303,1665471600"; d="scan'208";a="310028634" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jan 2023 08:00:17 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10580"; a="633194840" X-IronPort-AV: E=Sophos;i="5.96,303,1665471600"; d="scan'208";a="633194840" Received: from ciarapow-mobl1.ger.corp.intel.com (HELO [10.213.208.73]) ([10.213.208.73]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jan 2023 08:00:13 -0800 Message-ID: Date: Thu, 5 Jan 2023 16:00:11 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH] drm/i915: Fix potential context UAFs Content-Language: en-US To: Andi Shyti , Rob Clark Cc: dri-devel@lists.freedesktop.org, Rob Clark , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , David Airlie , Daniel Vetter , Chris Wilson , John Harrison , Matthew Brost , katrinzhou , =?UTF-8?Q?Thomas_Hellstr=c3=b6m?= , "open list:INTEL DRM DRIVERS" , open list References: <20230103234948.1218393-1-robdclark@gmail.com> From: Tvrtko Ursulin Organization: Intel Corporation UK Plc In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HK_RANDOM_ENVFROM,HK_RANDOM_FROM, NICE_REPLY_A,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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 On 05/01/2023 15:52, Andi Shyti wrote: > Hi Rob, > > On Tue, Jan 03, 2023 at 03:49:46PM -0800, Rob Clark wrote: >> From: Rob Clark >> >> gem_context_register() makes the context visible to userspace, and which >> point a separate thread can trigger the I915_GEM_CONTEXT_DESTROY ioctl. >> So we need to ensure that nothing uses the ctx ptr after this. And we >> need to ensure that adding the ctx to the xarray is the *last* thing >> that gem_context_register() does with the ctx pointer. >> >> Signed-off-by: Rob Clark > > Reviewed-by: Andi Shyti > > I also agree with Tvrtko that we should add Stable: and Fixes:. Yeah I'll add them all when merging. Just waiting for full CI results. It will be like this: Fixes: eb4dedae920a ("drm/i915/gem: Delay tracking the GEM context until it is registered") Fixes: a4c1cdd34e2c ("drm/i915/gem: Delay context creation (v3)") Fixes: 49bd54b390c2 ("drm/i915: Track all user contexts per client") Cc: # v5.10+ Regards, Tvrtko > One little thing, "user after free" is clearer that UAF :) > > Thanks, > Andi > >> --- >> drivers/gpu/drm/i915/gem/i915_gem_context.c | 24 +++++++++++++++------ >> 1 file changed, 18 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_context.c b/drivers/gpu/drm/i915/gem/i915_gem_context.c >> index 7f2831efc798..6250de9b9196 100644 >> --- a/drivers/gpu/drm/i915/gem/i915_gem_context.c >> +++ b/drivers/gpu/drm/i915/gem/i915_gem_context.c >> @@ -1688,6 +1688,10 @@ void i915_gem_init__contexts(struct drm_i915_private *i915) >> init_contexts(&i915->gem.contexts); >> } >> >> +/* >> + * Note that this implicitly consumes the ctx reference, by placing >> + * the ctx in the context_xa. >> + */ >> static void gem_context_register(struct i915_gem_context *ctx, >> struct drm_i915_file_private *fpriv, >> u32 id) >> @@ -1703,10 +1707,6 @@ static void gem_context_register(struct i915_gem_context *ctx, >> snprintf(ctx->name, sizeof(ctx->name), "%s[%d]", >> current->comm, pid_nr(ctx->pid)); >> >> - /* And finally expose ourselves to userspace via the idr */ >> - old = xa_store(&fpriv->context_xa, id, ctx, GFP_KERNEL); >> - WARN_ON(old); >> - >> spin_lock(&ctx->client->ctx_lock); >> list_add_tail_rcu(&ctx->client_link, &ctx->client->ctx_list); >> spin_unlock(&ctx->client->ctx_lock); >> @@ -1714,6 +1714,10 @@ static void gem_context_register(struct i915_gem_context *ctx, >> spin_lock(&i915->gem.contexts.lock); >> list_add_tail(&ctx->link, &i915->gem.contexts.list); >> spin_unlock(&i915->gem.contexts.lock); >> + >> + /* And finally expose ourselves to userspace via the idr */ >> + old = xa_store(&fpriv->context_xa, id, ctx, GFP_KERNEL); >> + WARN_ON(old); >> } >> >> int i915_gem_context_open(struct drm_i915_private *i915, >> @@ -2199,14 +2203,22 @@ finalize_create_context_locked(struct drm_i915_file_private *file_priv, >> if (IS_ERR(ctx)) >> return ctx; >> >> + /* >> + * One for the xarray and one for the caller. We need to grab >> + * the reference *prior* to making the ctx visble to userspace >> + * in gem_context_register(), as at any point after that >> + * userspace can try to race us with another thread destroying >> + * the context under our feet. >> + */ >> + i915_gem_context_get(ctx); >> + >> gem_context_register(ctx, file_priv, id); >> >> old = xa_erase(&file_priv->proto_context_xa, id); >> GEM_BUG_ON(old != pc); >> proto_context_close(file_priv->dev_priv, pc); >> >> - /* One for the xarray and one for the caller */ >> - return i915_gem_context_get(ctx); >> + return ctx; >> } >> >> struct i915_gem_context * >> -- >> 2.38.1