From: Elena Reshetova <elena.reshetova@intel.com> Date: 2017-10-20 07:38:43
This series, for dm and bcache parts, replaces atomic_t reference
counters with the new refcount_t type and API (see include/linux/refcount.h).
By doing this we prevent intentional or accidental
underflows or overflows that can led to use-after-free vulnerabilities.
The patches are fully independent and can be cherry-picked separately.
Patches are based on top of linux-next as of yesterday.
If there are no objections to the patches, please merge them via respective trees
Elena Reshetova (4):
bcache: convert cached_dev.count from atomic_t to refcount_t
dm cache: convert dm_cache_metadata.ref_count from atomic_t to
refcount_t
dm: convert dm_dev_internal.count from atomic_t to refcount_t
dm: convert table_device.count from atomic_t to refcount_t
drivers/md/bcache/bcache.h | 7 ++++---
drivers/md/bcache/super.c | 6 +++---
drivers/md/bcache/writeback.h | 2 +-
drivers/md/dm-cache-metadata.c | 9 +++++----
drivers/md/dm-table.c | 6 +++---
drivers/md/dm.c | 12 +++++++-----
drivers/md/dm.h | 3 ++-
7 files changed, 25 insertions(+), 20 deletions(-)
--
2.7.4
From: Elena Reshetova <elena.reshetova@intel.com> Date: 2017-10-20 07:38:45
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable dm_cache_metadata.ref_count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/dm-cache-metadata.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
From: Elena Reshetova <elena.reshetova@intel.com> Date: 2017-10-20 07:38:56
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable table_device.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/dm.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
From: Elena Reshetova <elena.reshetova@intel.com> Date: 2017-10-20 07:39:00
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable dm_dev_internal.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/dm-table.c | 6 +++---
drivers/md/dm.h | 3 ++-
2 files changed, 5 insertions(+), 4 deletions(-)
From: Elena Reshetova <elena.reshetova@intel.com> Date: 2017-10-20 07:40:11
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable cached_dev.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/bcache/bcache.h | 7 ++++---
drivers/md/bcache/super.c | 6 +++---
drivers/md/bcache/writeback.h | 2 +-
3 files changed, 8 insertions(+), 7 deletions(-)
@@ -299,7 +300,7 @@ struct cached_dev {structsemaphoresb_write_mutex;/* Refcount on the cache set. Always nonzero when we're caching. */-atomic_tcount;+refcount_tcount;structwork_structdetach;/*
@@ -806,13 +807,13 @@ do { \staticinlinevoidcached_dev_put(structcached_dev*dc){-if(atomic_dec_and_test(&dc->count))+if(refcount_dec_and_test(&dc->count))schedule_work(&dc->detach);}staticinlineboolcached_dev_get(structcached_dev*dc){-if(!atomic_inc_not_zero(&dc->count))+if(!refcount_inc_not_zero(&dc->count))returnfalse;/* Paired with the mb in cached_dev_attach */
From: Michael Lyle <hidden> Date: 2017-10-20 18:39:49
On 10/20/2017 12:37 AM, Elena Reshetova wrote:
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable cached_dev.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Reviewed-by: Michael Lyle <redacted>
Thanks for this-- I'm including it in my tree for possible inclusion in
4.15 (I've already sent my main chunk of changes upwards).
Mike
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable cached_dev.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Reviewed-by: Michael Lyle <redacted>
Thanks for this-- I'm including it in my tree for possible inclusion in
4.15 (I've already sent my main chunk of changes upwards).
Thank you Mike! I am dropping then this patch from my list of maintained
conversions.
Best Regards,
Elena.
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable table_device.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/dm.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
NACK
This patch (2a0b4682e09d76466f7b8f5e347ae2ff02f033af) currently breaks
accounting of opened devices.
I.e. multisegment device (target with 3 segments is not properly accounted)
Patch needs reworking and users of 'dm' and 4.15-rc0 kernel should rather
switch back to 4.14 ATM as it's unclear which other parts can be affected.
Zdenek
On Fri, Oct 20, 2017 at 10:37:38AM +0300, Elena Reshetova wrote:
quoted
} else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
r = upgrade_mode(dd, mode, t->md);
if (r)
return r;
+ refcount_inc(&dd->count);
}
Missing here:
else
refcount_inc(&dd->count);
?
Oh, yes, thanks for catching this! I think this got unnoticed so far and patch was merged, so I am going to send a followup patch now.
Best Regards,
Elena.
atomic_t variables are currently used to implement reference
counters with the following properties:
- counter is initialized to 1 using atomic_set()
- a resource is freed upon counter reaching zero
- once counter reaches zero, its further
increments aren't allowed
- counter schema uses basic atomic operations
(set, inc, inc_not_zero, dec_and_test, etc.)
Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.
The variable table_device.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: Kees Cook <redacted>
Reviewed-by: David Windsor <redacted>
Reviewed-by: Hans Liljestrand <redacted>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
drivers/md/dm.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
NACK
This patch (2a0b4682e09d76466f7b8f5e347ae2ff02f033af) currently breaks
accounting of opened devices.
I.e. multisegment device (target with 3 segments is not properly accounted)
Could you please explain what exactly happens (i.e. missing/wrong increment?)
or provide a error dump?
By looking at the code, I don't see where the change in the reference counting
could have caused this.
Best Regards,
Elena.
Patch needs reworking and users of 'dm' and 4.15-rc0 kernel should rather
switch back to 4.14 ATM as it's unclear which other parts can be affected.
Zdenek
From: Alasdair G Kergon <agk@redhat.com> Date: 2017-11-24 14:04:27
On Fri, Nov 24, 2017 at 08:29:42AM +0000, Reshetova, Elena wrote:
By looking at the code, I don't see where the change in the reference counting
could have caused this.
The cause was the bug I identified in patch 3, not this patch.
The regression is easily hit - tables that reference the same underlying device
more than once are very common.
Alasdair
Problem will be here if you hit this refcount_inc() after the refcount_set(&dd->count, 0) earlier.
refcount_inc() does not increment on zero value *ever* for security reasons and instead people
should initialize refcounters to 1 always and do increments from there if needed.
This was the reason for the initial change I did, my mistake was just to forget to increment it also
in case condition (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) fails.
I have issues with my intel smpt server for sending patches (I will get it fixed tomorrow from internal network),
so I am attaching the patch I did end of last week to this thread instead (or alternatively can properly send it tomorrow after fix).
Sorry for the delay!
Best Regards,
Elena.
Problem will be here if you hit this refcount_inc() after the refcount_set(&dd->count, 0) earlier.
refcount_inc() does not increment on zero value *ever* for security reasons and instead people
should initialize refcounters to 1 always and do increments from there if needed.
include/linux/refcount.h:refcount_inc() definitely doesn't avoid
incrementing zero value.
Neither does lib/refcount.c:refcount_inc() but it does spew a WARN_ON by
assuming a zero value means use-after-free.
This was the reason for the initial change I did, my mistake was just to forget to increment it also
in case condition (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) fails.
I have issues with my intel smpt server for sending patches (I will get it fixed tomorrow from internal network),
so I am attaching the patch I did end of last week to this thread instead (or alternatively can properly send it tomorrow after fix).
Sorry for the delay!
I was tempted to revert your original commits that switch DM code to
using refcount_t. Already proved more trouble than it is worth.
But I'll drop my commit and take your fix.
Mike
@@ -451,15 +451,15 @@ int dm_get_device(struct dm_target *ti, const char
*path, fmode_t mode,
quoted
return r;
}
- refcount_set(&dd->count, 1);
+ refcount_set(&dd->count, 0);
list_add(&dd->list, &t->devices);
} else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
r = upgrade_mode(dd, mode, t->md);
if (r)
return r;
- refcount_inc(&dd->count);
}
+ refcount_inc(&dd->count);
Problem will be here if you hit this refcount_inc() after the refcount_set(&dd-
count, 0) earlier.
refcount_inc() does not increment on zero value *ever* for security reasons
and instead people
quoted
should initialize refcounters to 1 always and do increments from there if
needed.
include/linux/refcount.h:refcount_inc() definitely doesn't avoid
incrementing zero value.
Ok, to be fully precise there are 3 different cases (depending on config options):
1) refcount_t = atomic_t and in this case yes, nothing prevents increment, but no
protection is given, so we hope such cases are disabled for any distros that care about
security
2) CONFIG_FULL_REFCOUNT is on and refcount_t uses arch. independent implementation
In lib/refcount.c. In this case refcount_inc() won't increment from zero. It is really more
than just a WARN(), increment fails inside refcount_inc_not_zero() used underneath.
3) arch. dependent implementation is used for refcount_t. Here different options are
possible based on how arch. decides to implement this. Currently we only have x86
one (arch/x86/include/asm/refcount.h) and it is indeed allows increments from zero to happen.
So, what I described above was the worst case, but since we need the code to work reliably
in each case, we have to take it into account.
Neither does lib/refcount.c:refcount_inc() but it does spew a WARN_ON by
assuming a zero value means use-after-free.
This was the reason for the initial change I did, my mistake was just to forget to
increment it also
quoted
in case condition (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) fails.
I have issues with my intel smpt server for sending patches (I will get it fixed
tomorrow from internal network),
quoted
so I am attaching the patch I did end of last week to this thread instead (or
alternatively can properly send it tomorrow after fix).
quoted
Sorry for the delay!
I was tempted to revert your original commits that switch DM code to
using refcount_t. Already proved more trouble than it is worth.
But I'll drop my commit and take your fix.
Thank you very much and sorry for the troubles!
Unfortunately none of us is free of mistakes, good that this one was caught so fast!
When it comes to value, it does provide security value for your code and makes
sure that your reference counters would not be a new target of many similar CVEs
we had in past around this. Overall each conversion matters since there is less and less
potential holes attackers can try to squeeze themselves!
Best Regards,
Elena.