Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp714977pxj; Thu, 10 Jun 2021 10:51:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbU2A463Af0iqqI7/OPiMWAKTOO4gC1+KarAQ//ie4ALHv/nJ660A6ASDtINDlrd4sEb0H X-Received: by 2002:a17:907:1de6:: with SMTP id og38mr727198ejc.471.1623347485477; Thu, 10 Jun 2021 10:51:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623347485; cv=none; d=google.com; s=arc-20160816; b=I/mx80skNrlTr3a1SmbMXZwSx70OYOWCZVUM83GJO4IYKpo0EvjJADZG0hvcFENJVA YZMHXsGcfrPZAB+BF8P8RKmx0EvCoILgQwtixofFtfF/ao7zgfFo9ljpk9afvuhs2+tA v0tQdTvbTvoO69erfaX14yCf4cDcORapG1tvXIfhampzlqa7DfqLmnZMgJu7OcqZtkNx gnqA82BEoVybSg6XDIMYsJj1qjvYJ33YJ9hY5/sa3hSnvw+6vSXXLP55KwSbMsklVdDX 75rO/AxshSXatFxaYbh/5EqsGzpx6b7rG4EuNICqDW56z7++CDaAivq+zG2h9qZzWmBP GmUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=MDcrEuTfbpypbWBozQCqe1ZKWSisJkXlaXl1ma/eVpk=; b=ZWDIjeZnmb+zZRLmbb0JAPsKgutEqNU6kNaUj4S+qq8fM+03PaY+jErvSgMgxI80Fl tCiIwRH+rgFhe0JwRODXo3kmaKoWmKZQJ9RpVdBz0TmxHvSXsN43uJ+aA3Ofrk94oZmY UvmChL7sTduxKs9Ct/LzvH8VqlUTqy3v08eLIvsfJk2KHziC3IvqHikntD3THX//PhMV Wa9W3v3GLWdeJc06yDytoL0POgsjnnTw2OH/maFqdvbtIw3dmbKJQj5jiXXRSUQeI1ut 0IgV07YxftcfZN5r+PfT4cGsV8DRjyMR4ZgJBis6NoDRGW3CldfPt/vuHs0S3NG6z68z heng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=sIVhKJzS; 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 i11si3250119edb.418.2021.06.10.10.51.01; Thu, 10 Jun 2021 10:51:25 -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=sIVhKJzS; 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 S229941AbhFJRvV (ORCPT + 99 others); Thu, 10 Jun 2021 13:51:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229802AbhFJRvT (ORCPT ); Thu, 10 Jun 2021 13:51:19 -0400 Received: from mail-vs1-xe35.google.com (mail-vs1-xe35.google.com [IPv6:2607:f8b0:4864:20::e35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE0B1C061574 for ; Thu, 10 Jun 2021 10:49:21 -0700 (PDT) Received: by mail-vs1-xe35.google.com with SMTP id x13so2323472vsf.11 for ; Thu, 10 Jun 2021 10:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=MDcrEuTfbpypbWBozQCqe1ZKWSisJkXlaXl1ma/eVpk=; b=sIVhKJzSJcid/btpVwrgFWr6EZyqasxBCn5OPC4c80X1QeYjcTd3zYZQ1s/ecpOdV1 JM8KMcokb2Pji4BKQ2UHgRUMJ748NZgKb76oEoYCtCTfLurhBj2bFq9LZOf95FYKXUSF PfkoaryH+rR+oOWw8Vsfp6ioSZ7JFIX/CVX2x1CAvjtXa3dmIOUnf/zj9fWwEY4NTV5k IBGrLdc7xiwvO6iAs7WItbLhbBcQ8DaPkMkZtX4ko/eW8uRuL4OE8NQOdf1gdr4mZ1gz rA55vaUBVLltZGfYAdjiDCcu16utzxTNytZ+irh72WXej1QjxAEGaxyKHjnuQiiocB+6 q8Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=MDcrEuTfbpypbWBozQCqe1ZKWSisJkXlaXl1ma/eVpk=; b=msgxyujqBL6ZeWTR7m/mf2lhjocxVjNTiaFOkNrIk2VHyHZbcE5JcuBX9wP/7ADdJs 1ItmWOeNwt14SxM5Pt8tkWPqDyzdg27GgBJSpBA1NijwKYzLBtHVc5XC8KB6xES6UfIE cl20/eOtOiqyl75Y/XXNFFGzu0B/D7fcYpf+VDMAJRLUpZf//N1i3J0/dIgwlPkjWUYG FcE9Gc/D2M7emp+RhOrE58ezfmMO9XrXJuEETYtRsUJwfuk05qWsY2EKhX/7s6GjO0p/ s+Dai0arE2uJgCwd8UMqoFHejwOvkvAgnf/5MkFk+CMuuTQ3USp59ia85rds13OSUSrv EVQg== X-Gm-Message-State: AOAM5335bPgufmGHmLpvz/Z7ZHQAbC/xpXfhzMg+iShDw8wew5xRUQk8 B03e/c56j1AyhZrREin+uqXNU2YGPwqzNGGmObM= X-Received: by 2002:a67:878a:: with SMTP id j132mr6463110vsd.18.1623347360746; Thu, 10 Jun 2021 10:49:20 -0700 (PDT) MIME-Version: 1.0 References: <20210609092119.173590-1-desmondcheongzx@gmail.com> In-Reply-To: From: Emil Velikov Date: Thu, 10 Jun 2021 18:49:09 +0100 Message-ID: Subject: Re: [PATCH] drm: Lock pointer access in drm_master_release() To: Desmond Cheong Zhi Xi , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Dave Airlie , ML dri-devel , "Linux-Kernel@Vger. Kernel. Org" , skhan@linuxfoundation.org, Greg Kroah-Hartman , linux-kernel-mentees@lists.linuxfoundation.org, Dan Carpenter Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 10 Jun 2021 at 11:10, Daniel Vetter wrote: > > On Wed, Jun 09, 2021 at 05:21:19PM +0800, Desmond Cheong Zhi Xi wrote: > > This patch eliminates the following smatch warning: > > drivers/gpu/drm/drm_auth.c:320 drm_master_release() warn: unlocked access 'master' (line 318) expected lock '&dev->master_mutex' > > > > The 'file_priv->master' field should be protected by the mutex lock to > > '&dev->master_mutex'. This is because other processes can concurrently > > modify this field and free the current 'file_priv->master' > > pointer. This could result in a use-after-free error when 'master' is > > dereferenced in subsequent function calls to > > 'drm_legacy_lock_master_cleanup()' or to 'drm_lease_revoke()'. > > > > An example of a scenario that would produce this error can be seen > > from a similar bug in 'drm_getunique()' that was reported by Syzbot: > > https://syzkaller.appspot.com/bug?id=148d2f1dfac64af52ffd27b661981a540724f803 > > > > In the Syzbot report, another process concurrently acquired the > > device's master mutex in 'drm_setmaster_ioctl()', then overwrote > > 'fpriv->master' in 'drm_new_set_master()'. The old value of > > 'fpriv->master' was subsequently freed before the mutex was unlocked. > > > > Reported-by: Dan Carpenter > > Signed-off-by: Desmond Cheong Zhi Xi > > Thanks a lot. I've done an audit of this code, and I found another > potential problem in drm_is_current_master. The callers from drm_auth.c > hold the dev->master_mutex, but all the external ones dont. I think we > need to split this into a _locked function for use within drm_auth.c, and > the exported one needs to grab the dev->master_mutex while it's checking > master status. Ofc there will still be races, those are ok, but right now > we run the risk of use-after free problems in drm_lease_owner. > Note that some code does acquire the mutex via drm_master_internal_acquire - so we should be careful. As mentioned elsewhere - having a _locked version of drm_is_current_master sounds good. Might as well throw a lockdep_assert_held_once in there just in case :-P Happy to help review the follow-up patches. -Emil