* [PATCH v2] Convert struct pid count to refcount_t
@ 2019-06-28 19:34 Joel Fernandes (Google)
2019-06-28 19:48 ` Kees Cook
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Joel Fernandes (Google) @ 2019-06-28 19:34 UTC (permalink / raw)
To: linux-kernel
Cc: Joel Fernandes (Google),
mathieu.desnoyers, willy, peterz, will.deacon, paulmck,
elena.reshetova, keescook, kernel-team, kernel-hardening,
Andrew Morton, Eric W. Biederman, Michal Hocko, Oleg Nesterov,
Stephen Rothwell
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.
For memory ordering, the only change is with the following:
- if ((atomic_read(&pid->count) == 1) ||
- atomic_dec_and_test(&pid->count)) {
+ if (refcount_dec_and_test(&pid->count)) {
kmem_cache_free(ns->pid_cachep, pid);
Here the change is from:
Fully ordered --> RELEASE + ACQUIRE (as per refcount-vs-atomic.rst)
This ACQUIRE should take care of making sure the free happens after the
refcount_dec_and_test().
The above hunk also removes atomic_read() since it is not needed for the
code to work and it is unclear how beneficial it is. The removal lets
refcount_dec_and_test() check for cases where get_pid() happened before
the object was freed.
Cc: mathieu.desnoyers@efficios.com
Cc: willy@infradead.org
Cc: peterz@infradead.org
Cc: will.deacon@arm.com
Cc: paulmck@linux.vnet.ibm.com
Cc: elena.reshetova@intel.com
Cc: keescook@chromium.org
Cc: kernel-team@android.com
Cc: kernel-hardening@lists.openwall.com
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
Only change from v1->v2 is to get rid of the atomic_read().
include/linux/pid.h | 5 +++--
kernel/pid.c | 7 +++----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/include/linux/pid.h b/include/linux/pid.h
index 14a9a39da9c7..8cb86d377ff5 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -3,6 +3,7 @@
#define _LINUX_PID_H
#include <linux/rculist.h>
+#include <linux/refcount.h>
enum pid_type
{
@@ -56,7 +57,7 @@ struct upid {
struct pid
{
- atomic_t count;
+ refcount_t count;
unsigned int level;
/* lists of tasks that use this pid */
struct hlist_head tasks[PIDTYPE_MAX];
@@ -69,7 +70,7 @@ extern struct pid init_struct_pid;
static inline struct pid *get_pid(struct pid *pid)
{
if (pid)
- atomic_inc(&pid->count);
+ refcount_inc(&pid->count);
return pid;
}
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)
return;
ns = pid->numbers[pid->level].ns;
- if ((atomic_read(&pid->count) == 1) ||
- atomic_dec_and_test(&pid->count)) {
+ if (refcount_dec_and_test(&pid->count)) {
kmem_cache_free(ns->pid_cachep, pid);
put_pid_ns(ns);
}
@@ -210,7 +209,7 @@ struct pid *alloc_pid(struct pid_namespace *ns)
}
get_pid_ns(ns);
- atomic_set(&pid->count, 1);
+ refcount_set(&pid->count, 1);
for (type = 0; type < PIDTYPE_MAX; ++type)
INIT_HLIST_HEAD(&pid->tasks[type]);
--
2.22.0.410.gd8fdbe21b5-goog
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] Convert struct pid count to refcount_t
2019-06-28 19:34 [PATCH v2] Convert struct pid count to refcount_t Joel Fernandes (Google)
@ 2019-06-28 19:48 ` Kees Cook
2019-07-01 17:48 ` Jann Horn
2019-07-01 18:46 ` kbuild test robot
2 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2019-06-28 19:48 UTC (permalink / raw)
To: Joel Fernandes (Google)
Cc: linux-kernel, mathieu.desnoyers, willy, peterz, will.deacon,
paulmck, elena.reshetova, kernel-team, kernel-hardening,
Andrew Morton, Eric W. Biederman, Michal Hocko, Oleg Nesterov,
Stephen Rothwell
On Fri, Jun 28, 2019 at 03:34:42PM -0400, Joel Fernandes (Google) 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.
>
> For memory ordering, the only change is with the following:
> - if ((atomic_read(&pid->count) == 1) ||
> - atomic_dec_and_test(&pid->count)) {
> + if (refcount_dec_and_test(&pid->count)) {
> kmem_cache_free(ns->pid_cachep, pid);
>
> Here the change is from:
> Fully ordered --> RELEASE + ACQUIRE (as per refcount-vs-atomic.rst)
> This ACQUIRE should take care of making sure the free happens after the
> refcount_dec_and_test().
>
> The above hunk also removes atomic_read() since it is not needed for the
> code to work and it is unclear how beneficial it is. The removal lets
> refcount_dec_and_test() check for cases where get_pid() happened before
> the object was freed.
>
> Cc: mathieu.desnoyers@efficios.com
> Cc: willy@infradead.org
> Cc: peterz@infradead.org
> Cc: will.deacon@arm.com
> Cc: paulmck@linux.vnet.ibm.com
> Cc: elena.reshetova@intel.com
> Cc: keescook@chromium.org
> Cc: kernel-team@android.com
> Cc: kernel-hardening@lists.openwall.com
> Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
-Kees
>
> ---
> Only change from v1->v2 is to get rid of the atomic_read().
>
> include/linux/pid.h | 5 +++--
> kernel/pid.c | 7 +++----
> 2 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/pid.h b/include/linux/pid.h
> index 14a9a39da9c7..8cb86d377ff5 100644
> --- a/include/linux/pid.h
> +++ b/include/linux/pid.h
> @@ -3,6 +3,7 @@
> #define _LINUX_PID_H
>
> #include <linux/rculist.h>
> +#include <linux/refcount.h>
>
> enum pid_type
> {
> @@ -56,7 +57,7 @@ struct upid {
>
> struct pid
> {
> - atomic_t count;
> + refcount_t count;
> unsigned int level;
> /* lists of tasks that use this pid */
> struct hlist_head tasks[PIDTYPE_MAX];
> @@ -69,7 +70,7 @@ extern struct pid init_struct_pid;
> static inline struct pid *get_pid(struct pid *pid)
> {
> if (pid)
> - atomic_inc(&pid->count);
> + refcount_inc(&pid->count);
> return pid;
> }
>
> 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)
> return;
>
> ns = pid->numbers[pid->level].ns;
> - if ((atomic_read(&pid->count) == 1) ||
> - atomic_dec_and_test(&pid->count)) {
> + if (refcount_dec_and_test(&pid->count)) {
> kmem_cache_free(ns->pid_cachep, pid);
> put_pid_ns(ns);
> }
> @@ -210,7 +209,7 @@ struct pid *alloc_pid(struct pid_namespace *ns)
> }
>
> get_pid_ns(ns);
> - atomic_set(&pid->count, 1);
> + refcount_set(&pid->count, 1);
> for (type = 0; type < PIDTYPE_MAX; ++type)
> INIT_HLIST_HEAD(&pid->tasks[type]);
>
> --
> 2.22.0.410.gd8fdbe21b5-goog
--
Kees Cook
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] Convert struct pid count to refcount_t
2019-06-28 19:34 [PATCH v2] Convert struct pid count to refcount_t Joel Fernandes (Google)
2019-06-28 19:48 ` Kees Cook
@ 2019-07-01 17:48 ` Jann Horn
2019-07-01 18:25 ` Joel Fernandes
2019-07-01 18:46 ` kbuild test robot
2 siblings, 1 reply; 6+ messages in thread
From: Jann Horn @ 2019-07-01 17:48 UTC (permalink / raw)
To: Joel Fernandes (Google)
Cc: kernel list, Mathieu Desnoyers, Matthew Wilcox, Peter Zijlstra,
Will Deacon, Paul E . McKenney, Elena Reshetova, Kees Cook,
kernel-team, Kernel Hardening, Andrew Morton, Eric W. Biederman,
Michal Hocko, Oleg Nesterov, Stephen Rothwell
On Fri, Jun 28, 2019 at 9:35 PM Joel Fernandes (Google)
<joel@joelfernandes.org> 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]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] Convert struct pid count to refcount_t
2019-07-01 17:48 ` Jann Horn
@ 2019-07-01 18:25 ` Joel Fernandes
0 siblings, 0 replies; 6+ messages in thread
From: Joel Fernandes @ 2019-07-01 18:25 UTC (permalink / raw)
To: Jann Horn
Cc: kernel list, Mathieu Desnoyers, Matthew Wilcox, Peter Zijlstra,
Will Deacon, Paul E . McKenney, Elena Reshetova, Kees Cook,
kernel-team, Kernel Hardening, Andrew Morton, Eric W. Biederman,
Michal Hocko, Oleg Nesterov, Stephen Rothwell
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@joelfernandes.org> 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] Convert struct pid count to refcount_t
2019-06-28 19:34 [PATCH v2] Convert struct pid count to refcount_t Joel Fernandes (Google)
2019-06-28 19:48 ` Kees Cook
2019-07-01 17:48 ` Jann Horn
@ 2019-07-01 18:46 ` kbuild test robot
2019-07-01 18:48 ` Joel Fernandes
2 siblings, 1 reply; 6+ messages in thread
From: kbuild test robot @ 2019-07-01 18:46 UTC (permalink / raw)
To: Joel Fernandes (Google)
Cc: kbuild-all, linux-kernel, Joel Fernandes (Google),
mathieu.desnoyers, willy, peterz, will.deacon, paulmck,
elena.reshetova, keescook, kernel-team, kernel-hardening,
Andrew Morton, Eric W. Biederman, Michal Hocko, Oleg Nesterov,
Stephen Rothwell
[-- Attachment #1: Type: text/plain, Size: 2616 bytes --]
Hi "Joel,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on linus/master]
[also build test WARNING on v5.2-rc6]
[cannot apply to next-20190625]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Joel-Fernandes-Google/Convert-struct-pid-count-to-refcount_t/20190702-003517
config: riscv-allnoconfig (attached as .config)
compiler: riscv64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=riscv
If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>
All warnings (new ones prefixed by >>):
>> 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]
vim +44 kernel/pid.c
^1da177e Linus Torvalds 2005-04-16 43
e1e871af David Howells 2018-01-02 @44 struct pid init_struct_pid = {
e1e871af David Howells 2018-01-02 45 .count = ATOMIC_INIT(1),
e1e871af David Howells 2018-01-02 46 .tasks = {
e1e871af David Howells 2018-01-02 47 { .first = NULL },
e1e871af David Howells 2018-01-02 48 { .first = NULL },
e1e871af David Howells 2018-01-02 49 { .first = NULL },
e1e871af David Howells 2018-01-02 50 },
e1e871af David Howells 2018-01-02 51 .level = 0,
e1e871af David Howells 2018-01-02 52 .numbers = { {
e1e871af David Howells 2018-01-02 53 .nr = 0,
e1e871af David Howells 2018-01-02 54 .ns = &init_pid_ns,
e1e871af David Howells 2018-01-02 55 }, }
e1e871af David Howells 2018-01-02 56 };
^1da177e Linus Torvalds 2005-04-16 57
:::::: The code at line 44 was first introduced by commit
:::::: e1e871aff3ded26348c631b1370e257d401cd22d Expand INIT_STRUCT_PID and remove
:::::: TO: David Howells <dhowells@redhat.com>
:::::: CC: David Howells <dhowells@redhat.com>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 5119 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] Convert struct pid count to refcount_t
2019-07-01 18:46 ` kbuild test robot
@ 2019-07-01 18:48 ` Joel Fernandes
0 siblings, 0 replies; 6+ messages in thread
From: Joel Fernandes @ 2019-07-01 18:48 UTC (permalink / raw)
To: kbuild test robot
Cc: Joel Fernandes (Google),
kbuild-all, LKML, Mathieu Desnoyers, Matthew Wilcox,
Peter Zijlstra, Will Deacon, Paul McKenney, elena.reshetova,
Kees Cook, Cc: Android Kernel, kernel-hardening, Andrew Morton,
Eric W. Biederman, Michal Hocko, Oleg Nesterov, Stephen Rothwell
On Mon, Jul 1, 2019 at 2:47 PM kbuild test robot <lkp@intel.com> wrote:
>
> Hi "Joel,
>
> Thank you for the patch! Perhaps something to improve:
>
> [auto build test WARNING on linus/master]
> [also build test WARNING on v5.2-rc6]
> [cannot apply to next-20190625]
> [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
Jann H was faster than the robot in catching this, so humanity still has hope!
Its fixed in v3.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-07-01 18:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-28 19:34 [PATCH v2] Convert struct pid count to refcount_t Joel Fernandes (Google)
2019-06-28 19:48 ` Kees Cook
2019-07-01 17:48 ` Jann Horn
2019-07-01 18:25 ` Joel Fernandes
2019-07-01 18:46 ` kbuild test robot
2019-07-01 18:48 ` Joel Fernandes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).