Skip to content

Commit fe3de01

Browse files
MaxKellermanngregkh
authored andcommitted
kernel/cgroup: use kernfs_create_dir_ns()
By passing the fsugid to kernfs_create_dir_ns(), we don't need cgroup_kn_set_ugid() any longer. That function was added for exactly this purpose by commit 49957f8 ("cgroup: newly created dirs and files should be owned by the creator"). Eliminating this piece of duplicate code means we benefit from future improvements to kernfs_create_dir_ns(); for example, both are lacking S_ISGID support currently, which my next patch will add to kernfs_create_dir_ns(). It cannot (easily) be added to cgroup_kn_set_ugid() because we can't dereference struct kernfs_iattrs from there. -- v1 -> v2: 12-digit commit id Signed-off-by: Max Kellermann <max.kellermann@ionos.com> Acked-by: Tejun Heo <tj@kernel.org> Link: https://lore.kernel.org/r/20231208093310.297233-1-max.kellermann@ionos.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7c23fb2 commit fe3de01

1 file changed

Lines changed: 4 additions & 27 deletions

File tree

kernel/cgroup/cgroup.c

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4169,20 +4169,6 @@ static struct kernfs_ops cgroup_kf_ops = {
41694169
.seq_show = cgroup_seqfile_show,
41704170
};
41714171

4172-
/* set uid and gid of cgroup dirs and files to that of the creator */
4173-
static int cgroup_kn_set_ugid(struct kernfs_node *kn)
4174-
{
4175-
struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
4176-
.ia_uid = current_fsuid(),
4177-
.ia_gid = current_fsgid(), };
4178-
4179-
if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
4180-
gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
4181-
return 0;
4182-
4183-
return kernfs_setattr(kn, &iattr);
4184-
}
4185-
41864172
static void cgroup_file_notify_timer(struct timer_list *timer)
41874173
{
41884174
cgroup_file_notify(container_of(timer, struct cgroup_file,
@@ -4195,25 +4181,18 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
41954181
char name[CGROUP_FILE_NAME_MAX];
41964182
struct kernfs_node *kn;
41974183
struct lock_class_key *key = NULL;
4198-
int ret;
41994184

42004185
#ifdef CONFIG_DEBUG_LOCK_ALLOC
42014186
key = &cft->lockdep_key;
42024187
#endif
42034188
kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
42044189
cgroup_file_mode(cft),
4205-
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
4190+
current_fsuid(), current_fsgid(),
42064191
0, cft->kf_ops, cft,
42074192
NULL, key);
42084193
if (IS_ERR(kn))
42094194
return PTR_ERR(kn);
42104195

4211-
ret = cgroup_kn_set_ugid(kn);
4212-
if (ret) {
4213-
kernfs_remove(kn);
4214-
return ret;
4215-
}
4216-
42174196
if (cft->file_offset) {
42184197
struct cgroup_file *cfile = (void *)css + cft->file_offset;
42194198

@@ -5616,7 +5595,9 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
56165595
goto out_cancel_ref;
56175596

56185597
/* create the directory */
5619-
kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
5598+
kn = kernfs_create_dir_ns(parent->kn, name, mode,
5599+
current_fsuid(), current_fsgid(),
5600+
cgrp, NULL);
56205601
if (IS_ERR(kn)) {
56215602
ret = PTR_ERR(kn);
56225603
goto out_stat_exit;
@@ -5761,10 +5742,6 @@ int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
57615742
*/
57625743
kernfs_get(cgrp->kn);
57635744

5764-
ret = cgroup_kn_set_ugid(cgrp->kn);
5765-
if (ret)
5766-
goto out_destroy;
5767-
57685745
ret = css_populate_dir(&cgrp->self);
57695746
if (ret)
57705747
goto out_destroy;

0 commit comments

Comments
 (0)