Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1690954ybh; Thu, 23 Jul 2020 15:35:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKg3kXVGvtJZVGVbVFeo8XcLvw2KtfEoa3/CBDRvjEWrPvb9pIRLp0nmBZd9eJElJfmLMM X-Received: by 2002:aa7:da8a:: with SMTP id q10mr6288602eds.139.1595543732460; Thu, 23 Jul 2020 15:35:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595543732; cv=none; d=google.com; s=arc-20160816; b=p7YrcwX8YGmCe6BuZjIoKqVMXdYKQbfK0J8hm2he4RvpHZkkzZwfJb1gLlhz9vJgvm /7+ejkrNNorORuwkMCacUHu4wOA17HB6Y17+ldGzlC3JaORCSiDfcbYNZNfQK8PQ5AJm +Nkr/q7t2s/+3+XXlmcpyvTq2R/mhC0swKJr+T4vrAu8wAGDpI+CHD9d7HL9CKV+UpZB O4R8eGJZu8l62tRzhi2hOzzgh2zAdfy8Id6bVpE5R2+in2HGuaqYQBhfqlEz1cuauP4p LS/Jh0v36XSYprZlfsnhId4zbuV77PmlMzze29zSeogzXesG0OuuNa7Ozc0lUdwZT3/9 epgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=rcHiH1Tqs5ADJ+MGb3vcjbWBOdmK6BVfKbafmPqedI4=; b=WfahwieuqjgMjGoOg4Tj5FTJ2Q8Dc1tpBBpuVFF1X69f7hZHTJYrq8L3lNEm307BSu jgNFYG3+mkOYppgPC3XLOK0MYSLSFxAesIZ6yVCtoemeAWIi0K2CqSyFrFE8Q6DSoWik IAE0es+R+yQknx3VUEfoSBF4fnJI6XxHalIlQsY6so68CqUSEY5PcOgxPSyhwvkrBlKE 7rShvbjXHa9YQDLnT3YjMRy+HoKLF/Vb+WPIi6SYFDzYsJC4ma/BjJDzkfIk4/pOfTeh WGbSlBV3IP2JB1/NyVedMH77xbiJUKXszHZd6XRBKF1XUuiSTasmJY1X2WzEexfr7zyY ah6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lvaXW0EH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u10si2206839edo.324.2020.07.23.15.35.00; Thu, 23 Jul 2020 15:35:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lvaXW0EH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727901AbgGWWcT (ORCPT + 99 others); Thu, 23 Jul 2020 18:32:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726608AbgGWWcT (ORCPT ); Thu, 23 Jul 2020 18:32:19 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1626AC0619D3 for ; Thu, 23 Jul 2020 15:32:19 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id l6so3333582plt.7 for ; Thu, 23 Jul 2020 15:32:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rcHiH1Tqs5ADJ+MGb3vcjbWBOdmK6BVfKbafmPqedI4=; b=lvaXW0EH58h+/+Oelkh/2EltLVN21JeGtH9oA9rq4r7QwYFgUzxZHdbdpcj6L0OT4+ NUzurvHLRVuiuFxDjQ4s+3XcN+azqQ4Z9/kmtqIXftHCj7BYQ13jO1kHZnF5qpLkb/5/ Db2jc9KY+6XmdRAlWCgKbg6WL/mHQCeBQS4JM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=rcHiH1Tqs5ADJ+MGb3vcjbWBOdmK6BVfKbafmPqedI4=; b=BJd39YCXfQTXzjP0JC3IVw0yvBG7+UBHq5ueMXqtG3R2kP643/SbMJGMKgtTmIairb c4h+l94fSUzE+5fNtiXk+m0zusdvJRabeEjBxSdEMWZhQVZH9EYAhouWLwBh51BRY5pE CNzsdy9bYRPFHNyreWyj+wvXXEv6fP9KiBm3WbLgEeCPDj4lUEiOGJu43woxAaHztcnR xG3DE53rjwOtJCjLgRnU+OZmUAmVHkgo5RVdmNv3zurw1Fi5fCV8mPDtwU/KBTllABGH OhLaoyXcM0D4DrgL8PxkwvxAW4xPdtNaBjDLh1AzGj6iCkDFGbB1fAEQwq75i2YnGsjy i/rg== X-Gm-Message-State: AOAM532qBbl14h3QOSLseqT700OnjGYkRchqEVboRgTuSfAWEf1ICt5n kLOK4/3GqQzngG+FcokRbbfoNQ== X-Received: by 2002:a17:90a:1b64:: with SMTP id q91mr2452247pjq.119.1595543538591; Thu, 23 Jul 2020 15:32:18 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id m68sm3815562pje.24.2020.07.23.15.32.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jul 2020 15:32:17 -0700 (PDT) Date: Thu, 23 Jul 2020 15:32:16 -0700 From: Kees Cook To: Mazin Rezk Cc: "linux-kernel@vger.kernel.org" , "amd-gfx@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" , "akpm@linux-foundation.org" , "christian.koenig@amd.com" , "harry.wentland@amd.com" , "nicholas.kazlauskas@amd.com" , "sunpeng.li@amd.com" , "alexander.deucher@amd.com" , "1i5t5.duncan@cox.net" <1i5t5.duncan@cox.net>, "mphantomx@yahoo.com.br" , "regressions@leemhuis.info" , "anthony.ruhier@gmail.com" , "pmenzel@molgen.mpg.de" Subject: Re: [PATCH] amdgpu_dm: fix nonblocking atomic commit use-after-free Message-ID: <202007231524.A24720C@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 23, 2020 at 09:10:15PM +0000, Mazin Rezk wrote: > When amdgpu_dm_atomic_commit_tail is running in the workqueue, > drm_atomic_state_put will get called while amdgpu_dm_atomic_commit_tail is > running, causing a race condition where state (and then dm_state) is > sometimes freed while amdgpu_dm_atomic_commit_tail is running. This bug has > occurred since 5.7-rc1 and is well documented among polaris11 users [1]. > > Prior to 5.7, this was not a noticeable issue since the freelist pointer > was stored at the beginning of dm_state (base), which was unused. After > changing the freelist pointer to be stored in the middle of the struct, the > freelist pointer overwrote the context, causing dc_state to become garbage > data and made the call to dm_enable_per_frame_crtc_master_sync dereference > a freelist pointer. > > This patch fixes the aforementioned issue by calling drm_atomic_state_get > in amdgpu_dm_atomic_commit before drm_atomic_helper_commit is called and > drm_atomic_state_put after amdgpu_dm_atomic_commit_tail is complete. > > According to my testing on 5.8.0-rc6, this should fix bug 207383 on > Bugzilla [1]. > > [1] https://bugzilla.kernel.org/show_bug.cgi?id=207383 Nice work tracking this down! > Fixes: 3202fa62f ("slub: relocate freelist pointer to middle of object") I do, however, object to this Fixes tag. :) The flaw appears to have been with amdgpu_dm's reference tracking of "state" in the nonblocking case. (How this reference counting is supposed to work correctly, though, I'm not sure.) If I look at where the drm helper was split from being the default callback, it looks like this was what introduced the bug: da5c47f682ab ("drm/amd/display: Remove acrtc->stream") ? 3202fa62f certainly exposed it much more quickly, but there was a race even without 3202fa62f where something could have realloced the memory and written over it. -- Kees Cook