Skip to content

Commit

Permalink
tracefs/eventfs: Use root and instance inodes as default ownership
Browse files Browse the repository at this point in the history
Instead of walking the dentries on mount/remount to update the gid values of
all the dentries if a gid option is specified on mount, just update the root
inode. Add .getattr, .setattr, and .permissions on the tracefs inode
operations to update the permissions of the files and directories.

For all files and directories in the top level instance:

 /sys/kernel/tracing/*

It will use the root inode as the default permissions. The inode that
represents: /sys/kernel/tracing (or wherever it is mounted).

When an instance is created:

 mkdir /sys/kernel/tracing/instance/foo

The directory "foo" and all its files and directories underneath will use
the default of what foo is when it was created. A remount of tracefs will
not affect it.

If a user were to modify the permissions of any file or directory in
tracefs, it will also no longer be modified by a change in ownership of a
remount.

The events directory, if it is in the top level instance, will use the
tracefs root inode as the default ownership for itself and all the files and
directories below it.

For the events directory in an instance ("foo"), it will keep the ownership
of what it was when it was created, and that will be used as the default
ownership for the files and directories beneath it.

Link: https://lore.kernel.org/linux-trace-kernel/CAHk-=wjVdGkjDXBbvLn2wbZnqP4UsH46E3gqJ9m7UG6DpX2+WA@mail.gmail.com/
Link: https://lore.kernel.org/linux-trace-kernel/[email protected]

Cc: Masami Hiramatsu <[email protected]>
Cc: Mathieu Desnoyers <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Steven Rostedt (Google) <[email protected]>
  • Loading branch information
rostedt committed Jan 4, 2024
1 parent 493ec81 commit 8186fff
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 90 deletions.
79 changes: 77 additions & 2 deletions fs/tracefs/event_inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum {
EVENTFS_SAVE_MODE = BIT(16),
EVENTFS_SAVE_UID = BIT(17),
EVENTFS_SAVE_GID = BIT(18),
EVENTFS_TOPLEVEL = BIT(19),
};

#define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
Expand Down Expand Up @@ -115,10 +116,17 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
* The events directory dentry is never freed, unless its
* part of an instance that is deleted. It's attr is the
* default for its child files and directories.
* Do not update it. It's not used for its own mode or ownership
* Do not update it. It's not used for its own mode or ownership.
*/
if (!ei->is_events)
if (ei->is_events) {
/* But it still needs to know if it was modified */
if (iattr->ia_valid & ATTR_UID)
ei->attr.mode |= EVENTFS_SAVE_UID;
if (iattr->ia_valid & ATTR_GID)
ei->attr.mode |= EVENTFS_SAVE_GID;
} else {
update_attr(&ei->attr, iattr);
}

} else {
name = dentry->d_name.name;
Expand All @@ -136,9 +144,66 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
return ret;
}

static void update_top_events_attr(struct eventfs_inode *ei, struct dentry *dentry)
{
struct inode *inode;

/* Only update if the "events" was on the top level */
if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
return;

/* Get the tracefs root inode. */
inode = d_inode(dentry->d_sb->s_root);
ei->attr.uid = inode->i_uid;
ei->attr.gid = inode->i_gid;
}

static void set_top_events_ownership(struct inode *inode)
{
struct tracefs_inode *ti = get_tracefs(inode);
struct eventfs_inode *ei = ti->private;
struct dentry *dentry;

/* The top events directory doesn't get automatically updated */
if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
return;

dentry = ei->dentry;

update_top_events_attr(ei, dentry);

if (!(ei->attr.mode & EVENTFS_SAVE_UID))
inode->i_uid = ei->attr.uid;

if (!(ei->attr.mode & EVENTFS_SAVE_GID))
inode->i_gid = ei->attr.gid;
}

static int eventfs_get_attr(struct mnt_idmap *idmap,
const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int flags)
{
struct dentry *dentry = path->dentry;
struct inode *inode = d_backing_inode(dentry);

set_top_events_ownership(inode);

generic_fillattr(idmap, request_mask, inode, stat);
return 0;
}

static int eventfs_permission(struct mnt_idmap *idmap,
struct inode *inode, int mask)
{
set_top_events_ownership(inode);
return generic_permission(idmap, inode, mask);
}

static const struct inode_operations eventfs_root_dir_inode_operations = {
.lookup = eventfs_root_lookup,
.setattr = eventfs_set_attr,
.getattr = eventfs_get_attr,
.permission = eventfs_permission,
};

static const struct inode_operations eventfs_file_inode_operations = {
Expand Down Expand Up @@ -174,6 +239,8 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)
} while (!ei->is_events);
mutex_unlock(&eventfs_mutex);

update_top_events_attr(ei, dentry);

return ei;
}

Expand Down Expand Up @@ -887,6 +954,14 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry
uid = d_inode(dentry->d_parent)->i_uid;
gid = d_inode(dentry->d_parent)->i_gid;

/*
* If the events directory is of the top instance, then parent
* is NULL. Set the attr.mode to reflect this and its permissions will
* default to the tracefs root dentry.
*/
if (!parent)
ei->attr.mode = EVENTFS_TOPLEVEL;

/* This is used as the default ownership of the files and directories */
ei->attr.uid = uid;
ei->attr.gid = gid;
Expand Down
Loading

0 comments on commit 8186fff

Please sign in to comment.