edger8r, psw: Add [enclave_id] non-pointer attribute

Signed-off-by: Andras Slemmer <andras.slemmer@r3cev.com>
This commit is contained in:
Andras Slemmer 2017-02-06 10:56:16 +00:00
parent d89c9abfd2
commit 7844d40e00
6 changed files with 76 additions and 40 deletions

View File

@ -43,7 +43,7 @@
using namespace std;
int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread);
int do_ocall(const bridge_fn_t bridge, void *ms);
int do_ocall(const bridge_fn_t bridge, sgx_enclave_id_t enclave_id, void *ms);
CEnclave::CEnclave(CLoader &ldr)
: m_loader(ldr)
@ -193,7 +193,7 @@ int CEnclave::ocall(const unsigned int proc, const sgx_ocall_table_t *ocall_tabl
se_rdunlock(&m_rwlock);
bridge_fn_t bridge = reinterpret_cast<bridge_fn_t>(ocall_table->ocall[proc]);
error = do_ocall(bridge, ms);
error = do_ocall(bridge, m_enclave_id, ms);
if (!se_try_rdlock(&m_rwlock))
{

View File

@ -248,11 +248,11 @@ int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread
return status;
}
int do_ocall(const bridge_fn_t bridge, void *ms)
int do_ocall(const bridge_fn_t bridge, sgx_enclave_id_t enclave_id, void *ms)
{
int error = SGX_ERROR_UNEXPECTED;
error = bridge(ms);
error = bridge(enclave_id, ms);
save_and_clean_xfeature_regs(NULL);

View File

@ -36,6 +36,7 @@
#include "se_wrapper.h"
#include "util.h"
#include "sgx_error.h"
#include "sgx_eid.h"
#include "se_debugger_lib.h"
#include "se_lock.hpp"
#include <vector>
@ -43,7 +44,7 @@
using namespace std;
typedef int (*bridge_fn_t)(const void*);
typedef int (*bridge_fn_t)(sgx_enclave_id_t enclave_id, const void*);
class CEnclave;

View File

@ -86,9 +86,14 @@ type ptr_attr = {
pa_chkptr : bool; (* Whether to generate code to check pointer *)
}
type non_ptr_attr = {
npa_enclave_id : bool;
}
let default_non_ptr_attr = { npa_enclave_id = false; }
(* parameter type *)
type parameter_type =
| PTVal of atype (* Passed by value *)
| PTVal of atype * non_ptr_attr (* Passed by value *)
| PTPtr of atype * ptr_attr (* Passed by address *)
type call_conv = CC_CDECL | CC_STDCALL | CC_FASTCALL | CC_NONE
@ -241,7 +246,7 @@ let rec get_tystr (ty: atype) =
(* Get the plain `atype' from a `parameter_type'. *)
let get_param_atype (pt: parameter_type) =
match pt with
| PTVal t -> t
| PTVal (t, _) -> t
| PTPtr (t, _) -> t
(* Convert attr_value to string *)

View File

@ -31,6 +31,7 @@
open Printf
open Util (* for failwithf *)
open List
(* --------------------------------------------------------------------
* We first introduce a `parse_enclave_ast' function (see below) to
@ -192,6 +193,7 @@ let conv_array_to_ptr (pd: Ast.pdecl): Ast.pdecl =
let retval_name = "retval"
let retval_declr = { Ast.identifier = retval_name; Ast.array_dims = []; }
let eid_name = "eid"
let enclave_id_name = "enclave_id"
let ms_ptr_name = "pms"
let ms_struct_val = "ms"
let mk_ms_member_name (pname: string) = "ms_" ^ pname
@ -209,8 +211,8 @@ let mk_ubridge_name (file_shortnm: string) (funcname: string) =
sprintf "%s_%s" file_shortnm funcname
let mk_ubridge_proto (file_shortnm: string) (funcname: string) =
sprintf "static sgx_status_t SGX_CDECL %s(void* %s)"
(mk_ubridge_name file_shortnm funcname) ms_ptr_name
sprintf "static sgx_status_t SGX_CDECL %s(sgx_enclave_id_t %s, void* %s)"
(mk_ubridge_name file_shortnm funcname) enclave_id_name ms_ptr_name
(* Common macro definitions. *)
let common_macros = "#include <stdlib.h> /* for size_t */\n\n\
@ -237,6 +239,13 @@ let get_theader_name (file_shortnm: string) =
let get_tsource_name (file_shortnm: string) =
!g_trusted_dir ^ separator_str ^ file_shortnm ^ "_t.c"
let filter_non_enclave_id_params (params: Ast.pdecl list) =
filter (fun (p,_) ->
match p with
Ast.PTVal(_, npattr) -> not npattr.Ast.npa_enclave_id
| _ -> true
) params
(* Construct the string of structure definition *)
let mk_struct_decl (fs: string) (name: string) =
sprintf "typedef struct %s {\n%s} %s;\n" name fs name
@ -317,7 +326,7 @@ let get_param_tystr (pt: Ast.parameter_type) =
(* Generate marshaling structure definition *)
let gen_marshal_struct (fd: Ast.func_decl) (errno: string) =
let member_list_str = errno ^
let new_param_list = List.map conv_array_to_ptr fd.Ast.plist in
let new_param_list = filter_non_enclave_id_params (List.map conv_array_to_ptr fd.Ast.plist) in
List.fold_left (fun acc (pt, declr) ->
acc ^ mk_ms_member_decl pt declr) "" new_param_list in
let struct_name = mk_ms_struct_name fd.Ast.fname in
@ -326,7 +335,7 @@ let gen_marshal_struct (fd: Ast.func_decl) (errno: string) =
a marshaling struct. *)
Ast.Void -> if fd.Ast.plist = [] && errno = "" then ""
else mk_struct_decl member_list_str struct_name
| _ -> let rv_str = mk_ms_member_decl (Ast.PTVal fd.Ast.rtype) retval_declr
| _ -> let rv_str = mk_ms_member_decl (Ast.PTVal(fd.Ast.rtype, Ast.default_non_ptr_attr)) retval_declr
in mk_struct_decl (rv_str ^ member_list_str) struct_name
let gen_ecall_marshal_struct (tf: Ast.trusted_func) =
@ -457,7 +466,7 @@ let gen_entry_table (ec: enclave_content) =
*)
let gen_tproxy_proto (fd: Ast.func_decl) =
let parm_list =
match fd.Ast.plist with
match filter_non_enclave_id_params fd.Ast.plist with
[] -> ""
| x :: xs ->
List.fold_left (fun acc pd ->
@ -654,7 +663,9 @@ let add_foreign_array_ptrref
else arg
let mk_parm_name_ubridge (pt: Ast.parameter_type) (declr: Ast.declarator) =
add_foreign_array_ptrref mk_parm_name_raw pt declr
match pt with
Ast.PTVal(_, attr) when attr.Ast.npa_enclave_id -> enclave_id_name
| _ -> add_foreign_array_ptrref mk_parm_name_raw pt declr
let mk_parm_name_ext (pt: Ast.parameter_type) (declr: Ast.declarator) =
let name = declr.Ast.identifier in
@ -723,7 +734,7 @@ let fill_ms_field (isptr: bool) (pd: Ast.pdecl) =
in
if declr.Ast.array_dims = [] then
match pt with
Ast.PTVal(aty) -> assignment_str false aty
Ast.PTVal(aty, npattr) -> assignment_str false aty
| Ast.PTPtr(aty, pattr) ->
if pattr.Ast.pa_isary
then gen_setup_foreign_array aty
@ -1282,7 +1293,7 @@ let gen_func_tproxy (ufunc: Ast.untrusted_func) (idx: int) =
begin
func_body := local_vars :: !func_body;
func_body := ocalloc_ms_struct:: !func_body;
List.iter (fun pd -> func_body := tproxy_fill_ms_field pd :: !func_body) fd.Ast.plist;
List.iter (fun pd -> func_body := tproxy_fill_ms_field pd :: !func_body) (filter_non_enclave_id_params fd.Ast.plist);
func_body := ocall_with_ms :: !func_body;
if fd.Ast.rtype <> Ast.Void then func_body := update_retval :: !func_body;
List.fold_left (fun acc s -> acc ^ "\t" ^ s ^ "\n") func_open (List.rev !func_body) ^ func_close

View File

@ -175,6 +175,22 @@ let get_ptr_attr (attr_list: (string * Ast.attr_value) list) =
then check_invalid_ary_attr pattr
else check_invalid_ptr_size pattr |> check_ptr_dir
let get_non_ptr_attr (attr_list: (string * Ast.attr_value) list) =
let update_attr (key: string) (value: Ast.attr_value) (ores: Ast.non_ptr_attr option) =
match ores with
None -> None
| Some res -> match key with
"enclave_id" -> Some { res with Ast.npa_enclave_id = true; }
| _ -> Some res
in
let rec do_get_non_ptr_attr alist res_attr =
match alist with
[] -> res_attr
| (k,v) :: xs -> do_get_non_ptr_attr xs (update_attr k v res_attr)
in do_get_non_ptr_attr attr_list (Some Ast.default_non_ptr_attr)
(* Untrusted functions can have these attributes:
*
* a. 3 mutual exclusive calling convention specifier:
@ -366,9 +382,9 @@ param_type: attr_block all_type {
Ast.Ptr _ -> fun x -> Ast.PTPtr($2, get_ptr_attr $1)
| _ ->
if $1 <> [] then
let attr = get_ptr_attr $1 in
match $2 with
Ast.Foreign s ->
let attr = get_ptr_attr $1 in
if attr.Ast.pa_isptr || attr.Ast.pa_isary then fun x -> Ast.PTPtr($2, attr)
else
(* thinking about 'user_defined_type var[4]' *)
@ -377,12 +393,15 @@ param_type: attr_block all_type {
else failwithf "`%s' is considerred plain type but decorated with pointer attributes" s
| _ ->
fun is_ary ->
if is_ary then Ast.PTPtr($2, attr)
else failwithf "unexpected pointer attributes for `%s'" (Ast.get_tystr $2)
if is_ary then Ast.PTPtr($2, get_ptr_attr $1)
else
match get_non_ptr_attr $1 with
Some attr -> Ast.PTVal($2, attr)
| None -> failwithf "unexpected pointer attributes for `%s'" (Ast.get_tystr $2)
else
fun is_ary ->
if is_ary then Ast.PTPtr($2, get_ptr_attr [])
else Ast.PTVal $2
else Ast.PTVal($2, Ast.default_non_ptr_attr)
}
| all_type {
match $1 with
@ -390,7 +409,7 @@ param_type: attr_block all_type {
| _ ->
fun is_ary ->
if is_ary then Ast.PTPtr($1, get_ptr_attr [])
else Ast.PTVal $1
else Ast.PTVal($1, Ast.default_non_ptr_attr)
}
| attr_block Tconst type_spec pointer {
let attr = get_ptr_attr $1
@ -551,7 +570,7 @@ parameter_def: param_type declarator {
let pt = $1 (Ast.is_array $2) in
let is_void =
match pt with
Ast.PTVal v -> v = Ast.Void
Ast.PTVal(v, _) -> v = Ast.Void
| _ -> false
in
if is_void then