Fix <rdar://problem/8961227> WebKit2 does not work with symlinked home directories.
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 5 Feb 2011 01:23:44 +0000 (01:23 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 5 Feb 2011 01:23:44 +0000 (01:23 +0000)
Reviewed by Sam Weinig.

* WebProcess/mac/WebProcessMac.mm:
(WebKit::WebProcess::platformSetCacheModel):
Don't adopt an autoreleased pointer, this would lead to a crash.

(WebKit::appendSandboxParameterPath):
(WebKit::appendSandboxParameterConfPath):
(WebKit::initializeSandbox):
Clean up the sandbox parameter code and make sure that realpath is called for all parameters.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@77707 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/mac/WebProcessMac.mm

index 7de8bca8cfcf0418a490b4600536463644e142a7..30b0f8f232aae5414960e03290262024d34a5e97 100644 (file)
@@ -1,3 +1,18 @@
+2011-02-04  Anders Carlsson  <andersca@apple.com>
+
+        Reviewed by Sam Weinig.
+
+        Fix <rdar://problem/8961227> WebKit2 does not work with symlinked home directories.
+
+        * WebProcess/mac/WebProcessMac.mm:
+        (WebKit::WebProcess::platformSetCacheModel):
+        Don't adopt an autoreleased pointer, this would lead to a crash.
+
+        (WebKit::appendSandboxParameterPath):
+        (WebKit::appendSandboxParameterConfPath):
+        (WebKit::initializeSandbox):
+        Clean up the sandbox parameter code and make sure that realpath is called for all parameters.
+
 2011-02-04  Sam Weinig  <sam@webkit.org>
 
         Reviewed by Mark Rowe.
index 54f03db6803cf49672efead096405dbbc137cb9c..437b38857a8dfd4677cbaee0aa8fc044dd5aed34 100644 (file)
@@ -76,7 +76,7 @@ void WebProcess::platformSetCacheModel(CacheModel cacheModel)
 {
     RetainPtr<NSString> nsurlCacheDirectory(AdoptNS, (NSString *)WKCopyFoundationCacheDirectory());
     if (!nsurlCacheDirectory)
-        nsurlCacheDirectory.adoptNS(NSHomeDirectory());
+        nsurlCacheDirectory = NSHomeDirectory();
 
     // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
     // count doesn't align exactly to a megabyte boundary.
@@ -110,6 +110,25 @@ void WebProcess::platformClearResourceCaches()
     [[NSURLCache sharedURLCache] removeAllCachedResponses];
 }
 
+static void appendSandboxParameterPath(Vector<const char*>& vector, const char* name, const char* path)
+{
+    char normalizedPath[PATH_MAX];
+    if (!realpath(path, normalizedPath))
+        normalizedPath[0] = '\0';
+
+    vector.append(name);
+    vector.append(fastStrDup(normalizedPath));
+}
+
+static void appendSandboxParameterConfPath(Vector<const char*>& vector, const char* name, int confID)
+{
+    char path[PATH_MAX];
+    if (confstr(confID, path, PATH_MAX) <= 0)
+        path[0] = '\0';
+
+    appendSandboxParameterPath(vector, name, path);
+}
+
 static void initializeSandbox(const WebProcessCreationParameters& parameters)
 {
 #if ENABLE(WEB_PROCESS_SANDBOX)
@@ -118,41 +137,29 @@ static void initializeSandbox(const WebProcessCreationParameters& parameters)
         return;
     }
 
-    char* errorBuf;
-    char tmpPath[PATH_MAX];
-    char tmpRealPath[PATH_MAX];
-    char homeRealPath[PATH_MAX];
-    char cachePath[PATH_MAX];
-    char cacheRealPath[PATH_MAX];
-    const char* frameworkPath = [[[[NSBundle bundleForClass:NSClassFromString(@"WKView")] bundlePath] stringByDeletingLastPathComponent] UTF8String];
-    const char* profilePath = [[[NSBundle mainBundle] pathForResource:@"com.apple.WebProcess" ofType:@"sb"] UTF8String];
-
-    if (!realpath([NSHomeDirectory() UTF8String], homeRealPath)) {
-        fprintf(stderr, "WebProcess: couldn't determine home directory when initializing sandbox");
-        exit(EX_CONFIG);
-    }
+    Vector<const char*> sandboxParameters;
+
+    appendSandboxParameterPath(sandboxParameters, "HOME_DIR", [NSHomeDirectory() fileSystemRepresentation]);
+    appendSandboxParameterPath(sandboxParameters, "WEBKIT2_FRAMEWORK_DIR", [[[[NSBundle bundleForClass:NSClassFromString(@"WKView")] bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]);
+    appendSandboxParameterConfPath(sandboxParameters, "DARWIN_USER_TEMP_DIR", _CS_DARWIN_USER_TEMP_DIR);
+    appendSandboxParameterConfPath(sandboxParameters, "DARWIN_USER_CACHE_DIR", _CS_DARWIN_USER_CACHE_DIR);
+    appendSandboxParameterPath(sandboxParameters, "WEBKIT_DATABASE_DIR", [(NSString *)parameters.databaseDirectory fileSystemRepresentation]);
+    appendSandboxParameterPath(sandboxParameters, "NSURL_CACHE_DIR", parameters.nsURLCachePath.data());
+    appendSandboxParameterPath(sandboxParameters, "UI_PROCESS_BUNDLE_RESOURCE_DIR", parameters.uiProcessBundleResourcePath.data());
+    sandboxParameters.append(static_cast<const char*>(0));
 
-    if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmpPath, PATH_MAX) <= 0 || !realpath(tmpPath, tmpRealPath))
-        tmpRealPath[0] = '\0';
-
-    if (confstr(_CS_DARWIN_USER_CACHE_DIR, cachePath, PATH_MAX) <= 0 || !realpath(cachePath, cacheRealPath))
-        cacheRealPath[0] = '\0';
-
-    const char* const sandboxParam[] = {
-        "HOME_DIR", (const char*)homeRealPath,
-        "WEBKIT2_FRAMEWORK_DIR", frameworkPath,
-        "DARWIN_USER_TEMP_DIR", (const char*)tmpRealPath,
-        "DARWIN_USER_CACHE_DIR", (const char*)cacheRealPath,
-        "WEBKIT_DATABASE_DIR", (const char*)[(NSString *)parameters.databaseDirectory fileSystemRepresentation],
-        "NSURL_CACHE_DIR", (const char*)parameters.nsURLCachePath.data(),
-        "UI_PROCESS_BUNDLE_RESOURCE_DIR", (const char*)parameters.uiProcessBundleResourcePath.data(),
-        NULL
-    };
-
-    if (sandbox_init_with_parameters(profilePath, SANDBOX_NAMED_EXTERNAL, sandboxParam, &errorBuf)) {
-        fprintf(stderr, "WebProcess: couldn't initialize sandbox profile [%s] with framework path [%s], tmp path [%s], cache path [%s]: %s\n", profilePath, frameworkPath, tmpRealPath, cacheRealPath, errorBuf);
+    const char* profilePath = [[[NSBundle mainBundle] pathForResource:@"com.apple.WebProcess" ofType:@"sb"] fileSystemRepresentation];
+
+    char* errorBuf;
+    if (sandbox_init_with_parameters(profilePath, SANDBOX_NAMED_EXTERNAL, sandboxParameters.data(), &errorBuf)) {
+        fprintf(stderr, "WebProcess: couldn't initialize sandbox profile [%s]\n", profilePath);
+        for (size_t i = 0; sandboxParameters[i]; i += 2)
+            fprintf(stderr, "%s=%s\n", sandboxParameters[i], sandboxParameters[i + 1]);
         exit(EX_NOPERM);
     }
+
+    for (size_t i = 0; sandboxParameters[i]; i += 2)
+        fastFree(const_cast<char*>(sandboxParameters[i + 1]));
 #endif
 }