2019-12-20 21:31:27 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "LibrarySandboxPreload.h"
|
|
|
|
|
|
|
|
#include "BinaryPath.h"
|
|
|
|
#include "prlink.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
static nsAutoCString GetSandboxedPath(const nsACString& libName) {
|
2019-12-20 21:31:27 +03:00
|
|
|
nsCOMPtr<nsIFile> binaryPath;
|
|
|
|
nsresult rv = mozilla::BinaryPath::GetFile(getter_AddRefs(binaryPath));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_CRASH("Library preload failure: Failed to get binary file\n");
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
nsCOMPtr<nsIFile> libFile;
|
|
|
|
rv = binaryPath->GetParent(getter_AddRefs(libFile));
|
2019-12-20 21:31:27 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_CRASH("Library preload failure: Failed to get binary folder\n");
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
rv = libFile->AppendNative(libName);
|
2020-02-01 13:38:29 +03:00
|
|
|
|
2019-12-20 21:31:27 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2020-04-22 14:16:10 +03:00
|
|
|
MOZ_CRASH("Library preload failure: Failed to get library file\n");
|
2019-12-20 21:31:27 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
nsAutoString fullPath;
|
|
|
|
rv = libFile->GetPath(fullPath);
|
2019-12-20 21:31:27 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2020-04-22 14:16:10 +03:00
|
|
|
MOZ_CRASH("Library preload failure: Failed to get library path\n");
|
2019-12-20 21:31:27 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
nsAutoCString converted_path = NS_ConvertUTF16toUTF8(fullPath);
|
2019-12-20 21:31:27 +03:00
|
|
|
return converted_path;
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:16:10 +03:00
|
|
|
nsAutoCString GetSandboxedGraphitePath() {
|
|
|
|
return GetSandboxedPath(
|
|
|
|
NS_LITERAL_CSTRING(MOZ_DLL_PREFIX "graphitewasm" MOZ_DLL_SUFFIX));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString GetSandboxedOggPath() {
|
|
|
|
return GetSandboxedPath(
|
|
|
|
NS_LITERAL_CSTRING(MOZ_DLL_PREFIX "oggwasm" MOZ_DLL_SUFFIX));
|
|
|
|
}
|
|
|
|
|
|
|
|
PRLibrary* PreloadLibrary(const nsAutoCString& path) {
|
|
|
|
PRLibSpec libSpec;
|
|
|
|
libSpec.type = PR_LibSpec_Pathname;
|
|
|
|
libSpec.value.pathname = path.get();
|
|
|
|
PRLibrary* ret = PR_LoadLibraryWithFlags(libSpec, PR_LD_LAZY);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:31:27 +03:00
|
|
|
void PreloadSandboxedDynamicLibraries() {
|
2020-02-01 13:38:29 +03:00
|
|
|
// The process level sandbox does not allow loading of dynamic libraries.
|
|
|
|
// This preloads wasm sandboxed libraries before the process level sandbox is
|
|
|
|
// enabled. Currently, this is only needed for Linux as Mac allows loading
|
|
|
|
// libraries from the package file.
|
2020-04-22 14:16:10 +03:00
|
|
|
#if defined(XP_LINUX)
|
|
|
|
# if defined(MOZ_WASM_SANDBOXING_GRAPHITE)
|
|
|
|
if (!PreloadLibrary(GetSandboxedGraphitePath())) {
|
2019-12-20 21:31:27 +03:00
|
|
|
MOZ_CRASH("Library preload failure: Failed to load libgraphite\n");
|
|
|
|
}
|
2020-04-22 14:16:10 +03:00
|
|
|
# endif
|
|
|
|
# if defined(MOZ_WASM_SANDBOXING_OGG)
|
|
|
|
if (!PreloadLibrary(GetSandboxedOggPath())) {
|
|
|
|
MOZ_CRASH("Library preload failure: Failed to load libogg\n");
|
|
|
|
}
|
|
|
|
# endif
|
2019-12-20 21:31:27 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|