2022-09-23 12:06:00

by Ming Lei

[permalink] [raw]
Subject: [PATCH] cgroup: fix cgroup_get_from_id

cgroup has to be one kernfs dir, otherwise kernel panic is caused,
especially cgroup id is provide from userspace.

Reported-by: Marco Patalano <[email protected]>
Fixes: 6b658c4863c1 ("scsi: cgroup: Add cgroup_get_from_id()")
Cc: Muneendra <[email protected]>
Signed-off-by: Ming Lei <[email protected]>
---
kernel/cgroup/cgroup.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index e4bb5d57f4d1..5f2090d051ac 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6049,6 +6049,9 @@ struct cgroup *cgroup_get_from_id(u64 id)
if (!kn)
goto out;

+ if (kernfs_type(kn) != KERNFS_DIR)
+ goto put;
+
rcu_read_lock();

cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
@@ -6056,7 +6059,7 @@ struct cgroup *cgroup_get_from_id(u64 id)
cgrp = NULL;

rcu_read_unlock();
-
+put:
kernfs_put(kn);
out:
return cgrp;
--
2.31.1


2022-09-23 16:06:36

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix cgroup_get_from_id

Hi,

On 9/23/2022 5:21 PM, Ming Lei wrote:
> cgroup has to be one kernfs dir, otherwise kernel panic is caused,
> especially cgroup id is provide from userspace.
>
> Reported-by: Marco Patalano <[email protected]>
> Fixes: 6b658c4863c1 ("scsi: cgroup: Add cgroup_get_from_id()")
> Cc: Muneendra <[email protected]>
> Signed-off-by: Ming Lei <[email protected]>
> ---
> kernel/cgroup/cgroup.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index e4bb5d57f4d1..5f2090d051ac 100644
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c
> @@ -6049,6 +6049,9 @@ struct cgroup *cgroup_get_from_id(u64 id)
> if (!kn)
> goto out;
>
> + if (kernfs_type(kn) != KERNFS_DIR)
> + goto put;
> +
> rcu_read_lock();
>
> cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
> @@ -6056,7 +6059,7 @@ struct cgroup *cgroup_get_from_id(u64 id)
> cgrp = NULL;
>
> rcu_read_unlock();
> -
> +put:
> kernfs_put(kn);
> out:
> return cgrp;

Good catch.

Acked-by: Mukesh Ojha <[email protected]>

-Mukesh

2022-09-23 17:59:25

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix cgroup_get_from_id

On Fri, Sep 23, 2022 at 07:51:19PM +0800, Ming Lei wrote:
> cgroup has to be one kernfs dir, otherwise kernel panic is caused,
> especially cgroup id is provide from userspace.
>
> Reported-by: Marco Patalano <[email protected]>
> Fixes: 6b658c4863c1 ("scsi: cgroup: Add cgroup_get_from_id()")
> Cc: Muneendra <[email protected]>
> Signed-off-by: Ming Lei <[email protected]>

Applied to cgroup/for-6.0-fixes with the subject changed to "cgroup:
cgroup_get_from_id() must check the looked-up kn is a directory" and stable
cc'd.

Thanks.

--
tejun