Asylo
enclave_manager.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 Asylo authors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef ASYLO_PLATFORM_CORE_ENCLAVE_MANAGER_H_
20 #define ASYLO_PLATFORM_CORE_ENCLAVE_MANAGER_H_
21 
22 // Declares the enclave client API, providing types and methods for loading,
23 // accessing, and finalizing enclaves.
24 
25 #include <string>
26 #include <utility>
27 
28 #include "absl/container/flat_hash_map.h"
29 #include "absl/memory/memory.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/synchronization/mutex.h"
32 #include "absl/time/time.h"
33 #include "absl/types/variant.h"
34 #include "asylo/enclave.pb.h" // IWYU pragma: export
35 #include "asylo/platform/arch/fork.pb.h"
36 #include "asylo/platform/core/enclave_client.h"
37 #include "asylo/platform/core/enclave_config_util.h"
38 #include "asylo/platform/core/shared_resource_manager.h"
39 #include "asylo/util/status.h" // IWYU pragma: export
40 #include "asylo/util/statusor.h"
41 
42 namespace asylo {
43 class EnclaveLoader;
44 
45 /// Enclave Manager configuration.
47  public:
48  /// Configuration server connection attributes.
49  ///
50  /// A part of an enclave's configuration is expected to be the same across all
51  /// enclaves running under a single instance of an OS. An Enclave manager can
52  /// obtain such configuration from the Asylo daemon running on the system.
53  /// Alternately, the creator of the enclave manager can directly provide such
54  /// configuration to the enclave manager. To this end, an
55  /// EnclaveManagerOptions instance either holds the information necessary for
56  /// connecting to the config server, or holds a HostConfig proto. If the
57  /// enclave manager is configured with an options object containing the
58  /// server-connection information, the enclave manager obtains the necessary
59  /// information by contacting the Asylo daemon. Else, the enclave manager
60  /// directly uses the HostConfig info stored within the options structure.
61  ///
62  /// The ConfigServerConnectionAttributes struct holds information necessary
63  /// for contacting the config server running inside the Asylo daemon.
65  ConfigServerConnectionAttributes(std::string address,
66  absl::Duration timeout)
69 
70  std::string server_address;
72  };
73 
74  /// Constructs a default EnclaveManagerOptions object.
76 
77  /// Configures a connection to the config server.
78  ///
79  /// Sets the information necessary for contacting the config server within the
80  /// Asylo daemon.
81  ///
82  /// \return A reference to this EnclaveManagerOptions object.
84  std::string address, absl::Duration timeout);
85 
86  /// Sets the HostConfig proto within this object.
87  ///
88  /// \return A reference to this EnclaveManagerOptions object.
89  EnclaveManagerOptions &set_host_config(HostConfig config);
90 
91  /// Returns the address of the configuration server.
92  ///
93  /// \return The address of the server from which the HostConfig information
94  /// can be obtained. Returns an error if
95  /// ConfigServerConnectionAttributes are not set.
97 
98  /// Returns the configuration server connection timeout.
99  ///
100  /// \return The connection timeout for the server from which the HostConfig
101  /// information can be obtained, or an error if
102  /// ConfigServerConnectionAttributes are not set.
104 
105  /// Returns the embedded HostConfig object.
106  ///
107  /// \return The HostConfig information embedded within this object, or an
108  /// error if such information is not embedded within the object.
110 
111  /// Returns true if a HostConfig instance is embedded in this object.
112  bool holds_host_config() const;
113 
114  private:
115  // A variant that either holds information necessary for connecting to the
116  // config server or a HostConfig proto.
117  absl::variant<ConfigServerConnectionAttributes, HostConfig> host_config_info_;
118 };
119 
120 /// A manager object responsible for creating and managing enclave instances.
121 ///
122 /// EnclaveManager is a singleton class that tracks the status of enclaves
123 /// within a process. Users of this class first supply a configuration using the
124 /// static Configure() method, and then get a pointer to the singleton instance
125 /// as specified by this configuration by calling the static Instance() method.
126 /// Note that the EnclaveManager class must be configured before the instance
127 /// pointer can be obtained.
128 ///
129 /// The EnclaveManager::Configure() method takes an instance of the
130 /// EnclaveManagerOptions as its only input. This instance can be configured by
131 /// calling its public setter methods. Note that these setter methods return an
132 /// instance of the EnclaveManagerOptions() by reference so that the various
133 /// setters could be chained together.
134 ///
135 /// Example Usage:
136 /// ```
137 /// EnclaveManager::Configure(
138 /// EnclaveManagerOptions()
139 /// .set_config_server_connection_attributes(
140 /// "[::]:8000",
141 /// absl::Milliseconds(100)));
142 /// auto manager_result = EnclaveManager::Instance();
143 /// if (!manager_result.ok()) {
144 /// LOG(QFATAL) << manager_result.status();
145 /// }
146 /// EnclaveManager *manager = manager_result.ValueOrDie();
147 /// ...
148 /// ```
149 ///
150 /// One of the responsibilities of the EnclaveManager class is to provide sane
151 /// initial configuration to the enclaves it launches. The contents of the
152 /// EnclaveManagerOptions instance control how the default values for the
153 /// configuration are chosen.
154 class EnclaveManager {
155  public:
156  /// Fetches the EnclaveManager singleton instance.
157  ///
158  /// \return A StatusOr containing either the global EnclaveManager instance or
159  /// an error describing why it could not be returned.
160  static StatusOr<EnclaveManager *> Instance();
161 
162  /// Configures the enclave manager.
163  ///
164  /// \param options Configuration options as described in
165  /// EnclaveManagerOptions.
167 
168  /// Loads an enclave.
169  ///
170  /// Loads a new enclave with default enclave config settings and binds it to a
171  /// name. The actual work of opening the enclave is delegated to the passed
172  /// loader object.
173  ///
174  /// It is an error to specify a name which is already bound to an enclave.
175  ///
176  /// Example:
177  /// ```
178  /// LoadEnclave("/EchoEnclave", SgxLoader("echoService.so"));
179  /// ```
180  ///
181  /// \param name Name to bind the loaded enclave under.
182  /// \param loader Configured enclave loader to load from.
183  /// \param base_address Start address to load enclave(optional).
184  /// \param enclave_size The size of the enclave in memory(only needed if
185  /// |base_address| is specified).
187  void *base_address = nullptr,
188  const size_t enclave_size = 0);
189 
190  /// Loads an enclave.
191  ///
192  /// Loads a new enclave with custom enclave config settings and binds it to a
193  /// name. The actual work of opening the enclave is delegated to the passed
194  /// loader object.
195  ///
196  /// It is an error to specify a name which is already bound to an enclave.
197  ///
198  /// Example:
199  ///
200  /// ```
201  /// EnclaveConfig config;
202  /// ... // populate config proto.
203  /// LoadEnclave("/EchoEnclave", SgxLoader("echoService.so"), config);
204  /// ```
205  ///
206  /// \param name Name to bind the loaded enclave under.
207  /// \param loader Configured enclave loader to load from.
208  /// \param config Enclave configuration to launch the enclave with.
209  /// \param base_address Start address to load enclave(optional).
210  /// \param enclave_size The size of the enclave in memory(only needed if
211  /// |base_address| is specified).
213  EnclaveConfig config, void *base_address = nullptr,
214  const size_t enclave_size = 0);
215 
216  /// Fetches a client to a loaded enclave.
217  ///
218  /// \param name The name of an EnclaveClient that may be registered in the
219  /// EnclaveManager.
220  /// \return A mutable pointer to the EnclaveClient if the name is
221  /// registered. Otherwise returns nullptr.
222  EnclaveClient *GetClient(const std::string &name) const
223  LOCKS_EXCLUDED(client_table_lock_);
224 
225  /// Returns the name of an enclave client.
226  ///
227  /// \param client A pointer to a client that may be registered in the
228  /// EnclaveManager.
229  /// \return The name of an enclave client. If no enclave matches `client` the
230  /// empty string will be returned.
231  const std::string GetName(const EnclaveClient *client) const
232  LOCKS_EXCLUDED(client_table_lock_);
233 
234  /// Destroys an enclave.
235  ///
236  /// Destroys an enclave. This method calls `client's` EnterAndFinalize entry
237  /// point with final_input unless `skip_finalize` is true, then calls
238  /// `client's` DestroyEnclave method, and then removes client's name from the
239  /// EnclaveManager client registry. The manager owns the client, so removing
240  /// it calls client's destructor and frees its memory. The client is destroyed
241  /// regardless of whether `client's` EnterAndFinalize method succeeds or
242  /// fails. This method must not be invoked more than once.
243  ///
244  /// \param client A client attached to the enclave to destroy.
245  /// \param final_input Input to pass the enclave's finalizer.
246  /// \param skip_finalize If true, the enclave is destroyed without invoking
247  /// its Finalize method.
248  /// \return The Status returned by the enclave's Finalize method, or an
249  /// OK Status if that was skipped.
251  bool skip_finalize = false)
252  LOCKS_EXCLUDED(client_table_lock_);
253 
254  /// Fetches the shared resource manager object.
255  ///
256  /// \return The SharedResourceManager instance.
258  return &shared_resource_manager_;
259  }
260 
261  /// Fetches the shared resource manager object.
262  ///
263  /// \return The SharedResourceManager instance.
265  return &shared_resource_manager_;
266  }
267 
268  /// Get the loader of an enclave. This should only be used during fork in
269  /// order to load an enclave with the same loader as the parent.
270  EnclaveLoader *GetLoaderFromClient(EnclaveClient *client)
271  LOCKS_EXCLUDED(client_table_lock_);
272 
273  private:
274  EnclaveManager() EXCLUSIVE_LOCKS_REQUIRED(mu_);
275  EnclaveManager(EnclaveManager const &) = delete;
276  EnclaveManager &operator=(EnclaveManager const &) = delete;
277 
278  // Retrieves and returns a HostConfig proto as specified by the
279  // EnclaveManagerOptions which the EnclaveManager was configured when its
280  // sngleton instance was created.
281  HostConfig GetHostConfig() EXCLUSIVE_LOCKS_REQUIRED(mu_);
282 
283  // Loads a new enclave with custom enclave config settings and binds it to a
284  // name. The actual work of opening the enclave is delegated to the passed
285  // loader object.
286  Status LoadEnclaveInternal(const std::string &name,
287  const EnclaveLoader &loader,
288  const EnclaveConfig &config,
289  void *base_address = nullptr,
290  const size_t enclave_size = 0)
291  LOCKS_EXCLUDED(client_table_lock_);
292 
293  // Deletes an enclave client reference that points to an enclave that no
294  // longer exists. This should only happen during fork.
295  void RemoveEnclaveReference(const std::string &name)
296  LOCKS_EXCLUDED(client_table_lock_);
297 
298  // Create a thread to periodically update logic.
299  void SpawnWorkerThread();
300 
301  // Top level loop run by the background worker thread.
302  void WorkerLoop();
303 
304  // Execute a single iteration of the work loop.
305  void Tick();
306 
307  // Manager object for untrusted resources shared with enclaves.
308  SharedResourceManager shared_resource_manager_;
309 
310  // Value synchronized to CLOCK_MONOTONIC by the worker loop.
311  std::atomic<int64_t> clock_monotonic_;
312 
313  // Value synchronized to CLOCK_REALTIME by the worker loop.
314  std::atomic<int64_t> clock_realtime_;
315 
316  // A mutex guarding |client_by_name_|, |name_by_client_|, and
317  // |loader_by_client_| tables.
318  mutable absl::Mutex client_table_lock_;
319 
320  absl::flat_hash_map<std::string, std::unique_ptr<EnclaveClient>>
321  client_by_name_ GUARDED_BY(client_table_lock_);
322  absl::flat_hash_map<const EnclaveClient *, std::string> name_by_client_
323  GUARDED_BY(client_table_lock_);
324 
325  absl::flat_hash_map<const EnclaveClient *, std::unique_ptr<EnclaveLoader>>
326  loader_by_client_ GUARDED_BY(client_table_lock_);
327 
328  // A part of the configuration for enclaves launched by the enclave manager
329  // comes from the Asylo daemon. This member caches such configuration.
330  HostConfig host_config_;
331 
332  // Mutex guarding the static state of this class.
333  static absl::Mutex mu_;
334 
335  // Indication whether the class has been configured so that an instance could
336  // be created.
337  static bool configured_ GUARDED_BY(mu_);
338 
339  // Configuration options for this class.
340  static EnclaveManagerOptions *options_ GUARDED_BY(mu_);
341 
342  // Singleton instance of this class.
343  static EnclaveManager *instance_ GUARDED_BY(mu_);
344 };
345 
346 /// An abstract enclave loader.
347 ///
348 /// Host applications must load an enclave before using it. This is accomplished
349 /// via an architecture specific implementation of the EnclaveLoader interface.
351  public:
352  virtual ~EnclaveLoader() = default;
353 
354  protected:
355  // Only allow the enclave loading via the manager object.
356  friend class EnclaveManager;
357 
358  // Loads an enclave, returning a pointer to a client on success and a non-ok
359  // status on failure.
361  const std::string &name) const {
363  return LoadEnclave(name, /*base_address=*/nullptr, /*enclave_size=*/0,
364  config);
365  }
366 
367  // Loads an enclave at the specified address, returning a pointer to a client
368  // on success and a non-ok status on failure.
370  const std::string &name, void *base_address, const size_t enclave_size,
371  const EnclaveConfig &config) const = 0;
372 
373  // Gets a copy of the loader that loaded a previous enclave. This is only used
374  // by fork to load a child enclave with the same loader as the parent.
375  virtual StatusOr<std::unique_ptr<EnclaveLoader>> Copy() const = 0;
376 };
377 
378 // Stores the mapping between signals and the enclave with a handler installed
379 // for that signal.
380 class EnclaveSignalDispatcher {
381  public:
382  static EnclaveSignalDispatcher *GetInstance();
383 
384  // Associates a signal with an enclave which registers a handler for it.
385  // It's not supported for multiple enclaves to register the same signal. In
386  // that case, the latter will overwrite the former.
387  //
388  // Returns the enclave client that previous registered |signum|, or nullptr if
389  // no enclave has registered |signum| yet.
391  LOCKS_EXCLUDED(signal_enclave_map_lock_);
392 
393  // Gets the enclave that registered a handler for |signum|.
395  LOCKS_EXCLUDED(signal_enclave_map_lock_);
396 
397  // Deregisters all the signals registered by |client|.
399  LOCKS_EXCLUDED(signal_enclave_map_lock_);
400 
401  // Looks for the enclave client that registered |signum|, and calls
402  // EnterAndHandleSignal() with that enclave client. |signum|, |info| and
403  // |ucontext| are passed into the enclave.
405  void *ucontext);
406 
407  private:
408  EnclaveSignalDispatcher() = default; // Private to enforce singleton.
409  EnclaveSignalDispatcher(EnclaveSignalDispatcher const &) = delete;
410  void operator=(EnclaveSignalDispatcher const &) = delete;
411 
412  // Mapping of signal number to the enclave client that registered it.
413  absl::flat_hash_map<int, EnclaveClient *> signal_to_client_map_
414  GUARDED_BY(signal_enclave_map_lock_);
415 
416  // A mutex that guards signal_to_client_map_ and client_to_signal_map_.
417  mutable absl::Mutex signal_enclave_map_lock_;
418 };
419 
420 } // namespace asylo
421 
422 #endif // ASYLO_PLATFORM_CORE_ENCLAVE_MANAGER_H_
Status EnterEnclaveAndHandleSignal(int signum, siginfo_t *info, void *ucontext)
virtual StatusOr< std::unique_ptr< EnclaveLoader > > Copy() const =0
ConfigServerConnectionAttributes(std::string address, absl::Duration timeout)
Definition: enclave_manager.h:65
static StatusOr< EnclaveManager * > Instance()
Fetches the EnclaveManager singleton instance.
Configuration server connection attributes.
Definition: enclave_manager.h:64
EnclaveManagerOptions & set_host_config(HostConfig config)
Sets the HostConfig proto within this object.
bool holds_host_config() const
Returns true if a HostConfig instance is embedded in this object.
EnclaveClient * GetClient(const std::string &name) const LOCKS_EXCLUDED(client_table_lock_)
Fetches a client to a loaded enclave.
StatusOr< absl::Duration > get_config_server_connection_timeout() const
Returns the configuration server connection timeout.
Status LoadEnclave(const std::string &name, const EnclaveLoader &loader, EnclaveConfig config, void *base_address=nullptr, const size_t enclave_size=0)
Loads an enclave.
StatusOr< EnclaveClient * > GetClientForSignal(int signum) const LOCKS_EXCLUDED(signal_enclave_map_lock_)
StatusOr< std::string > get_config_server_address() const
Returns the address of the configuration server.
Status LoadEnclave(const std::string &name, const EnclaveLoader &loader, void *base_address=nullptr, const size_t enclave_size=0)
Loads an enclave.
static Status Configure(const EnclaveManagerOptions &options)
Configures the enclave manager.
virtual ~EnclaveLoader()=default
static EnclaveSignalDispatcher * GetInstance()
SharedResourceManager * shared_resources()
Fetches the shared resource manager object.
Definition: enclave_manager.h:257
EnclaveManagerOptions & set_config_server_connection_attributes(std::string address, absl::Duration timeout)
Configures a connection to the config server.
const EnclaveClient * RegisterSignal(int signum, EnclaveClient *client) LOCKS_EXCLUDED(signal_enclave_map_lock_)
absl::Duration connection_timeout
Definition: enclave_manager.h:71
Enclave Manager configuration.
Definition: enclave_manager.h:46
const SharedResourceManager * shared_resources() const
Fetches the shared resource manager object.
Definition: enclave_manager.h:264
EnclaveManagerOptions()
Constructs a default EnclaveManagerOptions object.
Definition: aead_cryptor.h:32
Status DeregisterAllSignalsForClient(EnclaveClient *client) LOCKS_EXCLUDED(signal_enclave_map_lock_)
Status DestroyEnclave(EnclaveClient *client, const EnclaveFinal &final_input, bool skip_finalize=false) LOCKS_EXCLUDED(client_table_lock_)
Destroys an enclave.
EnclaveLoader * GetLoaderFromClient(EnclaveClient *client) LOCKS_EXCLUDED(client_table_lock_)
Get the loader of an enclave.
const std::string GetName(const EnclaveClient *client) const LOCKS_EXCLUDED(client_table_lock_)
Returns the name of an enclave client.
StatusOr< HostConfig > get_host_config() const
Returns the embedded HostConfig object.
std::string server_address
Definition: enclave_manager.h:70