Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lua/zencode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ function ZEN:run()
deepmap(zenguard, ACK)
-- check that everythink in HEAP.ACK has a CODEC
self:codecguard()
self:keyringguard()
end

self.OK = true
Expand Down Expand Up @@ -858,6 +859,15 @@ function ZEN:codecguard()
end
return true
end
function ZEN:keyringguard()
local keys <const> = ACK.keyring
if not keys then return end
for k,v in pairs(keys) do
if not v:octet():is_secure() then -- sfpool check
error("Key out of secure memory: "..k)
end
end
end

------------------------------------------
-- ZENCODE STATEMENT DECLARATION FUNCTIONS
Expand Down
16 changes: 15 additions & 1 deletion src/sfpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ static inline void _secure_zero(void *ptr, uint32_t size) {

#if defined(__x86_64__) || defined(_M_X64) || defined(__ppc64__) || defined(__LP64__)
#define ptr_t uint64_t
#define ptr_align 8
#else
#define ptr_t uint32_t
#define ptr_align 4
#endif
#if !defined(__MUSL__)
static_assert(sizeof(ptr_t) == sizeof(void*), "Unknown memory pointer size detected");
Expand All @@ -82,6 +84,11 @@ static inline bool _is_in_pool(sfpool_t *pool, const void *ptr) {
return(p >= (ptr_t)pool->data
&& p < (ptr_t)(pool->data + pool->total_bytes));
}
static inline void* memalign(const void* ptr) {
register ptr_t mask = ptr_align - 1;
ptr_t aligned = (ptr_t)ptr + mask & ~mask;
return (void*)aligned;
}

// Create memory manager
size_t sfpool_init(sfpool_t *pool, size_t nmemb, size_t blocksize) {
Expand All @@ -92,7 +99,7 @@ size_t sfpool_init(sfpool_t *pool, size_t nmemb, size_t blocksize) {
pool->secure_lock = false;
size_t totalsize = nmemb * blocksize;
#if defined(__EMSCRIPTEN__)
pool->data = (uint8_t *)malloc(totalsize);
pool->data = (uint8_t *)memalign(malloc(totalsize+4));
#elif defined(_WIN32)
pool->data = VirtualAlloc(NULL, totalsize,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
Expand Down Expand Up @@ -260,6 +267,13 @@ void *sfpool_realloc(void *restrict opaque, void *ptr, const size_t size) {
}
}

int sfpool_contains(void *restrict opaque, const void *ptr) {
sfpool_t *pool = (sfpool_t*)opaque;
int res = 0;
if( _is_in_pool(pool,ptr) ) res = 1;
return res;
}

// Debug function to print memory manager state
void sfpool_status(sfpool_t *restrict p) {
fprintf(stderr,"\n🌊 sfpool: %u blocks %u B each\n",
Expand Down
18 changes: 16 additions & 2 deletions src/zen_octet.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@

#include <zenroom.h>

// from sfpool.h
extern int sfpool_contains(void *restrict opaque, const void *ptr);

// from segwit_addr.c
extern int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len);
extern int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr);
Expand Down Expand Up @@ -219,7 +222,7 @@ octet* o_new(lua_State *L, const int size) {
return NULL; }
luaL_getmetatable(L, "zenroom.octet");
lua_setmetatable(L, -2);
o->val = malloc(size +0x0f);
o->val = malloc(size+0x0f);
if(HEDLEY_UNLIKELY(o->val==NULL)) {
zerror(L, "Cannot create octet, malloc failure");
zerror(L, "%s: %s",__func__,strerror(errno));
Expand Down Expand Up @@ -2560,6 +2563,16 @@ static int mempaste(lua_State *L) {
END(1);
}

static int is_secure_memory(lua_State *L) {
BEGIN();
const octet *arg = o_arg(L,1);
lua_pushboolean
(L, sfpool_contains(ZMM,(void*)arg->val));
END(1);
}



int luaopen_octet(lua_State *L) {
(void)L;
const struct luaL_Reg octet_class[] = {
Expand Down Expand Up @@ -2635,7 +2648,7 @@ int luaopen_octet(lua_State *L) {
{"find", memfind},
{"copy", memcopy},
{"paste", mempaste},

{"is_secure", is_secure_memory},
{NULL,NULL}
};
const struct luaL_Reg octet_methods[] = {
Expand Down Expand Up @@ -2689,6 +2702,7 @@ int luaopen_octet(lua_State *L) {
{"find", memfind},
{"copy", memcopy},
{"paste", mempaste},
{"is_secure", is_secure_memory},
// {"zcash_topoint", zcash_topoint},
// idiomatic operators
{"__len",octet_size},
Expand Down
3 changes: 3 additions & 0 deletions test/lua/secure_mem.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
oo = O.random(64);
I.print(oo:is_secure())