pal_linux.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "pal_defs.h"
  16. #include "pal_linux_defs.h"
  17. #include "pal.h"
  18. #include "api.h"
  19. #include "linux_types.h"
  20. #include "sgx_arch.h"
  21. #include "sgx_tls.h"
  22. #include "sgx_api.h"
  23. #include "sgx_attest.h"
  24. #include "enclave_ocalls.h"
  25. #include <linux/mman.h>
  26. #ifdef __x86_64__
  27. # include "sysdep-x86_64.h"
  28. #endif
  29. #define ENCLAVE_FILENAME RUNTIME_FILE("libpal-Linux-SGX.so")
  30. #define IS_ERR INTERNAL_SYSCALL_ERROR
  31. #define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
  32. #define ERRNO INTERNAL_SYSCALL_ERRNO
  33. #define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
  34. extern struct pal_linux_state {
  35. PAL_NUM parent_process_id;
  36. PAL_NUM process_id;
  37. const char ** environ;
  38. /* credentials */
  39. unsigned int uid, gid;
  40. /* currently enabled signals */
  41. __sigset_t sigset;
  42. __sigset_t blocked_signals;
  43. /* enclave */
  44. const char * runtime_dir;
  45. } linux_state;
  46. #include <asm/mman.h>
  47. #define PRESET_PAGESIZE (1 << 12)
  48. #define DEFAULT_BACKLOG 2048
  49. static inline int HOST_FLAGS (int alloc_type, int prot)
  50. {
  51. return ((alloc_type & PAL_ALLOC_RESERVE) ? MAP_NORESERVE|MAP_UNINITIALIZED : 0) |
  52. ((prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
  53. }
  54. static inline int HOST_PROT (int prot)
  55. {
  56. return prot & (PAL_PROT_READ|PAL_PROT_WRITE|PAL_PROT_EXEC);
  57. }
  58. #define ACCESS_R 4
  59. #define ACCESS_W 2
  60. #define ACCESS_X 1
  61. struct stat;
  62. bool stataccess (struct stat * stats, int acc);
  63. #ifdef IN_ENCLAVE
  64. struct pal_sec;
  65. void pal_linux_main(char * uptr_args, size_t args_size,
  66. char * uptr_env, size_t env_size,
  67. struct pal_sec * uptr_sec_info);
  68. void pal_start_thread (void);
  69. /* Locking and unlocking of Mutexes */
  70. int __DkMutexCreate (struct mutex_handle * mut);
  71. int _DkMutexAtomicCreate (struct mutex_handle * mut);
  72. int __DkMutexDestroy (struct mutex_handle * mut);
  73. int _DkMutexLock(struct mutex_handle* mut);
  74. int _DkMutexLockTimeout(struct mutex_handle* mut, int64_t timeout_us);
  75. int _DkMutexUnlock (struct mutex_handle * mut);
  76. int * get_futex (void);
  77. void free_futex (int * futex);
  78. extern char __text_start, __text_end, __data_start, __data_end;
  79. #define TEXT_START ((void*)(&__text_start))
  80. #define TEXT_END ((void*)(&__text_end))
  81. #define DATA_START ((void*)(&__text_start))
  82. #define DATA_END ((void*)(&__text_end))
  83. typedef struct { char bytes[32]; } sgx_checksum_t;
  84. typedef struct { char bytes[16]; } sgx_stub_t;
  85. int init_trusted_files (void);
  86. /* Function: load_trusted_file
  87. * checks if the file to be opened is trusted or allowed,
  88. * according to the setting in manifest
  89. *
  90. * file: file handle to be opened
  91. * stubptr: buffer for catching matched file stub.
  92. * sizeptr: size pointer
  93. * create: this file is newly created or not
  94. *
  95. * return: 0 succeed
  96. */
  97. int load_trusted_file
  98. (PAL_HANDLE file, sgx_stub_t ** stubptr, uint64_t * sizeptr, int create);
  99. enum {
  100. FILE_CHECK_POLICY_STRICT = 0,
  101. FILE_CHECK_POLICY_ALLOW_ALL_BUT_LOG,
  102. };
  103. int init_file_check_policy (void);
  104. int get_file_check_policy (void);
  105. int copy_and_verify_trusted_file (const char * path, const void * umem,
  106. uint64_t umem_start, uint64_t umem_end,
  107. void * buffer, uint64_t offset, uint64_t size,
  108. sgx_stub_t * stubs, uint64_t total_size);
  109. int init_trusted_children (void);
  110. int register_trusted_child (const char * uri, const char * mr_enclave_str);
  111. /* exchange and establish a 256-bit session key */
  112. int _DkStreamKeyExchange(PAL_HANDLE stream, PAL_SESSION_KEY* key);
  113. typedef uint8_t sgx_sign_data_t[48];
  114. /* enclave state used for generating report */
  115. extern struct pal_enclave_state {
  116. uint64_t enclave_flags; // Reserved for flags
  117. uint64_t enclave_id; // Unique identifier for authentication
  118. sgx_sign_data_t enclave_data; // Reserved for signing other data
  119. } __attribute__((packed)) pal_enclave_state;
  120. /*
  121. * sgx_verify_report: verify a CPU-signed report from another local enclave
  122. * @report: the buffer storing the report to verify
  123. */
  124. int sgx_verify_report(sgx_report_t* report);
  125. typedef int (*check_mr_enclave_t)(PAL_HANDLE, sgx_measurement_t*, struct pal_enclave_state*);
  126. /*
  127. * _DkStreamReportRequest, _DkStreamReportRespond:
  128. * Request and respond a local report on an RPC stream
  129. *
  130. * @stream: stream handle for sending and receiving messages
  131. * @data: data to sign in the outbound message
  132. * @check_mr_enclave: callback function for checking the measurement of the other end
  133. */
  134. int _DkStreamReportRequest(PAL_HANDLE stream, sgx_sign_data_t* data,
  135. check_mr_enclave_t check_mr_enclave);
  136. int _DkStreamReportRespond(PAL_HANDLE stream, sgx_sign_data_t* data,
  137. check_mr_enclave_t check_mr_enclave);
  138. #include "sgx_arch.h"
  139. #define PAL_ENCLAVE_INITIALIZED 0x0001ULL
  140. extern struct pal_enclave_config {
  141. sgx_measurement_t mr_enclave;
  142. sgx_attributes_t enclave_attributes;
  143. void * enclave_key;
  144. } pal_enclave_config;
  145. #include <hex.h>
  146. #else
  147. int sgx_create_process(const char* uri, int nargs, const char** args, int* retfds);
  148. #ifdef DEBUG
  149. # ifndef SIGCHLD
  150. # define SIGCHLD 17
  151. # endif
  152. # define ARCH_VFORK() \
  153. (current_enclave->pal_sec.in_gdb ? \
  154. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK|SIGCHLD, 0, NULL, NULL) :\
  155. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  156. #else
  157. # define ARCH_VFORK() \
  158. (INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  159. #endif
  160. #endif /* IN_ENCLAVE */
  161. #define DBG_E 0x01
  162. #define DBG_I 0x02
  163. #define DBG_D 0x04
  164. #define DBG_S 0x08
  165. #define DBG_P 0x10
  166. #define DBG_M 0x20
  167. #ifdef DEBUG
  168. # define DBG_LEVEL (DBG_E|DBG_I|DBG_D|DBG_S)
  169. #else
  170. # define DBG_LEVEL (DBG_E)
  171. #endif
  172. #ifdef IN_ENCLAVE
  173. #define SGX_DBG(class, fmt...) \
  174. do { if ((class) & DBG_LEVEL) printf(fmt); } while (0)
  175. #else
  176. #include <pal_debug.h>
  177. #define SGX_DBG(class, fmt...) \
  178. do { if ((class) & DBG_LEVEL) pal_printf(fmt); } while (0)
  179. #endif
  180. #endif /* PAL_LINUX_H */