Kernel Protections/refcount t
Revision as of 12:19, 6 February 2017 by DavidWindsor (talk | contribs) (Change <tt> tags to <code>)
Summary
HARDENED_ATOMIC is a kernel self-protection mechanism that greatly helps with the mitigation of use-after-free bugs. It is based off of work done by the PaX Team, originally called PAX_REFCOUNT.
Reference Counting API
HARDENED_ATOMIC introduces a new data type: refcount_t
. This type is to be used for all kernel reference counters.
The following is the kernel reference counting API. Please note that all operations are atomic, unless otherwise specified.
REFCOUNT_INIT(unsigned int)
- Initialize a
refcount_t
object.
void refcount_set(refcount_t *, unsigned int)
- Set a
refcount_t
object's internal value.
unsigned int refcount_read(refcount_t *)
- Returns the
refcount_t
object's internal value.
bool refcount_add_not_zero(unsigned int v, refcount_t *r)
- Add
v
tor
. Ifr + v
causes an overflow, the result of the addition operation is not saved tor
. Returnstrue
if the resulting value ofr
is non-zero,false
otherwise.
void refcount_add(unsigned int v, refcount_t *r)
- Adds
v
tor
and stores the value inr
.
bool refcount_inc_not_zero(refcount_t *r)
- Increments
r
and tests whetherr + 1
causes an overflow. If an overflow does occur, the result of the increment operation is not saved tor
. Will saturate atUINT_MAX
andWARN
. Returnstrue
if the resulting value ofr
is non-zero,false
otherwise.
void refcount_inc(refcount_t *r)
- Increment
r
. Will saturate atUINT_MAX
andWARN
.
bool refcount_sub_and_test(unsigned int v, refcount_t *r)
- Subtract
v
fromr
and tests whetherr - v
causes an underflow. If an underflow does occur, the result of the decrement operation is not saved tor
. Will fail to decrement when saturated atUINT_MAX
. Returnstrue
if the resulting value ofr
is non-zero,false
otherwise.
void refcount_dec(refcount_t *r)
- Decrement
r
. Ifr - 1
causes an underflow, the result of the decrement operation is not saved tor
. Will fail to decrement when saturated atUINT_MAX
.
bool refcount_dec_if_one(refcount_t *r)
- Attempts to transition
r
from 1 to 0. Ifr
is 1, decrement it to 0. Returnstrue
ifr
was decremented,false
otherwise.
bool refcount_dec_not_one(refcount_t *r)
- Decrement
r
unless the value ofr
is 1. Returnstrue
ifr
was decremented, false otherwise.
bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
- Decrement
r
and lock mutex ifr
becomes 0. WillWARN
on underflow and fail to decrement ifr
is saturated atUINT_MAX
. Returnstrue
ifr
is 0 and mutex is held,false
otherwise.
bool refcount_dec_and_lock(refcount_t *r, spinlock_t *s)
- Decrement
r
and lock spinlock ifr
becomes 0. WillWARN
on underflow and fail to decrement ifr
is saturated atUINT_MAX
. Returnstrue
ifr
is 0 and spinlock is held,false
otherwise.