sandbox.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2006-2010 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _SANDBOX_H_
  24. #define _SANDBOX_H_
  25. #include <sys/cdefs.h>
  26. #include <stdint.h>
  27. #include <unistd.h>
  28. __BEGIN_DECLS
  29. /*
  30. * @function sandbox_init
  31. * Places the current process in a sandbox with a profile as
  32. * specified. If the process is already in a sandbox, the new profile
  33. * is ignored and sandbox_init() returns an error.
  34. *
  35. * @param profile (input) The Sandbox profile to be used. The format
  36. * and meaning of this parameter is modified by the `flags' parameter.
  37. *
  38. * @param flags (input) Must be SANDBOX_NAMED. All other
  39. * values are reserved.
  40. *
  41. * @param errorbuf (output) In the event of an error, sandbox_init
  42. * will set `*errorbuf' to a pointer to a NUL-terminated string
  43. * describing the error. This string may contain embedded newlines.
  44. * This error information is suitable for developers and is not
  45. * intended for end users.
  46. *
  47. * If there are no errors, `*errorbuf' will be set to NULL. The
  48. * buffer `*errorbuf' should be deallocated with `sandbox_free_error'.
  49. *
  50. * @result 0 on success, -1 otherwise.
  51. */
  52. int sandbox_init(const char *profile, uint64_t flags, char **errorbuf);
  53. /*
  54. * @define SANDBOX_NAMED The `profile' argument specifies a Sandbox
  55. * profile named by one of the kSBXProfile* string constants.
  56. */
  57. #define SANDBOX_NAMED 0x0001
  58. #ifdef __APPLE_API_PRIVATE
  59. /* The following flags are reserved for Mac OS X. Developers should not
  60. * depend on their availability.
  61. */
  62. /*
  63. * @define SANDBOX_NAMED_BUILTIN The `profile' argument specifies the
  64. * name of a builtin profile that is statically compiled into the
  65. * system.
  66. */
  67. #define SANDBOX_NAMED_BUILTIN 0x0002
  68. /*
  69. * @define SANDBOX_NAMED_EXTERNAL The `profile' argument specifies the
  70. * pathname of a Sandbox profile. The pathname may be abbreviated: If
  71. * the name does not start with a `/' it is treated as relative to
  72. * /usr/share/sandbox and a `.sb' suffix is appended.
  73. */
  74. #define SANDBOX_NAMED_EXTERNAL 0x0003
  75. /*
  76. * @define SANDBOX_NAMED_MASK Mask for name types: 4 bits, 15 possible
  77. * name types, 3 currently defined.
  78. */
  79. #define SANDBOX_NAMED_MASK 0x000f
  80. #endif /* __APPLE_API_PRIVATE */
  81. /*
  82. * Available Sandbox profiles.
  83. */
  84. /* TCP/IP networking is prohibited. */
  85. extern const char kSBXProfileNoInternet[];
  86. /* All sockets-based networking is prohibited. */
  87. extern const char kSBXProfileNoNetwork[];
  88. /* File system writes are prohibited. */
  89. extern const char kSBXProfileNoWrite[];
  90. /* File system writes are restricted to temporary folders /var/tmp and
  91. * confstr(_CS_DARWIN_USER_DIR, ...).
  92. */
  93. extern const char kSBXProfileNoWriteExceptTemporary[];
  94. /* All operating system services are prohibited. */
  95. extern const char kSBXProfilePureComputation[];
  96. /*
  97. * @function sandbox_free_error
  98. * Deallocates an error string previously allocated by sandbox_init.
  99. *
  100. * @param errorbuf (input) The buffer to be freed. Must be a pointer
  101. * previously returned by sandbox_init in the `errorbuf' argument, or NULL.
  102. *
  103. * @result void
  104. */
  105. void sandbox_free_error(char *errorbuf);
  106. #ifdef __APPLE_API_PRIVATE
  107. /* The following definitions are reserved for Mac OS X. Developers should not
  108. * depend on their availability.
  109. */
  110. int sandbox_init_with_parameters(const char *profile, uint64_t flags, const char *const parameters[], char **errorbuf);
  111. int sandbox_init_with_extensions(const char *profile, uint64_t flags, const char *const extensions[], char **errorbuf);
  112. enum sandbox_filter_type {
  113. SANDBOX_FILTER_NONE,
  114. SANDBOX_FILTER_PATH,
  115. SANDBOX_FILTER_GLOBAL_NAME,
  116. SANDBOX_FILTER_LOCAL_NAME,
  117. SANDBOX_FILTER_APPLEEVENT_DESTINATION,
  118. SANDBOX_FILTER_RIGHT_NAME,
  119. };
  120. extern const enum sandbox_filter_type SANDBOX_CHECK_NO_REPORT __attribute__((weak_import));
  121. enum sandbox_extension_flags {
  122. FS_EXT_DEFAULTS = 0,
  123. FS_EXT_FOR_PATH = (1 << 0),
  124. FS_EXT_FOR_FILE = (1 << 1),
  125. FS_EXT_READ = (1 << 2),
  126. FS_EXT_WRITE = (1 << 3),
  127. FS_EXT_PREFER_FILEID = (1 << 4),
  128. };
  129. int sandbox_check(pid_t pid, const char *operation, enum sandbox_filter_type type, ...);
  130. int sandbox_note(const char *note);
  131. int sandbox_suspend(pid_t pid);
  132. int sandbox_unsuspend(void);
  133. int sandbox_issue_extension(const char *path, char **ext_token);
  134. int sandbox_issue_fs_extension(const char *path, uint64_t flags, char **ext_token);
  135. int sandbox_issue_fs_rw_extension(const char *path, char **ext_token);
  136. int sandbox_issue_mach_extension(const char *name, char **ext_token);
  137. int sandbox_consume_extension(const char *path, const char *ext_token);
  138. int sandbox_consume_fs_extension(const char *ext_token, char **path);
  139. int sandbox_consume_mach_extension(const char *ext_token, char **name);
  140. int sandbox_release_fs_extension(const char *ext_token);
  141. int sandbox_container_path_for_pid(pid_t pid, char *buffer, size_t bufsize);
  142. int sandbox_wakeup_daemon(char **errorbuf);
  143. const char *_amkrtemp(const char *);
  144. #endif /* __APPLE_API_PRIVATE */
  145. __END_DECLS
  146. #endif /* _SANDBOX_H_ */