pal_linux.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef PAL_LINUX_H
  14. #define PAL_LINUX_H
  15. #include "api.h"
  16. #include "pal.h"
  17. #include "pal_crypto.h"
  18. #include "pal_defs.h"
  19. #include "pal_linux_defs.h"
  20. #include "linux_types.h"
  21. #include "sgx_api.h"
  22. #include "sgx_arch.h"
  23. #include "sgx_attest.h"
  24. #include "sgx_tls.h"
  25. #include "enclave_ocalls.h"
  26. #include <linux/mman.h>
  27. #ifdef __x86_64__
  28. # include "sysdep-x86_64.h"
  29. #endif
  30. #define ENCLAVE_PAL_FILENAME RUNTIME_FILE("libpal-Linux-SGX.so")
  31. #define IS_ERR INTERNAL_SYSCALL_ERROR
  32. #define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
  33. #define ERRNO INTERNAL_SYSCALL_ERRNO
  34. #define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
  35. extern struct pal_linux_state {
  36. PAL_NUM parent_process_id;
  37. PAL_NUM process_id;
  38. const char ** environ;
  39. /* credentials */
  40. unsigned int uid, gid;
  41. /* currently enabled signals */
  42. __sigset_t sigset;
  43. __sigset_t blocked_signals;
  44. /* enclave */
  45. const char * runtime_dir;
  46. } linux_state;
  47. #include <asm/mman.h>
  48. #define PRESET_PAGESIZE (1 << 12)
  49. #define DEFAULT_BACKLOG 2048
  50. static inline int HOST_FLAGS (int alloc_type, int prot)
  51. {
  52. return ((alloc_type & PAL_ALLOC_RESERVE) ? MAP_NORESERVE|MAP_UNINITIALIZED : 0) |
  53. ((prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
  54. }
  55. static inline int HOST_PROT (int prot)
  56. {
  57. return prot & (PAL_PROT_READ|PAL_PROT_WRITE|PAL_PROT_EXEC);
  58. }
  59. #define ACCESS_R 4
  60. #define ACCESS_W 2
  61. #define ACCESS_X 1
  62. struct stat;
  63. bool stataccess (struct stat * stats, int acc);
  64. #ifdef IN_ENCLAVE
  65. struct pal_sec;
  66. void pal_linux_main(char * uptr_args, size_t args_size,
  67. char * uptr_env, size_t env_size,
  68. struct pal_sec * uptr_sec_info);
  69. void pal_start_thread (void);
  70. /* Locking and unlocking of Mutexes */
  71. int __DkMutexCreate (struct mutex_handle * mut);
  72. int _DkMutexAtomicCreate (struct mutex_handle * mut);
  73. int __DkMutexDestroy (struct mutex_handle * mut);
  74. int _DkMutexLock(struct mutex_handle* mut);
  75. int _DkMutexLockTimeout(struct mutex_handle* mut, int64_t timeout_us);
  76. int _DkMutexUnlock (struct mutex_handle * mut);
  77. int * get_futex (void);
  78. void free_futex (int * futex);
  79. extern char __text_start, __text_end, __data_start, __data_end;
  80. #define TEXT_START ((void*)(&__text_start))
  81. #define TEXT_END ((void*)(&__text_end))
  82. #define DATA_START ((void*)(&__text_start))
  83. #define DATA_END ((void*)(&__text_end))
  84. typedef struct { char bytes[32]; } sgx_checksum_t;
  85. typedef struct { char bytes[16]; } sgx_stub_t;
  86. extern int xsave_enabled;
  87. extern uint64_t xsave_features;
  88. extern uint32_t xsave_size;
  89. #define XSAVE_RESET_STATE_SIZE (512 + 64) // 512 for legacy regs, 64 for xsave header
  90. extern const uint32_t xsave_reset_state[];
  91. void init_xsave_size(uint64_t xfrm);
  92. void save_xregs(PAL_XREGS_STATE* xsave_area);
  93. void restore_xregs(const PAL_XREGS_STATE* xsave_area);
  94. noreturn void _restore_sgx_context(sgx_cpu_context_t* uc, PAL_XREGS_STATE* xsave_area);
  95. int init_trusted_files (void);
  96. /* Function: load_trusted_file
  97. * checks if the file to be opened is trusted or allowed,
  98. * according to the setting in manifest
  99. *
  100. * file: file handle to be opened
  101. * stubptr: buffer for catching matched file stub.
  102. * sizeptr: size pointer
  103. * create: this file is newly created or not
  104. *
  105. * return: 0 succeed
  106. */
  107. int load_trusted_file(PAL_HANDLE file, sgx_stub_t** stubptr, uint64_t* sizeptr, int create,
  108. void** umem);
  109. enum {
  110. FILE_CHECK_POLICY_STRICT = 0,
  111. FILE_CHECK_POLICY_ALLOW_ALL_BUT_LOG,
  112. };
  113. int init_file_check_policy (void);
  114. int get_file_check_policy (void);
  115. int copy_and_verify_trusted_file (const char * path, const void * umem,
  116. uint64_t umem_start, uint64_t umem_end,
  117. void * buffer, uint64_t offset, uint64_t size,
  118. sgx_stub_t * stubs, uint64_t total_size);
  119. int init_trusted_children (void);
  120. int register_trusted_child (const char * uri, const char * mr_enclave_str);
  121. /* exchange and establish a 256-bit session key */
  122. int _DkStreamKeyExchange(PAL_HANDLE stream, PAL_SESSION_KEY* key);
  123. typedef uint8_t sgx_sign_data_t[48];
  124. /* enclave state used for generating report */
  125. extern struct pal_enclave_state {
  126. uint64_t enclave_flags; // Reserved for flags
  127. uint64_t enclave_id; // Unique identifier for authentication
  128. sgx_sign_data_t enclave_data; // Reserved for signing other data
  129. } __attribute__((packed)) pal_enclave_state;
  130. /*
  131. * sgx_verify_report: verify a CPU-signed report from another local enclave
  132. * @report: the buffer storing the report to verify
  133. */
  134. int sgx_verify_report(sgx_report_t* report);
  135. typedef int (*check_mr_enclave_t)(PAL_HANDLE, sgx_measurement_t*, struct pal_enclave_state*);
  136. /*
  137. * _DkStreamReportRequest, _DkStreamReportRespond:
  138. * Request and respond a local report on an RPC stream
  139. *
  140. * @stream: stream handle for sending and receiving messages
  141. * @data: data to sign in the outbound message
  142. * @check_mr_enclave: callback function for checking the measurement of the other end
  143. */
  144. int _DkStreamReportRequest(PAL_HANDLE stream, sgx_sign_data_t* data,
  145. check_mr_enclave_t check_mr_enclave);
  146. int _DkStreamReportRespond(PAL_HANDLE stream, sgx_sign_data_t* data,
  147. check_mr_enclave_t check_mr_enclave);
  148. int _DkStreamSecureInit(PAL_HANDLE stream, bool is_server, PAL_SESSION_KEY* session_key,
  149. LIB_SSL_CONTEXT** out_ssl_ctx);
  150. int _DkStreamSecureFree(LIB_SSL_CONTEXT* ssl_ctx);
  151. int _DkStreamSecureRead(LIB_SSL_CONTEXT* ssl_ctx, uint8_t* buf, size_t len);
  152. int _DkStreamSecureWrite(LIB_SSL_CONTEXT* ssl_ctx, const uint8_t* buf, size_t len);
  153. #include "sgx_arch.h"
  154. #define PAL_ENCLAVE_INITIALIZED 0x0001ULL
  155. extern struct pal_enclave_config {
  156. sgx_measurement_t mr_enclave;
  157. sgx_attributes_t enclave_attributes;
  158. void * enclave_key;
  159. } pal_enclave_config;
  160. #include <hex.h>
  161. #else
  162. int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd, int* cargo_fd);
  163. #ifdef DEBUG
  164. # ifndef SIGCHLD
  165. # define SIGCHLD 17
  166. # endif
  167. # define ARCH_VFORK() \
  168. (pal_enclave.pal_sec.in_gdb ? \
  169. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK|SIGCHLD, 0, NULL, NULL) :\
  170. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  171. #else
  172. # define ARCH_VFORK() \
  173. (INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  174. #endif
  175. #endif /* IN_ENCLAVE */
  176. #define DBG_E 0x01
  177. #define DBG_I 0x02
  178. #define DBG_D 0x04
  179. #define DBG_S 0x08
  180. #define DBG_P 0x10
  181. #define DBG_M 0x20
  182. #ifdef DEBUG
  183. # define DBG_LEVEL (DBG_E|DBG_I|DBG_D|DBG_S)
  184. #else
  185. # define DBG_LEVEL (DBG_E)
  186. #endif
  187. #ifdef IN_ENCLAVE
  188. #define SGX_DBG(class, fmt...) \
  189. do { if ((class) & DBG_LEVEL) printf(fmt); } while (0)
  190. #else
  191. #include <pal_debug.h>
  192. #define SGX_DBG(class, fmt...) \
  193. do { if ((class) & DBG_LEVEL) pal_printf(fmt); } while (0)
  194. #endif
  195. #ifndef IN_ENCLAVE
  196. int clone(int (*__fn) (void* __arg), void* __child_stack,
  197. int __flags, const void* __arg, ...);
  198. #endif
  199. #endif /* PAL_LINUX_H */