From d155c165eb7e5ff68fe3a770829747052bd12afb Mon Sep 17 00:00:00 2001 From: Jean-Pierre de Villiers Date: Fri, 13 Jan 2023 13:06:21 +0200 Subject: [PATCH] feat(user-auth): initial implementation --- .gitignore | 6 + user-auth/Makefile | 52 + user-auth/README.md | 70 + user-auth/app/Cargo.lock | 396 ++ user-auth/app/Cargo.toml | 25 + user-auth/app/Enclave_u.c | 1241 +++++ user-auth/app/Enclave_u.h | 319 ++ user-auth/app/Makefile | 128 + user-auth/app/build.rs | 33 + user-auth/app/poetry.lock | 68 + user-auth/app/pyproject.toml | 18 + user-auth/app/src/ecall.rs | 107 + user-auth/app/src/lib.rs | 98 + user-auth/buildenv.mk | 167 + user-auth/enclave/Cargo.lock | 371 ++ user-auth/enclave/Cargo.toml | 24 + user-auth/enclave/Enclave.config.xml | 12 + user-auth/enclave/Enclave.edl | 50 + user-auth/enclave/Enclave.lds | 9 + user-auth/enclave/Enclave_t.c | 7009 ++++++++++++++++++++++++++ user-auth/enclave/Enclave_t.h | 105 + user-auth/enclave/Makefile | 146 + user-auth/enclave/src/lib.rs | 129 + 23 files changed, 10583 insertions(+) create mode 100644 .gitignore create mode 100644 user-auth/Makefile create mode 100644 user-auth/README.md create mode 100644 user-auth/app/Cargo.lock create mode 100644 user-auth/app/Cargo.toml create mode 100644 user-auth/app/Enclave_u.c create mode 100644 user-auth/app/Enclave_u.h create mode 100644 user-auth/app/Makefile create mode 100644 user-auth/app/build.rs create mode 100644 user-auth/app/poetry.lock create mode 100644 user-auth/app/pyproject.toml create mode 100644 user-auth/app/src/ecall.rs create mode 100644 user-auth/app/src/lib.rs create mode 100644 user-auth/buildenv.mk create mode 100644 user-auth/enclave/Cargo.lock create mode 100644 user-auth/enclave/Cargo.toml create mode 100644 user-auth/enclave/Enclave.config.xml create mode 100644 user-auth/enclave/Enclave.edl create mode 100644 user-auth/enclave/Enclave.lds create mode 100644 user-auth/enclave/Enclave_t.c create mode 100644 user-auth/enclave/Enclave_t.h create mode 100644 user-auth/enclave/Makefile create mode 100644 user-auth/enclave/src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4eb0a2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +**/build +**/enclave/target +**/app/target +**/app/.python-version +*.env +*.pem diff --git a/user-auth/Makefile b/user-auth/Makefile new file mode 100644 index 0000000..2883e8d --- /dev/null +++ b/user-auth/Makefile @@ -0,0 +1,52 @@ +# Dummy makefile, will call the host and enclave makefile when requested. + +SRC_U = app/ +SRC_T = enclave/ + +# Compilation process, will call the appropriate makefiles. + +all: host enclave + +install: host enclave + @make -C $(SRC_U) install + +uninstall: + @make -C $(SRC_U) uninstall + +host: + @echo "\033[32mRequest to compile the host part...\033[0m" + @make -C $(SRC_U) + +enclave: + @echo "\033[32mRequest to compile the enclave part...\033[0m" + @make -C $(SRC_T) + +clean: + @make -C $(SRC_U) clean + @make -C $(SRC_T) clean + +fclean: + @make -C $(SRC_U) fclean + @make -C $(SRC_T) fclean + +clean_host: + @make -C $(SRC_U) clean + +clean_enclave: + @make -C $(SRC_T) clean + +fclean_host: + @make -C $(SRC_U) fclean + +fclean_enclave: + @make -C $(SRC_T) fclean + +re_host: fclean_host host + +re_enclave: fclean_enclave enclave + +re: fclean all + +# Dummy rules to let make know that those rules are not files. + +.PHONY: host enclave clean clean_host clean_enclave fclean_host fclean_enclave fclean re re_host re_enclave install uninstall diff --git a/user-auth/README.md b/user-auth/README.md new file mode 100644 index 0000000..8d8aacf --- /dev/null +++ b/user-auth/README.md @@ -0,0 +1,70 @@ +# Rust SGX - Template project +================================== + +### This is a template project to start developing with the Rust SGX SDK (https://github.com/apache/incubator-teaclave-sgx-sdk/) easily. + +You will find in its template: +- Makefiles to build your project easily, and link the ```SGX EDL C``` generated files to your Rust SGX projects +- The file ```buildenv.mk``` that contains compilation rules when building enclave. No need to specify anymore where this file is located. +- The file ```build.rs``` already configured to build the app/host part properly. +- The file rust-toolchain, so we can force the use of one specific toolchain (```nightly-2020-10-25``` in this case) +- ```Cargo/Xargo.toml``` files to set up your project easily. All the dependencies you might need has been added. + +You can find those files in this template: + +``` +|-- app/ +| |-- src/ +| |-- main.rs +| |-- Cargo.toml +| |-- Makefile +| |-- build.rs +| +-- rust-toolchain +|-- enclave/ +| |-- src/ +| |-- lib.rs +| |-- Cargo.toml +| |-- Enclave.config.xml +| |-- Enclave.edl +| |-- Enclave.lds +| |-- Makefile +| |-- Xargo.toml +| +-- rust-toolchain +|-- Makefile ++-- buildenv.mk +``` + +## Setting up your project + +You need to follow a few steps to use this template properly: +- Add your ```.rs``` files to the ```src/``` folders (```lib.rs``` / your enclave source code goes in ```enclave/src```, your host/app source code goes in ```app/src```), or modify the ```.rs``` files already included with the project +- Add your own ```Enclave.edl``` file, or modify the one joined in the project. +- Change the ```Cargo.toml (or/and Xargo.toml if you want to use Xargo)``` files depending on of your needs (adding/removing dependencies). + - Be careful if you want to change the library name on the ```Cargo.toml``` file (enclave part), you will need to reflect this change on the enclave ```Makefile```, more specifically on the ```ENCLAVE_CARGO_LIB``` variable, and on the ```lib.rs``` file. + - If you need to change the app/host name, please make sure to edit the host ```Makefile```, and change the variable ```APP_U```. + +## Build your project + +### Before starting the building process, please make sure you downloaded the Rust SGX SDK repository, we're going to need the EDL and headers files joined in the SDK. + +Once you downloaded the Rust SGX SDK, you have multiple ways to start the building process: +- Run this command: ```CUSTOM_EDL_PATH=~/teaenclave/edl CUSTOM_COMMON_PATH=~/teaenclave/common make``` (replace ```~/teaenclave``` by the actual SDK location) +- You can also run the command export (```export CUSTOM_EDL_PATH=~/teaenclave/edl```), and specify the variables before calling make. It is advised to add this command on your ```.bashrc``` file (if you use bash), or your favorite shell configuration file. + +### By default, your project will be compiled in hardware mode. If you wish to compile your project in software/simulation mode, you will need to specify it, either by adding ```SGX_MODE=SW``` before make, or by setting the SGX_MODE variable environment to SW. + +### Cargo is used by default when compiling, but you can also use Xargo either by adding ```XARGO_SGX=1``` before make, or by setting the XARGO_SGX variable environment to 1. You will also need to specify Xargo library path with XARGO_PATH. + +### The makefile has those commands available: +- make (will compile everything) +- make host (will only compile the host part) +- make enclave (will only compile the enclave part) +- make clean (will clean the objects/C edl files generated) +- make clean_host (will clean the objects/C edl files generated for the host only) +- make clean_enclave (will clean the objects/C edl files generated for the enclave only) +- make fclean (will clean objects/C edl files and the binaries, plus calling cargo clean for everything) +- make fclean_host (will clean objects/C edl files and the binaries, plus calling cargo clean for the host only) +- make fclean_enclave (will clean objects/C edl files and the binaries, plus calling cargo clean for the enclave only) +- make re (re as relink, will clean everything then compile everything again) +- make re_host (re as relink, will clean the host part then compile it again) +- make re_enclave (re as relink, will clean the enclave part then compile it again) diff --git a/user-auth/app/Cargo.lock b/user-auth/app/Cargo.lock new file mode 100644 index 0000000..6692a57 --- /dev/null +++ b/user-auth/app/Cargo.lock @@ -0,0 +1,396 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base64ct" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "dotenvy" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0" + +[[package]] +name = "dotenvy_codegen_impl" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05611d49f33180c6989e426b1d63a5ac0651307f0459321b67b38cd9861d050" +dependencies = [ + "dotenvy", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dotenvy_macro" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f559fff643c8f40279ab9bc50dd3734c22efc1f386972995f52892fe414722a7" +dependencies = [ + "dotenvy_codegen_impl", + "proc-macro-hack", +] + +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "indoc" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2d6f23ffea9d7e76c53eee25dfb67bcd8fde7f1198b0855350698c9f07c780" + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memoffset" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] +name = "password-hash" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" +dependencies = [ + "base64ct", + "rand_core", + "subtle", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd4149c8c3975099622b4e1962dac27565cf5663b76452c3e2b66e0b6824277" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cd09fe469834db21ee60e0051030339e5d361293d8cb5ec02facf7fdcf52dbf" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c427c9a96b9c5b12156dbc11f76b14f49e9aae8905ca783ea87c249044ef137" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b822bbba9d60630a44d2109bc410489bb2f439b33e3a14ddeb8a40b378a7c4" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84ae898104f7c99db06231160770f3e40dad6eb9021daddc0fedfa3e41dff10a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "sgx_types" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#23ab3e05623ab9a9417a9c2908facf7e5a8197ac" + +[[package]] +name = "sgx_urts" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git#23ab3e05623ab9a9417a9c2908facf7e5a8197ac" +dependencies = [ + "libc", + "sgx_types", +] + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9410d0f6853b1d94f0e519fb95df60f29d2c1eff2d921ffdf01a4c8a3b54f12d" + +[[package]] +name = "thiserror" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "unindent" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" + +[[package]] +name = "user-auth-sgx" +version = "1.0.0" +dependencies = [ + "dotenvy_macro", + "password-hash", + "pyo3", + "sgx_types", + "sgx_urts", + "thiserror", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/user-auth/app/Cargo.toml b/user-auth/app/Cargo.toml new file mode 100644 index 0000000..23ba824 --- /dev/null +++ b/user-auth/app/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "user-auth-sgx" +version = "1.0.0" +authors = ["Nautilus"] +build = "build.rs" +edition = "2021" + +[lib] +# The name of the native library. This is the name which will be used in Python to import the +# library (i.e. `import string_sum`). If you change this, you must also change the name of the +# `#[pymodule]` in `src/lib.rs`. +name = "user_auth_sgx" + +# "cdylib" is necessary to produce a shared library for Python to import from. +crate-type = ["cdylib"] + +[dependencies] # You can specify the features you need for urts, such as global_exit and global_init +sgx_types = { git = "https://github.com/apache/teaclave-sgx-sdk.git" } +sgx_urts = { git = "https://github.com/apache/teaclave-sgx-sdk.git"} +pyo3 = { version = "0.18", features = ["extension-module"] } +dotenvy_macro = "0.15.1" +thiserror = "1.0.38" +password-hash = { version = "0.4.2", features = ["std"] } + +[patch.'https://github.com/apache/teaclave-sgx-sdk.git'] diff --git a/user-auth/app/Enclave_u.c b/user-auth/app/Enclave_u.c new file mode 100644 index 0000000..5ce7e07 --- /dev/null +++ b/user-auth/app/Enclave_u.c @@ -0,0 +1,1241 @@ +#include "Enclave_u.h" +#include + +typedef struct ms_hash_password_t { + sgx_status_t ms_retval; + const uint8_t* ms_password; + size_t ms_password_length; + const uint8_t* ms_user_id; + size_t ms_user_id_length; + uint8_t* ms_hash; + size_t* ms_hash_length; +} ms_hash_password_t; + +typedef struct ms_verify_password_t { + sgx_status_t ms_retval; + const uint8_t* ms_password; + size_t ms_password_length; + const uint8_t* ms_user_id; + size_t ms_user_id_length; + const uint8_t* ms_stored_hash; + size_t ms_hash_length; + uint8_t* ms_password_verified; +} ms_verify_password_t; + +typedef struct ms_t_global_init_ecall_t { + uint64_t ms_id; + const uint8_t* ms_path; + size_t ms_len; +} ms_t_global_init_ecall_t; + +typedef struct ms_u_thread_set_event_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_tcs; +} ms_u_thread_set_event_ocall_t; + +typedef struct ms_u_thread_wait_event_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_tcs; + const struct timespec* ms_timeout; +} ms_u_thread_wait_event_ocall_t; + +typedef struct ms_u_thread_set_multiple_events_ocall_t { + int ms_retval; + int* ms_error; + const void** ms_tcss; + int ms_total; +} ms_u_thread_set_multiple_events_ocall_t; + +typedef struct ms_u_thread_setwait_events_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_waiter_tcs; + const void* ms_self_tcs; + const struct timespec* ms_timeout; +} ms_u_thread_setwait_events_ocall_t; + +typedef struct ms_u_clock_gettime_ocall_t { + int ms_retval; + int* ms_error; + int ms_clk_id; + struct timespec* ms_tp; +} ms_u_clock_gettime_ocall_t; + +typedef struct ms_u_read_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + void* ms_buf; + size_t ms_count; +} ms_u_read_ocall_t; + +typedef struct ms_u_pread64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + void* ms_buf; + size_t ms_count; + int64_t ms_offset; +} ms_u_pread64_ocall_t; + +typedef struct ms_u_readv_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; +} ms_u_readv_ocall_t; + +typedef struct ms_u_preadv64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; + int64_t ms_offset; +} ms_u_preadv64_ocall_t; + +typedef struct ms_u_write_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const void* ms_buf; + size_t ms_count; +} ms_u_write_ocall_t; + +typedef struct ms_u_pwrite64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const void* ms_buf; + size_t ms_count; + int64_t ms_offset; +} ms_u_pwrite64_ocall_t; + +typedef struct ms_u_writev_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; +} ms_u_writev_ocall_t; + +typedef struct ms_u_pwritev64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; + int64_t ms_offset; +} ms_u_pwritev64_ocall_t; + +typedef struct ms_u_sendfile_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_out_fd; + int ms_in_fd; + int64_t* ms_offset; + size_t ms_count; +} ms_u_sendfile_ocall_t; + +typedef struct ms_u_copy_file_range_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd_in; + int64_t* ms_off_in; + int ms_fd_out; + int64_t* ms_off_out; + size_t ms_len; + unsigned int ms_flags; +} ms_u_copy_file_range_ocall_t; + +typedef struct ms_u_splice_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd_in; + int64_t* ms_off_in; + int ms_fd_out; + int64_t* ms_off_out; + size_t ms_len; + unsigned int ms_flags; +} ms_u_splice_ocall_t; + +typedef struct ms_u_fcntl_arg0_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_cmd; +} ms_u_fcntl_arg0_ocall_t; + +typedef struct ms_u_fcntl_arg1_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_cmd; + int ms_arg; +} ms_u_fcntl_arg1_ocall_t; + +typedef struct ms_u_ioctl_arg0_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_request; +} ms_u_ioctl_arg0_ocall_t; + +typedef struct ms_u_ioctl_arg1_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_request; + int* ms_arg; +} ms_u_ioctl_arg1_ocall_t; + +typedef struct ms_u_close_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_close_ocall_t; + +typedef struct ms_u_isatty_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_isatty_ocall_t; + +typedef struct ms_u_dup_ocall_t { + int ms_retval; + int* ms_error; + int ms_oldfd; +} ms_u_dup_ocall_t; + +typedef struct ms_u_eventfd_ocall_t { + int ms_retval; + int* ms_error; + unsigned int ms_initval; + int ms_flags; +} ms_u_eventfd_ocall_t; + +typedef struct ms_u_futimens_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + const struct timespec* ms_times; +} ms_u_futimens_ocall_t; + +typedef struct ms_u_malloc_ocall_t { + void* ms_retval; + int* ms_error; + size_t ms_size; +} ms_u_malloc_ocall_t; + +typedef struct ms_u_free_ocall_t { + void* ms_p; +} ms_u_free_ocall_t; + +typedef struct ms_u_mmap_ocall_t { + void* ms_retval; + int* ms_error; + void* ms_start; + size_t ms_length; + int ms_prot; + int ms_flags; + int ms_fd; + int64_t ms_offset; +} ms_u_mmap_ocall_t; + +typedef struct ms_u_munmap_ocall_t { + int ms_retval; + int* ms_error; + void* ms_start; + size_t ms_length; +} ms_u_munmap_ocall_t; + +typedef struct ms_u_msync_ocall_t { + int ms_retval; + int* ms_error; + void* ms_addr; + size_t ms_length; + int ms_flags; +} ms_u_msync_ocall_t; + +typedef struct ms_u_mprotect_ocall_t { + int ms_retval; + int* ms_error; + void* ms_addr; + size_t ms_length; + int ms_prot; +} ms_u_mprotect_ocall_t; + +typedef struct ms_u_open_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; + int ms_flags; +} ms_u_open_ocall_t; + +typedef struct ms_u_open64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int ms_oflag; + int ms_mode; +} ms_u_open64_ocall_t; + +typedef struct ms_u_openat_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + int ms_flags; +} ms_u_openat_ocall_t; + +typedef struct ms_u_fstat_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + struct stat_t* ms_buf; +} ms_u_fstat_ocall_t; + +typedef struct ms_u_fstat64_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + struct stat64_t* ms_buf; +} ms_u_fstat64_ocall_t; + +typedef struct ms_u_stat_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat_t* ms_buf; +} ms_u_stat_ocall_t; + +typedef struct ms_u_stat64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat64_t* ms_buf; +} ms_u_stat64_ocall_t; + +typedef struct ms_u_lstat_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat_t* ms_buf; +} ms_u_lstat_ocall_t; + +typedef struct ms_u_lstat64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat64_t* ms_buf; +} ms_u_lstat64_ocall_t; + +typedef struct ms_u_lseek_ocall_t { + uint64_t ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_offset; + int ms_whence; +} ms_u_lseek_ocall_t; + +typedef struct ms_u_lseek64_ocall_t { + int64_t ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_offset; + int ms_whence; +} ms_u_lseek64_ocall_t; + +typedef struct ms_u_ftruncate_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_length; +} ms_u_ftruncate_ocall_t; + +typedef struct ms_u_ftruncate64_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_length; +} ms_u_ftruncate64_ocall_t; + +typedef struct ms_u_truncate_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int64_t ms_length; +} ms_u_truncate_ocall_t; + +typedef struct ms_u_truncate64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int64_t ms_length; +} ms_u_truncate64_ocall_t; + +typedef struct ms_u_fsync_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fsync_ocall_t; + +typedef struct ms_u_fdatasync_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fdatasync_ocall_t; + +typedef struct ms_u_fchmod_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + uint32_t ms_mode; +} ms_u_fchmod_ocall_t; + +typedef struct ms_u_unlink_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_unlink_ocall_t; + +typedef struct ms_u_link_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_oldpath; + const char* ms_newpath; +} ms_u_link_ocall_t; + +typedef struct ms_u_unlinkat_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + int ms_flags; +} ms_u_unlinkat_ocall_t; + +typedef struct ms_u_linkat_ocall_t { + int ms_retval; + int* ms_error; + int ms_olddirfd; + const char* ms_oldpath; + int ms_newdirfd; + const char* ms_newpath; + int ms_flags; +} ms_u_linkat_ocall_t; + +typedef struct ms_u_rename_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_oldpath; + const char* ms_newpath; +} ms_u_rename_ocall_t; + +typedef struct ms_u_chmod_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + uint32_t ms_mode; +} ms_u_chmod_ocall_t; + +typedef struct ms_u_readlink_ocall_t { + size_t ms_retval; + int* ms_error; + const char* ms_path; + char* ms_buf; + size_t ms_bufsz; +} ms_u_readlink_ocall_t; + +typedef struct ms_u_symlink_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path1; + const char* ms_path2; +} ms_u_symlink_ocall_t; + +typedef struct ms_u_realpath_ocall_t { + char* ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_realpath_ocall_t; + +typedef struct ms_u_mkdir_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; + uint32_t ms_mode; +} ms_u_mkdir_ocall_t; + +typedef struct ms_u_rmdir_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_rmdir_ocall_t; + +typedef struct ms_u_fdopendir_ocall_t { + void* ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fdopendir_ocall_t; + +typedef struct ms_u_opendir_ocall_t { + void* ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_opendir_ocall_t; + +typedef struct ms_u_readdir64_r_ocall_t { + int ms_retval; + void* ms_dirp; + struct dirent64_t* ms_entry; + struct dirent64_t** ms_result; +} ms_u_readdir64_r_ocall_t; + +typedef struct ms_u_closedir_ocall_t { + int ms_retval; + int* ms_error; + void* ms_dirp; +} ms_u_closedir_ocall_t; + +typedef struct ms_u_dirfd_ocall_t { + int ms_retval; + int* ms_error; + void* ms_dirp; +} ms_u_dirfd_ocall_t; + +typedef struct ms_u_fstatat64_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + struct stat64_t* ms_buf; + int ms_flags; +} ms_u_fstatat64_ocall_t; + +typedef struct ms_sgx_oc_cpuidex_t { + int* ms_cpuinfo; + int ms_leaf; + int ms_subleaf; +} ms_sgx_oc_cpuidex_t; + +typedef struct ms_sgx_thread_wait_untrusted_event_ocall_t { + int ms_retval; + const void* ms_self; +} ms_sgx_thread_wait_untrusted_event_ocall_t; + +typedef struct ms_sgx_thread_set_untrusted_event_ocall_t { + int ms_retval; + const void* ms_waiter; +} ms_sgx_thread_set_untrusted_event_ocall_t; + +typedef struct ms_sgx_thread_setwait_untrusted_events_ocall_t { + int ms_retval; + const void* ms_waiter; + const void* ms_self; +} ms_sgx_thread_setwait_untrusted_events_ocall_t; + +typedef struct ms_sgx_thread_set_multiple_untrusted_events_ocall_t { + int ms_retval; + const void** ms_waiters; + size_t ms_total; +} ms_sgx_thread_set_multiple_untrusted_events_ocall_t; + +static sgx_status_t SGX_CDECL Enclave_u_thread_set_event_ocall(void* pms) +{ + ms_u_thread_set_event_ocall_t* ms = SGX_CAST(ms_u_thread_set_event_ocall_t*, pms); + ms->ms_retval = u_thread_set_event_ocall(ms->ms_error, ms->ms_tcs); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_thread_wait_event_ocall(void* pms) +{ + ms_u_thread_wait_event_ocall_t* ms = SGX_CAST(ms_u_thread_wait_event_ocall_t*, pms); + ms->ms_retval = u_thread_wait_event_ocall(ms->ms_error, ms->ms_tcs, ms->ms_timeout); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_thread_set_multiple_events_ocall(void* pms) +{ + ms_u_thread_set_multiple_events_ocall_t* ms = SGX_CAST(ms_u_thread_set_multiple_events_ocall_t*, pms); + ms->ms_retval = u_thread_set_multiple_events_ocall(ms->ms_error, ms->ms_tcss, ms->ms_total); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_thread_setwait_events_ocall(void* pms) +{ + ms_u_thread_setwait_events_ocall_t* ms = SGX_CAST(ms_u_thread_setwait_events_ocall_t*, pms); + ms->ms_retval = u_thread_setwait_events_ocall(ms->ms_error, ms->ms_waiter_tcs, ms->ms_self_tcs, ms->ms_timeout); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_clock_gettime_ocall(void* pms) +{ + ms_u_clock_gettime_ocall_t* ms = SGX_CAST(ms_u_clock_gettime_ocall_t*, pms); + ms->ms_retval = u_clock_gettime_ocall(ms->ms_error, ms->ms_clk_id, ms->ms_tp); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_read_ocall(void* pms) +{ + ms_u_read_ocall_t* ms = SGX_CAST(ms_u_read_ocall_t*, pms); + ms->ms_retval = u_read_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf, ms->ms_count); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_pread64_ocall(void* pms) +{ + ms_u_pread64_ocall_t* ms = SGX_CAST(ms_u_pread64_ocall_t*, pms); + ms->ms_retval = u_pread64_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf, ms->ms_count, ms->ms_offset); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_readv_ocall(void* pms) +{ + ms_u_readv_ocall_t* ms = SGX_CAST(ms_u_readv_ocall_t*, pms); + ms->ms_retval = u_readv_ocall(ms->ms_error, ms->ms_fd, ms->ms_iov, ms->ms_iovcnt); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_preadv64_ocall(void* pms) +{ + ms_u_preadv64_ocall_t* ms = SGX_CAST(ms_u_preadv64_ocall_t*, pms); + ms->ms_retval = u_preadv64_ocall(ms->ms_error, ms->ms_fd, ms->ms_iov, ms->ms_iovcnt, ms->ms_offset); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_write_ocall(void* pms) +{ + ms_u_write_ocall_t* ms = SGX_CAST(ms_u_write_ocall_t*, pms); + ms->ms_retval = u_write_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf, ms->ms_count); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_pwrite64_ocall(void* pms) +{ + ms_u_pwrite64_ocall_t* ms = SGX_CAST(ms_u_pwrite64_ocall_t*, pms); + ms->ms_retval = u_pwrite64_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf, ms->ms_count, ms->ms_offset); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_writev_ocall(void* pms) +{ + ms_u_writev_ocall_t* ms = SGX_CAST(ms_u_writev_ocall_t*, pms); + ms->ms_retval = u_writev_ocall(ms->ms_error, ms->ms_fd, ms->ms_iov, ms->ms_iovcnt); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_pwritev64_ocall(void* pms) +{ + ms_u_pwritev64_ocall_t* ms = SGX_CAST(ms_u_pwritev64_ocall_t*, pms); + ms->ms_retval = u_pwritev64_ocall(ms->ms_error, ms->ms_fd, ms->ms_iov, ms->ms_iovcnt, ms->ms_offset); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_sendfile_ocall(void* pms) +{ + ms_u_sendfile_ocall_t* ms = SGX_CAST(ms_u_sendfile_ocall_t*, pms); + ms->ms_retval = u_sendfile_ocall(ms->ms_error, ms->ms_out_fd, ms->ms_in_fd, ms->ms_offset, ms->ms_count); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_copy_file_range_ocall(void* pms) +{ + ms_u_copy_file_range_ocall_t* ms = SGX_CAST(ms_u_copy_file_range_ocall_t*, pms); + ms->ms_retval = u_copy_file_range_ocall(ms->ms_error, ms->ms_fd_in, ms->ms_off_in, ms->ms_fd_out, ms->ms_off_out, ms->ms_len, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_splice_ocall(void* pms) +{ + ms_u_splice_ocall_t* ms = SGX_CAST(ms_u_splice_ocall_t*, pms); + ms->ms_retval = u_splice_ocall(ms->ms_error, ms->ms_fd_in, ms->ms_off_in, ms->ms_fd_out, ms->ms_off_out, ms->ms_len, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fcntl_arg0_ocall(void* pms) +{ + ms_u_fcntl_arg0_ocall_t* ms = SGX_CAST(ms_u_fcntl_arg0_ocall_t*, pms); + ms->ms_retval = u_fcntl_arg0_ocall(ms->ms_error, ms->ms_fd, ms->ms_cmd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fcntl_arg1_ocall(void* pms) +{ + ms_u_fcntl_arg1_ocall_t* ms = SGX_CAST(ms_u_fcntl_arg1_ocall_t*, pms); + ms->ms_retval = u_fcntl_arg1_ocall(ms->ms_error, ms->ms_fd, ms->ms_cmd, ms->ms_arg); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_ioctl_arg0_ocall(void* pms) +{ + ms_u_ioctl_arg0_ocall_t* ms = SGX_CAST(ms_u_ioctl_arg0_ocall_t*, pms); + ms->ms_retval = u_ioctl_arg0_ocall(ms->ms_error, ms->ms_fd, ms->ms_request); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_ioctl_arg1_ocall(void* pms) +{ + ms_u_ioctl_arg1_ocall_t* ms = SGX_CAST(ms_u_ioctl_arg1_ocall_t*, pms); + ms->ms_retval = u_ioctl_arg1_ocall(ms->ms_error, ms->ms_fd, ms->ms_request, ms->ms_arg); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_close_ocall(void* pms) +{ + ms_u_close_ocall_t* ms = SGX_CAST(ms_u_close_ocall_t*, pms); + ms->ms_retval = u_close_ocall(ms->ms_error, ms->ms_fd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_isatty_ocall(void* pms) +{ + ms_u_isatty_ocall_t* ms = SGX_CAST(ms_u_isatty_ocall_t*, pms); + ms->ms_retval = u_isatty_ocall(ms->ms_error, ms->ms_fd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_dup_ocall(void* pms) +{ + ms_u_dup_ocall_t* ms = SGX_CAST(ms_u_dup_ocall_t*, pms); + ms->ms_retval = u_dup_ocall(ms->ms_error, ms->ms_oldfd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_eventfd_ocall(void* pms) +{ + ms_u_eventfd_ocall_t* ms = SGX_CAST(ms_u_eventfd_ocall_t*, pms); + ms->ms_retval = u_eventfd_ocall(ms->ms_error, ms->ms_initval, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_futimens_ocall(void* pms) +{ + ms_u_futimens_ocall_t* ms = SGX_CAST(ms_u_futimens_ocall_t*, pms); + ms->ms_retval = u_futimens_ocall(ms->ms_error, ms->ms_fd, ms->ms_times); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_malloc_ocall(void* pms) +{ + ms_u_malloc_ocall_t* ms = SGX_CAST(ms_u_malloc_ocall_t*, pms); + ms->ms_retval = u_malloc_ocall(ms->ms_error, ms->ms_size); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_free_ocall(void* pms) +{ + ms_u_free_ocall_t* ms = SGX_CAST(ms_u_free_ocall_t*, pms); + u_free_ocall(ms->ms_p); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_mmap_ocall(void* pms) +{ + ms_u_mmap_ocall_t* ms = SGX_CAST(ms_u_mmap_ocall_t*, pms); + ms->ms_retval = u_mmap_ocall(ms->ms_error, ms->ms_start, ms->ms_length, ms->ms_prot, ms->ms_flags, ms->ms_fd, ms->ms_offset); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_munmap_ocall(void* pms) +{ + ms_u_munmap_ocall_t* ms = SGX_CAST(ms_u_munmap_ocall_t*, pms); + ms->ms_retval = u_munmap_ocall(ms->ms_error, ms->ms_start, ms->ms_length); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_msync_ocall(void* pms) +{ + ms_u_msync_ocall_t* ms = SGX_CAST(ms_u_msync_ocall_t*, pms); + ms->ms_retval = u_msync_ocall(ms->ms_error, ms->ms_addr, ms->ms_length, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_mprotect_ocall(void* pms) +{ + ms_u_mprotect_ocall_t* ms = SGX_CAST(ms_u_mprotect_ocall_t*, pms); + ms->ms_retval = u_mprotect_ocall(ms->ms_error, ms->ms_addr, ms->ms_length, ms->ms_prot); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_open_ocall(void* pms) +{ + ms_u_open_ocall_t* ms = SGX_CAST(ms_u_open_ocall_t*, pms); + ms->ms_retval = u_open_ocall(ms->ms_error, ms->ms_pathname, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_open64_ocall(void* pms) +{ + ms_u_open64_ocall_t* ms = SGX_CAST(ms_u_open64_ocall_t*, pms); + ms->ms_retval = u_open64_ocall(ms->ms_error, ms->ms_path, ms->ms_oflag, ms->ms_mode); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_openat_ocall(void* pms) +{ + ms_u_openat_ocall_t* ms = SGX_CAST(ms_u_openat_ocall_t*, pms); + ms->ms_retval = u_openat_ocall(ms->ms_error, ms->ms_dirfd, ms->ms_pathname, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fstat_ocall(void* pms) +{ + ms_u_fstat_ocall_t* ms = SGX_CAST(ms_u_fstat_ocall_t*, pms); + ms->ms_retval = u_fstat_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fstat64_ocall(void* pms) +{ + ms_u_fstat64_ocall_t* ms = SGX_CAST(ms_u_fstat64_ocall_t*, pms); + ms->ms_retval = u_fstat64_ocall(ms->ms_error, ms->ms_fd, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_stat_ocall(void* pms) +{ + ms_u_stat_ocall_t* ms = SGX_CAST(ms_u_stat_ocall_t*, pms); + ms->ms_retval = u_stat_ocall(ms->ms_error, ms->ms_path, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_stat64_ocall(void* pms) +{ + ms_u_stat64_ocall_t* ms = SGX_CAST(ms_u_stat64_ocall_t*, pms); + ms->ms_retval = u_stat64_ocall(ms->ms_error, ms->ms_path, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_lstat_ocall(void* pms) +{ + ms_u_lstat_ocall_t* ms = SGX_CAST(ms_u_lstat_ocall_t*, pms); + ms->ms_retval = u_lstat_ocall(ms->ms_error, ms->ms_path, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_lstat64_ocall(void* pms) +{ + ms_u_lstat64_ocall_t* ms = SGX_CAST(ms_u_lstat64_ocall_t*, pms); + ms->ms_retval = u_lstat64_ocall(ms->ms_error, ms->ms_path, ms->ms_buf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_lseek_ocall(void* pms) +{ + ms_u_lseek_ocall_t* ms = SGX_CAST(ms_u_lseek_ocall_t*, pms); + ms->ms_retval = u_lseek_ocall(ms->ms_error, ms->ms_fd, ms->ms_offset, ms->ms_whence); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_lseek64_ocall(void* pms) +{ + ms_u_lseek64_ocall_t* ms = SGX_CAST(ms_u_lseek64_ocall_t*, pms); + ms->ms_retval = u_lseek64_ocall(ms->ms_error, ms->ms_fd, ms->ms_offset, ms->ms_whence); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_ftruncate_ocall(void* pms) +{ + ms_u_ftruncate_ocall_t* ms = SGX_CAST(ms_u_ftruncate_ocall_t*, pms); + ms->ms_retval = u_ftruncate_ocall(ms->ms_error, ms->ms_fd, ms->ms_length); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_ftruncate64_ocall(void* pms) +{ + ms_u_ftruncate64_ocall_t* ms = SGX_CAST(ms_u_ftruncate64_ocall_t*, pms); + ms->ms_retval = u_ftruncate64_ocall(ms->ms_error, ms->ms_fd, ms->ms_length); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_truncate_ocall(void* pms) +{ + ms_u_truncate_ocall_t* ms = SGX_CAST(ms_u_truncate_ocall_t*, pms); + ms->ms_retval = u_truncate_ocall(ms->ms_error, ms->ms_path, ms->ms_length); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_truncate64_ocall(void* pms) +{ + ms_u_truncate64_ocall_t* ms = SGX_CAST(ms_u_truncate64_ocall_t*, pms); + ms->ms_retval = u_truncate64_ocall(ms->ms_error, ms->ms_path, ms->ms_length); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fsync_ocall(void* pms) +{ + ms_u_fsync_ocall_t* ms = SGX_CAST(ms_u_fsync_ocall_t*, pms); + ms->ms_retval = u_fsync_ocall(ms->ms_error, ms->ms_fd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fdatasync_ocall(void* pms) +{ + ms_u_fdatasync_ocall_t* ms = SGX_CAST(ms_u_fdatasync_ocall_t*, pms); + ms->ms_retval = u_fdatasync_ocall(ms->ms_error, ms->ms_fd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fchmod_ocall(void* pms) +{ + ms_u_fchmod_ocall_t* ms = SGX_CAST(ms_u_fchmod_ocall_t*, pms); + ms->ms_retval = u_fchmod_ocall(ms->ms_error, ms->ms_fd, ms->ms_mode); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_unlink_ocall(void* pms) +{ + ms_u_unlink_ocall_t* ms = SGX_CAST(ms_u_unlink_ocall_t*, pms); + ms->ms_retval = u_unlink_ocall(ms->ms_error, ms->ms_pathname); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_link_ocall(void* pms) +{ + ms_u_link_ocall_t* ms = SGX_CAST(ms_u_link_ocall_t*, pms); + ms->ms_retval = u_link_ocall(ms->ms_error, ms->ms_oldpath, ms->ms_newpath); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_unlinkat_ocall(void* pms) +{ + ms_u_unlinkat_ocall_t* ms = SGX_CAST(ms_u_unlinkat_ocall_t*, pms); + ms->ms_retval = u_unlinkat_ocall(ms->ms_error, ms->ms_dirfd, ms->ms_pathname, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_linkat_ocall(void* pms) +{ + ms_u_linkat_ocall_t* ms = SGX_CAST(ms_u_linkat_ocall_t*, pms); + ms->ms_retval = u_linkat_ocall(ms->ms_error, ms->ms_olddirfd, ms->ms_oldpath, ms->ms_newdirfd, ms->ms_newpath, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_rename_ocall(void* pms) +{ + ms_u_rename_ocall_t* ms = SGX_CAST(ms_u_rename_ocall_t*, pms); + ms->ms_retval = u_rename_ocall(ms->ms_error, ms->ms_oldpath, ms->ms_newpath); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_chmod_ocall(void* pms) +{ + ms_u_chmod_ocall_t* ms = SGX_CAST(ms_u_chmod_ocall_t*, pms); + ms->ms_retval = u_chmod_ocall(ms->ms_error, ms->ms_path, ms->ms_mode); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_readlink_ocall(void* pms) +{ + ms_u_readlink_ocall_t* ms = SGX_CAST(ms_u_readlink_ocall_t*, pms); + ms->ms_retval = u_readlink_ocall(ms->ms_error, ms->ms_path, ms->ms_buf, ms->ms_bufsz); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_symlink_ocall(void* pms) +{ + ms_u_symlink_ocall_t* ms = SGX_CAST(ms_u_symlink_ocall_t*, pms); + ms->ms_retval = u_symlink_ocall(ms->ms_error, ms->ms_path1, ms->ms_path2); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_realpath_ocall(void* pms) +{ + ms_u_realpath_ocall_t* ms = SGX_CAST(ms_u_realpath_ocall_t*, pms); + ms->ms_retval = u_realpath_ocall(ms->ms_error, ms->ms_pathname); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_mkdir_ocall(void* pms) +{ + ms_u_mkdir_ocall_t* ms = SGX_CAST(ms_u_mkdir_ocall_t*, pms); + ms->ms_retval = u_mkdir_ocall(ms->ms_error, ms->ms_pathname, ms->ms_mode); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_rmdir_ocall(void* pms) +{ + ms_u_rmdir_ocall_t* ms = SGX_CAST(ms_u_rmdir_ocall_t*, pms); + ms->ms_retval = u_rmdir_ocall(ms->ms_error, ms->ms_pathname); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fdopendir_ocall(void* pms) +{ + ms_u_fdopendir_ocall_t* ms = SGX_CAST(ms_u_fdopendir_ocall_t*, pms); + ms->ms_retval = u_fdopendir_ocall(ms->ms_error, ms->ms_fd); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_opendir_ocall(void* pms) +{ + ms_u_opendir_ocall_t* ms = SGX_CAST(ms_u_opendir_ocall_t*, pms); + ms->ms_retval = u_opendir_ocall(ms->ms_error, ms->ms_pathname); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_readdir64_r_ocall(void* pms) +{ + ms_u_readdir64_r_ocall_t* ms = SGX_CAST(ms_u_readdir64_r_ocall_t*, pms); + ms->ms_retval = u_readdir64_r_ocall(ms->ms_dirp, ms->ms_entry, ms->ms_result); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_closedir_ocall(void* pms) +{ + ms_u_closedir_ocall_t* ms = SGX_CAST(ms_u_closedir_ocall_t*, pms); + ms->ms_retval = u_closedir_ocall(ms->ms_error, ms->ms_dirp); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_dirfd_ocall(void* pms) +{ + ms_u_dirfd_ocall_t* ms = SGX_CAST(ms_u_dirfd_ocall_t*, pms); + ms->ms_retval = u_dirfd_ocall(ms->ms_error, ms->ms_dirp); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_u_fstatat64_ocall(void* pms) +{ + ms_u_fstatat64_ocall_t* ms = SGX_CAST(ms_u_fstatat64_ocall_t*, pms); + ms->ms_retval = u_fstatat64_ocall(ms->ms_error, ms->ms_dirfd, ms->ms_pathname, ms->ms_buf, ms->ms_flags); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_sgx_oc_cpuidex(void* pms) +{ + ms_sgx_oc_cpuidex_t* ms = SGX_CAST(ms_sgx_oc_cpuidex_t*, pms); + sgx_oc_cpuidex(ms->ms_cpuinfo, ms->ms_leaf, ms->ms_subleaf); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_sgx_thread_wait_untrusted_event_ocall(void* pms) +{ + ms_sgx_thread_wait_untrusted_event_ocall_t* ms = SGX_CAST(ms_sgx_thread_wait_untrusted_event_ocall_t*, pms); + ms->ms_retval = sgx_thread_wait_untrusted_event_ocall(ms->ms_self); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_sgx_thread_set_untrusted_event_ocall(void* pms) +{ + ms_sgx_thread_set_untrusted_event_ocall_t* ms = SGX_CAST(ms_sgx_thread_set_untrusted_event_ocall_t*, pms); + ms->ms_retval = sgx_thread_set_untrusted_event_ocall(ms->ms_waiter); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_sgx_thread_setwait_untrusted_events_ocall(void* pms) +{ + ms_sgx_thread_setwait_untrusted_events_ocall_t* ms = SGX_CAST(ms_sgx_thread_setwait_untrusted_events_ocall_t*, pms); + ms->ms_retval = sgx_thread_setwait_untrusted_events_ocall(ms->ms_waiter, ms->ms_self); + + return SGX_SUCCESS; +} + +static sgx_status_t SGX_CDECL Enclave_sgx_thread_set_multiple_untrusted_events_ocall(void* pms) +{ + ms_sgx_thread_set_multiple_untrusted_events_ocall_t* ms = SGX_CAST(ms_sgx_thread_set_multiple_untrusted_events_ocall_t*, pms); + ms->ms_retval = sgx_thread_set_multiple_untrusted_events_ocall(ms->ms_waiters, ms->ms_total); + + return SGX_SUCCESS; +} + +static const struct { + size_t nr_ocall; + void * table[71]; +} ocall_table_Enclave = { + 71, + { + (void*)Enclave_u_thread_set_event_ocall, + (void*)Enclave_u_thread_wait_event_ocall, + (void*)Enclave_u_thread_set_multiple_events_ocall, + (void*)Enclave_u_thread_setwait_events_ocall, + (void*)Enclave_u_clock_gettime_ocall, + (void*)Enclave_u_read_ocall, + (void*)Enclave_u_pread64_ocall, + (void*)Enclave_u_readv_ocall, + (void*)Enclave_u_preadv64_ocall, + (void*)Enclave_u_write_ocall, + (void*)Enclave_u_pwrite64_ocall, + (void*)Enclave_u_writev_ocall, + (void*)Enclave_u_pwritev64_ocall, + (void*)Enclave_u_sendfile_ocall, + (void*)Enclave_u_copy_file_range_ocall, + (void*)Enclave_u_splice_ocall, + (void*)Enclave_u_fcntl_arg0_ocall, + (void*)Enclave_u_fcntl_arg1_ocall, + (void*)Enclave_u_ioctl_arg0_ocall, + (void*)Enclave_u_ioctl_arg1_ocall, + (void*)Enclave_u_close_ocall, + (void*)Enclave_u_isatty_ocall, + (void*)Enclave_u_dup_ocall, + (void*)Enclave_u_eventfd_ocall, + (void*)Enclave_u_futimens_ocall, + (void*)Enclave_u_malloc_ocall, + (void*)Enclave_u_free_ocall, + (void*)Enclave_u_mmap_ocall, + (void*)Enclave_u_munmap_ocall, + (void*)Enclave_u_msync_ocall, + (void*)Enclave_u_mprotect_ocall, + (void*)Enclave_u_open_ocall, + (void*)Enclave_u_open64_ocall, + (void*)Enclave_u_openat_ocall, + (void*)Enclave_u_fstat_ocall, + (void*)Enclave_u_fstat64_ocall, + (void*)Enclave_u_stat_ocall, + (void*)Enclave_u_stat64_ocall, + (void*)Enclave_u_lstat_ocall, + (void*)Enclave_u_lstat64_ocall, + (void*)Enclave_u_lseek_ocall, + (void*)Enclave_u_lseek64_ocall, + (void*)Enclave_u_ftruncate_ocall, + (void*)Enclave_u_ftruncate64_ocall, + (void*)Enclave_u_truncate_ocall, + (void*)Enclave_u_truncate64_ocall, + (void*)Enclave_u_fsync_ocall, + (void*)Enclave_u_fdatasync_ocall, + (void*)Enclave_u_fchmod_ocall, + (void*)Enclave_u_unlink_ocall, + (void*)Enclave_u_link_ocall, + (void*)Enclave_u_unlinkat_ocall, + (void*)Enclave_u_linkat_ocall, + (void*)Enclave_u_rename_ocall, + (void*)Enclave_u_chmod_ocall, + (void*)Enclave_u_readlink_ocall, + (void*)Enclave_u_symlink_ocall, + (void*)Enclave_u_realpath_ocall, + (void*)Enclave_u_mkdir_ocall, + (void*)Enclave_u_rmdir_ocall, + (void*)Enclave_u_fdopendir_ocall, + (void*)Enclave_u_opendir_ocall, + (void*)Enclave_u_readdir64_r_ocall, + (void*)Enclave_u_closedir_ocall, + (void*)Enclave_u_dirfd_ocall, + (void*)Enclave_u_fstatat64_ocall, + (void*)Enclave_sgx_oc_cpuidex, + (void*)Enclave_sgx_thread_wait_untrusted_event_ocall, + (void*)Enclave_sgx_thread_set_untrusted_event_ocall, + (void*)Enclave_sgx_thread_setwait_untrusted_events_ocall, + (void*)Enclave_sgx_thread_set_multiple_untrusted_events_ocall, + } +}; +sgx_status_t hash_password(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, uint8_t* hash, size_t* hash_length) +{ + sgx_status_t status; + ms_hash_password_t ms; + ms.ms_password = password; + ms.ms_password_length = password_length; + ms.ms_user_id = user_id; + ms.ms_user_id_length = user_id_length; + ms.ms_hash = hash; + ms.ms_hash_length = hash_length; + status = sgx_ecall(eid, 0, &ocall_table_Enclave, &ms); + if (status == SGX_SUCCESS && retval) *retval = ms.ms_retval; + return status; +} + +sgx_status_t verify_password(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, const uint8_t* stored_hash, size_t hash_length, uint8_t* password_verified) +{ + sgx_status_t status; + ms_verify_password_t ms; + ms.ms_password = password; + ms.ms_password_length = password_length; + ms.ms_user_id = user_id; + ms.ms_user_id_length = user_id_length; + ms.ms_stored_hash = stored_hash; + ms.ms_hash_length = hash_length; + ms.ms_password_verified = password_verified; + status = sgx_ecall(eid, 1, &ocall_table_Enclave, &ms); + if (status == SGX_SUCCESS && retval) *retval = ms.ms_retval; + return status; +} + +sgx_status_t t_global_init_ecall(sgx_enclave_id_t eid, uint64_t id, const uint8_t* path, size_t len) +{ + sgx_status_t status; + ms_t_global_init_ecall_t ms; + ms.ms_id = id; + ms.ms_path = path; + ms.ms_len = len; + status = sgx_ecall(eid, 2, &ocall_table_Enclave, &ms); + return status; +} + +sgx_status_t t_global_exit_ecall(sgx_enclave_id_t eid) +{ + sgx_status_t status; + status = sgx_ecall(eid, 3, &ocall_table_Enclave, NULL); + return status; +} + diff --git a/user-auth/app/Enclave_u.h b/user-auth/app/Enclave_u.h new file mode 100644 index 0000000..694ac41 --- /dev/null +++ b/user-auth/app/Enclave_u.h @@ -0,0 +1,319 @@ +#ifndef ENCLAVE_U_H__ +#define ENCLAVE_U_H__ + +#include +#include +#include +#include +#include "sgx_edger8r.h" /* for sgx_status_t etc. */ + +#include "time.h" +#include "inc/stat.h" +#include "sys/uio.h" +#include "time.h" +#include "inc/stat.h" +#include "inc/dirent.h" + +#include /* for size_t */ + +#define SGX_CAST(type, item) ((type)(item)) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef U_THREAD_SET_EVENT_OCALL_DEFINED__ +#define U_THREAD_SET_EVENT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_thread_set_event_ocall, (int* error, const void* tcs)); +#endif +#ifndef U_THREAD_WAIT_EVENT_OCALL_DEFINED__ +#define U_THREAD_WAIT_EVENT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_thread_wait_event_ocall, (int* error, const void* tcs, const struct timespec* timeout)); +#endif +#ifndef U_THREAD_SET_MULTIPLE_EVENTS_OCALL_DEFINED__ +#define U_THREAD_SET_MULTIPLE_EVENTS_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_thread_set_multiple_events_ocall, (int* error, const void** tcss, int total)); +#endif +#ifndef U_THREAD_SETWAIT_EVENTS_OCALL_DEFINED__ +#define U_THREAD_SETWAIT_EVENTS_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_thread_setwait_events_ocall, (int* error, const void* waiter_tcs, const void* self_tcs, const struct timespec* timeout)); +#endif +#ifndef U_CLOCK_GETTIME_OCALL_DEFINED__ +#define U_CLOCK_GETTIME_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_clock_gettime_ocall, (int* error, int clk_id, struct timespec* tp)); +#endif +#ifndef U_READ_OCALL_DEFINED__ +#define U_READ_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_read_ocall, (int* error, int fd, void* buf, size_t count)); +#endif +#ifndef U_PREAD64_OCALL_DEFINED__ +#define U_PREAD64_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_pread64_ocall, (int* error, int fd, void* buf, size_t count, int64_t offset)); +#endif +#ifndef U_READV_OCALL_DEFINED__ +#define U_READV_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_readv_ocall, (int* error, int fd, const struct iovec* iov, int iovcnt)); +#endif +#ifndef U_PREADV64_OCALL_DEFINED__ +#define U_PREADV64_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_preadv64_ocall, (int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset)); +#endif +#ifndef U_WRITE_OCALL_DEFINED__ +#define U_WRITE_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_write_ocall, (int* error, int fd, const void* buf, size_t count)); +#endif +#ifndef U_PWRITE64_OCALL_DEFINED__ +#define U_PWRITE64_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_pwrite64_ocall, (int* error, int fd, const void* buf, size_t count, int64_t offset)); +#endif +#ifndef U_WRITEV_OCALL_DEFINED__ +#define U_WRITEV_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_writev_ocall, (int* error, int fd, const struct iovec* iov, int iovcnt)); +#endif +#ifndef U_PWRITEV64_OCALL_DEFINED__ +#define U_PWRITEV64_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_pwritev64_ocall, (int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset)); +#endif +#ifndef U_SENDFILE_OCALL_DEFINED__ +#define U_SENDFILE_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_sendfile_ocall, (int* error, int out_fd, int in_fd, int64_t* offset, size_t count)); +#endif +#ifndef U_COPY_FILE_RANGE_OCALL_DEFINED__ +#define U_COPY_FILE_RANGE_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_copy_file_range_ocall, (int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags)); +#endif +#ifndef U_SPLICE_OCALL_DEFINED__ +#define U_SPLICE_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_splice_ocall, (int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags)); +#endif +#ifndef U_FCNTL_ARG0_OCALL_DEFINED__ +#define U_FCNTL_ARG0_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fcntl_arg0_ocall, (int* error, int fd, int cmd)); +#endif +#ifndef U_FCNTL_ARG1_OCALL_DEFINED__ +#define U_FCNTL_ARG1_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fcntl_arg1_ocall, (int* error, int fd, int cmd, int arg)); +#endif +#ifndef U_IOCTL_ARG0_OCALL_DEFINED__ +#define U_IOCTL_ARG0_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_ioctl_arg0_ocall, (int* error, int fd, int request)); +#endif +#ifndef U_IOCTL_ARG1_OCALL_DEFINED__ +#define U_IOCTL_ARG1_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_ioctl_arg1_ocall, (int* error, int fd, int request, int* arg)); +#endif +#ifndef U_CLOSE_OCALL_DEFINED__ +#define U_CLOSE_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_close_ocall, (int* error, int fd)); +#endif +#ifndef U_ISATTY_OCALL_DEFINED__ +#define U_ISATTY_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_isatty_ocall, (int* error, int fd)); +#endif +#ifndef U_DUP_OCALL_DEFINED__ +#define U_DUP_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_dup_ocall, (int* error, int oldfd)); +#endif +#ifndef U_EVENTFD_OCALL_DEFINED__ +#define U_EVENTFD_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_eventfd_ocall, (int* error, unsigned int initval, int flags)); +#endif +#ifndef U_FUTIMENS_OCALL_DEFINED__ +#define U_FUTIMENS_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_futimens_ocall, (int* error, int fd, const struct timespec* times)); +#endif +#ifndef U_MALLOC_OCALL_DEFINED__ +#define U_MALLOC_OCALL_DEFINED__ +void* SGX_UBRIDGE(SGX_NOCONVENTION, u_malloc_ocall, (int* error, size_t size)); +#endif +#ifndef U_FREE_OCALL_DEFINED__ +#define U_FREE_OCALL_DEFINED__ +void SGX_UBRIDGE(SGX_NOCONVENTION, u_free_ocall, (void* p)); +#endif +#ifndef U_MMAP_OCALL_DEFINED__ +#define U_MMAP_OCALL_DEFINED__ +void* SGX_UBRIDGE(SGX_NOCONVENTION, u_mmap_ocall, (int* error, void* start, size_t length, int prot, int flags, int fd, int64_t offset)); +#endif +#ifndef U_MUNMAP_OCALL_DEFINED__ +#define U_MUNMAP_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_munmap_ocall, (int* error, void* start, size_t length)); +#endif +#ifndef U_MSYNC_OCALL_DEFINED__ +#define U_MSYNC_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_msync_ocall, (int* error, void* addr, size_t length, int flags)); +#endif +#ifndef U_MPROTECT_OCALL_DEFINED__ +#define U_MPROTECT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_mprotect_ocall, (int* error, void* addr, size_t length, int prot)); +#endif +#ifndef U_OPEN_OCALL_DEFINED__ +#define U_OPEN_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_open_ocall, (int* error, const char* pathname, int flags)); +#endif +#ifndef U_OPEN64_OCALL_DEFINED__ +#define U_OPEN64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_open64_ocall, (int* error, const char* path, int oflag, int mode)); +#endif +#ifndef U_OPENAT_OCALL_DEFINED__ +#define U_OPENAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_openat_ocall, (int* error, int dirfd, const char* pathname, int flags)); +#endif +#ifndef U_FSTAT_OCALL_DEFINED__ +#define U_FSTAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fstat_ocall, (int* error, int fd, struct stat_t* buf)); +#endif +#ifndef U_FSTAT64_OCALL_DEFINED__ +#define U_FSTAT64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fstat64_ocall, (int* error, int fd, struct stat64_t* buf)); +#endif +#ifndef U_STAT_OCALL_DEFINED__ +#define U_STAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_stat_ocall, (int* error, const char* path, struct stat_t* buf)); +#endif +#ifndef U_STAT64_OCALL_DEFINED__ +#define U_STAT64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_stat64_ocall, (int* error, const char* path, struct stat64_t* buf)); +#endif +#ifndef U_LSTAT_OCALL_DEFINED__ +#define U_LSTAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_lstat_ocall, (int* error, const char* path, struct stat_t* buf)); +#endif +#ifndef U_LSTAT64_OCALL_DEFINED__ +#define U_LSTAT64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_lstat64_ocall, (int* error, const char* path, struct stat64_t* buf)); +#endif +#ifndef U_LSEEK_OCALL_DEFINED__ +#define U_LSEEK_OCALL_DEFINED__ +uint64_t SGX_UBRIDGE(SGX_NOCONVENTION, u_lseek_ocall, (int* error, int fd, int64_t offset, int whence)); +#endif +#ifndef U_LSEEK64_OCALL_DEFINED__ +#define U_LSEEK64_OCALL_DEFINED__ +int64_t SGX_UBRIDGE(SGX_NOCONVENTION, u_lseek64_ocall, (int* error, int fd, int64_t offset, int whence)); +#endif +#ifndef U_FTRUNCATE_OCALL_DEFINED__ +#define U_FTRUNCATE_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_ftruncate_ocall, (int* error, int fd, int64_t length)); +#endif +#ifndef U_FTRUNCATE64_OCALL_DEFINED__ +#define U_FTRUNCATE64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_ftruncate64_ocall, (int* error, int fd, int64_t length)); +#endif +#ifndef U_TRUNCATE_OCALL_DEFINED__ +#define U_TRUNCATE_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_truncate_ocall, (int* error, const char* path, int64_t length)); +#endif +#ifndef U_TRUNCATE64_OCALL_DEFINED__ +#define U_TRUNCATE64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_truncate64_ocall, (int* error, const char* path, int64_t length)); +#endif +#ifndef U_FSYNC_OCALL_DEFINED__ +#define U_FSYNC_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fsync_ocall, (int* error, int fd)); +#endif +#ifndef U_FDATASYNC_OCALL_DEFINED__ +#define U_FDATASYNC_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fdatasync_ocall, (int* error, int fd)); +#endif +#ifndef U_FCHMOD_OCALL_DEFINED__ +#define U_FCHMOD_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fchmod_ocall, (int* error, int fd, uint32_t mode)); +#endif +#ifndef U_UNLINK_OCALL_DEFINED__ +#define U_UNLINK_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_unlink_ocall, (int* error, const char* pathname)); +#endif +#ifndef U_LINK_OCALL_DEFINED__ +#define U_LINK_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_link_ocall, (int* error, const char* oldpath, const char* newpath)); +#endif +#ifndef U_UNLINKAT_OCALL_DEFINED__ +#define U_UNLINKAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_unlinkat_ocall, (int* error, int dirfd, const char* pathname, int flags)); +#endif +#ifndef U_LINKAT_OCALL_DEFINED__ +#define U_LINKAT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_linkat_ocall, (int* error, int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags)); +#endif +#ifndef U_RENAME_OCALL_DEFINED__ +#define U_RENAME_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_rename_ocall, (int* error, const char* oldpath, const char* newpath)); +#endif +#ifndef U_CHMOD_OCALL_DEFINED__ +#define U_CHMOD_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_chmod_ocall, (int* error, const char* path, uint32_t mode)); +#endif +#ifndef U_READLINK_OCALL_DEFINED__ +#define U_READLINK_OCALL_DEFINED__ +size_t SGX_UBRIDGE(SGX_NOCONVENTION, u_readlink_ocall, (int* error, const char* path, char* buf, size_t bufsz)); +#endif +#ifndef U_SYMLINK_OCALL_DEFINED__ +#define U_SYMLINK_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_symlink_ocall, (int* error, const char* path1, const char* path2)); +#endif +#ifndef U_REALPATH_OCALL_DEFINED__ +#define U_REALPATH_OCALL_DEFINED__ +char* SGX_UBRIDGE(SGX_NOCONVENTION, u_realpath_ocall, (int* error, const char* pathname)); +#endif +#ifndef U_MKDIR_OCALL_DEFINED__ +#define U_MKDIR_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_mkdir_ocall, (int* error, const char* pathname, uint32_t mode)); +#endif +#ifndef U_RMDIR_OCALL_DEFINED__ +#define U_RMDIR_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_rmdir_ocall, (int* error, const char* pathname)); +#endif +#ifndef U_FDOPENDIR_OCALL_DEFINED__ +#define U_FDOPENDIR_OCALL_DEFINED__ +void* SGX_UBRIDGE(SGX_NOCONVENTION, u_fdopendir_ocall, (int* error, int fd)); +#endif +#ifndef U_OPENDIR_OCALL_DEFINED__ +#define U_OPENDIR_OCALL_DEFINED__ +void* SGX_UBRIDGE(SGX_NOCONVENTION, u_opendir_ocall, (int* error, const char* pathname)); +#endif +#ifndef U_READDIR64_R_OCALL_DEFINED__ +#define U_READDIR64_R_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_readdir64_r_ocall, (void* dirp, struct dirent64_t* entry, struct dirent64_t** result)); +#endif +#ifndef U_CLOSEDIR_OCALL_DEFINED__ +#define U_CLOSEDIR_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_closedir_ocall, (int* error, void* dirp)); +#endif +#ifndef U_DIRFD_OCALL_DEFINED__ +#define U_DIRFD_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_dirfd_ocall, (int* error, void* dirp)); +#endif +#ifndef U_FSTATAT64_OCALL_DEFINED__ +#define U_FSTATAT64_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_NOCONVENTION, u_fstatat64_ocall, (int* error, int dirfd, const char* pathname, struct stat64_t* buf, int flags)); +#endif +#ifndef SGX_OC_CPUIDEX_DEFINED__ +#define SGX_OC_CPUIDEX_DEFINED__ +void SGX_UBRIDGE(SGX_CDECL, sgx_oc_cpuidex, (int cpuinfo[4], int leaf, int subleaf)); +#endif +#ifndef SGX_THREAD_WAIT_UNTRUSTED_EVENT_OCALL_DEFINED__ +#define SGX_THREAD_WAIT_UNTRUSTED_EVENT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_CDECL, sgx_thread_wait_untrusted_event_ocall, (const void* self)); +#endif +#ifndef SGX_THREAD_SET_UNTRUSTED_EVENT_OCALL_DEFINED__ +#define SGX_THREAD_SET_UNTRUSTED_EVENT_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_CDECL, sgx_thread_set_untrusted_event_ocall, (const void* waiter)); +#endif +#ifndef SGX_THREAD_SETWAIT_UNTRUSTED_EVENTS_OCALL_DEFINED__ +#define SGX_THREAD_SETWAIT_UNTRUSTED_EVENTS_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_CDECL, sgx_thread_setwait_untrusted_events_ocall, (const void* waiter, const void* self)); +#endif +#ifndef SGX_THREAD_SET_MULTIPLE_UNTRUSTED_EVENTS_OCALL_DEFINED__ +#define SGX_THREAD_SET_MULTIPLE_UNTRUSTED_EVENTS_OCALL_DEFINED__ +int SGX_UBRIDGE(SGX_CDECL, sgx_thread_set_multiple_untrusted_events_ocall, (const void** waiters, size_t total)); +#endif + +sgx_status_t hash_password(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, uint8_t* hash, size_t* hash_length); +sgx_status_t verify_password(sgx_enclave_id_t eid, sgx_status_t* retval, const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, const uint8_t* stored_hash, size_t hash_length, uint8_t* password_verified); +sgx_status_t t_global_init_ecall(sgx_enclave_id_t eid, uint64_t id, const uint8_t* path, size_t len); +sgx_status_t t_global_exit_ecall(sgx_enclave_id_t eid); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/user-auth/app/Makefile b/user-auth/app/Makefile new file mode 100644 index 0000000..81fefcf --- /dev/null +++ b/user-auth/app/Makefile @@ -0,0 +1,128 @@ +# Makefile settings - Host part + +WHEEL = ./target/wheels/user_auth_sgx-1.0.0-cp310-cp310-manylinux_2_34_x86_64.whl +PACKAGE_NAME = user-auth-sgx +LIB = ../build/lib/ +BIN = ../build/bin/ +SRC_RST = ./src/ +APP_U = app +APP_T = enclave.so +NAME_U = libEnclave_u.a +SRC_U = ./ +SRC_T = ../enclave/ +OBJ_U = ../build/obj/ +SGX_SDK ?= /opt/sgxsdk +FLAGS = -Wall -Wextra +GCC_STEP1_U = -I $(SRC_U) -I./include -I$(SGX_SDK)/include -I$(CUSTOM_EDL_PATH) -fPIC -Wno-attributes $(SGX_COMMON_CFLAGS) +FILES_U = Enclave_u.c +FILES_U_H = Enclave_u.h +BUILD_RS = build.rs +TOML = Cargo.toml +SGX_ARCH = x64 +TRTS_LIB = sgx_trts +SERVICE_LIB = sgx_tservice +# Addprefix dependant variables, no need to change those +OUTPUT_U = $(FILES_U:.c=.o) +LIB_U = $(addprefix $(BIN), $(APP_U)) +NAME_U_D = $(addprefix $(LIB), $(NAME_U)) +FILES_U_F=$(addprefix $(SRC_U), $(FILES_U)) +OUTPUT_W_FU=$(addprefix $(OBJ_U), $(OUTPUT_U)) +FILES_RUST_F= $(wildcard $(SRC_RST)*.rs) # Wildcard function used, no need to specify the rust files. Safe as we don't compile the rust files with the makefile. + +# Contains compilation rules for the enclave part + +include ../buildenv.mk + +# Custom libraries, EDL paths. Needs to be specified with make (CUSTOM_EDL_PATH) (CUSTOM_COMMON_PATH) + +# Directly imported from the original Intel SGX samples, helpful to detect the system architecture + +ifeq ($(shell getconf LONG_BIT), 32) + SGX_ARCH := x86 +else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32) + SGX_ARCH := x86 +endif + +ifeq ($(SGX_ARCH), x86) + SGX_COMMON_CFLAGS := -m32 + SGX_LIBRARY_PATH := $(SGX_SDK)/lib + SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign + SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r +else + SGX_COMMON_CFLAGS := -m64 + SGX_LIBRARY_PATH := $(SGX_SDK)/lib64 + SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign + SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r +endif + +# If specified, software / simulation mode. Otherwise, hardware mode no matter what. + +ifeq ($(SGX_MODE), SW) + TRTS_LIB := sgx_trts_sim + SERVICE_LIB := sgx_tservice_sim +endif + +# If debug mode, we can set up extra options such as the debug flags + +ifeq ($(SGX_DEBUG), 1) + SGX_COMMON_CFLAGS += -O0 -g +else + SGX_COMMON_CFLAGS += -O2 +endif + +# Compilation process, we set up all the dependencies needed to have the correct order of build, and avoid relink + +all: $(LIB_U) + +install: $(LIB_U) + @poetry run pip install --force-reinstall --no-color $(WHEEL) + +uninstall: + @poetry run pip uninstall --no-color -y $(PACKAGE_NAME) + +$(FILES_U_F): $(SGX_EDGER8R) $(SRC_T)/Enclave.edl + @echo "\033[32mGenerating untrusted SGX C edl files...\033[0m" + @$(SGX_EDGER8R) --untrusted $(SRC_T)/Enclave.edl --search-path $(SGX_SDK)/include --search-path $(CUSTOM_EDL_PATH) --untrusted-dir $(SRC_U) + +$(NAME_U_D): $(FILES_U_F) $(OUTPUT_W_FU) + @echo "\033[32mBuilding untrusted C edl static library...\033[0m" + @mkdir -p $(LIB) + @$(AR) rcsD $@ $(OUTPUT_W_FU) + +$(OBJ_U)%.o:$(SRC_U)%.c + @mkdir -p $(OBJ_U) + @echo "\033[32m$?: Build in progress...\033[0m" + @$(CC) $(FLAGS) $(GCC_STEP1_U) -o $@ -c $? + +# We print the compilation mode we're in (hardware/software mode), just as a reminder. + +$(LIB_U): $(NAME_U_D) $(FILES_RUST_F) $(FILES_U_H) $(BUILD_RS) $(TOML) # We added as a reference the rust files, along with the build.rs file and the cargo.toml file, so Make can detect if any change was made +ifeq ($(SGX_MODE), SW) + @echo "\033[32mSoftware / Simulation mode\033[0m" +else + @echo "\033[32mHardware mode\033[0m" +endif + @poetry install --sync + @echo "\033[32mStarting maturin to build the host...\033[0m" + @cd $(SRC_U) && SGX_SDK=$(SGX_SDK) poetry run maturin build --release + +clean: uninstall c_clean + @rm -rf $(OBJ_U) + @echo "\033[32mObject files deleted\033[0m" + +fclean: clean fclean_host + +fclean_host: + @echo "\033[32mBinary file $(LIB_U) deleted\033[0m" + @rm -f $(LIB_U) + @rm -f $(NAME_U_D) + @cargo clean && rm -f Cargo.lock + +c_clean: + @echo "\033[32mC edl generated files deleted\033[0m" + @rm -rf $(FILES_U_F) + @rm -rf $(FILES_U_H) + +re: fclean all + +.PHONY: all clean c_clean fclean re fclean_host install uninstall diff --git a/user-auth/app/build.rs b/user-auth/app/build.rs new file mode 100644 index 0000000..896bae9 --- /dev/null +++ b/user-auth/app/build.rs @@ -0,0 +1,33 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License.. + +use std::env; + +fn main() { + let sdk_dir = env::var("SGX_SDK").unwrap_or_else(|_| "/opt/sgxsdk".to_string()); + let is_sim = env::var("SGX_MODE").unwrap_or_else(|_| "HW".to_string()); + + println!("cargo:rustc-link-search=native=../build/lib"); + println!("cargo:rustc-link-lib=static=Enclave_u"); + + println!("cargo:rustc-link-search=native={}/lib64", sdk_dir); + match is_sim.as_ref() { + "SW" => println!("cargo:rustc-link-lib=dylib=sgx_urts_sim"), + "HW" => println!("cargo:rustc-link-lib=dylib=sgx_urts"), + _ => println!("cargo:rustc-link-lib=dylib=sgx_urts"), // Treat undefined as HW + } +} diff --git a/user-auth/app/poetry.lock b/user-auth/app/poetry.lock new file mode 100644 index 0000000..3ae4d4e --- /dev/null +++ b/user-auth/app/poetry.lock @@ -0,0 +1,68 @@ +# This file is automatically @generated by Poetry and should not be changed by hand. + +[[package]] +name = "maturin" +version = "0.14.10" +description = "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "maturin-0.14.10-py3-none-linux_armv6l.whl", hash = "sha256:ec8269c02cc435893308dfd50f57f14fb1be3554e4e61c5bf49b97363b289775"}, + {file = "maturin-0.14.10-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:e9c19dc0a28109280f7d091ca7b78e25f3fc340fcfac92801829a21198fa20eb"}, + {file = "maturin-0.14.10-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:cf950ebfe449a97617b91d75e09766509e21a389ce3f7b6ef15130ad8a95430a"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686.whl", hash = "sha256:c0d25e82cb6e5de9f1c028fcf069784be4165b083e79412371edce05010b68f3"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl", hash = "sha256:9da98bee0a548ecaaa924cc8cb94e49075d5e71511c62a1633a6962c7831a29b"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl", hash = "sha256:2f097a63f3bed20a7da56fc7ce4d44ef8376ee9870604da16b685f2d02c87c79"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l.whl", hash = "sha256:4946ad7545ba5fc0ad08bc98bc8e9f6ffabb6ded71db9ed282ad4596b998d42a"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl", hash = "sha256:98bfed21c3498857b3381efeb041d77e004a93b22261bf9690fe2b9fbb4c210f"}, + {file = "maturin-0.14.10-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b157e2e8a0216d02df1d0451201fcb977baf0dcd223890abfbfbfd01e0b44630"}, + {file = "maturin-0.14.10-py3-none-win32.whl", hash = "sha256:5abf311d4618b673efa30cacdac5ae2d462e49da58db9a5bf0d8bde16d9c16be"}, + {file = "maturin-0.14.10-py3-none-win_amd64.whl", hash = "sha256:11b8550ceba5b81465a18d06f0d3a4cfc1cd6cbf68eda117c253bbf3324b1264"}, + {file = "maturin-0.14.10-py3-none-win_arm64.whl", hash = "sha256:6cc9afb89f28bd591b62f8f3c29736c81c322cffe88f9ab8eb1749377bbc3521"}, + {file = "maturin-0.14.10.tar.gz", hash = "sha256:895c48cbe56ae994c2a1eeeef19475ca4819aa4c6412af727a63a772e8ef2d87"}, +] + +[package.dependencies] +patchelf = {version = "*", optional = true, markers = "extra == \"patchelf\""} +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} + +[package.extras] +patchelf = ["patchelf"] +zig = ["ziglang (>=0.10.0,<0.11.0)"] + +[[package]] +name = "patchelf" +version = "0.17.2.0" +description = "A small utility to modify the dynamic linker and RPATH of ELF executables." +category = "dev" +optional = false +python-versions = "*" +files = [ + {file = "patchelf-0.17.2.0-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl", hash = "sha256:b8d86f32e1414d6964d5d166ddd2cf829d156fba0d28d32a3bd0192f987f4529"}, + {file = "patchelf-0.17.2.0-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl", hash = "sha256:9233a0f2fc73820c5bd468f27507bdf0c9ac543f07c7f9888bb7cf910b1be22f"}, + {file = "patchelf-0.17.2.0-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.musllinux_1_1_s390x.whl", hash = "sha256:6601d7d831508bcdd3d8ebfa6435c2379bf11e41af2409ae7b88de572926841c"}, + {file = "patchelf-0.17.2.0-py2.py3-none-manylinux_2_5_i686.manylinux1_i686.musllinux_1_1_i686.whl", hash = "sha256:c62a34f0c25e6c2d6ae44389f819a00ccdf3f292ad1b814fbe1cc23cb27023ce"}, + {file = "patchelf-0.17.2.0-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.musllinux_1_1_x86_64.whl", hash = "sha256:1b9fd14f300341dc020ae05c49274dd1fa6727eabb4e61dd7fb6fb3600acd26e"}, + {file = "patchelf-0.17.2.0.tar.gz", hash = "sha256:dedf987a83d7f6d6f5512269e57f5feeec36719bd59567173b6d9beabe019efe"}, +] + +[package.extras] +test = ["importlib-metadata", "pytest"] + +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + +[metadata] +lock-version = "2.0" +python-versions = "^3.10" +content-hash = "6cf0aa69e59a3196bbfedbbfd1a95a48b982bd3d4fa97d2032119701f22c125f" diff --git a/user-auth/app/pyproject.toml b/user-auth/app/pyproject.toml new file mode 100644 index 0000000..6b28ee9 --- /dev/null +++ b/user-auth/app/pyproject.toml @@ -0,0 +1,18 @@ +# Dummy project +[tool.poetry] +name = "user-auth-sgx-dummy" +version = "0.1.0" +description = "" +authors = ["Nautilus"] +license = "AGPL-3.0" +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.10" + +[tool.poetry.group.dev.dependencies] +maturin = {extras = ["patchelf"], version = "^0.14.10"} + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/user-auth/app/src/ecall.rs b/user-auth/app/src/ecall.rs new file mode 100644 index 0000000..8d9e865 --- /dev/null +++ b/user-auth/app/src/ecall.rs @@ -0,0 +1,107 @@ +use password_hash::{Output, PasswordHashString}; +use sgx_types::*; +use thiserror::Error; + +extern "C" { + fn hash_password( + eid: sgx_enclave_id_t, + Retval: *mut sgx_status_t, + password: *const u8, + password_length: usize, + user_id: *const u8, + user_id_length: usize, + tag: *mut u8, + ) -> sgx_status_t; + fn verify_password( + eid: sgx_enclave_id_t, + retval: *mut sgx_status_t, + password: *const u8, + password_length: usize, + user_id: *const u8, + user_id_length: usize, + password_verified: *mut u8, + ) -> sgx_status_t; +} + +#[derive(Debug, Error)] +pub enum AuthError { + #[error("password does not match the given hash")] + InvalidPassword, + #[error("empty user id supplied")] + EmptyUserId, + #[error("empty password supplied")] + EmptyPassword, + #[error("supplied hash is not a valid PHC string")] + InvalidHashString, +} + +/// Compute a new password hash. +pub fn safe_hash_password( + eid: sgx_enclave_id_t, + user_id: &[u8], + password: &str, +) -> Result { + let mut hash = [0u8; 43]; + let ecall_return = &mut sgx_status_t::default(); + match (user_id, password) { + (&[], _) => Err(AuthError::EmptyUserId), + (_, "") => Err(AuthError::EmptyPassword), + _ => { + unsafe { + hash_password( + eid, + ecall_return, + password.as_bytes().as_ptr(), + password.len(), + user_id.first().unwrap(), + user_id.len(), + hash.as_mut_ptr(), + ) + }; + Ok(Output::b64_decode( + &String::from_utf8(hash.to_vec()).expect("invalid UTF-8 string received"), + ) + .unwrap() + .to_string()) + } + } +} + +/// Verify a user-supplied password against an existing hash (from the +/// database). +pub fn safe_verify_password( + eid: sgx_enclave_id_t, + user_id: &[u8], + password: &str, + stored_hash: &str, +) -> Result<(), AuthError> { + let mut password_verified: u8 = 0; + let ecall_return = &mut sgx_status_t::default(); + + match ( + user_id, + password, + PasswordHashString::parse(stored_hash, password_hash::Encoding::B64), + ) { + (_, _, Err(_)) => Err(AuthError::InvalidHashString), + (&[], _, Ok(_)) => Err(AuthError::EmptyUserId), + (_, "", Ok(_)) => Err(AuthError::EmptyPassword), + _ => { + unsafe { + verify_password( + eid, + ecall_return, + password.as_bytes().as_ptr(), + password.len(), + user_id.as_ptr(), + user_id.len(), + &mut password_verified, + ) + }; + if password_verified == 0 { + return Err(AuthError::InvalidPassword); + } + Ok(()) + } + } +} diff --git a/user-auth/app/src/lib.rs b/user-auth/app/src/lib.rs new file mode 100644 index 0000000..600ebe5 --- /dev/null +++ b/user-auth/app/src/lib.rs @@ -0,0 +1,98 @@ +use std::str::FromStr; + +use dotenvy_macro::dotenv; +use pyo3::exceptions::PyException; +use pyo3::types::PyModule; +use pyo3::{create_exception, pyclass, pymethods, pymodule, PyResult, Python}; +use sgx_types::*; +use sgx_urts::SgxEnclave; + +static ENCLAVE_FILE: &'static str = dotenv!("ENCLAVE_SHARED_OBJECT"); + +pub mod ecall; + +create_exception!( + user_auth_sgx, + InvalidPassword, + PyException, + "password does not match the supplied hash" +); +create_exception!( + user_auth_sgx, + EmptyUserId, + PyException, + "supplied user id is empty" +); +create_exception!( + user_auth_sgx, + EmptyPassword, + PyException, + "supplied user password is empty" +); +create_exception!( + user_auth_sgx, + InvalidHashString, + PyException, + "supplied hash is not a valid PHC string" +); + +#[pymodule] +fn user_auth_sgx(py: Python<'_>, m: &PyModule) -> PyResult<()> { + m.add("InvalidPassword", py.get_type::())?; + m.add("EmtpyUserId", py.get_type::())?; + m.add("EmtpyPassword", py.get_type::())?; + m.add("InvalidHashString", py.get_type::())?; + m.add_class::()?; + Ok(()) +} + +#[pyclass] +#[derive(Debug, Default, Clone)] +struct UserAuthEnclave(SgxEnclave); + +#[pymethods] +impl UserAuthEnclave { + #[new] + fn create_enclave() -> PyResult { + let mut launch_token: sgx_launch_token_t = [0; 1024]; + let mut launch_token_updated: i32 = 0; + // Debug Support: if desired, set "SGX_DEBUG_MODE" to 1 before + // compiling. Otherwise, set it to 0; + let debug = i32::from_str(dotenv!("SGX_DEBUG_MODE")).unwrap(); + // PANIC: panics if debug is set to any i32 other than 0 or 1 + assert!(debug == 0 || debug == 1); + let mut misc_attr = sgx_misc_attribute_t { + secs_attr: sgx_attributes_t { flags: 0, xfrm: 0 }, + misc_select: 0, + }; + Ok(UserAuthEnclave( + SgxEnclave::create( + ENCLAVE_FILE, + debug, + &mut launch_token, + &mut launch_token_updated, + &mut misc_attr, + ) + .expect("failed to initialize SGX enclave"), + )) + } + /// Compute a new password hash using the current enclave instance. + fn hash_password(&self, user_id: &[u8], password: &str) -> PyResult { + let Self(enclave) = self; + ecall::safe_hash_password(enclave.geteid(), user_id, password) + .map_err(|err| PyException::new_err(err.to_string())) + } + /// Verify a user-supplied password against an existing hash (from the + /// database) using the current enclave instance. + fn verify_password(&self, user_id: &[u8], password: &str, stored_hash: &str) -> PyResult<()> { + let Self(enclave) = self; + ecall::safe_verify_password(enclave.geteid(), user_id, password, stored_hash).map_err( + |err| match err { + ecall::AuthError::EmptyUserId => EmptyUserId::new_err(err.to_string()), + ecall::AuthError::EmptyPassword => EmptyPassword::new_err(err.to_string()), + ecall::AuthError::InvalidPassword => InvalidPassword::new_err(err.to_string()), + ecall::AuthError::InvalidHashString => InvalidHashString::new_err(err.to_string()), + }, + ) + } +} diff --git a/user-auth/buildenv.mk b/user-auth/buildenv.mk new file mode 100644 index 0000000..a86675e --- /dev/null +++ b/user-auth/buildenv.mk @@ -0,0 +1,167 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License.. +# +# + +CP := /bin/cp -f +MKDIR := mkdir -p +STRIP := strip +OBJCOPY := objcopy + +# clean the content of 'INCLUDE' - this variable will be set by vcvars32.bat +# thus it will cause build error when this variable is used by our Makefile, +# when compiling the code under Cygwin tainted by MSVC environment settings. +INCLUDE := + +# turn on stack protector for SDK +COMMON_FLAGS += -fstack-protector + +ifdef DEBUG + COMMON_FLAGS += -O0 -g -DDEBUG -UNDEBUG +else + COMMON_FLAGS += -O2 -D_FORTIFY_SOURCE=2 -UDEBUG -DNDEBUG +endif + +# turn on compiler warnings as much as possible +COMMON_FLAGS += -Wall -Wextra -Winit-self -Wpointer-arith -Wreturn-type \ + -Waddress -Wsequence-point -Wformat-security \ + -Wmissing-include-dirs -Wfloat-equal -Wundef -Wshadow \ + -Wcast-align -Wconversion -Wredundant-decls + +# additional warnings flags for C +CFLAGS += -Wjump-misses-init -Wstrict-prototypes -Wunsuffixed-float-constants + +# additional warnings flags for C++ +CXXFLAGS += -Wnon-virtual-dtor + +# for static_assert() +CXXFLAGS += -std=c++0x + +.DEFAULT_GOAL := all +# this turns off the RCS / SCCS implicit rules of GNU Make +% : RCS/%,v +% : RCS/% +% : %,v +% : s.% +% : SCCS/s.% + +# If a rule fails, delete $@. +.DELETE_ON_ERROR: + +HOST_FILE_PROGRAM := file + +UNAME := $(shell uname -m) +ifneq (,$(findstring 86,$(UNAME))) + HOST_ARCH := x86 + ifneq (,$(shell $(HOST_FILE_PROGRAM) -L $(SHELL) | grep 'x86[_-]64')) + HOST_ARCH := x86_64 + endif +else + $(info Unknown host CPU arhitecture $(UNAME)) + $(error Aborting) +endif + + +ifeq "$(findstring __INTEL_COMPILER, $(shell $(CC) -E -dM -xc /dev/null))" "__INTEL_COMPILER" + ifeq ($(shell test -f /usr/bin/dpkg; echo $$?), 0) + ADDED_INC := -I /usr/include/$(shell dpkg-architecture -qDEB_BUILD_MULTIARCH) + endif +endif + +ARCH := $(HOST_ARCH) +ifeq "$(findstring -m32, $(CXXFLAGS))" "-m32" + ARCH := x86 +endif + +ifeq ($(ARCH), x86) +COMMON_FLAGS += -DITT_ARCH_IA32 +else +COMMON_FLAGS += -DITT_ARCH_IA64 +endif + +CFLAGS += $(COMMON_FLAGS) +CXXFLAGS += $(COMMON_FLAGS) + +# Enable the security flags +COMMON_LDFLAGS := -Wl,-z,relro,-z,now,-z,noexecstack + +# mitigation options +MITIGATION_INDIRECT ?= 0 +MITIGATION_RET ?= 0 +MITIGATION_C ?= 0 +MITIGATION_ASM ?= 0 +MITIGATION_AFTERLOAD ?= 0 +MITIGATION_LIB_PATH := + +ifeq ($(MITIGATION-CVE-2020-0551), LOAD) + MITIGATION_C := 1 + MITIGATION_ASM := 1 + MITIGATION_INDIRECT := 1 + MITIGATION_RET := 1 + MITIGATION_AFTERLOAD := 1 + MITIGATION_LIB_PATH := cve_2020_0551_load +else ifeq ($(MITIGATION-CVE-2020-0551), CF) + MITIGATION_C := 1 + MITIGATION_ASM := 1 + MITIGATION_INDIRECT := 1 + MITIGATION_RET := 1 + MITIGATION_AFTERLOAD := 0 + MITIGATION_LIB_PATH := cve_2020_0551_cf +endif + +MITIGATION_CFLAGS := +MITIGATION_ASFLAGS := +ifeq ($(MITIGATION_C), 1) +ifeq ($(MITIGATION_INDIRECT), 1) + MITIGATION_CFLAGS += -mindirect-branch-register +endif +ifeq ($(MITIGATION_RET), 1) + MITIGATION_CFLAGS += -mfunction-return=thunk-extern +endif +endif + +ifeq ($(MITIGATION_ASM), 1) + MITIGATION_ASFLAGS += -fno-plt +ifeq ($(MITIGATION_AFTERLOAD), 1) + MITIGATION_ASFLAGS += -Wa,-mlfence-after-load=yes +else + MITIGATION_ASFLAGS += -Wa,-mlfence-before-indirect-branch=register +endif +ifeq ($(MITIGATION_RET), 1) + MITIGATION_ASFLAGS += -Wa,-mlfence-before-ret=not +endif +endif + +MITIGATION_CFLAGS += $(MITIGATION_ASFLAGS) + +# Compiler and linker options for an Enclave +# +# We are using '--export-dynamic' so that `g_global_data_sim' etc. +# will be exported to dynamic symbol table. +# +# When `pie' is enabled, the linker (both BFD and Gold) under Ubuntu 14.04 +# will hide all symbols from dynamic symbol table even if they are marked +# as `global' in the LD version script. +ENCLAVE_CFLAGS = -ffreestanding -nostdinc -fvisibility=hidden -fpie -fno-strict-overflow -fno-delete-null-pointer-checks +ENCLAVE_CXXFLAGS = $(ENCLAVE_CFLAGS) -nostdinc++ +ENCLAVE_LDFLAGS = $(COMMON_LDFLAGS) -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \ + -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \ + -Wl,--gc-sections \ + -Wl,--defsym,__ImageBase=0 + +ENCLAVE_CFLAGS += $(MITIGATION_CFLAGS) +ENCLAVE_ASFLAGS = $(MITIGATION_ASFLAGS) \ No newline at end of file diff --git a/user-auth/enclave/Cargo.lock b/user-auth/enclave/Cargo.lock new file mode 100644 index 0000000..0f61c39 --- /dev/null +++ b/user-auth/enclave/Cargo.lock @@ -0,0 +1,371 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "argon2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db4ce4441f99dbd377ca8a8f57b698c44d0d6e712d8329b5040da5a64aa1ce73" +dependencies = [ + "base64ct", + "blake2", + "password-hash", +] + +[[package]] +name = "base64ct" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + +[[package]] +name = "block-buffer" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cc" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "hashbrown_tstd" +version = "0.12.0" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "hkdf" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791a029f6b9fc27657f6f188ec6e5e43f6911f6f878e0dc5501396e09809d437" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "password-hash" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700" +dependencies = [ + "base64ct", + "rand_core", + "subtle", +] + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "sgx-tkeys" +version = "0.1.0" +source = "git+https://github.com/jdvlio/rooibos-sgx.git?rev=521728b24f08228e2c936cf8c018093efb0f8a14#521728b24f08228e2c936cf8c018093efb0f8a14" +dependencies = [ + "bitflags", + "hkdf", + "sgx_types 1.1.6 (git+https://github.com/apache/incubator-teaclave-sgx-sdk?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", + "sha2", + "zeroize", +] + +[[package]] +name = "sgx_alloc" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "sgx_backtrace_sys" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "cc", + "sgx_build_helper", + "sgx_libc", +] + +[[package]] +name = "sgx_build_helper" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "sgx_demangle" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "sgx_libc" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "sgx_types 1.1.6 (git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", +] + +[[package]] +name = "sgx_rand" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "sgx_trts", + "sgx_tstd", + "sgx_types 1.1.6 (git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", +] + +[[package]] +name = "sgx_tprotected_fs" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "sgx_trts", + "sgx_types 1.1.6 (git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", +] + +[[package]] +name = "sgx_trts" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "sgx_libc", + "sgx_types 1.1.6 (git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", +] + +[[package]] +name = "sgx_tstd" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "hashbrown_tstd", + "sgx_alloc", + "sgx_backtrace_sys", + "sgx_demangle", + "sgx_libc", + "sgx_tprotected_fs", + "sgx_trts", + "sgx_types 1.1.6 (git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", + "sgx_unwind", +] + +[[package]] +name = "sgx_types" +version = "1.1.6" +source = "git+https://github.com/apache/incubator-teaclave-sgx-sdk?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "sgx_types" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" + +[[package]] +name = "sgx_unwind" +version = "1.1.6" +source = "git+https://github.com/apache/teaclave-sgx-sdk.git?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2#27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" +dependencies = [ + "sgx_build_helper", +] + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "user-auth" +version = "1.0.0" +dependencies = [ + "argon2", + "once_cell", + "password-hash", + "sgx-tkeys", + "sgx_rand", + "sgx_tstd", + "sgx_types 1.1.6 (git+https://github.com/apache/incubator-teaclave-sgx-sdk?rev=27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2)", +] + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "zeroize" +version = "1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bf07cb3e50ea2003396695d58bf46bc9887a1f362260446fad6bc4e79bd36c" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] diff --git a/user-auth/enclave/Cargo.toml b/user-auth/enclave/Cargo.toml new file mode 100644 index 0000000..7476f1d --- /dev/null +++ b/user-auth/enclave/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "user-auth" +version = "1.0.0" +authors = ["Nautilus"] +edition = "2021" + +[lib] +name = "userauth" # Library name. If you change this, please reflect those changes in the Makefile on the variable ENCLAVE_CARGO_LIB +crate-type = ["staticlib"] + +[features] +default = [] + +[dependencies] +sgx_types = { git="https://github.com/apache/incubator-teaclave-sgx-sdk", rev="27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" } +sgx_tstd = { git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["backtrace"], rev="27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" } +sgx_rand = { git = "https://github.com/apache/teaclave-sgx-sdk.git", rev="27bd225ae6dbcd1d0a6d4d9590acc4d73c5195c2" } +sgx-tkeys = { git="https://github.com/jdvlio/rooibos-sgx.git", rev="521728b24f08228e2c936cf8c018093efb0f8a14" } + +argon2 = { version = "0.4", default-features = false, features = ["alloc", "password-hash"] } +password-hash = { version = "0.4", default-features = false, features = ["alloc"] } +once_cell = { version = "1.17.0", default-features = false, features = ["alloc"] } + +[patch.'https://github.com/apache/teaclave-sgx-sdk.git'] diff --git a/user-auth/enclave/Enclave.config.xml b/user-auth/enclave/Enclave.config.xml new file mode 100644 index 0000000..e743815 --- /dev/null +++ b/user-auth/enclave/Enclave.config.xml @@ -0,0 +1,12 @@ + + + 0 + 0 + 0x40000 + 0x500000 + 1 + 1 + 0 + 0 + 0xFFFFFFFF + diff --git a/user-auth/enclave/Enclave.edl b/user-auth/enclave/Enclave.edl new file mode 100644 index 0000000..dfd7aed --- /dev/null +++ b/user-auth/enclave/Enclave.edl @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +/* This is your enclave EDL file, please specify the EDL files you need and ECALLs/OCALLs needed */ + +enclave { + from "sgx_tstd.edl" import *; + from "sgx_stdio.edl" import *; + from "sgx_backtrace.edl" import *; + from "sgx_tstdc.edl" import *; + trusted + { + /* ECALLs */ + public sgx_status_t hash_password( + [in, size=password_length] const uint8_t *password, + size_t password_length, + [in, size=user_id_length] const uint8_t *user_id, + size_t user_id_length, + [out, size=64] uint8_t *hash, + [out] size_t *hash_length_used + ); + public sgx_status_t verify_password( + [in, size=password_length] const uint8_t *password, + size_t password_length, + [in, size=user_id_length] const uint8_t *user_id, + size_t user_id_length, + [in, size=hash_length] const uint8_t *stored_hash, + size_t hash_length, + [out] uint8_t *password_verified + ); + }; + untrusted + { + /* OCALLs */ + }; +}; diff --git a/user-auth/enclave/Enclave.lds b/user-auth/enclave/Enclave.lds new file mode 100644 index 0000000..e3d9d0e --- /dev/null +++ b/user-auth/enclave/Enclave.lds @@ -0,0 +1,9 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + local: + *; +}; diff --git a/user-auth/enclave/Enclave_t.c b/user-auth/enclave/Enclave_t.c new file mode 100644 index 0000000..c032e93 --- /dev/null +++ b/user-auth/enclave/Enclave_t.c @@ -0,0 +1,7009 @@ +#include "Enclave_t.h" + +#include "sgx_trts.h" /* for sgx_ocalloc, sgx_is_outside_enclave */ +#include "sgx_lfence.h" /* for sgx_lfence */ + +#include +#include /* for memcpy_s etc */ +#include /* for malloc/free etc */ + +#define CHECK_REF_POINTER(ptr, siz) do { \ + if (!(ptr) || ! sgx_is_outside_enclave((ptr), (siz))) \ + return SGX_ERROR_INVALID_PARAMETER;\ +} while (0) + +#define CHECK_UNIQUE_POINTER(ptr, siz) do { \ + if ((ptr) && ! sgx_is_outside_enclave((ptr), (siz))) \ + return SGX_ERROR_INVALID_PARAMETER;\ +} while (0) + +#define CHECK_ENCLAVE_POINTER(ptr, siz) do { \ + if ((ptr) && ! sgx_is_within_enclave((ptr), (siz))) \ + return SGX_ERROR_INVALID_PARAMETER;\ +} while (0) + +#define ADD_ASSIGN_OVERFLOW(a, b) ( \ + ((a) += (b)) < (b) \ +) + + +typedef struct ms_hash_password_t { + sgx_status_t ms_retval; + const uint8_t* ms_password; + size_t ms_password_length; + const uint8_t* ms_user_id; + size_t ms_user_id_length; + uint8_t* ms_hash; + size_t* ms_hash_length; +} ms_hash_password_t; + +typedef struct ms_verify_password_t { + sgx_status_t ms_retval; + const uint8_t* ms_password; + size_t ms_password_length; + const uint8_t* ms_user_id; + size_t ms_user_id_length; + const uint8_t* ms_stored_hash; + size_t ms_hash_length; + uint8_t* ms_password_verified; +} ms_verify_password_t; + +typedef struct ms_t_global_init_ecall_t { + uint64_t ms_id; + const uint8_t* ms_path; + size_t ms_len; +} ms_t_global_init_ecall_t; + +typedef struct ms_u_thread_set_event_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_tcs; +} ms_u_thread_set_event_ocall_t; + +typedef struct ms_u_thread_wait_event_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_tcs; + const struct timespec* ms_timeout; +} ms_u_thread_wait_event_ocall_t; + +typedef struct ms_u_thread_set_multiple_events_ocall_t { + int ms_retval; + int* ms_error; + const void** ms_tcss; + int ms_total; +} ms_u_thread_set_multiple_events_ocall_t; + +typedef struct ms_u_thread_setwait_events_ocall_t { + int ms_retval; + int* ms_error; + const void* ms_waiter_tcs; + const void* ms_self_tcs; + const struct timespec* ms_timeout; +} ms_u_thread_setwait_events_ocall_t; + +typedef struct ms_u_clock_gettime_ocall_t { + int ms_retval; + int* ms_error; + int ms_clk_id; + struct timespec* ms_tp; +} ms_u_clock_gettime_ocall_t; + +typedef struct ms_u_read_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + void* ms_buf; + size_t ms_count; +} ms_u_read_ocall_t; + +typedef struct ms_u_pread64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + void* ms_buf; + size_t ms_count; + int64_t ms_offset; +} ms_u_pread64_ocall_t; + +typedef struct ms_u_readv_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; +} ms_u_readv_ocall_t; + +typedef struct ms_u_preadv64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; + int64_t ms_offset; +} ms_u_preadv64_ocall_t; + +typedef struct ms_u_write_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const void* ms_buf; + size_t ms_count; +} ms_u_write_ocall_t; + +typedef struct ms_u_pwrite64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const void* ms_buf; + size_t ms_count; + int64_t ms_offset; +} ms_u_pwrite64_ocall_t; + +typedef struct ms_u_writev_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; +} ms_u_writev_ocall_t; + +typedef struct ms_u_pwritev64_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd; + const struct iovec* ms_iov; + int ms_iovcnt; + int64_t ms_offset; +} ms_u_pwritev64_ocall_t; + +typedef struct ms_u_sendfile_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_out_fd; + int ms_in_fd; + int64_t* ms_offset; + size_t ms_count; +} ms_u_sendfile_ocall_t; + +typedef struct ms_u_copy_file_range_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd_in; + int64_t* ms_off_in; + int ms_fd_out; + int64_t* ms_off_out; + size_t ms_len; + unsigned int ms_flags; +} ms_u_copy_file_range_ocall_t; + +typedef struct ms_u_splice_ocall_t { + size_t ms_retval; + int* ms_error; + int ms_fd_in; + int64_t* ms_off_in; + int ms_fd_out; + int64_t* ms_off_out; + size_t ms_len; + unsigned int ms_flags; +} ms_u_splice_ocall_t; + +typedef struct ms_u_fcntl_arg0_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_cmd; +} ms_u_fcntl_arg0_ocall_t; + +typedef struct ms_u_fcntl_arg1_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_cmd; + int ms_arg; +} ms_u_fcntl_arg1_ocall_t; + +typedef struct ms_u_ioctl_arg0_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_request; +} ms_u_ioctl_arg0_ocall_t; + +typedef struct ms_u_ioctl_arg1_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int ms_request; + int* ms_arg; +} ms_u_ioctl_arg1_ocall_t; + +typedef struct ms_u_close_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_close_ocall_t; + +typedef struct ms_u_isatty_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_isatty_ocall_t; + +typedef struct ms_u_dup_ocall_t { + int ms_retval; + int* ms_error; + int ms_oldfd; +} ms_u_dup_ocall_t; + +typedef struct ms_u_eventfd_ocall_t { + int ms_retval; + int* ms_error; + unsigned int ms_initval; + int ms_flags; +} ms_u_eventfd_ocall_t; + +typedef struct ms_u_futimens_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + const struct timespec* ms_times; +} ms_u_futimens_ocall_t; + +typedef struct ms_u_malloc_ocall_t { + void* ms_retval; + int* ms_error; + size_t ms_size; +} ms_u_malloc_ocall_t; + +typedef struct ms_u_free_ocall_t { + void* ms_p; +} ms_u_free_ocall_t; + +typedef struct ms_u_mmap_ocall_t { + void* ms_retval; + int* ms_error; + void* ms_start; + size_t ms_length; + int ms_prot; + int ms_flags; + int ms_fd; + int64_t ms_offset; +} ms_u_mmap_ocall_t; + +typedef struct ms_u_munmap_ocall_t { + int ms_retval; + int* ms_error; + void* ms_start; + size_t ms_length; +} ms_u_munmap_ocall_t; + +typedef struct ms_u_msync_ocall_t { + int ms_retval; + int* ms_error; + void* ms_addr; + size_t ms_length; + int ms_flags; +} ms_u_msync_ocall_t; + +typedef struct ms_u_mprotect_ocall_t { + int ms_retval; + int* ms_error; + void* ms_addr; + size_t ms_length; + int ms_prot; +} ms_u_mprotect_ocall_t; + +typedef struct ms_u_open_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; + int ms_flags; +} ms_u_open_ocall_t; + +typedef struct ms_u_open64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int ms_oflag; + int ms_mode; +} ms_u_open64_ocall_t; + +typedef struct ms_u_openat_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + int ms_flags; +} ms_u_openat_ocall_t; + +typedef struct ms_u_fstat_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + struct stat_t* ms_buf; +} ms_u_fstat_ocall_t; + +typedef struct ms_u_fstat64_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + struct stat64_t* ms_buf; +} ms_u_fstat64_ocall_t; + +typedef struct ms_u_stat_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat_t* ms_buf; +} ms_u_stat_ocall_t; + +typedef struct ms_u_stat64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat64_t* ms_buf; +} ms_u_stat64_ocall_t; + +typedef struct ms_u_lstat_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat_t* ms_buf; +} ms_u_lstat_ocall_t; + +typedef struct ms_u_lstat64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + struct stat64_t* ms_buf; +} ms_u_lstat64_ocall_t; + +typedef struct ms_u_lseek_ocall_t { + uint64_t ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_offset; + int ms_whence; +} ms_u_lseek_ocall_t; + +typedef struct ms_u_lseek64_ocall_t { + int64_t ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_offset; + int ms_whence; +} ms_u_lseek64_ocall_t; + +typedef struct ms_u_ftruncate_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_length; +} ms_u_ftruncate_ocall_t; + +typedef struct ms_u_ftruncate64_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + int64_t ms_length; +} ms_u_ftruncate64_ocall_t; + +typedef struct ms_u_truncate_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int64_t ms_length; +} ms_u_truncate_ocall_t; + +typedef struct ms_u_truncate64_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + int64_t ms_length; +} ms_u_truncate64_ocall_t; + +typedef struct ms_u_fsync_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fsync_ocall_t; + +typedef struct ms_u_fdatasync_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fdatasync_ocall_t; + +typedef struct ms_u_fchmod_ocall_t { + int ms_retval; + int* ms_error; + int ms_fd; + uint32_t ms_mode; +} ms_u_fchmod_ocall_t; + +typedef struct ms_u_unlink_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_unlink_ocall_t; + +typedef struct ms_u_link_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_oldpath; + const char* ms_newpath; +} ms_u_link_ocall_t; + +typedef struct ms_u_unlinkat_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + int ms_flags; +} ms_u_unlinkat_ocall_t; + +typedef struct ms_u_linkat_ocall_t { + int ms_retval; + int* ms_error; + int ms_olddirfd; + const char* ms_oldpath; + int ms_newdirfd; + const char* ms_newpath; + int ms_flags; +} ms_u_linkat_ocall_t; + +typedef struct ms_u_rename_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_oldpath; + const char* ms_newpath; +} ms_u_rename_ocall_t; + +typedef struct ms_u_chmod_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path; + uint32_t ms_mode; +} ms_u_chmod_ocall_t; + +typedef struct ms_u_readlink_ocall_t { + size_t ms_retval; + int* ms_error; + const char* ms_path; + char* ms_buf; + size_t ms_bufsz; +} ms_u_readlink_ocall_t; + +typedef struct ms_u_symlink_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_path1; + const char* ms_path2; +} ms_u_symlink_ocall_t; + +typedef struct ms_u_realpath_ocall_t { + char* ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_realpath_ocall_t; + +typedef struct ms_u_mkdir_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; + uint32_t ms_mode; +} ms_u_mkdir_ocall_t; + +typedef struct ms_u_rmdir_ocall_t { + int ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_rmdir_ocall_t; + +typedef struct ms_u_fdopendir_ocall_t { + void* ms_retval; + int* ms_error; + int ms_fd; +} ms_u_fdopendir_ocall_t; + +typedef struct ms_u_opendir_ocall_t { + void* ms_retval; + int* ms_error; + const char* ms_pathname; +} ms_u_opendir_ocall_t; + +typedef struct ms_u_readdir64_r_ocall_t { + int ms_retval; + void* ms_dirp; + struct dirent64_t* ms_entry; + struct dirent64_t** ms_result; +} ms_u_readdir64_r_ocall_t; + +typedef struct ms_u_closedir_ocall_t { + int ms_retval; + int* ms_error; + void* ms_dirp; +} ms_u_closedir_ocall_t; + +typedef struct ms_u_dirfd_ocall_t { + int ms_retval; + int* ms_error; + void* ms_dirp; +} ms_u_dirfd_ocall_t; + +typedef struct ms_u_fstatat64_ocall_t { + int ms_retval; + int* ms_error; + int ms_dirfd; + const char* ms_pathname; + struct stat64_t* ms_buf; + int ms_flags; +} ms_u_fstatat64_ocall_t; + +typedef struct ms_sgx_oc_cpuidex_t { + int* ms_cpuinfo; + int ms_leaf; + int ms_subleaf; +} ms_sgx_oc_cpuidex_t; + +typedef struct ms_sgx_thread_wait_untrusted_event_ocall_t { + int ms_retval; + const void* ms_self; +} ms_sgx_thread_wait_untrusted_event_ocall_t; + +typedef struct ms_sgx_thread_set_untrusted_event_ocall_t { + int ms_retval; + const void* ms_waiter; +} ms_sgx_thread_set_untrusted_event_ocall_t; + +typedef struct ms_sgx_thread_setwait_untrusted_events_ocall_t { + int ms_retval; + const void* ms_waiter; + const void* ms_self; +} ms_sgx_thread_setwait_untrusted_events_ocall_t; + +typedef struct ms_sgx_thread_set_multiple_untrusted_events_ocall_t { + int ms_retval; + const void** ms_waiters; + size_t ms_total; +} ms_sgx_thread_set_multiple_untrusted_events_ocall_t; + +static sgx_status_t SGX_CDECL sgx_hash_password(void* pms) +{ + CHECK_REF_POINTER(pms, sizeof(ms_hash_password_t)); + // + // fence after pointer checks + // + sgx_lfence(); + ms_hash_password_t* ms = SGX_CAST(ms_hash_password_t*, pms); + ms_hash_password_t __in_ms; + if (memcpy_s(&__in_ms, sizeof(ms_hash_password_t), ms, sizeof(ms_hash_password_t))) { + return SGX_ERROR_UNEXPECTED; + } + sgx_status_t status = SGX_SUCCESS; + const uint8_t* _tmp_password = __in_ms.ms_password; + size_t _tmp_password_length = __in_ms.ms_password_length; + size_t _len_password = _tmp_password_length; + uint8_t* _in_password = NULL; + const uint8_t* _tmp_user_id = __in_ms.ms_user_id; + size_t _tmp_user_id_length = __in_ms.ms_user_id_length; + size_t _len_user_id = _tmp_user_id_length; + uint8_t* _in_user_id = NULL; + uint8_t* _tmp_hash = __in_ms.ms_hash; + size_t _len_hash = 64; + uint8_t* _in_hash = NULL; + size_t* _tmp_hash_length = __in_ms.ms_hash_length; + size_t _len_hash_length = sizeof(size_t); + size_t* _in_hash_length = NULL; + sgx_status_t _in_retval; + + CHECK_UNIQUE_POINTER(_tmp_password, _len_password); + CHECK_UNIQUE_POINTER(_tmp_user_id, _len_user_id); + CHECK_UNIQUE_POINTER(_tmp_hash, _len_hash); + CHECK_UNIQUE_POINTER(_tmp_hash_length, _len_hash_length); + + // + // fence after pointer checks + // + sgx_lfence(); + + if (_tmp_password != NULL && _len_password != 0) { + if ( _len_password % sizeof(*_tmp_password) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_password = (uint8_t*)malloc(_len_password); + if (_in_password == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_password, _len_password, _tmp_password, _len_password)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + if (_tmp_user_id != NULL && _len_user_id != 0) { + if ( _len_user_id % sizeof(*_tmp_user_id) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_user_id = (uint8_t*)malloc(_len_user_id); + if (_in_user_id == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_user_id, _len_user_id, _tmp_user_id, _len_user_id)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + if (_tmp_hash != NULL && _len_hash != 0) { + if ( _len_hash % sizeof(*_tmp_hash) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + if ((_in_hash = (uint8_t*)malloc(_len_hash)) == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + memset((void*)_in_hash, 0, _len_hash); + } + if (_tmp_hash_length != NULL && _len_hash_length != 0) { + if ( _len_hash_length % sizeof(*_tmp_hash_length) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + if ((_in_hash_length = (size_t*)malloc(_len_hash_length)) == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + memset((void*)_in_hash_length, 0, _len_hash_length); + } + _in_retval = hash_password((const uint8_t*)_in_password, _tmp_password_length, (const uint8_t*)_in_user_id, _tmp_user_id_length, _in_hash, _in_hash_length); + if (memcpy_verw_s(&ms->ms_retval, sizeof(ms->ms_retval), &_in_retval, sizeof(_in_retval))) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + if (_in_hash) { + if (memcpy_verw_s(_tmp_hash, _len_hash, _in_hash, _len_hash)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + } + if (_in_hash_length) { + if (memcpy_verw_s(_tmp_hash_length, _len_hash_length, _in_hash_length, _len_hash_length)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + } + +err: + if (_in_password) free(_in_password); + if (_in_user_id) free(_in_user_id); + if (_in_hash) free(_in_hash); + if (_in_hash_length) free(_in_hash_length); + return status; +} + +static sgx_status_t SGX_CDECL sgx_verify_password(void* pms) +{ + CHECK_REF_POINTER(pms, sizeof(ms_verify_password_t)); + // + // fence after pointer checks + // + sgx_lfence(); + ms_verify_password_t* ms = SGX_CAST(ms_verify_password_t*, pms); + ms_verify_password_t __in_ms; + if (memcpy_s(&__in_ms, sizeof(ms_verify_password_t), ms, sizeof(ms_verify_password_t))) { + return SGX_ERROR_UNEXPECTED; + } + sgx_status_t status = SGX_SUCCESS; + const uint8_t* _tmp_password = __in_ms.ms_password; + size_t _tmp_password_length = __in_ms.ms_password_length; + size_t _len_password = _tmp_password_length; + uint8_t* _in_password = NULL; + const uint8_t* _tmp_user_id = __in_ms.ms_user_id; + size_t _tmp_user_id_length = __in_ms.ms_user_id_length; + size_t _len_user_id = _tmp_user_id_length; + uint8_t* _in_user_id = NULL; + const uint8_t* _tmp_stored_hash = __in_ms.ms_stored_hash; + size_t _tmp_hash_length = __in_ms.ms_hash_length; + size_t _len_stored_hash = _tmp_hash_length; + uint8_t* _in_stored_hash = NULL; + uint8_t* _tmp_password_verified = __in_ms.ms_password_verified; + size_t _len_password_verified = sizeof(uint8_t); + uint8_t* _in_password_verified = NULL; + sgx_status_t _in_retval; + + CHECK_UNIQUE_POINTER(_tmp_password, _len_password); + CHECK_UNIQUE_POINTER(_tmp_user_id, _len_user_id); + CHECK_UNIQUE_POINTER(_tmp_stored_hash, _len_stored_hash); + CHECK_UNIQUE_POINTER(_tmp_password_verified, _len_password_verified); + + // + // fence after pointer checks + // + sgx_lfence(); + + if (_tmp_password != NULL && _len_password != 0) { + if ( _len_password % sizeof(*_tmp_password) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_password = (uint8_t*)malloc(_len_password); + if (_in_password == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_password, _len_password, _tmp_password, _len_password)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + if (_tmp_user_id != NULL && _len_user_id != 0) { + if ( _len_user_id % sizeof(*_tmp_user_id) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_user_id = (uint8_t*)malloc(_len_user_id); + if (_in_user_id == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_user_id, _len_user_id, _tmp_user_id, _len_user_id)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + if (_tmp_stored_hash != NULL && _len_stored_hash != 0) { + if ( _len_stored_hash % sizeof(*_tmp_stored_hash) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_stored_hash = (uint8_t*)malloc(_len_stored_hash); + if (_in_stored_hash == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_stored_hash, _len_stored_hash, _tmp_stored_hash, _len_stored_hash)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + if (_tmp_password_verified != NULL && _len_password_verified != 0) { + if ( _len_password_verified % sizeof(*_tmp_password_verified) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + if ((_in_password_verified = (uint8_t*)malloc(_len_password_verified)) == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + memset((void*)_in_password_verified, 0, _len_password_verified); + } + _in_retval = verify_password((const uint8_t*)_in_password, _tmp_password_length, (const uint8_t*)_in_user_id, _tmp_user_id_length, (const uint8_t*)_in_stored_hash, _tmp_hash_length, _in_password_verified); + if (memcpy_verw_s(&ms->ms_retval, sizeof(ms->ms_retval), &_in_retval, sizeof(_in_retval))) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + if (_in_password_verified) { + if (memcpy_verw_s(_tmp_password_verified, _len_password_verified, _in_password_verified, _len_password_verified)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + } + +err: + if (_in_password) free(_in_password); + if (_in_user_id) free(_in_user_id); + if (_in_stored_hash) free(_in_stored_hash); + if (_in_password_verified) free(_in_password_verified); + return status; +} + +static sgx_status_t SGX_CDECL sgx_t_global_init_ecall(void* pms) +{ + CHECK_REF_POINTER(pms, sizeof(ms_t_global_init_ecall_t)); + // + // fence after pointer checks + // + sgx_lfence(); + ms_t_global_init_ecall_t* ms = SGX_CAST(ms_t_global_init_ecall_t*, pms); + ms_t_global_init_ecall_t __in_ms; + if (memcpy_s(&__in_ms, sizeof(ms_t_global_init_ecall_t), ms, sizeof(ms_t_global_init_ecall_t))) { + return SGX_ERROR_UNEXPECTED; + } + sgx_status_t status = SGX_SUCCESS; + const uint8_t* _tmp_path = __in_ms.ms_path; + size_t _tmp_len = __in_ms.ms_len; + size_t _len_path = _tmp_len; + uint8_t* _in_path = NULL; + + CHECK_UNIQUE_POINTER(_tmp_path, _len_path); + + // + // fence after pointer checks + // + sgx_lfence(); + + if (_tmp_path != NULL && _len_path != 0) { + if ( _len_path % sizeof(*_tmp_path) != 0) + { + status = SGX_ERROR_INVALID_PARAMETER; + goto err; + } + _in_path = (uint8_t*)malloc(_len_path); + if (_in_path == NULL) { + status = SGX_ERROR_OUT_OF_MEMORY; + goto err; + } + + if (memcpy_s(_in_path, _len_path, _tmp_path, _len_path)) { + status = SGX_ERROR_UNEXPECTED; + goto err; + } + + } + t_global_init_ecall(__in_ms.ms_id, (const uint8_t*)_in_path, _tmp_len); + +err: + if (_in_path) free(_in_path); + return status; +} + +static sgx_status_t SGX_CDECL sgx_t_global_exit_ecall(void* pms) +{ + sgx_status_t status = SGX_SUCCESS; + if (pms != NULL) return SGX_ERROR_INVALID_PARAMETER; + t_global_exit_ecall(); + return status; +} + +SGX_EXTERNC const struct { + size_t nr_ecall; + struct {void* ecall_addr; uint8_t is_priv; uint8_t is_switchless;} ecall_table[4]; +} g_ecall_table = { + 4, + { + {(void*)(uintptr_t)sgx_hash_password, 0, 0}, + {(void*)(uintptr_t)sgx_verify_password, 0, 0}, + {(void*)(uintptr_t)sgx_t_global_init_ecall, 0, 0}, + {(void*)(uintptr_t)sgx_t_global_exit_ecall, 0, 0}, + } +}; + +SGX_EXTERNC const struct { + size_t nr_ocall; + uint8_t entry_table[71][4]; +} g_dyn_entry_table = { + 71, + { + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + {0, 0, 0, 0, }, + } +}; + + +sgx_status_t SGX_CDECL u_thread_set_event_ocall(int* retval, int* error, const void* tcs) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_thread_set_event_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_thread_set_event_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_thread_set_event_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_thread_set_event_ocall_t)); + ocalloc_size -= sizeof(ms_u_thread_set_event_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_tcs, sizeof(ms->ms_tcs), &tcs, sizeof(tcs))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(0, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_thread_wait_event_ocall(int* retval, int* error, const void* tcs, const struct timespec* timeout) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_timeout = sizeof(struct timespec); + + ms_u_thread_wait_event_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_thread_wait_event_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(timeout, _len_timeout); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (timeout != NULL) ? _len_timeout : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_thread_wait_event_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_thread_wait_event_ocall_t)); + ocalloc_size -= sizeof(ms_u_thread_wait_event_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_tcs, sizeof(ms->ms_tcs), &tcs, sizeof(tcs))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (timeout != NULL) { + if (memcpy_verw_s(&ms->ms_timeout, sizeof(const struct timespec*), &__tmp, sizeof(const struct timespec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, timeout, _len_timeout)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_timeout); + ocalloc_size -= _len_timeout; + } else { + ms->ms_timeout = NULL; + } + + status = sgx_ocall(1, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_thread_set_multiple_events_ocall(int* retval, int* error, const void** tcss, int total) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_tcss = total * sizeof(void*); + + ms_u_thread_set_multiple_events_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_thread_set_multiple_events_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(tcss, _len_tcss); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (tcss != NULL) ? _len_tcss : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_thread_set_multiple_events_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_thread_set_multiple_events_ocall_t)); + ocalloc_size -= sizeof(ms_u_thread_set_multiple_events_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (tcss != NULL) { + if (memcpy_verw_s(&ms->ms_tcss, sizeof(const void**), &__tmp, sizeof(const void**))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_tcss % sizeof(*tcss) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, tcss, _len_tcss)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_tcss); + ocalloc_size -= _len_tcss; + } else { + ms->ms_tcss = NULL; + } + + if (memcpy_verw_s(&ms->ms_total, sizeof(ms->ms_total), &total, sizeof(total))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(2, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_thread_setwait_events_ocall(int* retval, int* error, const void* waiter_tcs, const void* self_tcs, const struct timespec* timeout) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_timeout = sizeof(struct timespec); + + ms_u_thread_setwait_events_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_thread_setwait_events_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(timeout, _len_timeout); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (timeout != NULL) ? _len_timeout : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_thread_setwait_events_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_thread_setwait_events_ocall_t)); + ocalloc_size -= sizeof(ms_u_thread_setwait_events_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_waiter_tcs, sizeof(ms->ms_waiter_tcs), &waiter_tcs, sizeof(waiter_tcs))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_self_tcs, sizeof(ms->ms_self_tcs), &self_tcs, sizeof(self_tcs))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (timeout != NULL) { + if (memcpy_verw_s(&ms->ms_timeout, sizeof(const struct timespec*), &__tmp, sizeof(const struct timespec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, timeout, _len_timeout)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_timeout); + ocalloc_size -= _len_timeout; + } else { + ms->ms_timeout = NULL; + } + + status = sgx_ocall(3, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_clock_gettime_ocall(int* retval, int* error, int clk_id, struct timespec* tp) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_tp = sizeof(struct timespec); + + ms_u_clock_gettime_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_clock_gettime_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_tp = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(tp, _len_tp); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (tp != NULL) ? _len_tp : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_clock_gettime_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_clock_gettime_ocall_t)); + ocalloc_size -= sizeof(ms_u_clock_gettime_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_clk_id, sizeof(ms->ms_clk_id), &clk_id, sizeof(clk_id))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (tp != NULL) { + if (memcpy_verw_s(&ms->ms_tp, sizeof(struct timespec*), &__tmp, sizeof(struct timespec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_tp = __tmp; + memset_verw(__tmp_tp, 0, _len_tp); + __tmp = (void *)((size_t)__tmp + _len_tp); + ocalloc_size -= _len_tp; + } else { + ms->ms_tp = NULL; + } + + status = sgx_ocall(4, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (tp) { + if (memcpy_s((void*)tp, _len_tp, __tmp_tp, _len_tp)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_read_ocall(size_t* retval, int* error, int fd, void* buf, size_t count) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_read_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_read_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_read_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_read_ocall_t)); + ocalloc_size -= sizeof(ms_u_read_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_buf, sizeof(ms->ms_buf), &buf, sizeof(buf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_count, sizeof(ms->ms_count), &count, sizeof(count))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(5, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_pread64_ocall(size_t* retval, int* error, int fd, void* buf, size_t count, int64_t offset) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_pread64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_pread64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_pread64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_pread64_ocall_t)); + ocalloc_size -= sizeof(ms_u_pread64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_buf, sizeof(ms->ms_buf), &buf, sizeof(buf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_count, sizeof(ms->ms_count), &count, sizeof(count))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(6, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_readv_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_iov = iovcnt * sizeof(struct iovec); + + ms_u_readv_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_readv_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(iov, _len_iov); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (iov != NULL) ? _len_iov : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_readv_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_readv_ocall_t)); + ocalloc_size -= sizeof(ms_u_readv_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (iov != NULL) { + if (memcpy_verw_s(&ms->ms_iov, sizeof(const struct iovec*), &__tmp, sizeof(const struct iovec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, iov, _len_iov)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_iov); + ocalloc_size -= _len_iov; + } else { + ms->ms_iov = NULL; + } + + if (memcpy_verw_s(&ms->ms_iovcnt, sizeof(ms->ms_iovcnt), &iovcnt, sizeof(iovcnt))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(7, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_preadv64_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_iov = iovcnt * sizeof(struct iovec); + + ms_u_preadv64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_preadv64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(iov, _len_iov); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (iov != NULL) ? _len_iov : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_preadv64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_preadv64_ocall_t)); + ocalloc_size -= sizeof(ms_u_preadv64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (iov != NULL) { + if (memcpy_verw_s(&ms->ms_iov, sizeof(const struct iovec*), &__tmp, sizeof(const struct iovec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, iov, _len_iov)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_iov); + ocalloc_size -= _len_iov; + } else { + ms->ms_iov = NULL; + } + + if (memcpy_verw_s(&ms->ms_iovcnt, sizeof(ms->ms_iovcnt), &iovcnt, sizeof(iovcnt))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(8, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_write_ocall(size_t* retval, int* error, int fd, const void* buf, size_t count) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_write_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_write_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_write_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_write_ocall_t)); + ocalloc_size -= sizeof(ms_u_write_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_buf, sizeof(ms->ms_buf), &buf, sizeof(buf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_count, sizeof(ms->ms_count), &count, sizeof(count))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(9, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_pwrite64_ocall(size_t* retval, int* error, int fd, const void* buf, size_t count, int64_t offset) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_pwrite64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_pwrite64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_pwrite64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_pwrite64_ocall_t)); + ocalloc_size -= sizeof(ms_u_pwrite64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_buf, sizeof(ms->ms_buf), &buf, sizeof(buf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_count, sizeof(ms->ms_count), &count, sizeof(count))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(10, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_writev_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_iov = iovcnt * sizeof(struct iovec); + + ms_u_writev_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_writev_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(iov, _len_iov); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (iov != NULL) ? _len_iov : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_writev_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_writev_ocall_t)); + ocalloc_size -= sizeof(ms_u_writev_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (iov != NULL) { + if (memcpy_verw_s(&ms->ms_iov, sizeof(const struct iovec*), &__tmp, sizeof(const struct iovec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, iov, _len_iov)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_iov); + ocalloc_size -= _len_iov; + } else { + ms->ms_iov = NULL; + } + + if (memcpy_verw_s(&ms->ms_iovcnt, sizeof(ms->ms_iovcnt), &iovcnt, sizeof(iovcnt))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(11, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_pwritev64_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_iov = iovcnt * sizeof(struct iovec); + + ms_u_pwritev64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_pwritev64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(iov, _len_iov); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (iov != NULL) ? _len_iov : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_pwritev64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_pwritev64_ocall_t)); + ocalloc_size -= sizeof(ms_u_pwritev64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (iov != NULL) { + if (memcpy_verw_s(&ms->ms_iov, sizeof(const struct iovec*), &__tmp, sizeof(const struct iovec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, iov, _len_iov)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_iov); + ocalloc_size -= _len_iov; + } else { + ms->ms_iov = NULL; + } + + if (memcpy_verw_s(&ms->ms_iovcnt, sizeof(ms->ms_iovcnt), &iovcnt, sizeof(iovcnt))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(12, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_sendfile_ocall(size_t* retval, int* error, int out_fd, int in_fd, int64_t* offset, size_t count) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_offset = sizeof(int64_t); + + ms_u_sendfile_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_sendfile_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_offset = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(offset, _len_offset); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (offset != NULL) ? _len_offset : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_sendfile_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_sendfile_ocall_t)); + ocalloc_size -= sizeof(ms_u_sendfile_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_out_fd, sizeof(ms->ms_out_fd), &out_fd, sizeof(out_fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_in_fd, sizeof(ms->ms_in_fd), &in_fd, sizeof(in_fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (offset != NULL) { + if (memcpy_verw_s(&ms->ms_offset, sizeof(int64_t*), &__tmp, sizeof(int64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_offset = __tmp; + if (_len_offset % sizeof(*offset) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, offset, _len_offset)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_offset); + ocalloc_size -= _len_offset; + } else { + ms->ms_offset = NULL; + } + + if (memcpy_verw_s(&ms->ms_count, sizeof(ms->ms_count), &count, sizeof(count))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(13, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (offset) { + if (memcpy_s((void*)offset, _len_offset, __tmp_offset, _len_offset)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_copy_file_range_ocall(size_t* retval, int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_off_in = sizeof(int64_t); + size_t _len_off_out = sizeof(int64_t); + + ms_u_copy_file_range_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_copy_file_range_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_off_in = NULL; + void *__tmp_off_out = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(off_in, _len_off_in); + CHECK_ENCLAVE_POINTER(off_out, _len_off_out); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (off_in != NULL) ? _len_off_in : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (off_out != NULL) ? _len_off_out : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_copy_file_range_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_copy_file_range_ocall_t)); + ocalloc_size -= sizeof(ms_u_copy_file_range_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd_in, sizeof(ms->ms_fd_in), &fd_in, sizeof(fd_in))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (off_in != NULL) { + if (memcpy_verw_s(&ms->ms_off_in, sizeof(int64_t*), &__tmp, sizeof(int64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_off_in = __tmp; + if (_len_off_in % sizeof(*off_in) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, off_in, _len_off_in)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_off_in); + ocalloc_size -= _len_off_in; + } else { + ms->ms_off_in = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd_out, sizeof(ms->ms_fd_out), &fd_out, sizeof(fd_out))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (off_out != NULL) { + if (memcpy_verw_s(&ms->ms_off_out, sizeof(int64_t*), &__tmp, sizeof(int64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_off_out = __tmp; + if (_len_off_out % sizeof(*off_out) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, off_out, _len_off_out)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_off_out); + ocalloc_size -= _len_off_out; + } else { + ms->ms_off_out = NULL; + } + + if (memcpy_verw_s(&ms->ms_len, sizeof(ms->ms_len), &len, sizeof(len))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(14, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (off_in) { + if (memcpy_s((void*)off_in, _len_off_in, __tmp_off_in, _len_off_in)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (off_out) { + if (memcpy_s((void*)off_out, _len_off_out, __tmp_off_out, _len_off_out)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_splice_ocall(size_t* retval, int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_off_in = sizeof(int64_t); + size_t _len_off_out = sizeof(int64_t); + + ms_u_splice_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_splice_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_off_in = NULL; + void *__tmp_off_out = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(off_in, _len_off_in); + CHECK_ENCLAVE_POINTER(off_out, _len_off_out); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (off_in != NULL) ? _len_off_in : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (off_out != NULL) ? _len_off_out : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_splice_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_splice_ocall_t)); + ocalloc_size -= sizeof(ms_u_splice_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd_in, sizeof(ms->ms_fd_in), &fd_in, sizeof(fd_in))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (off_in != NULL) { + if (memcpy_verw_s(&ms->ms_off_in, sizeof(int64_t*), &__tmp, sizeof(int64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_off_in = __tmp; + if (_len_off_in % sizeof(*off_in) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, off_in, _len_off_in)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_off_in); + ocalloc_size -= _len_off_in; + } else { + ms->ms_off_in = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd_out, sizeof(ms->ms_fd_out), &fd_out, sizeof(fd_out))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (off_out != NULL) { + if (memcpy_verw_s(&ms->ms_off_out, sizeof(int64_t*), &__tmp, sizeof(int64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_off_out = __tmp; + if (_len_off_out % sizeof(*off_out) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, off_out, _len_off_out)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_off_out); + ocalloc_size -= _len_off_out; + } else { + ms->ms_off_out = NULL; + } + + if (memcpy_verw_s(&ms->ms_len, sizeof(ms->ms_len), &len, sizeof(len))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(15, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (off_in) { + if (memcpy_s((void*)off_in, _len_off_in, __tmp_off_in, _len_off_in)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (off_out) { + if (memcpy_s((void*)off_out, _len_off_out, __tmp_off_out, _len_off_out)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fcntl_arg0_ocall(int* retval, int* error, int fd, int cmd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fcntl_arg0_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fcntl_arg0_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fcntl_arg0_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fcntl_arg0_ocall_t)); + ocalloc_size -= sizeof(ms_u_fcntl_arg0_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_cmd, sizeof(ms->ms_cmd), &cmd, sizeof(cmd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(16, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fcntl_arg1_ocall(int* retval, int* error, int fd, int cmd, int arg) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fcntl_arg1_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fcntl_arg1_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fcntl_arg1_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fcntl_arg1_ocall_t)); + ocalloc_size -= sizeof(ms_u_fcntl_arg1_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_cmd, sizeof(ms->ms_cmd), &cmd, sizeof(cmd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_arg, sizeof(ms->ms_arg), &arg, sizeof(arg))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(17, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_ioctl_arg0_ocall(int* retval, int* error, int fd, int request) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_ioctl_arg0_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_ioctl_arg0_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_ioctl_arg0_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_ioctl_arg0_ocall_t)); + ocalloc_size -= sizeof(ms_u_ioctl_arg0_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_request, sizeof(ms->ms_request), &request, sizeof(request))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(18, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_ioctl_arg1_ocall(int* retval, int* error, int fd, int request, int* arg) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_arg = sizeof(int); + + ms_u_ioctl_arg1_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_ioctl_arg1_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_arg = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(arg, _len_arg); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (arg != NULL) ? _len_arg : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_ioctl_arg1_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_ioctl_arg1_ocall_t)); + ocalloc_size -= sizeof(ms_u_ioctl_arg1_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_request, sizeof(ms->ms_request), &request, sizeof(request))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (arg != NULL) { + if (memcpy_verw_s(&ms->ms_arg, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_arg = __tmp; + if (_len_arg % sizeof(*arg) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, arg, _len_arg)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_arg); + ocalloc_size -= _len_arg; + } else { + ms->ms_arg = NULL; + } + + status = sgx_ocall(19, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (arg) { + if (memcpy_s((void*)arg, _len_arg, __tmp_arg, _len_arg)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_close_ocall(int* retval, int* error, int fd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_close_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_close_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_close_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_close_ocall_t)); + ocalloc_size -= sizeof(ms_u_close_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(20, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_isatty_ocall(int* retval, int* error, int fd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_isatty_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_isatty_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_isatty_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_isatty_ocall_t)); + ocalloc_size -= sizeof(ms_u_isatty_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(21, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_dup_ocall(int* retval, int* error, int oldfd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_dup_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_dup_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_dup_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_dup_ocall_t)); + ocalloc_size -= sizeof(ms_u_dup_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_oldfd, sizeof(ms->ms_oldfd), &oldfd, sizeof(oldfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(22, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_eventfd_ocall(int* retval, int* error, unsigned int initval, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_eventfd_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_eventfd_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_eventfd_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_eventfd_ocall_t)); + ocalloc_size -= sizeof(ms_u_eventfd_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_initval, sizeof(ms->ms_initval), &initval, sizeof(initval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(23, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_futimens_ocall(int* retval, int* error, int fd, const struct timespec* times) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_times = 2 * sizeof(struct timespec); + + ms_u_futimens_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_futimens_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(times, _len_times); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (times != NULL) ? _len_times : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_futimens_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_futimens_ocall_t)); + ocalloc_size -= sizeof(ms_u_futimens_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (times != NULL) { + if (memcpy_verw_s(&ms->ms_times, sizeof(const struct timespec*), &__tmp, sizeof(const struct timespec*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (memcpy_verw_s(__tmp, ocalloc_size, times, _len_times)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_times); + ocalloc_size -= _len_times; + } else { + ms->ms_times = NULL; + } + + status = sgx_ocall(24, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_malloc_ocall(void** retval, int* error, size_t size) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_malloc_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_malloc_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_malloc_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_malloc_ocall_t)); + ocalloc_size -= sizeof(ms_u_malloc_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_size, sizeof(ms->ms_size), &size, sizeof(size))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(25, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_free_ocall(void* p) +{ + sgx_status_t status = SGX_SUCCESS; + + ms_u_free_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_free_ocall_t); + void *__tmp = NULL; + + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_free_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_free_ocall_t)); + ocalloc_size -= sizeof(ms_u_free_ocall_t); + + if (memcpy_verw_s(&ms->ms_p, sizeof(ms->ms_p), &p, sizeof(p))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(26, ms); + + if (status == SGX_SUCCESS) { + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_mmap_ocall(void** retval, int* error, void* start, size_t length, int prot, int flags, int fd, int64_t offset) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_mmap_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_mmap_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_mmap_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_mmap_ocall_t)); + ocalloc_size -= sizeof(ms_u_mmap_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_start, sizeof(ms->ms_start), &start, sizeof(start))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_prot, sizeof(ms->ms_prot), &prot, sizeof(prot))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(27, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_munmap_ocall(int* retval, int* error, void* start, size_t length) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_munmap_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_munmap_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_munmap_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_munmap_ocall_t)); + ocalloc_size -= sizeof(ms_u_munmap_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_start, sizeof(ms->ms_start), &start, sizeof(start))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(28, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_msync_ocall(int* retval, int* error, void* addr, size_t length, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_msync_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_msync_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_msync_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_msync_ocall_t)); + ocalloc_size -= sizeof(ms_u_msync_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_addr, sizeof(ms->ms_addr), &addr, sizeof(addr))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(29, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_mprotect_ocall(int* retval, int* error, void* addr, size_t length, int prot) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_mprotect_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_mprotect_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_mprotect_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_mprotect_ocall_t)); + ocalloc_size -= sizeof(ms_u_mprotect_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_addr, sizeof(ms->ms_addr), &addr, sizeof(addr))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_prot, sizeof(ms->ms_prot), &prot, sizeof(prot))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(30, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_open_ocall(int* retval, int* error, const char* pathname, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_open_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_open_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_open_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_open_ocall_t)); + ocalloc_size -= sizeof(ms_u_open_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(31, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_open64_ocall(int* retval, int* error, const char* path, int oflag, int mode) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + + ms_u_open64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_open64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_open64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_open64_ocall_t)); + ocalloc_size -= sizeof(ms_u_open64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (memcpy_verw_s(&ms->ms_oflag, sizeof(ms->ms_oflag), &oflag, sizeof(oflag))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_mode, sizeof(ms->ms_mode), &mode, sizeof(mode))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(32, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_openat_ocall(int* retval, int* error, int dirfd, const char* pathname, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_openat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_openat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_openat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_openat_ocall_t)); + ocalloc_size -= sizeof(ms_u_openat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_dirfd, sizeof(ms->ms_dirfd), &dirfd, sizeof(dirfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(33, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fstat_ocall(int* retval, int* error, int fd, struct stat_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_buf = sizeof(struct stat_t); + + ms_u_fstat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fstat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fstat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fstat_ocall_t)); + ocalloc_size -= sizeof(ms_u_fstat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat_t*), &__tmp, sizeof(struct stat_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(34, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fstat64_ocall(int* retval, int* error, int fd, struct stat64_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_buf = sizeof(struct stat64_t); + + ms_u_fstat64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fstat64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fstat64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fstat64_ocall_t)); + ocalloc_size -= sizeof(ms_u_fstat64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat64_t*), &__tmp, sizeof(struct stat64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(35, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_stat_ocall(int* retval, int* error, const char* path, struct stat_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + size_t _len_buf = sizeof(struct stat_t); + + ms_u_stat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_stat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_stat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_stat_ocall_t)); + ocalloc_size -= sizeof(ms_u_stat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat_t*), &__tmp, sizeof(struct stat_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(36, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_stat64_ocall(int* retval, int* error, const char* path, struct stat64_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + size_t _len_buf = sizeof(struct stat64_t); + + ms_u_stat64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_stat64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_stat64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_stat64_ocall_t)); + ocalloc_size -= sizeof(ms_u_stat64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat64_t*), &__tmp, sizeof(struct stat64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(37, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_lstat_ocall(int* retval, int* error, const char* path, struct stat_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + size_t _len_buf = sizeof(struct stat_t); + + ms_u_lstat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_lstat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_lstat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_lstat_ocall_t)); + ocalloc_size -= sizeof(ms_u_lstat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat_t*), &__tmp, sizeof(struct stat_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(38, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_lstat64_ocall(int* retval, int* error, const char* path, struct stat64_t* buf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + size_t _len_buf = sizeof(struct stat64_t); + + ms_u_lstat64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_lstat64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_lstat64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_lstat64_ocall_t)); + ocalloc_size -= sizeof(ms_u_lstat64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat64_t*), &__tmp, sizeof(struct stat64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + status = sgx_ocall(39, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_lseek_ocall(uint64_t* retval, int* error, int fd, int64_t offset, int whence) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_lseek_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_lseek_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_lseek_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_lseek_ocall_t)); + ocalloc_size -= sizeof(ms_u_lseek_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_whence, sizeof(ms->ms_whence), &whence, sizeof(whence))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(40, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_lseek64_ocall(int64_t* retval, int* error, int fd, int64_t offset, int whence) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_lseek64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_lseek64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_lseek64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_lseek64_ocall_t)); + ocalloc_size -= sizeof(ms_u_lseek64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_offset, sizeof(ms->ms_offset), &offset, sizeof(offset))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_whence, sizeof(ms->ms_whence), &whence, sizeof(whence))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(41, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_ftruncate_ocall(int* retval, int* error, int fd, int64_t length) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_ftruncate_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_ftruncate_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_ftruncate_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_ftruncate_ocall_t)); + ocalloc_size -= sizeof(ms_u_ftruncate_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(42, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_ftruncate64_ocall(int* retval, int* error, int fd, int64_t length) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_ftruncate64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_ftruncate64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_ftruncate64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_ftruncate64_ocall_t)); + ocalloc_size -= sizeof(ms_u_ftruncate64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(43, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_truncate_ocall(int* retval, int* error, const char* path, int64_t length) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + + ms_u_truncate_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_truncate_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_truncate_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_truncate_ocall_t)); + ocalloc_size -= sizeof(ms_u_truncate_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(44, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_truncate64_ocall(int* retval, int* error, const char* path, int64_t length) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + + ms_u_truncate64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_truncate64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_truncate64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_truncate64_ocall_t)); + ocalloc_size -= sizeof(ms_u_truncate64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (memcpy_verw_s(&ms->ms_length, sizeof(ms->ms_length), &length, sizeof(length))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(45, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fsync_ocall(int* retval, int* error, int fd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fsync_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fsync_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fsync_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fsync_ocall_t)); + ocalloc_size -= sizeof(ms_u_fsync_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(46, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fdatasync_ocall(int* retval, int* error, int fd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fdatasync_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fdatasync_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fdatasync_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fdatasync_ocall_t)); + ocalloc_size -= sizeof(ms_u_fdatasync_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(47, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fchmod_ocall(int* retval, int* error, int fd, uint32_t mode) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fchmod_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fchmod_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fchmod_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fchmod_ocall_t)); + ocalloc_size -= sizeof(ms_u_fchmod_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_mode, sizeof(ms->ms_mode), &mode, sizeof(mode))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(48, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_unlink_ocall(int* retval, int* error, const char* pathname) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_unlink_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_unlink_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_unlink_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_unlink_ocall_t)); + ocalloc_size -= sizeof(ms_u_unlink_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + status = sgx_ocall(49, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_link_ocall(int* retval, int* error, const char* oldpath, const char* newpath) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_oldpath = oldpath ? strlen(oldpath) + 1 : 0; + size_t _len_newpath = newpath ? strlen(newpath) + 1 : 0; + + ms_u_link_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_link_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(oldpath, _len_oldpath); + CHECK_ENCLAVE_POINTER(newpath, _len_newpath); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (oldpath != NULL) ? _len_oldpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (newpath != NULL) ? _len_newpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_link_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_link_ocall_t)); + ocalloc_size -= sizeof(ms_u_link_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (oldpath != NULL) { + if (memcpy_verw_s(&ms->ms_oldpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_oldpath % sizeof(*oldpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, oldpath, _len_oldpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_oldpath); + ocalloc_size -= _len_oldpath; + } else { + ms->ms_oldpath = NULL; + } + + if (newpath != NULL) { + if (memcpy_verw_s(&ms->ms_newpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_newpath % sizeof(*newpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, newpath, _len_newpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_newpath); + ocalloc_size -= _len_newpath; + } else { + ms->ms_newpath = NULL; + } + + status = sgx_ocall(50, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_unlinkat_ocall(int* retval, int* error, int dirfd, const char* pathname, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_unlinkat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_unlinkat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_unlinkat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_unlinkat_ocall_t)); + ocalloc_size -= sizeof(ms_u_unlinkat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_dirfd, sizeof(ms->ms_dirfd), &dirfd, sizeof(dirfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(51, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_linkat_ocall(int* retval, int* error, int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_oldpath = oldpath ? strlen(oldpath) + 1 : 0; + size_t _len_newpath = newpath ? strlen(newpath) + 1 : 0; + + ms_u_linkat_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_linkat_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(oldpath, _len_oldpath); + CHECK_ENCLAVE_POINTER(newpath, _len_newpath); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (oldpath != NULL) ? _len_oldpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (newpath != NULL) ? _len_newpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_linkat_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_linkat_ocall_t)); + ocalloc_size -= sizeof(ms_u_linkat_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_olddirfd, sizeof(ms->ms_olddirfd), &olddirfd, sizeof(olddirfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (oldpath != NULL) { + if (memcpy_verw_s(&ms->ms_oldpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_oldpath % sizeof(*oldpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, oldpath, _len_oldpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_oldpath); + ocalloc_size -= _len_oldpath; + } else { + ms->ms_oldpath = NULL; + } + + if (memcpy_verw_s(&ms->ms_newdirfd, sizeof(ms->ms_newdirfd), &newdirfd, sizeof(newdirfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (newpath != NULL) { + if (memcpy_verw_s(&ms->ms_newpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_newpath % sizeof(*newpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, newpath, _len_newpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_newpath); + ocalloc_size -= _len_newpath; + } else { + ms->ms_newpath = NULL; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(52, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_rename_ocall(int* retval, int* error, const char* oldpath, const char* newpath) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_oldpath = oldpath ? strlen(oldpath) + 1 : 0; + size_t _len_newpath = newpath ? strlen(newpath) + 1 : 0; + + ms_u_rename_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_rename_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(oldpath, _len_oldpath); + CHECK_ENCLAVE_POINTER(newpath, _len_newpath); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (oldpath != NULL) ? _len_oldpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (newpath != NULL) ? _len_newpath : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_rename_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_rename_ocall_t)); + ocalloc_size -= sizeof(ms_u_rename_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (oldpath != NULL) { + if (memcpy_verw_s(&ms->ms_oldpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_oldpath % sizeof(*oldpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, oldpath, _len_oldpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_oldpath); + ocalloc_size -= _len_oldpath; + } else { + ms->ms_oldpath = NULL; + } + + if (newpath != NULL) { + if (memcpy_verw_s(&ms->ms_newpath, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_newpath % sizeof(*newpath) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, newpath, _len_newpath)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_newpath); + ocalloc_size -= _len_newpath; + } else { + ms->ms_newpath = NULL; + } + + status = sgx_ocall(53, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_chmod_ocall(int* retval, int* error, const char* path, uint32_t mode) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + + ms_u_chmod_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_chmod_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_chmod_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_chmod_ocall_t)); + ocalloc_size -= sizeof(ms_u_chmod_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (memcpy_verw_s(&ms->ms_mode, sizeof(ms->ms_mode), &mode, sizeof(mode))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(54, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_readlink_ocall(size_t* retval, int* error, const char* path, char* buf, size_t bufsz) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path = path ? strlen(path) + 1 : 0; + size_t _len_buf = bufsz; + + ms_u_readlink_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_readlink_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path, _len_path); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path != NULL) ? _len_path : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_readlink_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_readlink_ocall_t)); + ocalloc_size -= sizeof(ms_u_readlink_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path != NULL) { + if (memcpy_verw_s(&ms->ms_path, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path % sizeof(*path) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path, _len_path)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path); + ocalloc_size -= _len_path; + } else { + ms->ms_path = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(char*), &__tmp, sizeof(char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + if (_len_buf % sizeof(*buf) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + if (memcpy_verw_s(&ms->ms_bufsz, sizeof(ms->ms_bufsz), &bufsz, sizeof(bufsz))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(55, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_symlink_ocall(int* retval, int* error, const char* path1, const char* path2) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_path1 = path1 ? strlen(path1) + 1 : 0; + size_t _len_path2 = path2 ? strlen(path2) + 1 : 0; + + ms_u_symlink_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_symlink_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(path1, _len_path1); + CHECK_ENCLAVE_POINTER(path2, _len_path2); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path1 != NULL) ? _len_path1 : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (path2 != NULL) ? _len_path2 : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_symlink_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_symlink_ocall_t)); + ocalloc_size -= sizeof(ms_u_symlink_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (path1 != NULL) { + if (memcpy_verw_s(&ms->ms_path1, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path1 % sizeof(*path1) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path1, _len_path1)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path1); + ocalloc_size -= _len_path1; + } else { + ms->ms_path1 = NULL; + } + + if (path2 != NULL) { + if (memcpy_verw_s(&ms->ms_path2, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_path2 % sizeof(*path2) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, path2, _len_path2)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_path2); + ocalloc_size -= _len_path2; + } else { + ms->ms_path2 = NULL; + } + + status = sgx_ocall(56, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_realpath_ocall(char** retval, int* error, const char* pathname) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_realpath_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_realpath_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_realpath_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_realpath_ocall_t)); + ocalloc_size -= sizeof(ms_u_realpath_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + status = sgx_ocall(57, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_mkdir_ocall(int* retval, int* error, const char* pathname, uint32_t mode) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_mkdir_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_mkdir_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_mkdir_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_mkdir_ocall_t)); + ocalloc_size -= sizeof(ms_u_mkdir_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + if (memcpy_verw_s(&ms->ms_mode, sizeof(ms->ms_mode), &mode, sizeof(mode))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(58, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_rmdir_ocall(int* retval, int* error, const char* pathname) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_rmdir_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_rmdir_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_rmdir_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_rmdir_ocall_t)); + ocalloc_size -= sizeof(ms_u_rmdir_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + status = sgx_ocall(59, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fdopendir_ocall(void** retval, int* error, int fd) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_fdopendir_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fdopendir_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fdopendir_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fdopendir_ocall_t)); + ocalloc_size -= sizeof(ms_u_fdopendir_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_fd, sizeof(ms->ms_fd), &fd, sizeof(fd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(60, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_opendir_ocall(void** retval, int* error, const char* pathname) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + + ms_u_opendir_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_opendir_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_opendir_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_opendir_ocall_t)); + ocalloc_size -= sizeof(ms_u_opendir_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + status = sgx_ocall(61, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_readdir64_r_ocall(int* retval, void* dirp, struct dirent64_t* entry, struct dirent64_t** result) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_entry = sizeof(struct dirent64_t); + size_t _len_result = sizeof(struct dirent64_t*); + + ms_u_readdir64_r_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_readdir64_r_ocall_t); + void *__tmp = NULL; + + void *__tmp_entry = NULL; + void *__tmp_result = NULL; + + CHECK_ENCLAVE_POINTER(entry, _len_entry); + CHECK_ENCLAVE_POINTER(result, _len_result); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (entry != NULL) ? _len_entry : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (result != NULL) ? _len_result : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_readdir64_r_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_readdir64_r_ocall_t)); + ocalloc_size -= sizeof(ms_u_readdir64_r_ocall_t); + + if (memcpy_verw_s(&ms->ms_dirp, sizeof(ms->ms_dirp), &dirp, sizeof(dirp))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (entry != NULL) { + if (memcpy_verw_s(&ms->ms_entry, sizeof(struct dirent64_t*), &__tmp, sizeof(struct dirent64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_entry = __tmp; + if (memcpy_verw_s(__tmp, ocalloc_size, entry, _len_entry)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_entry); + ocalloc_size -= _len_entry; + } else { + ms->ms_entry = NULL; + } + + if (result != NULL) { + if (memcpy_verw_s(&ms->ms_result, sizeof(struct dirent64_t**), &__tmp, sizeof(struct dirent64_t**))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_result = __tmp; + if (_len_result % sizeof(*result) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_result, 0, _len_result); + __tmp = (void *)((size_t)__tmp + _len_result); + ocalloc_size -= _len_result; + } else { + ms->ms_result = NULL; + } + + status = sgx_ocall(62, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (entry) { + if (memcpy_s((void*)entry, _len_entry, __tmp_entry, _len_entry)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (result) { + if (memcpy_s((void*)result, _len_result, __tmp_result, _len_result)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_closedir_ocall(int* retval, int* error, void* dirp) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_closedir_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_closedir_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_closedir_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_closedir_ocall_t)); + ocalloc_size -= sizeof(ms_u_closedir_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_dirp, sizeof(ms->ms_dirp), &dirp, sizeof(dirp))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(63, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_dirfd_ocall(int* retval, int* error, void* dirp) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + + ms_u_dirfd_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_dirfd_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_dirfd_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_dirfd_ocall_t)); + ocalloc_size -= sizeof(ms_u_dirfd_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_dirp, sizeof(ms->ms_dirp), &dirp, sizeof(dirp))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(64, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL u_fstatat64_ocall(int* retval, int* error, int dirfd, const char* pathname, struct stat64_t* buf, int flags) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_error = sizeof(int); + size_t _len_pathname = pathname ? strlen(pathname) + 1 : 0; + size_t _len_buf = sizeof(struct stat64_t); + + ms_u_fstatat64_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_u_fstatat64_ocall_t); + void *__tmp = NULL; + + void *__tmp_error = NULL; + void *__tmp_buf = NULL; + + CHECK_ENCLAVE_POINTER(error, _len_error); + CHECK_ENCLAVE_POINTER(pathname, _len_pathname); + CHECK_ENCLAVE_POINTER(buf, _len_buf); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (error != NULL) ? _len_error : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (pathname != NULL) ? _len_pathname : 0)) + return SGX_ERROR_INVALID_PARAMETER; + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (buf != NULL) ? _len_buf : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_u_fstatat64_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_u_fstatat64_ocall_t)); + ocalloc_size -= sizeof(ms_u_fstatat64_ocall_t); + + if (error != NULL) { + if (memcpy_verw_s(&ms->ms_error, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_error = __tmp; + if (_len_error % sizeof(*error) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_error, 0, _len_error); + __tmp = (void *)((size_t)__tmp + _len_error); + ocalloc_size -= _len_error; + } else { + ms->ms_error = NULL; + } + + if (memcpy_verw_s(&ms->ms_dirfd, sizeof(ms->ms_dirfd), &dirfd, sizeof(dirfd))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (pathname != NULL) { + if (memcpy_verw_s(&ms->ms_pathname, sizeof(const char*), &__tmp, sizeof(const char*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_pathname % sizeof(*pathname) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, pathname, _len_pathname)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_pathname); + ocalloc_size -= _len_pathname; + } else { + ms->ms_pathname = NULL; + } + + if (buf != NULL) { + if (memcpy_verw_s(&ms->ms_buf, sizeof(struct stat64_t*), &__tmp, sizeof(struct stat64_t*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_buf = __tmp; + memset_verw(__tmp_buf, 0, _len_buf); + __tmp = (void *)((size_t)__tmp + _len_buf); + ocalloc_size -= _len_buf; + } else { + ms->ms_buf = NULL; + } + + if (memcpy_verw_s(&ms->ms_flags, sizeof(ms->ms_flags), &flags, sizeof(flags))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(65, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (error) { + if (memcpy_s((void*)error, _len_error, __tmp_error, _len_error)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + if (buf) { + if (memcpy_s((void*)buf, _len_buf, __tmp_buf, _len_buf)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL sgx_oc_cpuidex(int cpuinfo[4], int leaf, int subleaf) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_cpuinfo = 4 * sizeof(int); + + ms_sgx_oc_cpuidex_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_sgx_oc_cpuidex_t); + void *__tmp = NULL; + + void *__tmp_cpuinfo = NULL; + + CHECK_ENCLAVE_POINTER(cpuinfo, _len_cpuinfo); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (cpuinfo != NULL) ? _len_cpuinfo : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_sgx_oc_cpuidex_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_sgx_oc_cpuidex_t)); + ocalloc_size -= sizeof(ms_sgx_oc_cpuidex_t); + + if (cpuinfo != NULL) { + if (memcpy_verw_s(&ms->ms_cpuinfo, sizeof(int*), &__tmp, sizeof(int*))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp_cpuinfo = __tmp; + if (_len_cpuinfo % sizeof(*cpuinfo) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + memset_verw(__tmp_cpuinfo, 0, _len_cpuinfo); + __tmp = (void *)((size_t)__tmp + _len_cpuinfo); + ocalloc_size -= _len_cpuinfo; + } else { + ms->ms_cpuinfo = NULL; + } + + if (memcpy_verw_s(&ms->ms_leaf, sizeof(ms->ms_leaf), &leaf, sizeof(leaf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_subleaf, sizeof(ms->ms_subleaf), &subleaf, sizeof(subleaf))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(66, ms); + + if (status == SGX_SUCCESS) { + if (cpuinfo) { + if (memcpy_s((void*)cpuinfo, _len_cpuinfo, __tmp_cpuinfo, _len_cpuinfo)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL sgx_thread_wait_untrusted_event_ocall(int* retval, const void* self) +{ + sgx_status_t status = SGX_SUCCESS; + + ms_sgx_thread_wait_untrusted_event_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_sgx_thread_wait_untrusted_event_ocall_t); + void *__tmp = NULL; + + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_sgx_thread_wait_untrusted_event_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_sgx_thread_wait_untrusted_event_ocall_t)); + ocalloc_size -= sizeof(ms_sgx_thread_wait_untrusted_event_ocall_t); + + if (memcpy_verw_s(&ms->ms_self, sizeof(ms->ms_self), &self, sizeof(self))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(67, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL sgx_thread_set_untrusted_event_ocall(int* retval, const void* waiter) +{ + sgx_status_t status = SGX_SUCCESS; + + ms_sgx_thread_set_untrusted_event_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_sgx_thread_set_untrusted_event_ocall_t); + void *__tmp = NULL; + + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_sgx_thread_set_untrusted_event_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_sgx_thread_set_untrusted_event_ocall_t)); + ocalloc_size -= sizeof(ms_sgx_thread_set_untrusted_event_ocall_t); + + if (memcpy_verw_s(&ms->ms_waiter, sizeof(ms->ms_waiter), &waiter, sizeof(waiter))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(68, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL sgx_thread_setwait_untrusted_events_ocall(int* retval, const void* waiter, const void* self) +{ + sgx_status_t status = SGX_SUCCESS; + + ms_sgx_thread_setwait_untrusted_events_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_sgx_thread_setwait_untrusted_events_ocall_t); + void *__tmp = NULL; + + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_sgx_thread_setwait_untrusted_events_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_sgx_thread_setwait_untrusted_events_ocall_t)); + ocalloc_size -= sizeof(ms_sgx_thread_setwait_untrusted_events_ocall_t); + + if (memcpy_verw_s(&ms->ms_waiter, sizeof(ms->ms_waiter), &waiter, sizeof(waiter))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + if (memcpy_verw_s(&ms->ms_self, sizeof(ms->ms_self), &self, sizeof(self))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(69, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + +sgx_status_t SGX_CDECL sgx_thread_set_multiple_untrusted_events_ocall(int* retval, const void** waiters, size_t total) +{ + sgx_status_t status = SGX_SUCCESS; + size_t _len_waiters = total * sizeof(void*); + + ms_sgx_thread_set_multiple_untrusted_events_ocall_t* ms = NULL; + size_t ocalloc_size = sizeof(ms_sgx_thread_set_multiple_untrusted_events_ocall_t); + void *__tmp = NULL; + + + CHECK_ENCLAVE_POINTER(waiters, _len_waiters); + + if (ADD_ASSIGN_OVERFLOW(ocalloc_size, (waiters != NULL) ? _len_waiters : 0)) + return SGX_ERROR_INVALID_PARAMETER; + + __tmp = sgx_ocalloc(ocalloc_size); + if (__tmp == NULL) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + ms = (ms_sgx_thread_set_multiple_untrusted_events_ocall_t*)__tmp; + __tmp = (void *)((size_t)__tmp + sizeof(ms_sgx_thread_set_multiple_untrusted_events_ocall_t)); + ocalloc_size -= sizeof(ms_sgx_thread_set_multiple_untrusted_events_ocall_t); + + if (waiters != NULL) { + if (memcpy_verw_s(&ms->ms_waiters, sizeof(const void**), &__tmp, sizeof(const void**))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + if (_len_waiters % sizeof(*waiters) != 0) { + sgx_ocfree(); + return SGX_ERROR_INVALID_PARAMETER; + } + if (memcpy_verw_s(__tmp, ocalloc_size, waiters, _len_waiters)) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + __tmp = (void *)((size_t)__tmp + _len_waiters); + ocalloc_size -= _len_waiters; + } else { + ms->ms_waiters = NULL; + } + + if (memcpy_verw_s(&ms->ms_total, sizeof(ms->ms_total), &total, sizeof(total))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + + status = sgx_ocall(70, ms); + + if (status == SGX_SUCCESS) { + if (retval) { + if (memcpy_s((void*)retval, sizeof(*retval), &ms->ms_retval, sizeof(ms->ms_retval))) { + sgx_ocfree(); + return SGX_ERROR_UNEXPECTED; + } + } + } + sgx_ocfree(); + return status; +} + diff --git a/user-auth/enclave/Enclave_t.h b/user-auth/enclave/Enclave_t.h new file mode 100644 index 0000000..dd57d1a --- /dev/null +++ b/user-auth/enclave/Enclave_t.h @@ -0,0 +1,105 @@ +#ifndef ENCLAVE_T_H__ +#define ENCLAVE_T_H__ + +#include +#include +#include +#include "sgx_edger8r.h" /* for sgx_ocall etc. */ + +#include "time.h" +#include "inc/stat.h" +#include "sys/uio.h" +#include "time.h" +#include "inc/stat.h" +#include "inc/dirent.h" + +#include /* for size_t */ + +#define SGX_CAST(type, item) ((type)(item)) + +#ifdef __cplusplus +extern "C" { +#endif + +sgx_status_t hash_password(const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, uint8_t* hash, size_t* hash_length); +sgx_status_t verify_password(const uint8_t* password, size_t password_length, const uint8_t* user_id, size_t user_id_length, const uint8_t* stored_hash, size_t hash_length, uint8_t* password_verified); +void t_global_init_ecall(uint64_t id, const uint8_t* path, size_t len); +void t_global_exit_ecall(void); + +sgx_status_t SGX_CDECL u_thread_set_event_ocall(int* retval, int* error, const void* tcs); +sgx_status_t SGX_CDECL u_thread_wait_event_ocall(int* retval, int* error, const void* tcs, const struct timespec* timeout); +sgx_status_t SGX_CDECL u_thread_set_multiple_events_ocall(int* retval, int* error, const void** tcss, int total); +sgx_status_t SGX_CDECL u_thread_setwait_events_ocall(int* retval, int* error, const void* waiter_tcs, const void* self_tcs, const struct timespec* timeout); +sgx_status_t SGX_CDECL u_clock_gettime_ocall(int* retval, int* error, int clk_id, struct timespec* tp); +sgx_status_t SGX_CDECL u_read_ocall(size_t* retval, int* error, int fd, void* buf, size_t count); +sgx_status_t SGX_CDECL u_pread64_ocall(size_t* retval, int* error, int fd, void* buf, size_t count, int64_t offset); +sgx_status_t SGX_CDECL u_readv_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt); +sgx_status_t SGX_CDECL u_preadv64_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset); +sgx_status_t SGX_CDECL u_write_ocall(size_t* retval, int* error, int fd, const void* buf, size_t count); +sgx_status_t SGX_CDECL u_pwrite64_ocall(size_t* retval, int* error, int fd, const void* buf, size_t count, int64_t offset); +sgx_status_t SGX_CDECL u_writev_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt); +sgx_status_t SGX_CDECL u_pwritev64_ocall(size_t* retval, int* error, int fd, const struct iovec* iov, int iovcnt, int64_t offset); +sgx_status_t SGX_CDECL u_sendfile_ocall(size_t* retval, int* error, int out_fd, int in_fd, int64_t* offset, size_t count); +sgx_status_t SGX_CDECL u_copy_file_range_ocall(size_t* retval, int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags); +sgx_status_t SGX_CDECL u_splice_ocall(size_t* retval, int* error, int fd_in, int64_t* off_in, int fd_out, int64_t* off_out, size_t len, unsigned int flags); +sgx_status_t SGX_CDECL u_fcntl_arg0_ocall(int* retval, int* error, int fd, int cmd); +sgx_status_t SGX_CDECL u_fcntl_arg1_ocall(int* retval, int* error, int fd, int cmd, int arg); +sgx_status_t SGX_CDECL u_ioctl_arg0_ocall(int* retval, int* error, int fd, int request); +sgx_status_t SGX_CDECL u_ioctl_arg1_ocall(int* retval, int* error, int fd, int request, int* arg); +sgx_status_t SGX_CDECL u_close_ocall(int* retval, int* error, int fd); +sgx_status_t SGX_CDECL u_isatty_ocall(int* retval, int* error, int fd); +sgx_status_t SGX_CDECL u_dup_ocall(int* retval, int* error, int oldfd); +sgx_status_t SGX_CDECL u_eventfd_ocall(int* retval, int* error, unsigned int initval, int flags); +sgx_status_t SGX_CDECL u_futimens_ocall(int* retval, int* error, int fd, const struct timespec* times); +sgx_status_t SGX_CDECL u_malloc_ocall(void** retval, int* error, size_t size); +sgx_status_t SGX_CDECL u_free_ocall(void* p); +sgx_status_t SGX_CDECL u_mmap_ocall(void** retval, int* error, void* start, size_t length, int prot, int flags, int fd, int64_t offset); +sgx_status_t SGX_CDECL u_munmap_ocall(int* retval, int* error, void* start, size_t length); +sgx_status_t SGX_CDECL u_msync_ocall(int* retval, int* error, void* addr, size_t length, int flags); +sgx_status_t SGX_CDECL u_mprotect_ocall(int* retval, int* error, void* addr, size_t length, int prot); +sgx_status_t SGX_CDECL u_open_ocall(int* retval, int* error, const char* pathname, int flags); +sgx_status_t SGX_CDECL u_open64_ocall(int* retval, int* error, const char* path, int oflag, int mode); +sgx_status_t SGX_CDECL u_openat_ocall(int* retval, int* error, int dirfd, const char* pathname, int flags); +sgx_status_t SGX_CDECL u_fstat_ocall(int* retval, int* error, int fd, struct stat_t* buf); +sgx_status_t SGX_CDECL u_fstat64_ocall(int* retval, int* error, int fd, struct stat64_t* buf); +sgx_status_t SGX_CDECL u_stat_ocall(int* retval, int* error, const char* path, struct stat_t* buf); +sgx_status_t SGX_CDECL u_stat64_ocall(int* retval, int* error, const char* path, struct stat64_t* buf); +sgx_status_t SGX_CDECL u_lstat_ocall(int* retval, int* error, const char* path, struct stat_t* buf); +sgx_status_t SGX_CDECL u_lstat64_ocall(int* retval, int* error, const char* path, struct stat64_t* buf); +sgx_status_t SGX_CDECL u_lseek_ocall(uint64_t* retval, int* error, int fd, int64_t offset, int whence); +sgx_status_t SGX_CDECL u_lseek64_ocall(int64_t* retval, int* error, int fd, int64_t offset, int whence); +sgx_status_t SGX_CDECL u_ftruncate_ocall(int* retval, int* error, int fd, int64_t length); +sgx_status_t SGX_CDECL u_ftruncate64_ocall(int* retval, int* error, int fd, int64_t length); +sgx_status_t SGX_CDECL u_truncate_ocall(int* retval, int* error, const char* path, int64_t length); +sgx_status_t SGX_CDECL u_truncate64_ocall(int* retval, int* error, const char* path, int64_t length); +sgx_status_t SGX_CDECL u_fsync_ocall(int* retval, int* error, int fd); +sgx_status_t SGX_CDECL u_fdatasync_ocall(int* retval, int* error, int fd); +sgx_status_t SGX_CDECL u_fchmod_ocall(int* retval, int* error, int fd, uint32_t mode); +sgx_status_t SGX_CDECL u_unlink_ocall(int* retval, int* error, const char* pathname); +sgx_status_t SGX_CDECL u_link_ocall(int* retval, int* error, const char* oldpath, const char* newpath); +sgx_status_t SGX_CDECL u_unlinkat_ocall(int* retval, int* error, int dirfd, const char* pathname, int flags); +sgx_status_t SGX_CDECL u_linkat_ocall(int* retval, int* error, int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags); +sgx_status_t SGX_CDECL u_rename_ocall(int* retval, int* error, const char* oldpath, const char* newpath); +sgx_status_t SGX_CDECL u_chmod_ocall(int* retval, int* error, const char* path, uint32_t mode); +sgx_status_t SGX_CDECL u_readlink_ocall(size_t* retval, int* error, const char* path, char* buf, size_t bufsz); +sgx_status_t SGX_CDECL u_symlink_ocall(int* retval, int* error, const char* path1, const char* path2); +sgx_status_t SGX_CDECL u_realpath_ocall(char** retval, int* error, const char* pathname); +sgx_status_t SGX_CDECL u_mkdir_ocall(int* retval, int* error, const char* pathname, uint32_t mode); +sgx_status_t SGX_CDECL u_rmdir_ocall(int* retval, int* error, const char* pathname); +sgx_status_t SGX_CDECL u_fdopendir_ocall(void** retval, int* error, int fd); +sgx_status_t SGX_CDECL u_opendir_ocall(void** retval, int* error, const char* pathname); +sgx_status_t SGX_CDECL u_readdir64_r_ocall(int* retval, void* dirp, struct dirent64_t* entry, struct dirent64_t** result); +sgx_status_t SGX_CDECL u_closedir_ocall(int* retval, int* error, void* dirp); +sgx_status_t SGX_CDECL u_dirfd_ocall(int* retval, int* error, void* dirp); +sgx_status_t SGX_CDECL u_fstatat64_ocall(int* retval, int* error, int dirfd, const char* pathname, struct stat64_t* buf, int flags); +sgx_status_t SGX_CDECL sgx_oc_cpuidex(int cpuinfo[4], int leaf, int subleaf); +sgx_status_t SGX_CDECL sgx_thread_wait_untrusted_event_ocall(int* retval, const void* self); +sgx_status_t SGX_CDECL sgx_thread_set_untrusted_event_ocall(int* retval, const void* waiter); +sgx_status_t SGX_CDECL sgx_thread_setwait_untrusted_events_ocall(int* retval, const void* waiter, const void* self); +sgx_status_t SGX_CDECL sgx_thread_set_multiple_untrusted_events_ocall(int* retval, const void** waiters, size_t total); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/user-auth/enclave/Makefile b/user-auth/enclave/Makefile new file mode 100644 index 0000000..923786f --- /dev/null +++ b/user-auth/enclave/Makefile @@ -0,0 +1,146 @@ +# Makefile settings + +APP_T_SIGNED = enclave.signed.so +LIB = ../build/lib/ +BIN = ../build/bin/ +SRC_RST = ./src/ +APP_T = enclave.so +NAME_T = libenclave.a +SRC_U = ../app/ +SRC_T = ./ +OBJ_T = ../build/obj/ +SGX_SDK ?= /opt/sgxsdk +FLAGS = -Wall -Wextra +GCC_STEP1_T = -fstack-protector -I$(CUSTOM_COMMON_PATH)/inc -I$(CUSTOM_EDL_PATH) -I$(SGX_SDK)/include \ + -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -I$(SGX_SDK)/include/epid -I $(SRC_T) \ + -L$(LIB) $(ENCLAVE_CFLAGS) $(SGX_COMMON_CFLAGS) +GCC_STEP2_T = -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \ + -Wl,--whole-archive -l$(TRTS_LIB) -Wl,--no-whole-archive \ + -Wl,--start-group -lsgx_tstdc -l$(SERVICE_LIB) -lsgx_tcrypto -L$(LIB) -lenclave -Wl,--end-group \ + -Wl,--version-script=$(SRC_T)Enclave.lds $(ENCLAVE_LDFLAGS) +FILES_T = Enclave_t.c +FILES_T_H = Enclave_t.h +EDL_FILE = Enclave.edl +TOML = Cargo.toml +ENCLAVE_CONFIG = Enclave.config.xml +SGX_ARCH = x64 +TRTS_LIB = sgx_trts +SERVICE_LIB = sgx_tservice +ENCLAVE_CARGO_LIB=libuserauth.a # This is the name of the enclave static library compiled by Cargo/Xargo. You will have to change it, depending of your project. +# Addprefix dependant variables, no need to change those +OUTPUT_T = $(FILES_T:.c=.o) +NAME = $(addprefix $(BIN), $(APP_T_SIGNED)) +BIN_T = $(addprefix $(BIN), $(APP_T)) +NAME_T_D = $(addprefix $(LIB), $(NAME_T)) +OUTPUT_W_FU=$(addprefix $(OBJ_U), $(OUTPUT_U)) +FILES_T_F=$(addprefix $(SRC_T), $(FILES_T)) +FILES_T_F_RUST=$(addprefix $(SRC_T), $(FILES_T_RUST)) +OUTPUT_W_FT=$(addprefix $(OBJ_T), $(OUTPUT_T)) +FILES_RUST_F= $(wildcard $(SRC_RST)*.rs) # Wildcard function used, no need to specify the rust files. Safe as we don't compile the rust files with the makefile. + +# Contains compilation rules for the enclave part + +include ../buildenv.mk + +# Custom header files and EDL paths needs to be specified with make (CUSTOM_EDL_PATH) (CUSTOM_COMMON_PATH) Same goes for Xargo location (XARGO_PATH) + +# Directly imported from the original Intel SGX samples, helpful to detect the system architecture + +ifeq ($(shell getconf LONG_BIT), 32) + SGX_ARCH := x86 +else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32) + SGX_ARCH := x86 +endif + +ifeq ($(SGX_ARCH), x86) + SGX_COMMON_CFLAGS := -m32 + SGX_LIBRARY_PATH := $(SGX_SDK)/lib + SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign + SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r +else + SGX_COMMON_CFLAGS := -m64 + SGX_LIBRARY_PATH := $(SGX_SDK)/lib64 + SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign + SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r +endif + +ifeq ($(MITIGATION-CVE-2020-0551), LOAD) +export MITIGATION_CVE_2020_0551=LOAD +else ifeq ($(MITIGATION-CVE-2020-0551), CF) +export MITIGATION_CVE_2020_0551=CF +endif + +# If specified, software / simulation mode. Otherwise, hardware mode no matter what. + +ifeq ($(SGX_MODE), SW) + TRTS_LIB := sgx_trts_sim + SERVICE_LIB := sgx_tservice_sim +endif + +# If debug mode, we can set up extra options such as the debug flags + +ifeq ($(SGX_DEBUG), 1) + SGX_COMMON_CFLAGS += -O0 -g +else + SGX_COMMON_CFLAGS += -O2 +endif + +# Compilation process, we set up all the dependencies needed to have the correct order of build, and avoid relink + +all: $(NAME) + +# We print the compilation mode we're in (hardware/software mode), just as a reminder. + +$(NAME): $(BIN_T) +ifeq ($(SGX_MODE), SW) + @echo "\033[32mSoftware / Simulation mode\033[0m" +else + @echo "\033[32mHardware mode\033[0m" +endif + @echo "\033[32mGenerating keys and signing the enclave...\033[0m" + @mkdir -p $(BIN) + @openssl genrsa -out Enclave_private.pem -3 3072 + @openssl rsa -in Enclave_private.pem -pubout -out Enclave_public.pem + @$(SGX_ENCLAVE_SIGNER) sign -key $(SRC_T)Enclave_private.pem -enclave $(BIN_T) -out $@ -config $(SRC_T)Enclave.config.xml + +$(BIN_T): $(NAME_T_D) + @echo "\033[32mBuilding the enclave...\033[0m" + mkdir -p $(BIN) + @$(CXX) $(OUTPUT_W_FT) -o $@ $(GCC_STEP2_T) + +$(NAME_T_D): $(FILES_T_F) $(OUTPUT_W_FT) $(FILES_RUST_F) $(EDL_FILE) $(ENCLAVE_CONFIG) $(TOML) # We added as a reference the rust files, along with the EDL, the XML config file and the cargo.toml file, so Make can detect if any change was made + @echo "\033[32mBuilding enclave static library with Cargo...\033[0m" + @cargo build --release + mkdir -p $(LIB) + @cp ./target/release/$(ENCLAVE_CARGO_LIB) $(LIB)libenclave.a + +$(FILES_T_F): $(SGX_EDGER8R) $(SRC_T)/Enclave.edl + @echo "\033[32mGenerating trusted SGX C edl files...\033[0m" + @$(SGX_EDGER8R) --trusted $(SRC_T)/Enclave.edl --search-path $(SGX_SDK)/include --search-path $(CUSTOM_EDL_PATH) --trusted-dir $(SRC_T) + +$(OBJ_T)%.o:$(SRC_T)%.c + @mkdir -p $(OBJ_T) + @echo "\033[32m$?: Build in progress...\033[0m" + @$(CC) $(FLAGS) $(GCC_STEP1_T) -o $@ -c $? + +clean: c_clean + @rm -rf $(OBJ_T) + @echo "\033[32mObject files deleted\033[0m" + +fclean: clean fclean_enclave + +fclean_enclave: + @echo "\033[32mBinary file $(NAME) deleted\033[0m" + @rm -f $(NAME) + @rm -f $(BIN_T) + @rm -f $(LIB)libenclave.a + @cargo clean && rm -f Cargo.lock + +c_clean: + @echo "\033[32mC edl generated files deleted\033[0m" + @rm -rf $(FILES_T_F) + @rm -f $(FILES_T_H) + +re: fclean all + +.PHONY: all clean c_clean fclean re fclean_enclave diff --git a/user-auth/enclave/src/lib.rs b/user-auth/enclave/src/lib.rs new file mode 100644 index 0000000..342edcc --- /dev/null +++ b/user-auth/enclave/src/lib.rs @@ -0,0 +1,129 @@ +#![no_std] +#![deny(unsafe_op_in_unsafe_fn)] + +use std::slice; +use std::vec::Vec; + +use argon2::{Algorithm, Argon2, Params, PasswordVerifier, Version}; +use once_cell::unsync::OnceCell; +use password_hash::PasswordHashString; +use sgx_rand::{Rng, SgxRng}; +use sgx_tkeys::{SgxKeyPolicy, SgxSecret, SgxSecretBuilder}; +use sgx_tstd as std; +use sgx_types::*; + +const SALT_LENGTH: usize = 16; + +fn new_pepper(user_id: &[u8]) -> SgxSecret { + SgxSecretBuilder::new() + .key_id(&user_id) + .policy(SgxKeyPolicy::MRSIGNER) + .build() +} +fn new_hash_context<'a>(pepper: &'a [u8]) -> Result, argon2::Error> { + Argon2::<'a>::new_with_secret( + pepper.as_ref(), + Algorithm::default(), + Version::default(), + Params::default(), + ) +} + +///# Safety: guaranteed by the trusted bridge routine generated with the +/// Intel SDK's `sgx_edger8r` tool +#[no_mangle] +pub unsafe extern "C" fn hash_password( + // input paramaters + password: *const u8, + password_length: usize, + user_id: *const u8, + user_id_length: usize, + // output paramaters + hash: *mut u8, + hash_length_used: *mut usize, +) -> sgx_status_t { + let password_buf = OnceCell::<&[u8]>::new(); + let user_id_buf = OnceCell::<&[u8]>::new(); + let mut salt_buf = [0u8; SALT_LENGTH]; + unsafe { + password_buf + .set(slice::from_raw_parts(password, password_length)) + .unwrap(); + user_id_buf + .set(slice::from_raw_parts(user_id, user_id_length)) + .unwrap(); + } + + let pepper = new_pepper(user_id_buf.get().unwrap()); + let hash_ctx = new_hash_context(pepper.as_ref()); + + match (hash_ctx, SgxRng::new()) { + (Err(_), _) => sgx_status_t::SGX_ERROR_UNEXPECTED, + (_, Err(_)) => sgx_status_t::SGX_ERROR_UNEXPECTED, + (Ok(ctx), Ok(mut rng)) => { + rng.fill_bytes(&mut salt_buf); + let out_length = ctx.params().output_len().unwrap(); + let mut hash_buf = Vec::with_capacity(out_length); + ctx.hash_password_into(password_buf.get().unwrap(), &salt_buf, &mut hash_buf) + .map_or_else( + |_| sgx_status_t::SGX_ERROR_UNEXPECTED, + |_| { + unsafe { + hash.copy_from_nonoverlapping(hash_buf.as_ptr(), out_length); + *hash_length_used = out_length; + } + sgx_status_t::SGX_SUCCESS + }, + ) + } + } +} + +///# Safety: guaranteed by the trusted bridge routine generated with the +/// Intel SDK's `sgx_edger8r` tool +#[no_mangle] +pub unsafe extern "C" fn verify_password( + // input paramaters + password: *const u8, + password_length: usize, + user_id: *const u8, + user_id_length: usize, + stored_hash: *const u8, + hash_length: usize, + // output paramaters + password_verified: *mut u8, +) -> sgx_status_t { + let password_buf = OnceCell::<&[u8]>::new(); + let user_id_buf = OnceCell::<&[u8]>::new(); + let hash_buf = OnceCell::<&[u8]>::new(); + unsafe { + password_buf + .set(slice::from_raw_parts(password, password_length)) + .unwrap(); + user_id_buf + .set(slice::from_raw_parts(user_id, user_id_length)) + .unwrap(); + user_id_buf + .set(slice::from_raw_parts(user_id, user_id_length)) + .unwrap(); + } + + let hash = std::str::from_utf8(hash_buf.get().unwrap()).expect("not a valid UTF-8 string"); + let pepper = new_pepper(user_id_buf.get().unwrap()); + new_hash_context(pepper.as_ref()).map_or_else( + |_| sgx_status_t::SGX_ERROR_UNEXPECTED, + |ctx| match ctx.verify_password( + password_buf.get().unwrap(), + &PasswordHashString::parse(hash, password_hash::Encoding::default()) + .expect("invalid PHC string") + .password_hash(), + ) { + Ok(()) => { + unsafe { *password_verified = 1u8 }; + sgx_status_t::SGX_SUCCESS + } + Err(password_hash::Error::Password) => sgx_status_t::SGX_SUCCESS, + Err(_) => sgx_status_t::SGX_ERROR_UNEXPECTED, + }, + ) +}