tracefs: Check for dentry->d_inode exists in set_gid()
Steven Rostedt
maintainerPATCH v2 1/1 tracefs: Check for dentry->d_inode exists in set_gid()
From: "Steven Rostedt (Google)" <[email protected]>
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.
---
Changes since v1: https://lore.kernel.org/linux-trace-kernel/[email protected]
- Simplify the logic to just continue the loop if the cursor dentry is hit.
fs/tracefs/inode.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/tracefs/inode.c +4 -0
@@ -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);
Signature · 1 line
2.42.0