File: NetscapeSandboxFunctions.mm

package info (click to toggle)
qtwebkit 2.3.4.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 290,632 kB
  • sloc: cpp: 1,417,515; python: 85,048; ansic: 39,357; perl: 38,862; ruby: 10,313; objc: 9,505; xml: 8,679; asm: 3,864; yacc: 2,458; sh: 1,237; lex: 813; makefile: 592; java: 228; php: 79
file content (208 lines) | stat: -rw-r--r-- 8,159 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2012 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "config.h"
#import "NetscapeSandboxFunctions.h"

#if ENABLE(NETSCAPE_PLUGIN_API) && ENABLE(PLUGIN_PROCESS)

#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070

#import "PluginProcess.h"
#import "NetscapePluginModule.h"
#import "WebKitSystemInterface.h"
#import <WebCore/FileSystem.h>
#import <WebCore/SoftLinking.h>
#import <sys/stat.h>
#import <sysexits.h>
#import <wtf/RetainPtr.h>
#import <wtf/Vector.h>
#import <wtf/text/CString.h>

SOFT_LINK_FRAMEWORK(CoreServices)
SOFT_LINK_OPTIONAL(CoreServices, CFURLStopAccessingSecurityScopedResource, void, unused, (CFURLRef))

using namespace WebKit;
using namespace WebCore;

WKNSandboxFunctions* netscapeSandboxFunctions()
{
    static WKNSandboxFunctions functions = {
        sizeof(WKNSandboxFunctions),
        WKNVSandboxFunctionsVersionCurrent,
        WKN_EnterSandbox,
        WKN_FileStopAccessing
    };
    return &functions;
}

static bool enteredSandbox;

static CString readSandboxProfile()
{
    RetainPtr<CFURLRef> profileURL(AdoptCF, CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("com.apple.WebKit.PluginProcess"), CFSTR("sb"), 0));
    char profilePath[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(profileURL.get(), false, reinterpret_cast<UInt8*>(profilePath), sizeof(profilePath))) {
        WTFLogAlways("Could not get file system representation of plug-in sandbox URL\n");
        return CString();
    }

    FILE *file = fopen(profilePath, "r");
    if (!file) {
        WTFLogAlways("Could not open plug-in sandbox file '%s'\n", profilePath);
        return CString();
    }

    struct stat fileInfo;
    if (stat(profilePath, &fileInfo)) {
        WTFLogAlways("Could not get plug-in sandbox file size '%s'\n", profilePath);
        return CString();
    }

    char* characterBuffer;
    CString result = CString::newUninitialized(fileInfo.st_size, characterBuffer);

    if (1 != fread(characterBuffer, fileInfo.st_size, 1, file)) {
        WTFLogAlways("Could not read plug-in sandbox file '%s'\n", profilePath);
        return CString();
    }

    fclose(file);

    return result;
}

