|
43 | 43 | #endif
|
44 | 44 |
|
45 | 45 | #if TARGET_OS_OSX || TARGET_OS_MACCATALYST
|
| 46 | +#if __has_include(<sys/codesign.h>) |
| 47 | +#include <sys/codesign.h> |
| 48 | +#else |
| 49 | +// SPI |
| 50 | +#define CS_OPS_STATUS 0 |
| 51 | +#define CS_GET_TASK_ALLOW 0x00000004 |
| 52 | +#define CS_RUNTIME 0x00010000 |
| 53 | +#define CS_PLATFORM_BINARY 0x04000000 |
| 54 | +#define CS_PLATFORM_PATH 0x08000000 |
| 55 | +extern "C" int csops(int, unsigned int, void *, size_t); |
| 56 | +#endif |
46 | 57 | #include <spawn.h>
|
47 | 58 | #endif
|
48 | 59 | #include <unistd.h>
|
@@ -145,11 +156,6 @@ BacktraceInitializer backtraceInitializer;
|
145 | 156 |
|
146 | 157 | SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_END
|
147 | 158 |
|
148 |
| -#if TARGET_OS_OSX || TARGET_OS_MACCATALYST |
149 |
| -posix_spawnattr_t backtraceSpawnAttrs; |
150 |
| -posix_spawn_file_actions_t backtraceFileActions; |
151 |
| -#endif |
152 |
| - |
153 | 159 | #if SWIFT_BACKTRACE_ON_CRASH_SUPPORTED
|
154 | 160 |
|
155 | 161 | // We need swiftBacktracePath to be aligned on a page boundary, and it also
|
@@ -254,7 +260,24 @@ const char *presetToString(Preset preset) {
|
254 | 260 | bool isPrivileged() {
|
255 | 261 | return getauxval(AT_SECURE);
|
256 | 262 | }
|
257 |
| -#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 263 | +#elif defined(__APPLE__) |
| 264 | +bool isPrivileged() { |
| 265 | + if (issetugid()) |
| 266 | + return true; |
| 267 | + |
| 268 | + uint32_t flags = 0; |
| 269 | + if (csops(getpid(), |
| 270 | + CS_OPS_STATUS, |
| 271 | + &flags, |
| 272 | + sizeof(flags)) != 0) |
| 273 | + return true; |
| 274 | + |
| 275 | + if (flags & (CS_PLATFORM_BINARY | CS_PLATFORM_PATH | CS_RUNTIME)) |
| 276 | + return true; |
| 277 | + |
| 278 | + return !(flags & CS_GET_TASK_ALLOW); |
| 279 | +} |
| 280 | +#elif defined(__FreeBSD__) || defined(__OpenBSD__) |
258 | 281 | bool isPrivileged() {
|
259 | 282 | return issetugid();
|
260 | 283 | }
|
@@ -451,17 +474,6 @@ BacktraceInitializer::BacktraceInitializer() {
|
451 | 474 | }
|
452 | 475 |
|
453 | 476 | if (_swift_backtraceSettings.enabled == OnOffTty::On) {
|
454 |
| -#if TARGET_OS_OSX || TARGET_OS_MACCATALYST |
455 |
| - // Make sure that all fds are closed except for stdin/stdout/stderr. |
456 |
| - posix_spawnattr_init(&backtraceSpawnAttrs); |
457 |
| - posix_spawnattr_setflags(&backtraceSpawnAttrs, POSIX_SPAWN_CLOEXEC_DEFAULT); |
458 |
| - |
459 |
| - posix_spawn_file_actions_init(&backtraceFileActions); |
460 |
| - posix_spawn_file_actions_addinherit_np(&backtraceFileActions, STDIN_FILENO); |
461 |
| - posix_spawn_file_actions_addinherit_np(&backtraceFileActions, STDOUT_FILENO); |
462 |
| - posix_spawn_file_actions_addinherit_np(&backtraceFileActions, STDERR_FILENO); |
463 |
| -#endif |
464 |
| - |
465 | 477 | ErrorCode err = _swift_installCrashHandler();
|
466 | 478 | if (err != 0) {
|
467 | 479 | swift::warning(0,
|
@@ -993,7 +1005,7 @@ _swift_spawnBacktracer(const ArgChar * const *argv)
|
993 | 1005 | const_cast<char * const *>(env));
|
994 | 1006 | #else
|
995 | 1007 | int ret = posix_spawn(&child, swiftBacktracePath,
|
996 |
| - &backtraceFileActions, &backtraceSpawnAttrs, |
| 1008 | + nullptr, nullptr, |
997 | 1009 | const_cast<char * const *>(argv),
|
998 | 1010 | const_cast<char * const *>(env));
|
999 | 1011 | #endif
|
|
0 commit comments