[go: up one dir, main page]

Skip to content

Commit

Permalink
src/hostip.c: Move macOS-specific calls into global init call
Browse files Browse the repository at this point in the history
curl#7121 introduced a macOS system call
to `SCDynamicStoreCopyProxies`, which is invoked every time an IP
address needs to be resolved.

However, this system call is not thread-safe, and macOS will kill the
process if the system call is run first in a fork. To make it possible
for the parent process to call this once and prevent the crash, only
invoke this system call in the global initialization routine.

Closes curl#11252
  • Loading branch information
stanhu committed Jun 5, 2023
1 parent c78a185 commit d21e2b2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 21 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ LIB_CFILES = \
krb5.c \
ldap.c \
llist.c \
macos.c \
md4.c \
md5.c \
memdebug.c \
Expand Down Expand Up @@ -315,6 +316,7 @@ LIB_HFILES = \
inet_ntop.h \
inet_pton.h \
llist.h \
macos.h \
memdebug.h \
mime.h \
mqtt.h \
Expand Down
8 changes: 8 additions & 0 deletions lib/easy.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "slist.h"
#include "mime.h"
#include "amigaos.h"
#include "macos.h"
#include "warnless.h"
#include "sigpipe.h"
#include "vssh/ssh.h"
Expand Down Expand Up @@ -181,6 +182,13 @@ static CURLcode global_init(long flags, bool memoryfuncs)
}
#endif

#if defined(__APPLE__)
if(Curl_macos_init()) {
DEBUGF(fprintf(stderr, "Error: Curl_macos_init failed\n"));
goto fail;
}
#endif

if(Curl_resolver_global_init()) {
DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
goto fail;
Expand Down
21 changes: 0 additions & 21 deletions lib/hostip.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@
#include "curl_memory.h"
#include "memdebug.h"

#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#endif

#if defined(CURLRES_SYNCH) && \
defined(HAVE_ALARM) && \
defined(SIGALRM) && \
Expand Down Expand Up @@ -743,23 +739,6 @@ enum resolve_t Curl_resolv(struct Curl_easy *data,
return CURLRESOLV_ERROR;
}

#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
{
/*
* The automagic conversion from IPv4 literals to IPv6 literals only
* works if the SCDynamicStoreCopyProxies system function gets called
* first. As Curl currently doesn't support system-wide HTTP proxies, we
* therefore don't use any value this function might return.
*
* This function is only available on a macOS and is not needed for
* IPv4-only builds, hence the conditions above.
*/
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
if(dict)
CFRelease(dict);
}
#endif

#ifndef USE_RESOLVE_ON_IPS
/* First check if this is an IPv4 address string */
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
Expand Down
60 changes: 60 additions & 0 deletions lib/macos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/

#include "curl_setup.h"

#if defined(__APPLE__)

#if defined(TARGET_OS_OSX) && TARGET_OS_OSX

#include <curl/curl.h>

#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#endif

CURLcode Curl_macos_init(void)
{
#if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
{
/*
* The automagic conversion from IPv4 literals to IPv6 literals only
* works if the SCDynamicStoreCopyProxies system function gets called
* first. As Curl currently doesn't support system-wide HTTP proxies, we
* therefore don't use any value this function might return.
*
* This function is only available on a macOS and is not needed for
* IPv4-only builds, hence the conditions above.
*/
CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
if(dict)
CFRelease(dict);
}
#endif
return CURLE_OK;
}

#endif /* TARGET_OS_OSX */

#endif /* __APPLE__ */
38 changes: 38 additions & 0 deletions lib/macos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef HEADER_CURL_MACOS_H
#define HEADER_CURL_MACOS_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"

#if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX

CURLcode Curl_macos_init(void);

#else

#define Curl_macos_init() CURLE_OK

#endif

#endif /* HEADER_CURL_MACOS_H */

0 comments on commit d21e2b2

Please sign in to comment.