eventfs: Have event files and directories default to parent uid and gid

Dongliang reported:

I found that in the latest version, the nodes of tracefs have been
changed to dynamically created.

This has caused me to encounter a problem where the gid I specified in
the mounting parameters cannot apply to all files, as in the following
situation:

/data/tmp/events # mount | grep tracefs
tracefs on /data/tmp type tracefs (rw,seclabel,relatime,gid=3012)

gid 3012 = readtracefs

/data/tmp # ls -lh
total 0
-r--r----- 1 root readtracefs 0 1970-01-01 08:00 README
-r--r----- 1 root readtracefs 0 1970-01-01 08:00 available_events

ums9621_1h10:/data/tmp/events # ls -lh
total 0
drwxr-xr-x 2 root root 0 2023-12-19 00:56 alarmtimer
drwxr-xr-x 2 root root 0 2023-12-19 00:56 asoc

It will prevent certain applications from accessing tracefs properly, I
try to avoid this issue by making the following modifications.

To fix this, have the files created default to taking the ownership of
the parent dentry unless the ownership was previously set by the user.

Link: https://lore.kernel.org/linux-trace-kernel/1703063706-30539-1-git-send-email-dongliang.cui@unisoc.com/
Link: https://lore.kernel.org/linux-trace-kernel/20231220105017.1489d790@gandalf.local.home

Cc: stable@vger.kernel.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Hongyu Jin <hongyu.jin@unisoc.com>
Fixes: 28e12c09f5aa0 ("eventfs: Save ownership and mode")
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Reported-by: Dongliang Cui <cuidongliang390@gmail.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+9 -3
+9 -3
fs/tracefs/event_inode.c
··· 148 .release = eventfs_release, 149 }; 150 151 - static void update_inode_attr(struct inode *inode, struct eventfs_attr *attr, umode_t mode) 152 { 153 if (!attr) { 154 inode->i_mode = mode; ··· 163 164 if (attr->mode & EVENTFS_SAVE_UID) 165 inode->i_uid = attr->uid; 166 167 if (attr->mode & EVENTFS_SAVE_GID) 168 inode->i_gid = attr->gid; 169 } 170 171 /** ··· 211 return eventfs_failed_creating(dentry); 212 213 /* If the user updated the directory's attributes, use them */ 214 - update_inode_attr(inode, attr, mode); 215 216 inode->i_op = &eventfs_file_inode_operations; 217 inode->i_fop = fop; ··· 247 return eventfs_failed_creating(dentry); 248 249 /* If the user updated the directory's attributes, use them */ 250 - update_inode_attr(inode, &ei->attr, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO); 251 252 inode->i_op = &eventfs_root_dir_inode_operations; 253 inode->i_fop = &eventfs_file_operations;
··· 148 .release = eventfs_release, 149 }; 150 151 + static void update_inode_attr(struct dentry *dentry, struct inode *inode, 152 + struct eventfs_attr *attr, umode_t mode) 153 { 154 if (!attr) { 155 inode->i_mode = mode; ··· 162 163 if (attr->mode & EVENTFS_SAVE_UID) 164 inode->i_uid = attr->uid; 165 + else 166 + inode->i_uid = d_inode(dentry->d_parent)->i_uid; 167 168 if (attr->mode & EVENTFS_SAVE_GID) 169 inode->i_gid = attr->gid; 170 + else 171 + inode->i_gid = d_inode(dentry->d_parent)->i_gid; 172 } 173 174 /** ··· 206 return eventfs_failed_creating(dentry); 207 208 /* If the user updated the directory's attributes, use them */ 209 + update_inode_attr(dentry, inode, attr, mode); 210 211 inode->i_op = &eventfs_file_inode_operations; 212 inode->i_fop = fop; ··· 242 return eventfs_failed_creating(dentry); 243 244 /* If the user updated the directory's attributes, use them */ 245 + update_inode_attr(dentry, inode, &ei->attr, 246 + S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO); 247 248 inode->i_op = &eventfs_root_dir_inode_operations; 249 inode->i_fop = &eventfs_file_operations;