NPError enterSandbox(const char* sandboxProfile, const char* readOnlyPaths[], const char* readWritePaths[])
{
    if (enteredSandbox)
        return NPERR_GENERIC_ERROR;

#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
    // Use private temporary and cache directories.
    String systemDirectorySuffix = "com.apple.WebKit.PluginProcess+" + PluginProcess::shared().netscapePluginModule()->module()->bundleIdentifier();
    setenv("DIRHELPER_USER_DIR_SUFFIX", fileSystemRepresentation(systemDirectorySuffix).data(), 0);
    char temporaryDirectory[PATH_MAX];
    if (!confstr(_CS_DARWIN_USER_TEMP_DIR, temporaryDirectory, sizeof(temporaryDirectory))) {
        WTFLogAlways("PluginProcess: couldn't retrieve private temporary directory path: %d\n", errno);
        exit(EX_NOPERM);
    }
    setenv("TMPDIR", temporaryDirectory, 1);
    if (chdir(temporaryDirectory) == -1) {
        WTFLogAlways("PluginProcess: couldn't change working directory to temporary path: %s, errno %d\n", temporaryDirectory, errno);
        exit(EX_OSERR);
    }
#endif


    Vector<const char*> extendedReadOnlyPaths;
    if (readOnlyPaths) {
        for (unsigned i = 0; readOnlyPaths[i]; ++i)
            extendedReadOnlyPaths.append(readOnlyPaths[i]);
    }

    CString pluginModulePath = fileSystemRepresentation(PluginProcess::shared().pluginPath());
    extendedReadOnlyPaths.append(pluginModulePath.data());

    // On-disk WebKit framework locations, to account for debug installations.
    // Allowing the whole directory containing WebKit2.framework for the sake of APIs that implicitly load other WebKit frameworks.
    // We don't want to load them now, and thus don't have any better idea of where they are located on disk.
    extendedReadOnlyPaths.append([[[[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] bundleURL] URLByDeletingLastPathComponent] path] fileSystemRepresentation]);

    extendedReadOnlyPaths.append(static_cast<const char*>(0));

    Vector<const char*> extendedReadWritePaths;
    if (readWritePaths) {
        for (unsigned i = 0; readWritePaths[i]; ++i)
            extendedReadWritePaths.append(readWritePaths[i]);
    }

    char darwinUserTempDirectory[PATH_MAX];
    if (confstr(_CS_DARWIN_USER_TEMP_DIR, darwinUserTempDirectory, PATH_MAX) > 0)
        extendedReadWritePaths.append(darwinUserTempDirectory);

    char darwinUserCacheDirectory[PATH_MAX];
    if (confstr(_CS_DARWIN_USER_CACHE_DIR, darwinUserCacheDirectory, PATH_MAX) > 0)
        extendedReadWritePaths.append(darwinUserCacheDirectory);

    RetainPtr<CFStringRef> cachePath(AdoptCF, WKCopyFoundationCacheDirectory());
    extendedReadWritePaths.append([(NSString *)cachePath.get() fileSystemRepresentation]);

    extendedReadWritePaths.append(static_cast<const char*>(0));

    // WKEnterPluginSandbox canonicalizes path arrays, but not parameters (because it cannot know if one is a path).
    char* homeDirectory = realpath([NSHomeDirectory() fileSystemRepresentation], 0);
    if (!homeDirectory)
        exit(EX_NOPERM);
    const char* sandboxParameters[] = { "HOME_DIR", homeDirectory, 0, 0 };

    if (!WKEnterPluginSandbox(sandboxProfile, sandboxParameters, extendedReadOnlyPaths.data(), extendedReadWritePaths.data())) {
        WTFLogAlways("Couldn't initialize sandbox profile\n");
        exit(EX_NOPERM);
    }

    if (noErr != WKEnableSandboxStyleFileQuarantine()) {
        WTFLogAlways("Couldn't enable file quarantine\n");
        exit(EX_NOPERM);
    }

    free(homeDirectory);
    enteredSandbox = true;

    RetainPtr<NSDictionary> defaults = adoptNS([[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"NSUseRemoteSavePanel", nil]);
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults.get()];

    return NPERR_NO_ERROR;
}

NPError WKN_EnterSandbox(const char* readOnlyPaths[], const char* readWritePaths[])
{
    CString profile = readSandboxProfile();
    if (profile.isNull())
        exit(EX_NOPERM);

    return enterSandbox(profile.data(), readOnlyPaths, readWritePaths);
}

NPError WKN_FileStopAccessing(const char* path)
{
    if (!enteredSandbox)
        return NPERR_GENERIC_ERROR;

    if (!CFURLStopAccessingSecurityScopedResourcePtr())
        return NPERR_NO_ERROR;

    RetainPtr<CFStringRef> urlString(AdoptCF, CFStringCreateWithFileSystemRepresentation(0, path));
    if (!urlString)
        return NPERR_INVALID_PARAM;
    RetainPtr<CFURLRef> url(AdoptCF, CFURLCreateWithFileSystemPath(0, urlString.get(), kCFURLPOSIXPathStyle, false));

    CFURLStopAccessingSecurityScopedResourcePtr()(url.get());

    return NPERR_NO_ERROR;
}

#endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070

#endif // ENABLE(NETSCAPE_PLUGIN_API) && ENABLE(PLUGIN_PROCESS)