Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2322839ybb; Thu, 2 Apr 2020 18:17:03 -0700 (PDT) X-Google-Smtp-Source: APiQypInivdnmZ6KTYlldVDRGUiaAaVKb0IuOIkuqpEhg++u34Ia2f+LAQ3pZxaX5QTwGpC00tH1 X-Received: by 2002:a05:6830:2391:: with SMTP id l17mr4789132ots.339.1585876623099; Thu, 02 Apr 2020 18:17:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585876623; cv=none; d=google.com; s=arc-20160816; b=FHqkTInoFcamq/8yF5LvJPWL6mkSE3nLk1u1hVE6dQE/6/XD7bL8zXsFFzWl+gEN0n +bPqOhLp3cF4HWk7jey11KlnY4W5MXPdd+kDIZwqNDZ26qECq9J3l40V306cUr+EVtR+ gA8oqiuzm1fSG3cJAx9igqib5H7gTMZV7w+pF0JPExJt220TpbXZzipVnt08TTu2YhP+ yV9iH6bvbn1fRKBJjb5iRfFh2lq6Er8FjR51/xQS9X6tvyY/Lg0SO9swqxQVY/qcfN42 FOOufFmq/hkD4uAV+5hz8mfjU681wUJtbcPpFsNYbM3KBesjwZeZlNvBeiOJMYiXWExX UReA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=a4w6WNUXo0OrnIhl+pN95pim8+mHdrQuMjAwNVyujGg=; b=DWduqtWdbeoMweQbebqalP9kz06nCqTbsGYeEwJo3PQyj2Ii9M1dLLLc1E5ZoZtqrE JeecL8j0bWOSXRHZX/M5Pop7Fiv36P9rnYoNRCScL4+3AGSLxHPuqYqpIAeXf8hXRxLJ mFe97OpBzr80wwjbEMJ3Kx+MOnNAXuvBMr5jdAiwQI2U1x9yZWSKJWNJZRsUgbCyqwun Yg1Uak6HE45+T/rj5LhmDI2hwXg0O3QwkvkopU+TEa6aU/98K6tzE2gL5VhpoX6/dMGg F5PWgresDKT8xnX4pnLVA9Z8fY5obrLwZAxlAeXBE6/GeiGRos8u97imDu09v1kNfh82 08BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Cl6oxazq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k70si2974270oih.24.2020.04.02.18.16.50; Thu, 02 Apr 2020 18:17:03 -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=fail header.i=@gmail.com header.s=20161025 header.b=Cl6oxazq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390104AbgDCBNw (ORCPT + 99 others); Thu, 2 Apr 2020 21:13:52 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:43821 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731783AbgDCBNw (ORCPT ); Thu, 2 Apr 2020 21:13:52 -0400 Received: by mail-pf1-f193.google.com with SMTP id f206so2651365pfa.10; Thu, 02 Apr 2020 18:13:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=a4w6WNUXo0OrnIhl+pN95pim8+mHdrQuMjAwNVyujGg=; b=Cl6oxazqwil6UKwahFv6KFn7LXSZt8iHP/QsrKl6rvYj5FScx9Cl79/OqvpItS+e1a wfJB3vjipRbG10zLOKy7Rm2AuH3ZEMPQn896o7k5sktqj2cbxYZSzsnQa11J/Y+pBfG0 5MOFkx3gvmZrGUKFqUzPhDOLB6WQHxdBFOrsMezSZ8tn5OGpAvK4+JPfw+OKDqI7Dca5 4Cs+TEcvicUO5XVfCbyDSTS6SmXnao7fILJ5ZmBQMda2Ai0hO0fpB7KKRUhNiEyB2edg 8AA+K29FMj3XBQeyw9uOujT7vCxpmH1kM3YxM6HbXhF38qPxs/0YpkY7uEvPpmrdbmxj 1QgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=a4w6WNUXo0OrnIhl+pN95pim8+mHdrQuMjAwNVyujGg=; b=UhsK3QTrsJ8Elsw7vfx7NKuieXKmv+ZZEn5UWkfFWtmSgXQsimRXZVTMRfadDo8jaF Bpo1qGUeFAwAIvg+o6Nj+YJIKsFDYWMlFr3SGGzkUB5tNeyZbtrf8zM0QrhPcRz/h3Nb dX0WpEX0RVNf4uN63eTFgzKG9lgOwkAhsG+FIGIiVsyRTQXUshgtOZiXCJgrDcgr5psH NbuuXyUVDd0Sqx0LnbDgWqqHET82cO1MrorOTbpGmwNgL2Z2xixcv977sjpzGW3SsIDO zm8/B+1HE6OO2rvQze1rMo52sRGJ/0WPlXwlej0ge8mTMd3gsUkyIElrfccYH4TF7sIw Y4dw== X-Gm-Message-State: AGi0Publusf45axHbe/Pqk/dkaNuxPcwm4t8QeTZau8r1SJr+1GKzJHm J9x7stc2uP1WZYoRiMTFDTw= X-Received: by 2002:a62:7c82:: with SMTP id x124mr6202533pfc.280.1585876430597; Thu, 02 Apr 2020 18:13:50 -0700 (PDT) Received: from sultan-box.localdomain (static-198-54-129-52.cust.tzulo.com. [198.54.129.52]) by smtp.gmail.com with ESMTPSA id b189sm4630053pfa.209.2020.04.02.18.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2020 18:13:49 -0700 (PDT) From: Sultan Alsawaf X-Google-Original-From: Sultan Alsawaf Cc: Sultan Alsawaf , stable@vger.kernel.org, Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , David Airlie , Daniel Vetter , Matthew Auld , Chris Wilson , intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH] drm/i915: Fix use-after-free due to intel_context_pin/unpin race Date: Thu, 2 Apr 2020 18:13:18 -0700 Message-Id: <20200403011318.2280-1-sultan@kerneltoast.com> X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sultan Alsawaf The retire and active callbacks can run simultaneously, allowing intel_context_pin() and intel_context_unpin() to run at the same time, trashing the ring and page tables. In 5.4, this was more noticeable because intel_ring_unpin() would set ring->vaddr to NULL and cause a clean NULL-pointer-dereference panic, but in newer kernels the use-after-free goes unnoticed. The NULL-pointer-dereference looks like this: BUG: unable to handle page fault for address: 0000000000003448 RIP: 0010:gen8_emit_flush_render+0x163/0x190 Call Trace: execlists_request_alloc+0x25/0x40 __i915_request_create+0x1f4/0x2c0 i915_request_create+0x71/0xc0 i915_gem_do_execbuffer+0xb98/0x1a80 ? preempt_count_add+0x68/0xa0 ? _raw_spin_lock+0x13/0x30 ? _raw_spin_unlock+0x16/0x30 i915_gem_execbuffer2_ioctl+0x1de/0x3c0 ? i915_gem_busy_ioctl+0x7f/0x1d0 ? i915_gem_execbuffer_ioctl+0x2d0/0x2d0 drm_ioctl_kernel+0xb2/0x100 drm_ioctl+0x209/0x360 ? i915_gem_execbuffer_ioctl+0x2d0/0x2d0 ksys_ioctl+0x87/0xc0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x4e/0x150 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Protect the retire callback with ref->mutex to complement the active callback and fix the corruption. Fixes: 12c255b5dad1 ("drm/i915: Provide an i915_active.acquire callback") Cc: Signed-off-by: Sultan Alsawaf --- drivers/gpu/drm/i915/i915_active.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/gpu/drm/i915/i915_active.c b/drivers/gpu/drm/i915/i915_active.c index c4048628188a..0478bcf061b5 100644 --- a/drivers/gpu/drm/i915/i915_active.c +++ b/drivers/gpu/drm/i915/i915_active.c @@ -148,8 +148,10 @@ __active_retire(struct i915_active *ref) spin_unlock_irqrestore(&ref->tree_lock, flags); /* After the final retire, the entire struct may be freed */ + mutex_lock(&ref->mutex); if (ref->retire) ref->retire(ref); + mutex_unlock(&ref->mutex); /* ... except if you wait on it, you must manage your own references! */ wake_up_var(ref); -- 2.26.0