Refactored ksecprintf -> secprintf. Secure code is now all in common libtest161.

This library gets linked in by default in userland, and the common files are
included in the kernel.
This commit is contained in:
Scott Haseley
2016-02-23 15:31:37 -05:00
parent 2e74764d49
commit 1b99c0e18f
22 changed files with 241 additions and 139 deletions

View File

@@ -1,65 +1,7 @@
#include <kern/secret.h>
#include <types.h>
#include <thread.h>
#include <test.h>
#include <lib.h>
#include <kern/secure.h>
/*
* Common success function for kernel tests. If SECRET_TESTING is defined,
* ksecprintf will compute the hmac/sha256 hash of any message using the
* shared secret and a random salt value. The (secure) server also knows
* the secret and can verify the message was generated by a trusted source.
* The salt value prevents against replay attacks.
*/
int
success(bool status, const char * secret, const char * name) {
if (status == SUCCESS) {
return ksecprintf(secret, "SUCCESS", name);
} else {
return ksecprintf(secret, "FAIL", name);
}
}
#ifndef SECRET_TESTING
int
ksecprintf(const char * secret, const char * msg, const char * name)
{
(void)secret;
return kprintf("%s: %s\n", name, msg);
}
#else
int
ksecprintf(const char * secret, const char * msg, const char * name)
{
char *hash, *salt, *fullmsg;
int res;
size_t len;
hash = salt = fullmsg = NULL;
// test161 expects "name: msg"
len = strlen(name) + strlen(msg) + 3; // +3 for " :" and null terminator
fullmsg = (char *)kmalloc(len);
KASSERT(fullmsg != NULL);
snprintf(fullmsg, len, "%s: %s", name, msg);
res = hmac_salted(fullmsg, len-1, secret, strlen(secret), &hash, &salt);
KASSERT(res == 0);
res = kprintf("(%s, %s, %s, %s: %s)\n", name, hash, salt, name, msg);
kfree(hash);
kfree(salt);
kfree(fullmsg);
return res;
}
#endif
/*
* Helper functions used by testing and problem driver code

View File

@@ -9,7 +9,7 @@
#include <thread.h>
#include <synch.h>
#include <test.h>
#include <kern/secret.h>
#include <kern/test161.h>
#include <spinlock.h>
/*
@@ -21,7 +21,7 @@ int rwtest(int nargs, char **args) {
(void)args;
kprintf_n("rwt1 unimplemented\n");
success(FAIL, SECRET, "rwt1");
success(TEST161_FAIL, SECRET, "rwt1");
return 0;
}
@@ -31,7 +31,7 @@ int rwtest2(int nargs, char **args) {
(void)args;
kprintf_n("rwt2 unimplemented\n");
success(FAIL, SECRET, "rwt2");
success(TEST161_FAIL, SECRET, "rwt2");
return 0;
}
@@ -41,7 +41,7 @@ int rwtest3(int nargs, char **args) {
(void)args;
kprintf_n("rwt3 unimplemented\n");
success(FAIL, SECRET, "rwt3");
success(TEST161_FAIL, SECRET, "rwt3");
return 0;
}
@@ -51,7 +51,7 @@ int rwtest4(int nargs, char **args) {
(void)args;
kprintf_n("rwt4 unimplemented\n");
success(FAIL, SECRET, "rwt4");
success(TEST161_FAIL, SECRET, "rwt4");
return 0;
}
@@ -61,7 +61,7 @@ int rwtest5(int nargs, char **args) {
(void)args;
kprintf_n("rwt5 unimplemented\n");
success(FAIL, SECRET, "rwt5");
success(TEST161_FAIL, SECRET, "rwt5");
return 0;
}

View File

@@ -9,14 +9,14 @@
#include <test.h>
#include <current.h>
#include <synch.h>
#include <kern/secret.h>
#include <kern/test161.h>
#include <spinlock.h>
#define PROBLEMS_MAX_YIELDER 16
#define PROBLEMS_MAX_SPINNER 8192
#define SUCCESS 0
#define FAIL 1
#define TEST161_SUCCESS 0
#define TEST161_FAIL 1
/*
* Shared initialization routines
@@ -29,7 +29,7 @@ static struct semaphore *startsem;
static struct semaphore *endsem;
struct spinlock status_lock;
static bool test_status = FAIL;
static bool test_status = TEST161_FAIL;
const char *test_message;
static
@@ -37,7 +37,7 @@ bool
failif(bool condition, const char *message) {
if (condition) {
spinlock_acquire(&status_lock);
test_status = FAIL;
test_status = TEST161_FAIL;
test_message = message;
spinlock_release(&status_lock);
}
@@ -273,7 +273,7 @@ whalemating(int nargs, char **args) {
panic("sp1: sem_create failed\n");
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
test_message = "";
whalemating_init();
@@ -308,18 +308,18 @@ whalemating(int nargs, char **args) {
}
/* Make sure nothing is happening... */
loop_status = SUCCESS;
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; i++) {
loop_status = TEST161_SUCCESS;
for (i = 0; i < CHECK_TIMES && loop_status == TEST161_SUCCESS; i++) {
kprintf_t(".");
random_spinner(PROBLEMS_MAX_SPINNER);
lock_acquire(testlock);
if ((male_start_count != NMATING) || (female_start_count != NMATING) ||
(matchmaker_start_count + male_end_count + female_end_count + matchmaker_end_count != 0)) {
loop_status = FAIL;
loop_status = TEST161_FAIL;
}
lock_release(testlock);
}
if (failif((loop_status == FAIL), "failed: uncoordinated matchmaking is occurring")) {
if (failif((loop_status == TEST161_FAIL), "failed: uncoordinated matchmaking is occurring")) {
goto done;
}
@@ -352,19 +352,19 @@ whalemating(int nargs, char **args) {
}
/* Make sure nothing else is happening... */
loop_status = SUCCESS;
for (i = 0; i < CHECK_TIMES && loop_status == SUCCESS; i++) {
loop_status = TEST161_SUCCESS;
for (i = 0; i < CHECK_TIMES && loop_status == TEST161_SUCCESS; i++) {
kprintf_t(".");
random_spinner(PROBLEMS_MAX_SPINNER);
lock_acquire(testlock);
if ((male_start_count != NMATING) || (female_start_count != NMATING) ||
(matchmaker_start_count != pivot) || (male_end_count != pivot) ||
(female_end_count != pivot) || (matchmaker_end_count != pivot)) {
loop_status = FAIL;
loop_status = TEST161_FAIL;
}
lock_release(testlock);
}
if (failif((loop_status == FAIL), "failed: uncoordinated matchmaking is occurring")) {
if (failif((loop_status == TEST161_FAIL), "failed: uncoordinated matchmaking is occurring")) {
goto done;
}
@@ -399,8 +399,8 @@ done:
sem_destroy(matcher_sem);
kprintf_t("\n");
if (test_status != SUCCESS) {
ksecprintf(SECRET, test_message, "sp1");
if (test_status != TEST161_SUCCESS) {
secprintf(SECRET, test_message, "sp1");
}
success(test_status, SECRET, "sp1");
@@ -628,7 +628,7 @@ int stoplight(int nargs, char **args) {
panic("sp2: sem_create failed\n");
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
stoplight_init();
@@ -669,7 +669,7 @@ int stoplight(int nargs, char **args) {
for (i = 0; i < NCARS; i++) {
passed += car_locations[i] == PASSED_CAR ? 1 : 0;
}
if ((test_status == SUCCESS) &&
if ((test_status == TEST161_SUCCESS) &&
(!(failif((passed != NCARS), "failed: not enough cars"))) &&
(!(failif((all_quadrant != required_quadrant), "failed: didn't do the right turns"))) &&
(!(failif((max_car_count <= 1), "failed: no concurrency achieved")))) {};
@@ -679,8 +679,8 @@ int stoplight(int nargs, char **args) {
sem_destroy(endsem);
kprintf_t("\n");
if (test_status != SUCCESS) {
ksecprintf(SECRET, test_message, "sp2");
if (test_status != TEST161_SUCCESS) {
secprintf(SECRET, test_message, "sp2");
}
success(test_status, SECRET, "sp2");

View File

@@ -40,7 +40,7 @@
#include <thread.h>
#include <synch.h>
#include <test.h>
#include <kern/secret.h>
#include <kern/test161.h>
#include <spinlock.h>
#define CREATELOOPS 8
@@ -63,7 +63,7 @@ static struct cv *testcv = NULL;
static struct semaphore *donesem = NULL;
struct spinlock status_lock;
static bool test_status = FAIL;
static bool test_status = TEST161_FAIL;
static unsigned long semtest_current;
@@ -72,7 +72,7 @@ bool
failif(bool condition) {
if (condition) {
spinlock_acquire(&status_lock);
test_status = FAIL;
test_status = TEST161_FAIL;
spinlock_release(&status_lock);
}
return condition;
@@ -131,7 +131,7 @@ semtest(int nargs, char **args)
}
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
kprintf_n("If this hangs, it's broken: ");
P(testsem);
@@ -264,7 +264,7 @@ locktest(int nargs, char **args)
}
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
for (i=0; i<NTHREADS; i++) {
kprintf_t(".");
@@ -302,12 +302,12 @@ locktest2(int nargs, char **args) {
panic("lt2: lock_create failed\n");
}
ksecprintf(SECRET, "Should panic...", "lt2");
secprintf(SECRET, "Should panic...", "lt2");
lock_release(testlock);
/* Should not get here on success. */
success(FAIL, SECRET, "lt2");
success(TEST161_FAIL, SECRET, "lt2");
lock_destroy(testlock);
testlock = NULL;
@@ -328,13 +328,13 @@ locktest3(int nargs, char **args) {
panic("lt3: lock_create failed\n");
}
ksecprintf(SECRET, "Should panic...", "lt3");
secprintf(SECRET, "Should panic...", "lt3");
lock_acquire(testlock);
lock_destroy(testlock);
/* Should not get here on success. */
success(FAIL, SECRET, "lt3");
success(TEST161_FAIL, SECRET, "lt3");
testlock = NULL;
@@ -426,7 +426,7 @@ cvtest(int nargs, char **args)
}
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
testval1 = NTHREADS-1;
for (i=0; i<NTHREADS; i++) {
@@ -562,7 +562,7 @@ cvtest2(int nargs, char **args)
testcvs[i] = cv_create("cvtest2 cv");
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
result = thread_fork("cvt2", NULL, sleepthread, NULL, 0);
if (result) {
@@ -609,12 +609,12 @@ cvtest3(int nargs, char **args) {
panic("cvt3: cv_create failed\n");
}
ksecprintf(SECRET, "Should panic...", "cvt3");
secprintf(SECRET, "Should panic...", "cvt3");
cv_wait(testcv, testlock);
/* Should not get here on success. */
success(FAIL, SECRET, "cvt3");
success(TEST161_FAIL, SECRET, "cvt3");
lock_destroy(testlock);
cv_destroy(testcv);
@@ -641,12 +641,12 @@ cvtest4(int nargs, char **args) {
panic("cvt4: cv_create failed\n");
}
ksecprintf(SECRET, "Should panic...", "cvt4");
secprintf(SECRET, "Should panic...", "cvt4");
cv_broadcast(testcv, testlock);
/* Should not get here on success. */
success(FAIL, SECRET, "cvt4");
success(TEST161_FAIL, SECRET, "cvt4");
lock_destroy(testlock);
cv_destroy(testcv);
@@ -726,7 +726,7 @@ cvtest5(int nargs, char **args) {
panic("cvt5: sem_create failed\n");
}
spinlock_init(&status_lock);
test_status = SUCCESS;
test_status = TEST161_SUCCESS;
testval1 = 0;
lock_acquire(testlock);