pal_linux.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. int init_trusted_files (void);
  87. /* Function: load_trusted_file
  88. * checks if the file to be opened is trusted or allowed,
  89. * according to the setting in manifest
  90. *
  91. * file: file handle to be opened
  92. * stubptr: buffer for catching matched file stub.
  93. * sizeptr: size pointer
  94. * create: this file is newly created or not
  95. *
  96. * return: 0 succeed
  97. */
  98. int load_trusted_file
  99. (PAL_HANDLE file, sgx_stub_t ** stubptr, uint64_t * sizeptr, int create);
  100. enum {
  101. FILE_CHECK_POLICY_STRICT = 0,
  102. FILE_CHECK_POLICY_ALLOW_ALL_BUT_LOG,
  103. };
  104. int init_file_check_policy (void);
  105. int get_file_check_policy (void);
  106. int copy_and_verify_trusted_file (const char * path, const void * umem,
  107. uint64_t umem_start, uint64_t umem_end,
  108. void * buffer, uint64_t offset, uint64_t size,
  109. sgx_stub_t * stubs, uint64_t total_size);
  110. int init_trusted_children (void);
  111. int register_trusted_child (const char * uri, const char * mr_enclave_str);
  112. /* exchange and establish a 256-bit session key */
  113. int _DkStreamKeyExchange(PAL_HANDLE stream, PAL_SESSION_KEY* key);
  114. typedef uint8_t sgx_sign_data_t[48];
  115. /* enclave state used for generating report */
  116. extern struct pal_enclave_state {
  117. uint64_t enclave_flags; // Reserved for flags
  118. uint64_t enclave_id; // Unique identifier for authentication
  119. sgx_sign_data_t enclave_data; // Reserved for signing other data
  120. } __attribute__((packed)) pal_enclave_state;
  121. /*
  122. * sgx_verify_report: verify a CPU-signed report from another local enclave
  123. * @report: the buffer storing the report to verify
  124. */
  125. int sgx_verify_report(sgx_report_t* report);
  126. typedef int (*check_mr_enclave_t)(PAL_HANDLE, sgx_measurement_t*, struct pal_enclave_state*);
  127. /*
  128. * _DkStreamReportRequest, _DkStreamReportRespond:
  129. * Request and respond a local report on an RPC stream
  130. *
  131. * @stream: stream handle for sending and receiving messages
  132. * @data: data to sign in the outbound message
  133. * @check_mr_enclave: callback function for checking the measurement of the other end
  134. */
  135. int _DkStreamReportRequest(PAL_HANDLE stream, sgx_sign_data_t* data,
  136. check_mr_enclave_t check_mr_enclave);
  137. int _DkStreamReportRespond(PAL_HANDLE stream, sgx_sign_data_t* data,
  138. check_mr_enclave_t check_mr_enclave);
  139. int _DkStreamSecureInit(PAL_HANDLE stream, bool is_server, PAL_SESSION_KEY* session_key,
  140. LIB_SSL_CONTEXT** out_ssl_ctx);
  141. int _DkStreamSecureFree(LIB_SSL_CONTEXT* ssl_ctx);
  142. int _DkStreamSecureRead(LIB_SSL_CONTEXT* ssl_ctx, uint8_t* buf, size_t len);
  143. int _DkStreamSecureWrite(LIB_SSL_CONTEXT* ssl_ctx, const uint8_t* buf, size_t len);
  144. #include "sgx_arch.h"
  145. #define PAL_ENCLAVE_INITIALIZED 0x0001ULL
  146. extern struct pal_enclave_config {
  147. sgx_measurement_t mr_enclave;
  148. sgx_attributes_t enclave_attributes;
  149. void * enclave_key;
  150. } pal_enclave_config;
  151. #include <hex.h>
  152. #else
  153. int sgx_create_process(const char* uri, int nargs, const char** args, int* stream_fd, int* cargo_fd);
  154. #ifdef DEBUG
  155. # ifndef SIGCHLD
  156. # define SIGCHLD 17
  157. # endif
  158. # define ARCH_VFORK() \
  159. (pal_enclave.pal_sec.in_gdb ? \
  160. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK|SIGCHLD, 0, NULL, NULL) :\
  161. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  162. #else
  163. # define ARCH_VFORK() \
  164. (INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  165. #endif
  166. #endif /* IN_ENCLAVE */
  167. #define DBG_E 0x01
  168. #define DBG_I 0x02
  169. #define DBG_D 0x04
  170. #define DBG_S 0x08
  171. #define DBG_P 0x10
  172. #define DBG_M 0x20
  173. #ifdef DEBUG
  174. # define DBG_LEVEL (DBG_E|DBG_I|DBG_D|DBG_S)
  175. #else
  176. # define DBG_LEVEL (DBG_E)
  177. #endif
  178. #ifdef IN_ENCLAVE
  179. #define SGX_DBG(class, fmt...) \
  180. do { if ((class) & DBG_LEVEL) printf(fmt); } while (0)
  181. #else
  182. #include <pal_debug.h>
  183. #define SGX_DBG(class, fmt...) \
  184. do { if ((class) & DBG_LEVEL) pal_printf(fmt); } while (0)
  185. #endif
  186. #ifndef IN_ENCLAVE
  187. int clone(int (*__fn) (void* __arg), void* __child_stack,
  188. int __flags, const void* __arg, ...);
  189. #endif
  190. #endif /* PAL_LINUX_H */