From patchwork Wed Jan 3 01:50:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 184556 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4783833dyb; Tue, 2 Jan 2024 17:52:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDmeIdD2d6uoU/J77KPvGl7D+TZBuf/5WHQ9/spSTW2OivpFDFRC6MgauYDg9w8siKENsv X-Received: by 2002:a05:6808:1984:b0:3bc:10bb:f40b with SMTP id bj4-20020a056808198400b003bc10bbf40bmr3416690oib.107.1704246722462; Tue, 02 Jan 2024 17:52:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704246722; cv=none; d=google.com; s=arc-20160816; b=gddz7YsrYgTJIge29fxZQ+N9jqXsHnfwqEnD10aWJBmsh4BX1nN8NYP2GTKjQRJi/e T8ULOUoI4NPDkjPyOamzbokkR4sGaeAVd3Xc4xVAknckfKirRGgoJfpUzGoL4jdsgsXr QJYTJlCZAlLOnCHi/fTdlOnJTxXvEn7lSoqx2wZkqjkZTivH9XfdNEXHmAObVm4Dncrw MZgzavhtkeHJGsqc2ULj4qorxg1Zo9e/raH6ve2ILsJ6rGAWA0YDooKkckhFpkj8b9h2 W86i9ZYljMZiw3UuK93GbVMkViYAWdy2DqEJ91hvJefYR9tfNTdF3mDJ9i9W5U8WAvJi ojdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=2Egp9jcn20cPhbiyXLQ19G6ylZ28lkPdxCiU8GCxBxQ=; fh=M+Y3U2/c/DNCLJKUpdT2m/LZ+fYXtiCnkl7DR0Ch7Vg=; b=FtlwIKEkhvCheatGmXj8plESP5OjMbMNB6FMEkfvL91jJXQIS6dXnG+w53E9SzscEt Q+KI+xKhOV7VODzIhfznMaFcl4mFf/sX6zesxAyyvxIVw/qvh8rmIhD2Zwxh2cXoyOYr AuMmlfPDe75ydu+tij6YCbA5SFVcvgqafHewvT6uxPI9UnOg45NH20MvTFOYwcTzIwjm 3KlPZdJ5pcNrgoOdGrLOID/VWea6ny6yEKBbg8myV8RSEtpV9meRVA8/JhTR+yJ6M5tQ +CYCnoNE2J4koT36C/rsHLAQ800IKjKbqw8x9UtRo2JdBUAuLaaO4vC0zOoxI+TTagPv gxVA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-15049-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15049-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id cj20-20020a056a00299400b006d9b31e9640si15976525pfb.90.2024.01.02.17.52.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 17:52:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15049-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-15049-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15049-ouuuleilei=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 39BBE283EDC for ; Wed, 3 Jan 2024 01:52:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B31310A32; Wed, 3 Jan 2024 01:51:28 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9D01617984; Wed, 3 Jan 2024 01:51:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40330C433C7; Wed, 3 Jan 2024 01:51:26 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rKqQm-00000000k5m-1xzs; Tue, 02 Jan 2024 20:52:28 -0500 Message-ID: <20240103015228.329181041@goodmis.org> User-Agent: quilt/0.67 Date: Tue, 02 Jan 2024 20:50:44 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org, "Ubisectech Sirius" Subject: [for-linus][PATCH 1/2] tracefs: Check for dentry->d_inode exists in set_gid() References: <20240103015043.912131206@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787032211208340067 X-GMAIL-MSGID: 1787032211208340067 From: "Steven Rostedt (Google)" If a getdents() is called on the tracefs directory but does not get all the files, it can leave a "cursor" dentry in the d_subdirs list of tracefs dentry. This cursor dentry does not have a d_inode for it. Before referencing tracefs_inode from the dentry, the d_inode must first be checked if it has content. If not, then it's not a tracefs_inode and can be ignored. The following caused a crash: #define getdents64(fd, dirp, count) syscall(SYS_getdents64, fd, dirp, count) #define BUF_SIZE 256 #define TDIR "/tmp/file0" int main(void) { char buf[BUF_SIZE]; int fd; int n; mkdir(TDIR, 0777); mount(NULL, TDIR, "tracefs", 0, NULL); fd = openat(AT_FDCWD, TDIR, O_RDONLY); n = getdents64(fd, buf, BUF_SIZE); ret = mount(NULL, TDIR, NULL, MS_NOSUID|MS_REMOUNT|MS_RELATIME|MS_LAZYTIME, "gid=1000"); return 0; } That's because the 256 BUF_SIZE was not big enough to read all the dentries of the tracefs file system and it left a "cursor" dentry in the subdirs of the tracefs root inode. Then on remounting with "gid=1000", it would cause an iteration of all dentries which hit: ti = get_tracefs(dentry->d_inode); if (ti && (ti->flags & TRACEFS_EVENT_INODE)) eventfs_update_gid(dentry, gid); Which crashed because of the dereference of the cursor dentry which had a NULL d_inode. In the subdir loop of the dentry lookup of set_gid(), if a child has a NULL d_inode, simply skip it. Link: https://lore.kernel.org/all/20240102135637.3a21fb10@gandalf.local.home/ Link: https://lore.kernel.org/linux-trace-kernel/20240102151249.05da244d@gandalf.local.home Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Fixes: 7e8358edf503e ("eventfs: Fix file and directory uid and gid ownership") Reported-by: "Ubisectech Sirius" Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/inode.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 62524b20964e..bc86ffdb103b 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -215,6 +215,10 @@ static void set_gid(struct dentry *parent, kgid_t gid) struct dentry *dentry = list_entry(tmp, struct dentry, d_child); next = tmp->next; + /* Note, getdents() can add a cursor dentry with no inode */ + if (!dentry->d_inode) + continue; + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); change_gid(dentry, gid);