Skip to content

Commit 8186fff

Browse files
committed
tracefs/eventfs: Use root and instance inodes as default ownership
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]>
1 parent 493ec81 commit 8186fff

File tree

3 files changed

+190
-90
lines changed

3 files changed

+190
-90
lines changed

fs/tracefs/event_inode.c

+77-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum {
4545
EVENTFS_SAVE_MODE = BIT(16),
4646
EVENTFS_SAVE_UID = BIT(17),
4747
EVENTFS_SAVE_GID = BIT(18),
48+
EVENTFS_TOPLEVEL = BIT(19),
4849
};
4950

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

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

147+
static void update_top_events_attr(struct eventfs_inode *ei, struct dentry *dentry)
148+
{
149+
struct inode *inode;
150+
151+
/* Only update if the "events" was on the top level */
152+
if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL))
153+
return;
154+
155+
/* Get the tracefs root inode. */
156+
inode = d_inode(dentry->d_sb->s_root);
157+
ei->attr.uid = inode->i_uid;
158+
ei->attr.gid = inode->i_gid;
159+
}
160+
161+
static void set_top_events_ownership(struct inode *inode)
162+
{
163+
struct tracefs_inode *ti = get_tracefs(inode);
164+
struct eventfs_inode *ei = ti->private;
165+
struct dentry *dentry;
166+
167+
/* The top events directory doesn't get automatically updated */
168+
if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL))
169+
return;
170+
171+
dentry = ei->dentry;
172+
173+
update_top_events_attr(ei, dentry);
174+
175+
if (!(ei->attr.mode & EVENTFS_SAVE_UID))
176+
inode->i_uid = ei->attr.uid;
177+
178+
if (!(ei->attr.mode & EVENTFS_SAVE_GID))
179+
inode->i_gid = ei->attr.gid;
180+
}
181+
182+
static int eventfs_get_attr(struct mnt_idmap *idmap,
183+
const struct path *path, struct kstat *stat,
184+
u32 request_mask, unsigned int flags)
185+
{
186+
struct dentry *dentry = path->dentry;
187+
struct inode *inode = d_backing_inode(dentry);
188+
189+
set_top_events_ownership(inode);
190+
191+
generic_fillattr(idmap, request_mask, inode, stat);
192+
return 0;
193+
}
194+
195+
static int eventfs_permission(struct mnt_idmap *idmap,
196+
struct inode *inode, int mask)
197+
{
198+
set_top_events_ownership(inode);
199+
return generic_permission(idmap, inode, mask);
200+
}
201+
139202
static const struct inode_operations eventfs_root_dir_inode_operations = {
140203
.lookup = eventfs_root_lookup,
141204
.setattr = eventfs_set_attr,
205+
.getattr = eventfs_get_attr,
206+
.permission = eventfs_permission,
142207
};
143208

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

242+
update_top_events_attr(ei, dentry);
243+
177244
return ei;
178245
}
179246

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

957+
/*
958+
* If the events directory is of the top instance, then parent
959+
* is NULL. Set the attr.mode to reflect this and its permissions will
960+
* default to the tracefs root dentry.
961+
*/
962+
if (!parent)
963+
ei->attr.mode = EVENTFS_TOPLEVEL;
964+
890965
/* This is used as the default ownership of the files and directories */
891966
ei->attr.uid = uid;
892967
ei->attr.gid = gid;

0 commit comments

Comments
 (0)