Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trust: Forcibly mark "Default Trust" read-only #123

Merged
merged 1 commit into from Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion trust/Makefile.am
Expand Up @@ -46,6 +46,8 @@ module_LTLIBRARIES += \
p11-kit-trust.la

p11_kit_trust_la_CFLAGS = \
-DP11_DEFAULT_TRUST_PREFIX=DATA_DIR \
-DP11_SYSTEM_TRUST_PREFIX=SYSCONFDIR \
$(LIBTASN1_CFLAGS)

p11_kit_trust_la_LIBADD = \
Expand All @@ -70,6 +72,8 @@ libtrust_testable_la_LDFLAGS = \
libtrust_testable_la_SOURCES = $(TRUST_SRCS)

libtrust_testable_la_CFLAGS = \
-DP11_DEFAULT_TRUST_PREFIX=\"$(builddir)/trust/default\" \
-DP11_SYSTEM_TRUST_PREFIX=\"$(builddir)/trust/system\" \
$(LIBTASN1_CFLAGS)

libtrust_testable_la_LIBADD = \
Expand Down Expand Up @@ -125,7 +129,7 @@ asn:
# Tests ----------------------------------------------------------------

trust_CFLAGS = \
$(LIBTASN1_CFLAGS) \
$(libtrust_testable_la_CFLAGS) \
$(NULL)

trust_LIBS = \
Expand Down
2 changes: 1 addition & 1 deletion trust/frob-token.c
Expand Up @@ -52,7 +52,7 @@ main (int argc,
return 2;
}

token = p11_token_new (1, argv[1], "Label");
token = p11_token_new (1, argv[1], "Label", P11_TOKEN_FLAG_NONE);
count = p11_token_load (token);

