Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2417568ybt; Tue, 16 Jun 2020 05:44:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxjUXPy4q2pUZF7cF74GJqVagu/WE7bYblJBDFwp6d9oVEruUhyZaVihWzHcpR5Enzmy1kJ X-Received: by 2002:a17:906:d0d7:: with SMTP id bq23mr2690189ejb.259.1592311487392; Tue, 16 Jun 2020 05:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592311487; cv=none; d=google.com; s=arc-20160816; b=WxiUKx6+WN1Hb7K7YsDuM9Smr/VejRufr//WcJ1tNe4Ml/LrnW0GJdReVqABPIxOvB VkGS/E0QVcMf7inBN123Yjvo7hweeAN0dFKFYArV7aMbxeVxVB8ioikqBAH4DppSlDh2 FzTW9fC40rO/41OW/lM9w3U8PX/mEPm1UWeeif0i3384ro8H0HT+HjyPujSX7UhRyb4c wYABPWCY8/CRtveLqocEQD8Hoe6wR4slE+lg+3g+5gx1QkCnLmXihnExCENMn7Uvf1T+ nl5hzJr+KRiKjDCkagZSg8TEOKkQxBVBC5DPGFrxR+O3DR/MZMUC3o/XbGfmtznzbYAC NNtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Q13V/CJ4C0CyeaU3PQKnFBowYarfzkVCYNjXfCo/M1g=; b=X8R0SXq17OJADKMqUQzqPC43l6w3MLp/n6c76UVsO2RZyLVFTWhrYZ4FMiAs9D48DM QJKSQunvPyCm2cSaAtFVXLyvZaIsH27pHdHdj4vtg+f+S7m5HRYaME8vR0crZIBgdIfH 6F/VrAmQNR34QIWU+V/3QKYAzsmvp9UdMMfggACrC6BZ236DlPVNHD78DjXKgoiLLHpo m00lLpAwr0upqNwtgOE5PpyQjvIjOoloLykTIuAdNM0GmbISo2nUaS+OwajdUgdNfLnq TDfM2C5Cj/5lx0psolkjxnPuxiAn7YoBoh75XdOvM79Piffft5dprCSmifk72tQUT3k9 nXsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WWDe74GX; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k6si9031068ejr.212.2020.06.16.05.44.24; Tue, 16 Jun 2020 05:44:47 -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=@linaro.org header.s=google header.b=WWDe74GX; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728782AbgFPMm1 (ORCPT + 99 others); Tue, 16 Jun 2020 08:42:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728348AbgFPMm0 (ORCPT ); Tue, 16 Jun 2020 08:42:26 -0400 Received: from mail-ot1-x344.google.com (mail-ot1-x344.google.com [IPv6:2607:f8b0:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18DC9C08C5C2 for ; Tue, 16 Jun 2020 05:42:25 -0700 (PDT) Received: by mail-ot1-x344.google.com with SMTP id n6so15817120otl.0 for ; Tue, 16 Jun 2020 05:42:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Q13V/CJ4C0CyeaU3PQKnFBowYarfzkVCYNjXfCo/M1g=; b=WWDe74GX3eV9epE3ig+bqnOkHDawoFwWRSShn5mcl7Dye5ohesVAu0q4F8rVGjEhLZ JN6AlrPmz3owghYEzUlSpgWZzQl0v1IqN3WTfmQv9bdrsaLyKsAxc29Yp8ptZCwnB0y4 4CSM40Uw2c/HcQuFvkL9qVj52iJTTP45/8XfXv+lEEffujS0YwQMtP8L3jZ5XnaWv4R3 Drf1Tw9CF7jvkz9G+z5SqBY+pp3giX38K6S/gELzGMRgZMDW5AAYTXyl6lIYg5cnP0Rm tzak88/KUjE5utQzbQEEbTrCUTFoJ4As3bEGi0Bh3yZrB2fnIAsIl201PhAiHgz5nqeS +ahw== 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:cc; bh=Q13V/CJ4C0CyeaU3PQKnFBowYarfzkVCYNjXfCo/M1g=; b=Sv7FLrqBcgDQWR/Kj/CkHywyIQv5+IrVsO+3O1eYfB504TGf63qvm2Hj+qciJTEBxM Q0nmXbDmlDyZMjZG/Mi774omGa+iw/vAzGjHdSpAPfshRnkU2il4bl4me+QxjKh/54bn jra+1P2qotdb4hQKswtmH9STmwwepTagBafHUWk+IC522wwo0hjuZMxosktxkO0rucv/ kYKIGmhsUW0NJsK+NEZkh1BYPqo0xwtl9MxPvl9QNgxwhaqzxWWxjCXvJ5PFwqUqzO2J glrhyTgrZ0usgpT+XH8ZW8iyQ+9Z/tnoBQCFfxAC7Z6eqZh0/T38alKBDQp+RdHQg0I5 53vA== X-Gm-Message-State: AOAM531NMm1XX/T9Kpx7MJhBNKrHB1MVQ2leI57zLHfuGZ7XTp3WKZVA HT6/VM0nUOLr3V/3lr1fIIQ1dPYA+UHodRDyYJPJ8Q== X-Received: by 2002:a9d:6a85:: with SMTP id l5mr1098400otq.371.1592311344349; Tue, 16 Jun 2020 05:42:24 -0700 (PDT) MIME-Version: 1.0 References: <20200611114418.19852-1-sumit.semwal@linaro.org> In-Reply-To: <20200611114418.19852-1-sumit.semwal@linaro.org> From: Sumit Semwal Date: Tue, 16 Jun 2020 18:12:13 +0530 Message-ID: Subject: Re: [PATCH v2] dma-buf: Move dma_buf_release() from fops to dentry_ops To: "open list:DMA BUFFER SHARING FRAMEWORK" , DRI mailing list , Daniel Vetter , Chris Wilson Cc: Chenbo Feng , LKML , Linaro MM SIG , Charan Teja Reddy , syzbot+3643a18836bce555bff6@syzkaller.appspotmail.com, "# 3.4.x" , Arnd Bergmann Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, If there are no objections to this, I will plan to merge it soon. Daniel, Chris, Chenbo? On Thu, 11 Jun 2020 at 17:14, Sumit Semwal wrote: > > Charan Teja reported a 'use-after-free' in dmabuffs_dname [1], which > happens if the dma_buf_release() is called while the userspace is > accessing the dma_buf pseudo fs's dmabuffs_dname() in another process, > and dma_buf_release() releases the dmabuf object when the last reference > to the struct file goes away. > > I discussed with Arnd Bergmann, and he suggested that rather than tying > the dma_buf_release() to the file_operations' release(), we can tie it to > the dentry_operations' d_release(), which will be called when the last ref > to the dentry is removed. > > The path exercised by __fput() calls f_op->release() first, and then calls > dput, which eventually calls d_op->d_release(). > > In the 'normal' case, when no userspace access is happening via dma_buf > pseudo fs, there should be exactly one fd, file, dentry and inode, so > closing the fd will kill of everything right away. > > In the presented case, the dentry's d_release() will be called only when > the dentry's last ref is released. > > Therefore, lets move dma_buf_release() from fops->release() to > d_ops->d_release() > > Many thanks to Arnd for his FS insights :) > > [1]: https://lore.kernel.org/patchwork/patch/1238278/ > > Fixes: bb2bb9030425 ("dma-buf: add DMA_BUF_SET_NAME ioctls") > Reported-by: syzbot+3643a18836bce555bff6@syzkaller.appspotmail.com > Cc: [5.3+] > Cc: Arnd Bergmann > Reported-by: Charan Teja Reddy > Reviewed-by: Arnd Bergmann > Signed-off-by: Sumit Semwal > > --- > v2: per Arnd: Moved dma_buf_release() above to avoid forward declaration; > removed dentry_ops check. > --- > drivers/dma-buf/dma-buf.c | 54 ++++++++++++++++++--------------------- > 1 file changed, 25 insertions(+), 29 deletions(-) > > diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c > index 01ce125f8e8d..412629601ad3 100644 > --- a/drivers/dma-buf/dma-buf.c > +++ b/drivers/dma-buf/dma-buf.c > @@ -54,37 +54,11 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) > dentry->d_name.name, ret > 0 ? name : ""); > } > > -static const struct dentry_operations dma_buf_dentry_ops = { > - .d_dname = dmabuffs_dname, > -}; > - > -static struct vfsmount *dma_buf_mnt; > - > -static int dma_buf_fs_init_context(struct fs_context *fc) > -{ > - struct pseudo_fs_context *ctx; > - > - ctx = init_pseudo(fc, DMA_BUF_MAGIC); > - if (!ctx) > - return -ENOMEM; > - ctx->dops = &dma_buf_dentry_ops; > - return 0; > -} > - > -static struct file_system_type dma_buf_fs_type = { > - .name = "dmabuf", > - .init_fs_context = dma_buf_fs_init_context, > - .kill_sb = kill_anon_super, > -}; > - > -static int dma_buf_release(struct inode *inode, struct file *file) > +static void dma_buf_release(struct dentry *dentry) > { > struct dma_buf *dmabuf; > > - if (!is_dma_buf_file(file)) > - return -EINVAL; > - > - dmabuf = file->private_data; > + dmabuf = dentry->d_fsdata; > > BUG_ON(dmabuf->vmapping_counter); > > @@ -110,9 +84,32 @@ static int dma_buf_release(struct inode *inode, struct file *file) > module_put(dmabuf->owner); > kfree(dmabuf->name); > kfree(dmabuf); > +} > + > +static const struct dentry_operations dma_buf_dentry_ops = { > + .d_dname = dmabuffs_dname, > + .d_release = dma_buf_release, > +}; > + > +static struct vfsmount *dma_buf_mnt; > + > +static int dma_buf_fs_init_context(struct fs_context *fc) > +{ > + struct pseudo_fs_context *ctx; > + > + ctx = init_pseudo(fc, DMA_BUF_MAGIC); > + if (!ctx) > + return -ENOMEM; > + ctx->dops = &dma_buf_dentry_ops; > return 0; > } > > +static struct file_system_type dma_buf_fs_type = { > + .name = "dmabuf", > + .init_fs_context = dma_buf_fs_init_context, > + .kill_sb = kill_anon_super, > +}; > + > static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) > { > struct dma_buf *dmabuf; > @@ -412,7 +409,6 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) > } > > static const struct file_operations dma_buf_fops = { > - .release = dma_buf_release, > .mmap = dma_buf_mmap_internal, > .llseek = dma_buf_llseek, > .poll = dma_buf_poll, > -- > 2.27.0 > Best, Sumit.