Re: [PATCH v2] Convert struct pid count to refcount_t
From: Jann Horn
Date: Mon Jul 01 2019 - 13:48:55 EST
On Fri, Jun 28, 2019 at 9:35 PM Joel Fernandes (Google)
<joel@xxxxxxxxxxxxxxxxx> wrote:
> struct pid's count is an atomic_t field used as a refcount. Use
> refcount_t for it which is basically atomic_t but does additional
> checking to prevent use-after-free bugs.
[...]
> struct pid
> {
> - atomic_t count;
> + refcount_t count;
[...]
> diff --git a/kernel/pid.c b/kernel/pid.c
> index 20881598bdfa..89c4849fab5d 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -37,7 +37,7 @@
> #include <linux/init_task.h>
> #include <linux/syscalls.h>
> #include <linux/proc_ns.h>
> -#include <linux/proc_fs.h>
> +#include <linux/refcount.h>
> #include <linux/sched/task.h>
> #include <linux/idr.h>
>
> @@ -106,8 +106,7 @@ void put_pid(struct pid *pid)
init_struct_pid is defined as follows:
struct pid init_struct_pid = {
.count = ATOMIC_INIT(1),
[...]
};
This should be changed to REFCOUNT_INIT(1).
You should have received a compiler warning about this; I get the
following when trying to build with your patch applied:
jannh@jannh2:~/git/foreign/linux$ make kernel/pid.o
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CC kernel/pid.o
kernel/pid.c:44:30: warning: missing braces around initializer
[-Wmissing-braces]
struct pid init_struct_pid = {
^
kernel/pid.c:44:30: warning: missing braces around initializer
[-Wmissing-braces]
kernel/pid.c:44:30: warning: missing braces around initializer
[-Wmissing-braces]
kernel/pid.c:44:30: warning: missing braces around initializer
[-Wmissing-braces]
kernel/pid.c:44:30: warning: missing braces around initializer
[-Wmissing-braces]