printf ("%d files loaded\n", count);
Expand Down
12 changes: 8 additions & 4 deletions trust/module.c
Expand Up @@ -198,10 +198,11 @@ create_tokens_inlock (p11_array *tokens,
struct {
const char *prefix;
const char *label;
int flags;
} labels[] = {
{ "~/", "User Trust" },
{ DATA_DIR, "Default Trust" },
{ SYSCONFDIR, "System Trust" },
{ "~/", "User Trust", P11_TOKEN_FLAG_NONE },
{ P11_DEFAULT_TRUST_PREFIX, "Default Trust", P11_TOKEN_FLAG_WRITE_PROTECTED },
{ P11_SYSTEM_TRUST_PREFIX, "System Trust", P11_TOKEN_FLAG_NONE },
{ NULL },
};

Expand All @@ -210,6 +211,7 @@ create_tokens_inlock (p11_array *tokens,
CK_SLOT_ID slot;
const char *path;
const char *label;
int flags;
char *alloc;
char *remaining;
char *base;
Expand All @@ -236,12 +238,14 @@ create_tokens_inlock (p11_array *tokens,
slot = BASE_SLOT_ID + tokens->num;

label = NULL;
flags = P11_TOKEN_FLAG_NONE;
base = NULL;

/* Claim the various labels based on prefix */
for (i = 0; label == NULL && labels[i].prefix != NULL; i++) {
if (strncmp (path, labels[i].prefix, strlen (labels[i].prefix)) == 0) {
label = labels[i].label;
flags = labels[i].flags;
labels[i].label = NULL;
}
}
Expand All @@ -252,7 +256,7 @@ create_tokens_inlock (p11_array *tokens,
return_val_if_fail (base != NULL, false);
}

token = p11_token_new (slot, path, label);
token = p11_token_new (slot, path, label, flags);
return_val_if_fail (token != NULL, false);

if (!p11_array_push (tokens, token))
Expand Down
69 changes: 67 additions & 2 deletions trust/test-module.c
Expand Up @@ -315,8 +315,8 @@ test_get_token_info (void)

memset (&args, 0, sizeof (args));
args.pReserved = "paths='" \
SYSCONFDIR "/trust/input" P11_PATH_SEP \
DATA_DIR "/trust/fixtures/blah" P11_PATH_SEP \
P11_SYSTEM_TRUST_PREFIX "/trust/input" P11_PATH_SEP \
P11_DEFAULT_TRUST_PREFIX "/trust/fixtures/blah" P11_PATH_SEP \
"/some/other/path/the-basename'";
args.flags = CKF_OS_LOCKING_OK;

Expand Down Expand Up @@ -1217,6 +1217,68 @@ test_modify_and_write (void)
test_check_attrs (expected, parsed->elem[0]);
}

static void
test_token_write_protected (void)
{
CK_C_INITIALIZE_ARGS args;
CK_FUNCTION_LIST *module;
CK_SLOT_ID slots[NUM_SLOTS];
CK_TOKEN_INFO info;
char label[32];
CK_ULONG count;
CK_RV rv;
int i;

/* These are the paths passed in in setup() */
const char *labels[] = {
"System Trust",
"Default Trust",
"the-basename",
};

/* This is the entry point of the trust module, linked to this test */
rv = C_GetFunctionList (&module);
assert (rv == CKR_OK);

memset (&args, 0, sizeof (args));
args.pReserved = "paths='" \
P11_SYSTEM_TRUST_PREFIX "/trust/input" P11_PATH_SEP \
P11_DEFAULT_TRUST_PREFIX "/trust/fixtures/blah" P11_PATH_SEP \
"/some/other/path/the-basename'";
args.flags = CKF_OS_LOCKING_OK;

rv = module->C_Initialize (&args);
assert (rv == CKR_OK);

count = NUM_SLOTS;
rv = module->C_GetSlotList (CK_TRUE, slots, &count);
assert (rv == CKR_OK);
assert (count == NUM_SLOTS);

for (i = 0; i < NUM_SLOTS; i++) {
rv = module->C_GetTokenInfo (slots[i], &info);
assert_num_eq (CKR_OK, rv);

memset (label, ' ', sizeof (label));
memcpy (label, labels[i], strlen (labels[i]));
assert (memcmp (info.label, label, sizeof (label)) == 0);

switch (i) {
case 0:
assert_num_cmp (0, ==, info.flags & CKF_WRITE_PROTECTED);
break;
case 1:
assert_num_cmp (0, !=, info.flags & CKF_WRITE_PROTECTED);
break;
default:
break;
}
}

rv = module->C_Finalize (NULL);
assert_num_eq (CKR_OK, rv);
}

int
main (int argc,
char *argv[])
Expand Down Expand Up @@ -1257,5 +1319,8 @@ main (int argc,
p11_test (test_create_and_write, "/module/create-and-write");
p11_test (test_modify_and_write, "/module/modify-and-write");

p11_fixture (NULL, NULL);
p11_test (test_token_write_protected, "/module/token-write-protected");

return p11_test_run (argc, argv);
}
10 changes: 5 additions & 5 deletions trust/test-token.c
Expand Up @@ -63,7 +63,7 @@ struct {
static void
setup (void *path)
{
test.token = p11_token_new (333, path, "Label");
test.token = p11_token_new (333, path, "Label", P11_TOKEN_FLAG_NONE);
assert_ptr_not_null (test.token);

test.index = p11_token_index (test.token);
Expand Down Expand Up @@ -241,18 +241,18 @@ test_not_writable (void)
#ifdef OS_UNIX
if (getuid () != 0) {
#endif
token = p11_token_new (333, "/", "Label");
token = p11_token_new (333, "/", "Label", P11_TOKEN_FLAG_NONE);
assert (!p11_token_is_writable (token));
p11_token_free (token);
#ifdef OS_UNIX
}
#endif

token = p11_token_new (333, "", "Label");
token = p11_token_new (333, "", "Label", P11_TOKEN_FLAG_NONE);
assert (!p11_token_is_writable (token));
p11_token_free (token);

token = p11_token_new (333, "/non-existant", "Label");
token = p11_token_new (333, "/non-existant", "Label", P11_TOKEN_FLAG_NONE);
assert (!p11_token_is_writable (token));
p11_token_free (token);
}
Expand All @@ -276,7 +276,7 @@ test_writable_no_exist (void)
path = p11_path_build (directory, "subdir", NULL);
assert (path != NULL);

token = p11_token_new (333, path, "Label");
token = p11_token_new (333, path, "Label", P11_TOKEN_FLAG_NONE);
free (path);

/* A writable directory since parent is writable */
Expand Down
9 changes: 8 additions & 1 deletion trust/token.c
Expand Up @@ -817,7 +817,8 @@ p11_token_free (p11_token *token)
p11_token *
p11_token_new (CK_SLOT_ID slot,
const char *path,
const char *label)
const char *label,
int flags)
{
p11_token *token;

Expand Down Expand Up @@ -859,6 +860,12 @@ p11_token_new (CK_SLOT_ID slot,

token->slot = slot;

if (flags & P11_TOKEN_FLAG_WRITE_PROTECTED) {
token->checked_path = true;
token->make_directory = false;
token->is_writable = false;
}

load_builtin_objects (token);

p11_debug ("token: %s: %s", token->label, token->path);
Expand Down
8 changes: 7 additions & 1 deletion trust/token.h
Expand Up @@ -40,11 +40,17 @@
#include "parser.h"
#include "pkcs11.h"

enum {
P11_TOKEN_FLAG_NONE = 0,
P11_TOKEN_FLAG_WRITE_PROTECTED = 1 << 0,
};

typedef struct _p11_token p11_token;

p11_token * p11_token_new (CK_SLOT_ID slot,
const char *path,
const char *label);
const char *label,
int flags);

void p11_token_free (p11_token *token);

Expand Down