<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>230206</bug_id>
          
          <creation_ts>2021-09-12 20:45:03 -0700</creation_ts>
          <short_desc>WebKit2 crashes when initializing due to not-threadsafe call to _NSGetEnviron()</short_desc>
          <delta_ts>2021-09-17 16:48:56 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit2</component>
          <version>Safari Technology Preview</version>
          <rep_platform>iPhone / iPad</rep_platform>
          <op_sys>iOS 14</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Major</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="white">jinhao.zhang</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>kevin_neal</cc>
    
    <cc>kkinnunen</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1792839</commentid>
    <comment_count>0</comment_count>
    <who name="white">jinhao.zhang</who>
    <bug_when>2021-09-12 20:45:03 -0700</bug_when>
    <thetext>When WebKit2 is to initialize, it calls _NSGetEnviron() to get all env variables.
The code is as below: ( in file: Options.cpp, method: void Options::initialize() )

#if PLATFORM(COCOA)
            bool hasBadOptions = false;
            for (char** envp = *_NSGetEnviron(); *envp; envp++) {
                const char* env = *envp;
                if (!strncmp(&quot;JSC_&quot;, env, 4)) {
                    if (!Options::setOption(&amp;env[4])) {
                        dataLog(&quot;ERROR: invalid option: &quot;, *envp, &quot;\n&quot;);
                        hasBadOptions = true;
                    }
                }
            }

However, _NSGetEnviron is not thread-safe. If there is another thread calling putenv(),
there is a small chance that WebKit crashes due to invalid pointer sent to strncpm().
This is due to putenv() is using &apos;realloc&apos; when necessary, which invalidates the old pointer values.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792877</commentid>
    <comment_count>1</comment_count>
    <who name="Kimmo Kinnunen">kkinnunen</who>
    <bug_when>2021-09-13 01:10:29 -0700</bug_when>
    <thetext>I think modifying environment is inherently single-threaded operation.
Is there a place in WebKit which modifies the environment in such a way that causes WebKit to crash in this location during initialisation?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792917</commentid>
    <comment_count>2</comment_count>
    <who name="white">jinhao.zhang</who>
    <bug_when>2021-09-13 04:50:49 -0700</bug_when>
    <thetext>(In reply to Kimmo Kinnunen from comment #1)
&gt; I think modifying environment is inherently single-threaded operation.
&gt; Is there a place in WebKit which modifies the environment in such a way that
&gt; causes WebKit to crash in this location during initialisation?

AFAIK there isn&apos;t. 

This crashing is due to some third party code naively called putenv() in non-main threads and caused racing condition issues while WebKit2 was initializing itself in main thread.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1793101</commentid>
    <comment_count>3</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2021-09-13 14:33:01 -0700</bug_when>
    <thetext>Given the above, is there a WebKit issue to track here?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1793203</commentid>
    <comment_count>4</comment_count>
    <who name="Kimmo Kinnunen">kkinnunen</who>
    <bug_when>2021-09-13 23:07:59 -0700</bug_when>
    <thetext>(In reply to white from comment #2)
&gt; (In reply to Kimmo Kinnunen from comment #1)
&gt; &gt; I think modifying environment is inherently single-threaded operation.
&gt; &gt; Is there a place in WebKit which modifies the environment in such a way that
&gt; &gt; causes WebKit to crash in this location during initialisation?
&gt; 
&gt; AFAIK there isn&apos;t. 
&gt; 
&gt; This crashing is due to some third party code naively called putenv() in
&gt; non-main threads and caused racing condition issues while WebKit2 was
&gt; initializing itself in main thread.

So spelled out what Alexey mentioned:
There are two options:
A) Third-party code modifies the environment in single-threaded manner
or
B) WebKit should never use the environment

I believe this option is impossible to get correct:
C) Third-party code modifies the environment in random threads at random points in time, WebKit uses the environment


I think B would be a progression in many ways, one of which would be the bug you mention, but this is just an opinion. However, there is a lot of environment use in WebKit, so it might not be such an easy task for what it accomplishes. Also, other components could use the environment.

This particular crash is a bit odd since intuitively the client process should not use JSC, and hence should not crash here. However, there is still the issue of other locations using the environment.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1793736</commentid>
    <comment_count>5</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-09-15 09:56:20 -0700</bug_when>
    <thetext>&lt;rdar://problem/83155440&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1794695</commentid>
    <comment_count>6</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2021-09-17 16:48:56 -0700</bug_when>
    <thetext>&gt; Also, other components could use the environment.

Other system frameworks certainly do use the environment. There is also a huge number of getenv calls in WebKit, not just this one _NSGetEnviron use in JavaScriptCore.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>