Re: [PATCH v2] Convert struct pid count to refcount_t
From: Joel Fernandes
Date: Mon Jul 01 2019 - 14:25:24 EST
On Mon, Jul 01, 2019 at 07:48:26PM +0200, Jann Horn wrote:
> 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:
Thanks. Andrew had fixed this in patch v1 but Linus dropped it for other
reasons. Anyway, I should have fixed this in my resubmit.
Sorry, I'll fix and resend!
- Joel