123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
#include "lkdtm.h"
#include <linux/refcount.h>
static void overflow_check(refcount_t *ref)
{
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Overflow detected: saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Overflow detected: unsafely reset to max\n");
break;
default:
pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
}
}
static void lkdtm_REFCOUNT_INC_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
pr_info("attempting good refcount_inc() without overflow\n");
refcount_dec(&over);
refcount_inc(&over);
pr_info("attempting bad refcount_inc() overflow\n");
refcount_inc(&over);
refcount_inc(&over);
overflow_check(&over);
}
static void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
pr_info("attempting good refcount_add() without overflow\n");
refcount_dec(&over);
refcount_dec(&over);
refcount_dec(&over);
refcount_dec(&over);
refcount_add(4, &over);
pr_info("attempting bad refcount_add() overflow\n");
refcount_add(4, &over);
overflow_check(&over);
}
static void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
pr_info("attempting bad refcount_inc_not_zero() overflow\n");
if (!refcount_inc_not_zero(&over))
pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
overflow_check(&over);
}
static void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
pr_info("attempting bad refcount_add_not_zero() overflow\n");
if (!refcount_add_not_zero(6, &over))
pr_warn("Weird: refcount_add_not_zero() reported zero\n");
overflow_check(&over);
}
static void check_zero(refcount_t *ref)
{
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Zero detected: saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Zero detected: unsafely reset to max\n");
break;
case 0:
pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
break;
default:
pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
}
}
static void lkdtm_REFCOUNT_DEC_ZERO(void)
{
refcount_t zero = REFCOUNT_INIT(2);
pr_info("attempting good refcount_dec()\n");
refcount_dec(&zero);
pr_info("attempting bad refcount_dec() to zero\n");
refcount_dec(&zero);
check_zero(&zero);
}
static void check_negative(refcount_t *ref, int start)
{
if (refcount_read(ref) == start) {
pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
start);
return;
}
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Negative detected: saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Negative detected: unsafely reset to max\n");
break;
default:
pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
}
}
static void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
{
refcount_t neg = REFCOUNT_INIT(0);
pr_info("attempting bad refcount_dec() below zero\n");
refcount_dec(&neg);
check_negative(&neg, 0);
}
static void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
{
refcount_t neg = REFCOUNT_INIT(0);
pr_info("attempting bad refcount_dec_and_test() below zero\n");
if (refcount_dec_and_test(&neg))
pr_warn("Weird: refcount_dec_and_test() reported zero\n");
check_negative(&neg, 0);
}
static void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
{
refcount_t neg = REFCOUNT_INIT(3);
pr_info("attempting bad refcount_sub_and_test() below zero\n");
if (refcount_sub_and_test(5, &neg))
pr_warn("Weird: refcount_sub_and_test() reported zero\n");
check_negative(&neg, 3);
}
static void lkdtm_REFCOUNT_SUB_AND_TEST_ZERO(void)
{
refcount_t neg = REFCOUNT_INIT(0);
pr_info("attempting bad refcount_sub_and_test() at zero\n");
if (refcount_sub_and_test(0, &neg))
pr_warn("Weird: refcount_sub_and_test() reported zero\n");
check_negative(&neg, 0);
}
static void check_from_zero(refcount_t *ref)
{
switch (refcount_read(ref)) {
case 0:
pr_info("Zero detected: stayed at zero\n");
break;
case REFCOUNT_SATURATED:
pr_info("Zero detected: saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Zero detected: unsafely reset to max\n");
break;
default:
pr_info("Fail: zero not detected, incremented to %d\n",
refcount_read(ref));
}
}
static void lkdtm_REFCOUNT_INC_ZERO(void)
{
refcount_t zero = REFCOUNT_INIT(0);
pr_info("attempting safe refcount_inc_not_zero() from zero\n");
if (!refcount_inc_not_zero(&zero)) {
pr_info("Good: zero detected\n");
if (refcount_read(&zero) == 0)
pr_info("Correctly stayed at zero\n");
else
pr_err("Fail: refcount went past zero!\n");
} else {
pr_err("Fail: Zero not detected!?\n");
}
pr_info("attempting bad refcount_inc() from zero\n");
refcount_inc(&zero);
check_from_zero(&zero);
}
static void lkdtm_REFCOUNT_ADD_ZERO(void)
{
refcount_t zero = REFCOUNT_INIT(0);
pr_info("attempting safe refcount_add_not_zero() from zero\n");
if (!refcount_add_not_zero(3, &zero)) {
pr_info("Good: zero detected\n");
if (refcount_read(&zero) == 0)
pr_info("Correctly stayed at zero\n");
else
pr_err("Fail: refcount went past zero\n");
} else {
pr_err("Fail: Zero not detected!?\n");
}
pr_info("attempting bad refcount_add() from zero\n");
refcount_add(3, &zero);
check_from_zero(&zero);
}
static void check_saturated(refcount_t *ref)
{
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Saturation detected: still saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Saturation detected: unsafely reset to max\n");
break;
default:
pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
}
}
static void lkdtm_REFCOUNT_INC_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_inc() from saturated\n");
refcount_inc(&sat);
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_DEC_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_dec() from saturated\n");
refcount_dec(&sat);
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_ADD_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_dec() from saturated\n");
refcount_add(8, &sat);
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
if (!refcount_inc_not_zero(&sat))
pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_add_not_zero() from saturated\n");
if (!refcount_add_not_zero(7, &sat))
pr_warn("Weird: refcount_add_not_zero() reported zero\n");
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_dec_and_test() from saturated\n");
if (refcount_dec_and_test(&sat))
pr_warn("Weird: refcount_dec_and_test() reported zero\n");
check_saturated(&sat);
}
static void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
{
refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
pr_info("attempting bad refcount_sub_and_test() from saturated\n");
if (refcount_sub_and_test(8, &sat))
pr_warn("Weird: refcount_sub_and_test() reported zero\n");
check_saturated(&sat);
}
static void lkdtm_ATOMIC_TIMING(void)
{
unsigned int i;
atomic_t count = ATOMIC_INIT(1);
for (i = 0; i < INT_MAX - 1; i++)
atomic_inc(&count);
for (i = INT_MAX; i > 0; i--)
if (atomic_dec_and_test(&count))
break;
if (i != 1)
pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
else
pr_info("atomic timing: done\n");
}
static void lkdtm_REFCOUNT_TIMING(void)
{
unsigned int i;
refcount_t count = REFCOUNT_INIT(1);
for (i = 0; i < INT_MAX - 1; i++)
refcount_inc(&count);
for (i = INT_MAX; i > 0; i--)
if (refcount_dec_and_test(&count))
break;
if (i != 1)
pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
else
pr_info("refcount timing: done\n");
}
static struct crashtype crashtypes[] = {
CRASHTYPE(REFCOUNT_INC_OVERFLOW),
CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
CRASHTYPE(REFCOUNT_DEC_ZERO),
CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
CRASHTYPE(REFCOUNT_SUB_AND_TEST_ZERO),
CRASHTYPE(REFCOUNT_INC_ZERO),
CRASHTYPE(REFCOUNT_ADD_ZERO),
CRASHTYPE(REFCOUNT_INC_SATURATED),
CRASHTYPE(REFCOUNT_DEC_SATURATED),
CRASHTYPE(REFCOUNT_ADD_SATURATED),
CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
CRASHTYPE(ATOMIC_TIMING),
CRASHTYPE(REFCOUNT_TIMING),
};
struct crashtype_category refcount_crashtypes = {
.crashtypes = crashtypes,
.len = ARRAY_SIZE(crashtypes),
};