Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp398909rdb; Mon, 22 Jan 2024 07:36:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHceHbQTp7VXR4zlMnn1g3aOB3YqomGdqSIa5GweEbi/m50cnEYxbVaB/Vvso/GiG2ePSKn X-Received: by 2002:a37:ef12:0:b0:781:56a7:3841 with SMTP id j18-20020a37ef12000000b0078156a73841mr5375254qkk.77.1705937801614; Mon, 22 Jan 2024 07:36:41 -0800 (PST) Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id vr10-20020a05620a55aa00b007835a97fa96si5398174qkn.528.2024.01.22.07.36.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 07:36:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33329-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; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-33329-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33329-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 229271C22739 for ; Mon, 22 Jan 2024 15:36:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D22F260EE3; Mon, 22 Jan 2024 15:05:04 +0000 (UTC) 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 34B373D981 for ; Mon, 22 Jan 2024 15:05:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705935904; cv=none; b=JPdJM5Q/INdC1bbUqWO+ZF1LEQFPzS9nH4dZrS22XeA7tD7iFjGU4vi4Q9HLdOrk72vZolJHJ22TOs2GYsYwGlzvPmKIJcxL1FzOCF2gtoskP9DoGf/0XClV1LLRLlWiBZI54PHzhjheagiy31Hak1vnviun755jVOftMG1Koec= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705935904; c=relaxed/simple; bh=3lfCvPhZrkU82LwCUZZgALjuBUjL/R+aLt6h+1xcMls=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Gvd33FCeaqTCnOSsGjwpE2BuGB1pItdJRenCphGFZXlRm0ZXf9oKo9fdPHlEfn1cjnPpofUlMr1kRlqRbDrcNUuizcIHh6vmatmM6Q8VzVoBB4uOvHKfCWUFOi8XkOQfUOZcVuy+SbGkBP8Ke82wIPJkdhMvohWGEI+vuq90ql4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2F17C43390; Mon, 22 Jan 2024 15:05:02 +0000 (UTC) Date: Mon, 22 Jan 2024 10:06:30 -0500 From: Steven Rostedt To: Geert Uytterhoeven , Kees Cook Cc: linux-kernel@vger.kernel.org, Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Christian Brauner , Al Viro , Ajay Kaher , Linus Torvalds Subject: Re: [for-linus][PATCH 1/3] eventfs: Have the inodes all for files and directories all be the same Message-ID: <20240122100630.6a400dd3@gandalf.local.home> In-Reply-To: References: <20240117143548.595884070@goodmis.org> <20240117143810.531966508@goodmis.org> 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, 22 Jan 2024 11:38:52 +0100 Geert Uytterhoeven wrote: > Hi Stephen, I don't know who "Stephen" is, but I'll reply to this message. >=20 > On Wed, Jan 17, 2024 at 3:37=E2=80=AFPM Steven Rostedt wrote: > > From: "Steven Rostedt (Google)" > > > > The dentries and inodes are created in the readdir for the sole purpose= of > > getting a consistent inode number. Linus stated that is unnecessary, and > > that all inodes can have the same inode number. For a virtual file syst= em > > they are pretty meaningless. > > > > Instead use a single unique inode number for all files and one for all > > directories. > > > > Link: https://lore.kernel.org/all/20240116133753.2808d45e@gandalf.local= home/ Yeah, Linus wanted me to try this first and see if there's any regressions. Well, I guess you just answered that. The above link has me saying to Linus: It was me being paranoid that using the same inode number would break user space. If that is not a concern, then I'm happy to just make it either the same, or maybe just hash the ei and name that it is associated with. > > Link: https://lore.kernel.org/linux-trace-kernel/20240116211353.4121803= 63@goodmis.org > > > > Cc: Masami Hiramatsu > > Cc: Mark Rutland > > Cc: Mathieu Desnoyers > > Cc: Christian Brauner > > Cc: Al Viro > > Cc: Ajay Kaher > > Suggested-by: Linus Torvalds > > Signed-off-by: Steven Rostedt (Google) =20 >=20 > Thanks for your patch, which is now commit 53c41052ba312176 ("eventfs: > Have the inodes all for files and directories all be the same") in > v6.8-rc1, to which I have bisected the issue below. >=20 > > --- a/fs/tracefs/event_inode.c > > +++ b/fs/tracefs/event_inode.c > > @@ -32,6 +32,10 @@ > > */ > > static DEFINE_MUTEX(eventfs_mutex); > > > > +/* Choose something "unique" ;-) */ > > +#define EVENTFS_FILE_INODE_INO 0x12c4e37 > > +#define EVENTFS_DIR_INODE_INO 0x134b2f5 > > + > > /* > > * The eventfs_inode (ei) itself is protected by SRCU. It is released = from > > * its parent's list and will have is_freed set (under eventfs_mutex). > > @@ -352,6 +356,9 @@ static struct dentry *create_file(const char *name,= umode_t mode, > > inode->i_fop =3D fop; > > inode->i_private =3D data; > > > > + /* All files will have the same inode number */ > > + inode->i_ino =3D EVENTFS_FILE_INODE_INO; > > + > > ti =3D get_tracefs(inode); > > ti->flags |=3D TRACEFS_EVENT_INODE; > > d_instantiate(dentry, inode); > > @@ -388,6 +395,9 @@ static struct dentry *create_dir(struct eventfs_ino= de *ei, struct dentry *parent > > inode->i_op =3D &eventfs_root_dir_inode_operations; > > inode->i_fop =3D &eventfs_file_operations; > > > > + /* All directories will have the same inode number */ > > + inode->i_ino =3D EVENTFS_DIR_INODE_INO; > > + > > ti =3D get_tracefs(inode); > > ti->flags |=3D TRACEFS_EVENT_INODE; =20 >=20 > This confuses "find". > Running "find /sys/" now prints lots of error messages to stderr: >=20 > find: File system loop detected; > =E2=80=98/sys/kernel/debug/tracing/events/initcall/initcall_finish=E2=80= =99 is part of > the same file system loop as > =E2=80=98/sys/kernel/debug/tracing/events/initcall=E2=80=99. So at a minimum, the directories need to have unique inode numbers. > find: File system loop detected; > =E2=80=98/sys/kernel/debug/tracing/events/initcall/initcall_start=E2=80= =99 is part of > the same file system loop as > =E2=80=98/sys/kernel/debug/tracing/events/initcall=E2=80=99. > find: File system loop detected; > =E2=80=98/sys/kernel/debug/tracing/events/initcall/initcall_level=E2=80= =99 is part of > the same file system loop as > =E2=80=98/sys/kernel/debug/tracing/events/initcall=E2=80=99. > [...] Does this fix it for you? It hashes the eventfs_inode data structure after adding some salt to it. Kees,=20 I'm using the eventfs_inode pointer to create a unique value for the inode. But it's being salted, hashed and then truncated. As it is very easy to read inodes (although by default, only root has access to read these inodes), the inode numbers themselves shouldn't be able to leak kernel addresses via the results of these inode numbers, would it? -- Steve diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 6795fda2af19..d54897b84596 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,31 @@ static DEFINE_MUTEX(eventfs_mutex); #define EVENTFS_FILE_INODE_INO 0x12c4e37 #define EVENTFS_DIR_INODE_INO 0x134b2f5 =20 +/* Used for making inode numbers */ +static siphash_key_t inode_key; + +/* Copied from scripts/kconfig/symbol.c */ +static unsigned strhash(const char *s) +{ + /* fnv32 hash */ + unsigned hash =3D 2166136261U; + for (; *s; s++) + hash =3D (hash ^ *s) * 0x01000193; + return hash; +} + +/* Just try to make something consistent and unique */ +static int eventfs_dir_ino(struct event_inode *ei, const char *name) +{ + unsigned long sip =3D (unsigned long)ei; + + sip +=3D strhash(name) + EVENTFS_DIR_INODE_INO; + sip =3D siphash_1u32((int)sip, &inode_key); + + /* keep it positive */ + return sip & ((1U << 31) - 1); +} + /* * The eventfs_inode (ei) itself is protected by SRCU. It is released from * its parent's list and will have is_freed set (under eventfs_mutex). @@ -396,7 +422,7 @@ static struct dentry *create_dir(struct eventfs_inode *= ei, struct dentry *parent inode->i_fop =3D &eventfs_file_operations; =20 /* All directories will have the same inode number */ - inode->i_ino =3D EVENTFS_DIR_INODE_INO; + inode->i_ino =3D eventfs_dir_ino(ei, ei->name); =20 ti =3D get_tracefs(inode); ti->flags |=3D TRACEFS_EVENT_INODE; @@ -802,7 +828,7 @@ static int eventfs_iterate(struct file *file, struct di= r_context *ctx) =20 name =3D ei_child->name; =20 - ino =3D EVENTFS_DIR_INODE_INO; + ino =3D eventfs_dir_ino(ei_child, name); =20 if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) goto out_dec; @@ -932,6 +958,9 @@ struct eventfs_inode *eventfs_create_events_dir(const c= har *name, struct dentry if (IS_ERR(dentry)) return ERR_CAST(dentry); =20 + if (siphash_key_is_zero(&inode_key)) + get_random_bytes(&inode_key, sizeof(inode_key)); + ei =3D kzalloc(sizeof(*ei), GFP_KERNEL); if (!ei) goto fail_ei;