Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1146405pxj; Fri, 18 Jun 2021 00:06:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1APC5AJMmbV3M8txhFP3/vgVtYTPHdabwrsvM04ROaEoTOuSykmTCcbd2skQYgrk5otW/ X-Received: by 2002:a92:d902:: with SMTP id s2mr6225108iln.278.1623999997342; Fri, 18 Jun 2021 00:06:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623999997; cv=none; d=google.com; s=arc-20160816; b=AYzDMWPg5zYw1GYha8uk4omInKlaSvBSXA6h+y/lA3Nz100Zy6l8rhueiuWaGgZSVD 546jlercaFL+1Ig68bUZy1OvQgWHbiC2K/nSMg6AjaHMiMsRflcAv09E1TWzmjHnBsX4 /t2F67GxyPHh7N28FUIZWgMX4jcFGB6qCRn7IK2jWGln0BUweT73gGJYxdWl1XnWFCcY 1uE34hKPSzSnI3TTKm+R+5Mi87CkFStBVOEkDQYFJRjdqLqIGXaTAfQUrelSvsT2KCsK /GpP6bH402eXuvnx7adL20sG+XO0R2FlmlE61gK0AP9ZqgO0YsuOvds3a7kych11hNp6 4tJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject:dkim-signature; bh=5Hkc1Bod9XeIHJJRLDjduZE/jHZcAso4vB+S4dzyd3A=; b=VaUIPGtcsQO3CLnwP5ieBT/aEg9jnsc3z0klMdCEEKNNTgEIuf9X7/44GbJ//ssecs J+iCo4iX046RWEGmpHEZP3alSl7cGLb4QTKIA295NXbWB5WJCSlzsRusTi+YBWU9H4Dy rFxDp3w/du6yKQlRTI14SnMfWWo2vIxdDPBZT2hJevptG1yiJ+oivNULfYtMkmL5AIbr kRr357ZZ4xYIcaRroYr0isZLLpKA/8HXMIVL/bPmjkX1Lg/Xch+HQ63qKV9bgDcU/sGF uu7EsuFpEyWBkXLP3VAbBviGLgGJXtdse1qfIz/mSaz0GsazquEujwuL/OMRNRNoisF0 IC7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Kjcy42HJ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d1si1428000ila.19.2021.06.18.00.06.23; Fri, 18 Jun 2021 00:06:37 -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=@gmail.com header.s=20161025 header.b=Kjcy42HJ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230268AbhFRDHW (ORCPT + 99 others); Thu, 17 Jun 2021 23:07:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbhFRDHU (ORCPT ); Thu, 17 Jun 2021 23:07:20 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86D10C061574 for ; Thu, 17 Jun 2021 20:05:10 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id v7so6617338pgl.2 for ; Thu, 17 Jun 2021 20:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=5Hkc1Bod9XeIHJJRLDjduZE/jHZcAso4vB+S4dzyd3A=; b=Kjcy42HJCHBpM0LWBplRFsAQd1+Iv2qTJwbxsL8oRWoeFc8dRWnP4bqO/lpX1qny5F sAhnvJfXBj6HJZW4FfXRvjgLnbF5zVp5xZhsr2PBm7LkEjC3KRKBfXeBRPARTIbNxPQK 631lLr0VPBj93bHbIJAxQXohxwtVcmRNkCm6KZXJYi0DI4A+xCpVasF/MZwj9GlqynW4 drbP9t1nRR2qR7C8b6lf9ltMdU2g4SeMzj+0DHhk7oe87BsTWRoHVCHrfUITNulDHbME g0GrxFjSgWjNW5eDdoMJo0o5tWg/gwxRG+GFJajXe97oqrWMAHognSTMKJJRAFv/Mvv4 OHwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=5Hkc1Bod9XeIHJJRLDjduZE/jHZcAso4vB+S4dzyd3A=; b=Yacmgdh7fxnbJv4Cvg2/LO5F0WsuGwRsP5dSfvUyDY/sFLfBsyFGm0WS5wdminGAM/ z4LUFJTs5HITMIYxJtPWzsYS67nl4KlK/hEfIHiY9NDBp/oiRMdqsOCMOpNx77B5V2pC AK+IVUWBttuU1nKFDI28zf5ADsNs11Mfu997C6cITpheEOE7VFuavy8CE75j/NPOfUAn imzxQWfR0gOt73vLaRkFDAyfwExQ1gvXDiR7NOgWnrsmsfkuRClGhadfQnvwuVHEOIGf tLI3hBp9xzjiD/lzUPk7cv4Z6UQeSTycz2llxgEiJRPsbGWZPzIZI7dx8ORqSmO9VqWA Od1w== X-Gm-Message-State: AOAM532bDhiXgVQGsYKmVyWEjceKHzSLNGuJNF9+raWLIYooi1wWqHO4 CVKdGcDldF0DvPF502k4vSY= X-Received: by 2002:a62:7b4c:0:b029:2e9:cec2:e252 with SMTP id w73-20020a627b4c0000b02902e9cec2e252mr2802617pfc.56.1623985509826; Thu, 17 Jun 2021 20:05:09 -0700 (PDT) Received: from [192.168.1.237] ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id w18sm6798863pjg.50.2021.06.17.20.05.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Jun 2021 20:05:09 -0700 (PDT) Subject: Re: [PATCH v2 2/2] drm: Protect drm_master pointers in drm_lease.c To: maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@linux.ie, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, emil.l.velikov@gmail.com References: <20210615023645.6535-1-desmondcheongzx@gmail.com> <20210615023645.6535-3-desmondcheongzx@gmail.com> From: Desmond Cheong Zhi Xi Message-ID: Date: Fri, 18 Jun 2021 11:05:04 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/6/21 1:12 am, Daniel Vetter wrote: > On Tue, Jun 15, 2021 at 10:36:45AM +0800, Desmond Cheong Zhi Xi wrote: >> This patch ensures that the device's master mutex is acquired before >> accessing pointers to struct drm_master that are subsequently >> dereferenced. Without the mutex, the struct drm_master may be freed >> concurrently by another process calling drm_setmaster_ioctl(). This >> could then lead to use-after-free errors. >> >> Reported-by: Daniel Vetter >> Signed-off-by: Desmond Cheong Zhi Xi >> Reviewed-by: Emil Velikov >> --- >> drivers/gpu/drm/drm_lease.c | 58 +++++++++++++++++++++++++++---------- >> 1 file changed, 43 insertions(+), 15 deletions(-) >> >> diff --git a/drivers/gpu/drm/drm_lease.c b/drivers/gpu/drm/drm_lease.c >> index da4f085fc09e..3e6f689236e5 100644 >> --- a/drivers/gpu/drm/drm_lease.c >> +++ b/drivers/gpu/drm/drm_lease.c >> @@ -107,10 +107,16 @@ static bool _drm_has_leased(struct drm_master *master, int id) >> */ >> bool _drm_lease_held(struct drm_file *file_priv, int id) >> { >> + bool ret; >> + >> if (!file_priv || !file_priv->master) >> return true; >> >> - return _drm_lease_held_master(file_priv->master, id); >> + mutex_lock(&file_priv->master->dev->master_mutex); > > So maybe we have a bug somewhere, and the kerneldoc isn't 100% clear, but > I thought file_priv->master is invariant over the lifetime of file_priv. > So we don't need a lock to check anything here. > > It's the drm_device->master derefence that gets us into trouble. Well also > file_priv->is_owner is protected by dev->master_mutex. > > So I think with your previous patch all the access here in drm_lease.c is > ok and already protected? Or am I missing something? > > Thanks, Daniel > My thinking was that file_priv->master is invariant only if it is the creator of master. If file_priv->is_master is false, then a call to drm_setmaster_ioctl will invoke drm_new_set_master, which then allocates a new master for file_priv, and puts the old master. This could be an issue in _drm_lease_held_master, because we dereference master to get master->dev, master->lessor, and master->leases. With the same reasoning, in other parts of drm_lease.c, if there's an access to drm_file->master that's subsequently dereferenced, I added a lock around them. I could definitely be mistaken on this, so apologies if this scenario doesn't arise. Best wishes, Desmond