Skip to content

Commit

Permalink
fs/kernfs/dir: obey S_ISGID
Browse files Browse the repository at this point in the history
Handling of S_ISGID is usually done by inode_init_owner() in all other
filesystems, but kernfs doesn't use that function.  In kernfs, struct
kernfs_node is the primary data structure, and struct inode is only
created from it on demand.  Therefore, inode_init_owner() can't be
used and we need to imitate its behavior.

S_ISGID support is useful for the cgroup filesystem; it allows
subtrees managed by an unprivileged process to retain a certain owner
gid, which then enables sharing access to the subtree with another
unprivileged process.

--
v1 -> v2: minor coding style fix (comment)

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20231208093310.297233-2-max.kellermann@ionos.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Max Kellermann authored and Greg Kroah-Hartman committed Dec 15, 2023
1 parent fe3de01 commit 5133bee
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fs/kernfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,18 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
{
struct kernfs_node *kn;

if (parent->mode & S_ISGID) {
/* this code block imitates inode_init_owner() for
* kernfs
*/

if (parent->iattr)
gid = parent->iattr->ia_gid;

if (flags & KERNFS_DIR)
mode |= S_ISGID;
}

kn = __kernfs_new_node(kernfs_root(parent), parent,
name, mode, uid, gid, flags);
if (kn) {
Expand Down

0 comments on commit 5133bee

Please sign in to comment.