Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread Kees Cook
On Tue, Apr 25, 2017 at 7:14 PM, PaX Team wrote: > On 25 Apr 2017 at 9:39, Kees Cook wrote: > >> On Tue, Apr 25, 2017 at 4:26 AM, PaX Team wrote: >> > INT_MAX threads would be needed when the leaking path is locked so >> > that it can only be exercised once and you'll need to get normal >> > (bal

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread PaX Team
On 25 Apr 2017 at 9:39, Kees Cook wrote: > On Tue, Apr 25, 2017 at 4:26 AM, PaX Team wrote: > > INT_MAX threads would be needed when the leaking path is locked so > > that it can only be exercised once and you'll need to get normal > > (balanced) paths preempted just after the increment. if the l

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread Kees Cook
On Tue, Apr 25, 2017 at 4:26 AM, PaX Team wrote: > INT_MAX threads would be needed when the leaking path is locked so > that it can only be exercised once and you'll need to get normal > (balanced) paths preempted just after the increment. if the leaking > path is lockless (can be exercised in par

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread Kees Cook
On Tue, Apr 25, 2017 at 4:26 AM, PaX Team wrote: > On 25 Apr 2017 at 0:01, Peter Zijlstra wrote: >> How is the below not useful fodder for an exploit? It might be a less >> common bug, and perhaps a bit more fiddly to make work, but afaict its >> still a full use-after-free and therefore useful. >

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread PaX Team
On 24 Apr 2017 at 13:33, Kees Cook wrote: > On Mon, Apr 24, 2017 at 4:00 AM, PaX Team wrote: > > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > > > >> On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > >> > This patch ports the x86-specific atomic overflow handling from PaX's > >> > P

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread PaX Team
On 25 Apr 2017 at 12:23, Peter Zijlstra wrote: > So what avoids this: simple, you noted it yourself in your previous mail: > Well, your setup (panic_on_warn et al) would have it panic the box. That > will effectively stop the exploit by virtue of stopping everything. with that in mind the actua

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread PaX Team
On 25 Apr 2017 at 0:01, Peter Zijlstra wrote: > On Mon, Apr 24, 2017 at 01:40:56PM -0700, Kees Cook wrote: > > I think we're way off in the weeds here. The "cannot inc from 0" check > > is about general sanity checks on refcounts. > > I disagree, although sanity check are good too. exactly, an a

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread Peter Zijlstra
On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > +static __always_inline void refcount_inc(refcount_t *r) > +{ > + asm volatile(LOCK_PREFIX "incl %0\n\t" > + REFCOUNT_CHECK_OVERFLOW(4) > + : [counter] "+m" (r->refs.counter) > + : : "cc", "cx"); >

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-25 Thread Peter Zijlstra
On Mon, Apr 24, 2017 at 03:37:32PM -0700, Kees Cook wrote: > On Mon, Apr 24, 2017 at 3:01 PM, Peter Zijlstra wrote: > > It does not. It just got free'ed. Nothing will stop the free from > > happening (or already having happened). > > Well, yes, but that's kind of my point. Detecting inc-from-0 i

Re: [kernel-hardening] Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Rik van Riel
On Mon, 2017-04-24 at 15:37 -0700, Kees Cook wrote: > On Mon, Apr 24, 2017 at 3:01 PM, Peter Zijlstra > wrote: > > On Mon, Apr 24, 2017 at 01:40:56PM -0700, Kees Cook wrote: > > > I think we're way off in the weeds here. The "cannot inc from 0" > > > check > > > is about general sanity checks on r

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 3:01 PM, Peter Zijlstra wrote: > On Mon, Apr 24, 2017 at 01:40:56PM -0700, Kees Cook wrote: >> I think we're way off in the weeds here. The "cannot inc from 0" check >> is about general sanity checks on refcounts. > > I disagree, although sanity check are good too. > >> It

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Mon, Apr 24, 2017 at 01:40:56PM -0700, Kees Cook wrote: > I think we're way off in the weeds here. The "cannot inc from 0" check > is about general sanity checks on refcounts. I disagree, although sanity check are good too. > It should never happen, and if it does, there's a bug. The very sam

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 8:15 AM, PaX Team wrote: > On 24 Apr 2017 at 15:33, Peter Zijlstra wrote: >> On Mon, Apr 24, 2017 at 03:08:20PM +0200, PaX Team wrote: >> > On 24 Apr 2017 at 13:15, Peter Zijlstra wrote: >> > that was exactly my point: all this applies to you as well. so let me ask >> > the

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 4:00 AM, PaX Team wrote: > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > >> On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> > This patch ports the x86-specific atomic overflow handling from PaX's >> > PAX_REFCOUNT to the upstream refcount_t API. This is an

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 3:48 AM, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> diff --git a/drivers/misc/lkdtm_bugs.c b/drivers/misc/lkdtm_bugs.c >> index e3f4cd8876b5..1bdafb29b802 100644 >> --- a/drivers/misc/lkdtm_bugs.c >> +++ b/drivers/misc/lkdtm_bugs.c

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 3:45 AM, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> +static __always_inline __must_check bool refcount_inc_not_zero(refcount_t >> *r) >> +{ >> + const int a = 1; >> + const int u = 0; >> + int c, old; >> + >> + c =

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Kees Cook
On Mon, Apr 24, 2017 at 1:32 AM, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> This patch ports the x86-specific atomic overflow handling from PaX's >> PAX_REFCOUNT to the upstream refcount_t API. This is an updated version >> from PaX that eliminates the sa

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread PaX Team
On 24 Apr 2017 at 15:33, Peter Zijlstra wrote: > On Mon, Apr 24, 2017 at 03:08:20PM +0200, PaX Team wrote: > > On 24 Apr 2017 at 13:15, Peter Zijlstra wrote: > > > > > On Mon, Apr 24, 2017 at 01:00:18PM +0200, PaX Team wrote: > > > > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > > > > > > > >

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Mon, Apr 24, 2017 at 03:08:20PM +0200, PaX Team wrote: > On 24 Apr 2017 at 13:15, Peter Zijlstra wrote: > > > On Mon, Apr 24, 2017 at 01:00:18PM +0200, PaX Team wrote: > > > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > > > > > > Also, you forgot nr_cpus in your bound. Afaict the worst case

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread PaX Team
On 24 Apr 2017 at 13:15, Peter Zijlstra wrote: > On Mon, Apr 24, 2017 at 01:00:18PM +0200, PaX Team wrote: > > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > > > > Also, you forgot nr_cpus in your bound. Afaict the worst case here is > > > O(nr_tasks + 3*nr_cpus). > > > > what does nr_cpus hav

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Mon, Apr 24, 2017 at 01:00:18PM +0200, PaX Team wrote: > On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > > Also, you forgot nr_cpus in your bound. Afaict the worst case here is > > O(nr_tasks + 3*nr_cpus). > > what does nr_cpus have to do with winning the race? The CPUs could each run nested

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread PaX Team
On 24 Apr 2017 at 10:32, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > > This patch ports the x86-specific atomic overflow handling from PaX's > > PAX_REFCOUNT to the upstream refcount_t API. This is an updated version > > from PaX that eliminates the saturat

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > diff --git a/drivers/misc/lkdtm_bugs.c b/drivers/misc/lkdtm_bugs.c > index e3f4cd8876b5..1bdafb29b802 100644 > --- a/drivers/misc/lkdtm_bugs.c > +++ b/drivers/misc/lkdtm_bugs.c > @@ -135,9 +135,15 @@ void lkdtm_HUNG_TASK(void) > sch

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > +static __always_inline __must_check bool refcount_inc_not_zero(refcount_t *r) > +{ > + const int a = 1; > + const int u = 0; > + int c, old; > + > + c = atomic_read(&(r->refs)); > + for (;;) { > + if (unli

Re: [kernel-hardening] Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Mon, Apr 24, 2017 at 10:53:30AM +0200, Jann Horn wrote: > On Mon, Apr 24, 2017 at 10:32 AM, Peter Zijlstra wrote: > > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > >> This patch ports the x86-specific atomic overflow handling from PaX's > >> PAX_REFCOUNT to the upstream refcount_

Re: [kernel-hardening] Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Jann Horn
On Mon, Apr 24, 2017 at 10:32 AM, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> This patch ports the x86-specific atomic overflow handling from PaX's >> PAX_REFCOUNT to the upstream refcount_t API. This is an updated version >> from PaX that eliminates the s

Re: [PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-24 Thread Peter Zijlstra
On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: > This patch ports the x86-specific atomic overflow handling from PaX's > PAX_REFCOUNT to the upstream refcount_t API. This is an updated version > from PaX that eliminates the saturation race condition by resetting the > atomic counter bac

[PATCH] x86/refcount: Implement fast refcount_t handling

2017-04-21 Thread Kees Cook
This patch ports the x86-specific atomic overflow handling from PaX's PAX_REFCOUNT to the upstream refcount_t API. This is an updated version from PaX that eliminates the saturation race condition by resetting the atomic counter back to the INT_MAX saturation value on both overflow and underflow. T