Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1603785 > unrolled thread

[PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t

Started byElena Reshetova <elena.reshetova@intel.com>
First post2017-03-18 14:00 +0100
Last post2017-03-20 13:50 +0100
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-03-18 14:00 +0100
    Re: [PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t Sergei Shtylyov <sergei.shtylyov@cogentembedded.com> - 2017-03-18 18:30 +0100
      RE: [PATCH] net: convert sk_filter.refcnt from atomic_t to  refcount_t "Reshetova, Elena" <elena.reshetova@intel.com> - 2017-03-20 13:50 +0100

#1603785 — [PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t

FromElena Reshetova <elena.reshetova@intel.com>
Date2017-03-18 14:00 +0100
Subject[PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t
Message-ID<tmm7E-6sP-7@gated-at.bofh.it>
refcount_t type and corresponding API should be
used instead of atomic_t when the variable is used as
a reference counter. This allows to avoid accidental
refcounter overflows that might lead to use-after-free
situations.

Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David Windsor <dwindsor@gmail.com>
---
 include/linux/filter.h | 3 ++-
 net/core/filter.c      | 7 ++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/linux/filter.h b/include/linux/filter.h
index 8053c38..20247e7 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -7,6 +7,7 @@
 #include <stdarg.h>
 
 #include <linux/atomic.h>
+#include <linux/refcount.h>
 #include <linux/compat.h>
 #include <linux/skbuff.h>
 #include <linux/linkage.h>
@@ -431,7 +432,7 @@ struct bpf_prog {
 };
 
 struct sk_filter {
-	atomic_t	refcnt;
+	refcount_t	refcnt;
 	struct rcu_head	rcu;
 	struct bpf_prog	*prog;
 };
diff --git a/net/core/filter.c b/net/core/filter.c
index ebaeaf2..62267e2 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -928,7 +928,7 @@ static void sk_filter_release_rcu(struct rcu_head *rcu)
  */
 static void sk_filter_release(struct sk_filter *fp)
 {
-	if (atomic_dec_and_test(&fp->refcnt))
+	if (refcount_dec_and_test(&fp->refcnt))
 		call_rcu(&fp->rcu, sk_filter_release_rcu);
 }
 
@@ -950,7 +950,7 @@ bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
 	/* same check as in sock_kmalloc() */
 	if (filter_size <= sysctl_optmem_max &&
 	    atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
-		atomic_inc(&fp->refcnt);
+		refcount_inc(&fp->refcnt);
 		atomic_add(filter_size, &sk->sk_omem_alloc);
 		return true;
 	}
@@ -1179,12 +1179,13 @@ static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
 		return -ENOMEM;
 
 	fp->prog = prog;
-	atomic_set(&fp->refcnt, 0);
+	refcount_set(&fp->refcnt, 1);
 
 	if (!sk_filter_charge(sk, fp)) {
 		kfree(fp);
 		return -ENOMEM;
 	}
+	refcount_set(&fp->refcnt, 1);
 
 	old_fp = rcu_dereference_protected(sk->sk_filter,
 					   lockdep_sock_is_held(sk));
-- 
2.7.4

[toc] | [next] | [standalone]


#1603823

FromSergei Shtylyov <sergei.shtylyov@cogentembedded.com>
Date2017-03-18 18:30 +0100
Message-ID<tmqkV-1az-1@gated-at.bofh.it>
In reply to#1603785
Hello!

On 3/18/2017 3:58 PM, Elena Reshetova wrote:

> refcount_t type and corresponding API should be
> used instead of atomic_t when the variable is used as
> a reference counter. This allows to avoid accidental
> refcounter overflows that might lead to use-after-free
> situations.
>
> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
> Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> Signed-off-by: David Windsor <dwindsor@gmail.com>
[...]
> diff --git a/net/core/filter.c b/net/core/filter.c
> index ebaeaf2..62267e2 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
[...]
> @@ -1179,12 +1179,13 @@ static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
>  		return -ENOMEM;
>
>  	fp->prog = prog;
> -	atomic_set(&fp->refcnt, 0);
> +	refcount_set(&fp->refcnt, 1);
>
>  	if (!sk_filter_charge(sk, fp)) {
>  		kfree(fp);
>  		return -ENOMEM;
>  	}
> +	refcount_set(&fp->refcnt, 1);

    Why do it twice?

[...]

MBR, Sergei

[toc] | [prev] | [next] | [standalone]


#1604508 — RE: [PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t

From"Reshetova, Elena" <elena.reshetova@intel.com>
Date2017-03-20 13:50 +0100
SubjectRE: [PATCH] net: convert sk_filter.refcnt from atomic_t to refcount_t
Message-ID<tn4V4-4Nu-33@gated-at.bofh.it>
In reply to#1603823
> Hello!
> 
> On 3/18/2017 3:58 PM, Elena Reshetova wrote:
> 
> > refcount_t type and corresponding API should be
> > used instead of atomic_t when the variable is used as
> > a reference counter. This allows to avoid accidental
> > refcounter overflows that might lead to use-after-free
> > situations.
> >
> > Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
> > Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > Signed-off-by: David Windsor <dwindsor@gmail.com>
> [...]
> > diff --git a/net/core/filter.c b/net/core/filter.c
> > index ebaeaf2..62267e2 100644
> > --- a/net/core/filter.c
> > +++ b/net/core/filter.c
> [...]
> > @@ -1179,12 +1179,13 @@ static int __sk_attach_prog(struct bpf_prog *prog,
> struct sock *sk)
> >  		return -ENOMEM;
> >
> >  	fp->prog = prog;
> > -	atomic_set(&fp->refcnt, 0);
> > +	refcount_set(&fp->refcnt, 1);
> >
> >  	if (!sk_filter_charge(sk, fp)) {
> >  		kfree(fp);
> >  		return -ENOMEM;
> >  	}
> > +	refcount_set(&fp->refcnt, 1);
> 
>     Why do it twice?
> 
> [...]
> 
> MBR, Sergei

Sorry I just realized that I had unsquashed changes when I sent this patch, so this is still the old version. 
Will send a new one now. 

Best Regards,
Elena.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web