From patchwork Tue Jan 2 20:12:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 184441 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4661134dyb; Tue, 2 Jan 2024 12:12:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuGVU5n4qDkpQ5vBBaOTXArJFi5JQS+OZzei/x5VekyMnzE5niM6Vw9jnE8MVH77vPQJ4F X-Received: by 2002:a17:902:6505:b0:1d3:f1ca:5acc with SMTP id b5-20020a170902650500b001d3f1ca5accmr15653023plk.15.1704226323160; Tue, 02 Jan 2024 12:12:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704226323; cv=none; d=google.com; s=arc-20160816; b=u95dzeKd7RR6/PfohWDZrQdn4vJZkfwszM9cCke09IdqZOaBIAI+nWwEClTWKgJfzi 0JmM3+3aiui6bpyUEh5ACo/+aYF0cwWrfHoCs+CYwsAUd/FsyiuYKwRsP09kcz5Zk2Re AmepeCzUSOEqcbOKQDc/i6P7Va8z9LkYYTZ6j0OECH1e57+eOlQze0imZLHEGqfm2I5b ImngGa0c0QfVKdo+1qLgbPh1LmUvPJjD4xBfwOI76djWkIIhKkWIp+05HoReclJBYPFg iMxqD8k1XJbeiTLGSU4EnoREBOl0b1c1isej54rET8mVwFFP3SK/uyNjvs4NBnGwzVnb r/1A== ARC-Message-Signature: i=1; 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:message-id:subject:cc:to:from :date; bh=l5sMlNoLRTmdCd7vjzSsf2QdMn+6jK07kKY3+DdLEkw=; fh=sAx3v3Km8G9H/cb+FD4m/z+XEp+4ebn6tFwpvmH7e8s=; b=LZ2KkNRv+dGLMPFNugelbeZ8PEBZW1Bbod4UMmMgoMmnuAFBEppcfWl1RbYtimK3cr UsBjRLOowpij3cffwZIpZhpQK2dMt2cd7+PFXoxhAPPFYpy/po2BHSW8+Fo+fLb5zwx8 4iZLyR84hQ6qz/llPTWlm2bKXznB6cjtNIZRNnGTQotde76MNuFJC4fS6xvw0GXZrnSC cJjQOpIY+AYdvqK9gNAG9LSizlGxeGvDyL8IS2u3oo/FRCpewklSGPrp8wPs1Aiojlh6 5RSJOfMnoCw5KNWbJCIqoWZKIzbDrVW0BOKio0k/ZjLaLpxzl9rlH7/E4I2zZgHzhBPp t48g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14795-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14795-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b6-20020a170902d50600b001d406fd0f22si12106949plg.423.2024.01.02.12.12.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 12:12:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14795-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14795-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14795-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 3009B2847EA for ; Tue, 2 Jan 2024 20:12:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A09016421; Tue, 2 Jan 2024 20:11:51 +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 3517516404; Tue, 2 Jan 2024 20:11:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA8F3C433C7; Tue, 2 Jan 2024 20:11:48 +0000 (UTC) Date: Tue, 2 Jan 2024 15:12:49 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , "Ubisectech Sirius" Subject: [PATCH v2] tracefs: Check for dentry->d_inode exists in set_gid() Message-ID: <20240102151249.05da244d@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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: 1787007696050587549 X-GMAIL-MSGID: 1787010821162348281 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/ Cc: stable@vger.kernel.org Fixes: 7e8358edf503e ("eventfs: Fix file and directory uid and gid ownership") Reported-by: "Ubisectech Sirius" Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240102142311.5670813d@gandalf.local.home - Simplify the logic to just continue the loop if the cursor dentry is hit. 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);