Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp389586lqp; Wed, 22 May 2024 07:36:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdY8lbm+2gid/MwYemIAJeVY19IKSspW9b+Fu1p15EQIgpkecEqi04LeIWw0wXh1ej8/0IWUB4yAE4WzdRIFestAVuzkTnBCCooDDGYw== X-Google-Smtp-Source: AGHT+IGfqSD+rUuQw5Y5IteVVSMT2l7Rb76JDNOLfOaiYuWp3vubbXa0K4gvsXysi+NCq+Thoms/ X-Received: by 2002:a05:6214:4806:b0:6ab:7270:fcbe with SMTP id 6a1803df08f44-6ab8090334amr23409056d6.58.1716388563002; Wed, 22 May 2024 07:36:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716388562; cv=pass; d=google.com; s=arc-20160816; b=I3TH7P7rQIKVFIpWwQ9OCUFt5V4ggU6UAzBBJGfb7bzgvoMZhOXypLhBHqc11GqVYw +6tx/7b1V7hGyYbXVAjN8vcpG6mZ9puxd3xYOBPH77lwvI9rPzhjE2OhBwHnRKbJoMHo OU00bZheIrBqRbq2LmPlXmawVSMmlysWhpsJTctjBqieCJuGXeULlf4xGCa6Eegh65ec cXIAVMGWX2pbEmt7RyOLAXByqc4CouaJiO7Rjy7BB3pkjgjv8T9rR0VKT8ZR0v8yrIp6 wbUEoJ9Ci4ASe9ej7VDL/CuZgHNNXxV/fZiZPVOJS0xOiTUYcn6oYC1UkxbBjaP5v4MB FAyg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ZNmiJed3LQ9K2TAQzrIkg8tVC5riieBoVY7FS58SXFo=; fh=lRhIsydr1jpqMMaIy+AMJxSqJxi+BMKayhkUyscAZzo=; b=TNDuV6qGCJFggNAxqH9yKC1NISEcawNZ/kZlQyte3B2oVtjL99kJ4hzvOOwNT4Y/b/ BOsHyRamzAGDdyCx2veoroMv2ZsYpKAosRJH5ElUYsnj676CQ4FltL4sTjb/fCy8nIO9 JXu0dNMAd1bYWy5ZMveyxbtTO+Fu8vD3Pv5IAz7oojPyaHM9xt7ty5VGqk1xa2adoG4H BzKqkf5uf5rlXAc6ljYX38UC0/Yo7YKLXvXdp6bkUoYEVjFCFApA+mGotak8XDKB+AMU 1h0UGcbIASUMq8oP/JQ1DkAjcFY2RH8DLidkoHhJFSqhW6T9teTlLIJSc8lD6XY0/MC0 aRgQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@crudebyte.com header.s=kylie header.b=cBINWo57; arc=pass (i=1 spf=pass spfdomain=crudebyte.com dkim=pass dkdomain=crudebyte.com dmarc=pass fromdomain=crudebyte.com); spf=pass (google.com: domain of linux-kernel+bounces-186369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186369-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=crudebyte.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6a9e3e6de11si84158216d6.42.2024.05.22.07.36.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 07:36:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@crudebyte.com header.s=kylie header.b=cBINWo57; arc=pass (i=1 spf=pass spfdomain=crudebyte.com dkim=pass dkdomain=crudebyte.com dmarc=pass fromdomain=crudebyte.com); spf=pass (google.com: domain of linux-kernel+bounces-186369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186369-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=crudebyte.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B5CED1C22DE1 for ; Wed, 22 May 2024 14:36:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B1BC1E502; Wed, 22 May 2024 14:35:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (4096-bit key) header.d=crudebyte.com header.i=@crudebyte.com header.b="cBINWo57" Received: from kylie.crudebyte.com (kylie.crudebyte.com [5.189.157.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DECC01BDD0; Wed, 22 May 2024 14:35:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.189.157.229 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716388545; cv=none; b=hmPgY9pQTcr4+MoCe49XuE26Z/oH5sc/7B6C3N2bZn1UnH699AXsZ5HR4p/g5zyHjjKM9prcATkA/EuUx+KH0JGmLK2Oyt6OH9Ql+2AMoBpahjJ95JSnd1FCIIv8925XYeGTMph5DJ+/A6wX4lEFM7TryeMAo1t4j8NXyZPLD8g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716388545; c=relaxed/simple; bh=i4rasA2acN66D6Z+ulCdlX0xWV7X3xQAZp4uOXv0umI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EC8m0sgeTSizKS0QMAJNdvkx32xLuDEUBzAOVDKaMq9YTn4/2KDzBHACpQw5ZP8i2HLHrl7xGaxVDt7SzimsRKvZ2HPreUEksduVEW5zScQsW3aUcs/jF8GXDy7fP+ZgmBmaSPd0Ep5bU6kXXXLPkobIJmbptacOl93Z+6kLTTc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=crudebyte.com; spf=pass smtp.mailfrom=crudebyte.com; dkim=pass (4096-bit key) header.d=crudebyte.com header.i=@crudebyte.com header.b=cBINWo57; arc=none smtp.client-ip=5.189.157.229 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=crudebyte.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=crudebyte.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=kylie; h=Content-Type:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Content-ID:Content-Description; bh=ZNmiJed3LQ9K2TAQzrIkg8tVC5riieBoVY7FS58SXFo=; b=cBINWo57ruofAmBa2/cgxzV4Nh BzfV0kxDMZI4si3mDluTmX8HhOs7QwGqOwRMUoVSNv9OjcKO6Lg54mV9TvR3IhPCtn6AdbvkibI4a LtKjxpyOGEW9R65efq9U7manVdmBr1Phdo5vlWBu0iEKOuouli+MqdSNkvqlgmNUfKhSwNG88GvUm h3z5gmearYVp/GXhD6xrhKf7WI2qKWnaRwJqkRZFMwFFmYdqV+OKqM6nE6IBD2DMPTeGf/6Ec6CK4 TunDQk87MaBXT8mCDFWzYssoOoFxSGM9dfz8bDKHt2WWCq9L0Ub0NXPa11oZqHsJK1IbjzW8sLBVd K74NV7XL3/Tki5kGnVZFJfbkavm+QwihTIZq2VAQPtNlXRykc2SVDO7dT/sCM0H7kz5XZkzXtRoSF iUK9FTiDQYTHNGPZOrrDmSm6xhnjWWZKi8qg1Sw1nNpRThALJIW2/uUCrn/5AATx/yVS78JYcKRtR Km5a20H3vBih284cusO0zpHuaMK9ihU5sBzfM9dSfuH5owR8cHn4kvuuPHWeE8lCQ7WIrR+tcvget v/WgrqlIugyowjZqsohLMsZKXEb1/UfoC93usUCnghyy/SjNNi7CjYvMkHZEXzVD1bHQVr4Bk9k4O r8ea0Y0XjK/tCuL+iKfDcqoYqFPF9qFT/25NazoLc=; From: Christian Schoenebeck To: Eric Van Hensbergen , Latchesar Ionkov , Dominique Martinet , Greg Kurz , Jianyong Wu , Dominique Martinet Cc: stable@vger.kernel.org, Eric Van Hensbergen , v9fs@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] 9p: add missing locking around taking dentry fid list Date: Wed, 22 May 2024 16:35:19 +0200 Message-ID: <1738699.kjPCCGL2iY@silver> In-Reply-To: <20240521122947.1080227-1-asmadeus@codewreck.org> References: <20240521122947.1080227-1-asmadeus@codewreck.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Tuesday, May 21, 2024 2:29:46 PM CEST Dominique Martinet wrote: > Fix a use-after-free on dentry's d_fsdata fid list when a thread > lookups a fid through dentry while another thread unlinks it: I guess that's "looks up". :) > UAF thread: > refcount_t: addition on 0; use-after-free. > p9_fid_get linux/./include/net/9p/client.h:262 > v9fs_fid_find+0x236/0x280 linux/fs/9p/fid.c:129 > v9fs_fid_lookup_with_uid linux/fs/9p/fid.c:181 > v9fs_fid_lookup+0xbf/0xc20 linux/fs/9p/fid.c:314 > v9fs_vfs_getattr_dotl+0xf9/0x360 linux/fs/9p/vfs_inode_dotl.c:400 > vfs_statx+0xdd/0x4d0 linux/fs/stat.c:248 > > Freed by: > p9_client_clunk+0xb0/0xe0 linux/net/9p/client.c:1456 That line number looks weird. > p9_fid_put linux/./include/net/9p/client.h:278 > v9fs_dentry_release+0xb5/0x140 linux/fs/9p/vfs_dentry.c:55 > v9fs_remove+0x38f/0x620 linux/fs/9p/vfs_inode.c:518 > vfs_unlink+0x29a/0x810 linux/fs/namei.c:4335 > > The problem is that d_fsdata was not accessed under d_lock, because > d_release() normally is only called once the dentry is otherwise no > longer accessible but since we also call it explicitly in v9fs_remove > that lock is required: > move the hlist out of the dentry under lock then unref its fids once > they are no longer accessible. > > Fixes: 154372e67d40 ("fs/9p: fix create-unlink-getattr idiom") > Cc: stable@vger.kernel.org > Reported-by: Meysam Firouzi > Reported-by: Amirmohammad Eftekhar > Signed-off-by: Dominique Martinet > --- > fs/9p/vfs_dentry.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/fs/9p/vfs_dentry.c b/fs/9p/vfs_dentry.c > index f16f73581634..01338d4c2d9e 100644 > --- a/fs/9p/vfs_dentry.c > +++ b/fs/9p/vfs_dentry.c > @@ -48,12 +48,17 @@ static int v9fs_cached_dentry_delete(const struct dentry *dentry) > static void v9fs_dentry_release(struct dentry *dentry) > { > struct hlist_node *p, *n; > + struct hlist_head head; > > p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n", > dentry, dentry); > - hlist_for_each_safe(p, n, (struct hlist_head *)&dentry->d_fsdata) > + > + spin_lock(&dentry->d_lock); > + hlist_move_list((struct hlist_head *)&dentry->d_fsdata, &head); > + spin_unlock(&dentry->d_lock); > + > + hlist_for_each_safe(p, n, &head) > p9_fid_put(hlist_entry(p, struct p9_fid, dlist)); > - dentry->d_fsdata = NULL; > } I'm not sure if that works out. So you are moving the list from dentry to a local variable. But if you look at v9fs_fid_find() [fs/9p/fid.c#123] it reads dentry->d_fsdata (twice) and holds it as local variable before taking a lock. So the lock in v9fs_fid_find() should happen earlier, no? > > static int v9fs_lookup_revalidate(struct dentry *dentry, unsigned int flags) >