2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: sw=2 ts=4 et :
|
2014-11-14 11:26:24 +03:00
|
|
|
* 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 "GMPLoader.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "gmp-entrypoints.h"
|
|
|
|
#include "prlink.h"
|
2015-06-10 05:42:10 +03:00
|
|
|
#include "prenv.h"
|
2017-01-19 05:39:03 +03:00
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
# include "mozilla/sandboxTarget.h"
|
|
|
|
# include "mozilla/sandboxing/SandboxInitialization.h"
|
|
|
|
# include "mozilla/sandboxing/sandboxLogging.h"
|
|
|
|
#endif
|
2019-03-19 01:31:59 +03:00
|
|
|
#if defined(XP_LINUX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:39:03 +03:00
|
|
|
# include "mozilla/Sandbox.h"
|
|
|
|
# include "mozilla/SandboxInfo.h"
|
|
|
|
#endif
|
2014-11-14 11:26:24 +03:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-07-29 13:27:07 +03:00
|
|
|
#ifdef XP_WIN
|
2014-11-14 11:39:24 +03:00
|
|
|
# include "windows.h"
|
2015-10-29 18:27:00 +03:00
|
|
|
#endif
|
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
2016-04-12 07:12:20 +03:00
|
|
|
class PassThroughGMPAdapter : public GMPAdapter {
|
|
|
|
public:
|
2016-11-15 14:09:07 +03:00
|
|
|
~PassThroughGMPAdapter() override {
|
2016-04-12 07:12:20 +03:00
|
|
|
// Ensure we're always shutdown, even if caller forgets to call
|
|
|
|
// GMPShutdown().
|
|
|
|
GMPShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAdaptee(PRLibrary* aLib) override { mLib = aLib; }
|
|
|
|
|
|
|
|
GMPErr GMPInit(const GMPPlatformAPI* aPlatformAPI) override {
|
|
|
|
if (!mLib) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
GMPInitFunc initFunc =
|
|
|
|
reinterpret_cast<GMPInitFunc>(PR_FindFunctionSymbol(mLib, "GMPInit"));
|
|
|
|
if (!initFunc) {
|
|
|
|
return GMPNotImplementedErr;
|
|
|
|
}
|
|
|
|
return initFunc(aPlatformAPI);
|
|
|
|
}
|
|
|
|
|
2016-11-11 04:55:56 +03:00
|
|
|
GMPErr GMPGetAPI(const char* aAPIName, void* aHostAPI, void** aPluginAPI,
|
|
|
|
uint32_t aDecryptorId) override {
|
2016-04-12 07:12:20 +03:00
|
|
|
if (!mLib) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
GMPGetAPIFunc getapiFunc = reinterpret_cast<GMPGetAPIFunc>(
|
|
|
|
PR_FindFunctionSymbol(mLib, "GMPGetAPI"));
|
|
|
|
if (!getapiFunc) {
|
|
|
|
return GMPNotImplementedErr;
|
|
|
|
}
|
|
|
|
return getapiFunc(aAPIName, aHostAPI, aPluginAPI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GMPShutdown() override {
|
|
|
|
if (mLib) {
|
|
|
|
GMPShutdownFunc shutdownFunc = reinterpret_cast<GMPShutdownFunc>(
|
|
|
|
PR_FindFunctionSymbol(mLib, "GMPShutdown"));
|
|
|
|
if (shutdownFunc) {
|
|
|
|
shutdownFunc();
|
|
|
|
}
|
|
|
|
PR_UnloadLibrary(mLib);
|
|
|
|
mLib = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRLibrary* mLib = nullptr;
|
|
|
|
};
|
|
|
|
|
2017-01-19 05:44:54 +03:00
|
|
|
bool GMPLoader::Load(const char* aUTF8LibPath, uint32_t aUTF8LibPathLen,
|
|
|
|
const GMPPlatformAPI* aPlatformAPI, GMPAdapter* aAdapter) {
|
2018-01-25 03:11:21 +03:00
|
|
|
if (!getenv("MOZ_DISABLE_GMP_SANDBOX") && mSandboxStarter &&
|
|
|
|
!mSandboxStarter->Start(aUTF8LibPath)) {
|
2015-10-21 10:46:57 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the GMP.
|
|
|
|
PRLibSpec libSpec;
|
2015-07-29 13:27:07 +03:00
|
|
|
#ifdef XP_WIN
|
2015-07-17 02:09:49 +03:00
|
|
|
int pathLen = MultiByteToWideChar(CP_UTF8, 0, aUTF8LibPath, -1, nullptr, 0);
|
2015-01-26 13:14:39 +03:00
|
|
|
if (pathLen == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
auto widePath = MakeUnique<wchar_t[]>(pathLen);
|
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, aUTF8LibPath, -1, widePath.get(),
|
|
|
|
pathLen) == 0) {
|
2015-01-26 13:14:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
libSpec.value.pathname_u = widePath.get();
|
2015-07-17 02:09:49 +03:00
|
|
|
libSpec.type = PR_LibSpec_PathnameU;
|
|
|
|
#else
|
|
|
|
libSpec.value.pathname = aUTF8LibPath;
|
2014-11-14 11:26:24 +03:00
|
|
|
libSpec.type = PR_LibSpec_Pathname;
|
2015-07-17 02:09:49 +03:00
|
|
|
#endif
|
2016-04-12 07:12:20 +03:00
|
|
|
PRLibrary* lib = PR_LoadLibraryWithFlags(libSpec, 0);
|
|
|
|
if (!lib) {
|
2014-11-14 11:26:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:20 +03:00
|
|
|
mAdapter.reset((!aAdapter) ? new PassThroughGMPAdapter() : aAdapter);
|
|
|
|
mAdapter->SetAdaptee(lib);
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2016-05-06 05:49:12 +03:00
|
|
|
if (mAdapter->GMPInit(aPlatformAPI) != GMPNoErr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:44:54 +03:00
|
|
|
GMPErr GMPLoader::GetAPI(const char* aAPIName, void* aHostAPI,
|
|
|
|
void** aPluginAPI, uint32_t aDecryptorId) {
|
2016-11-11 04:55:56 +03:00
|
|
|
return mAdapter->GMPGetAPI(aAPIName, aHostAPI, aPluginAPI, aDecryptorId);
|
2014-11-14 11:26:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:44:54 +03:00
|
|
|
void GMPLoader::Shutdown() {
|
2016-04-12 07:12:20 +03:00
|
|
|
if (mAdapter) {
|
|
|
|
mAdapter->GMPShutdown();
|
2014-11-14 11:26:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:31:59 +03:00
|
|
|
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:44:54 +03:00
|
|
|
void GMPLoader::SetSandboxInfo(MacSandboxInfo* aSandboxInfo) {
|
2015-04-03 19:51:41 +03:00
|
|
|
if (mSandboxStarter) {
|
|
|
|
mSandboxStarter->SetSandboxInfo(aSandboxInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-19 05:39:03 +03:00
|
|
|
|
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
class WinSandboxStarter : public mozilla::gmp::SandboxStarter {
|
|
|
|
public:
|
|
|
|
bool Start(const char* aLibPath) override {
|
2017-06-29 01:19:30 +03:00
|
|
|
// Cause advapi32 to load before the sandbox is turned on, as
|
|
|
|
// Widevine version 970 and later require it and the sandbox
|
|
|
|
// blocks it on Win7.
|
|
|
|
unsigned int dummy_rand;
|
|
|
|
rand_s(&dummy_rand);
|
|
|
|
|
2017-01-19 05:39:03 +03:00
|
|
|
mozilla::SandboxTarget::Instance()->StartSandbox();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 01:31:59 +03:00
|
|
|
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:39:03 +03:00
|
|
|
class MacSandboxStarter : public mozilla::gmp::SandboxStarter {
|
|
|
|
public:
|
|
|
|
bool Start(const char* aLibPath) override {
|
|
|
|
std::string err;
|
|
|
|
bool rv = mozilla::StartMacSandbox(mInfo, err);
|
|
|
|
if (!rv) {
|
|
|
|
fprintf(stderr, "sandbox_init() failed! Error \"%s\"\n", err.c_str());
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
void SetSandboxInfo(MacSandboxInfo* aSandboxInfo) override {
|
|
|
|
mInfo = *aSandboxInfo;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-01-19 05:39:03 +03:00
|
|
|
private:
|
|
|
|
MacSandboxInfo mInfo;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 01:31:59 +03:00
|
|
|
#if defined(XP_LINUX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:39:03 +03:00
|
|
|
namespace {
|
|
|
|
class LinuxSandboxStarter : public mozilla::gmp::SandboxStarter {
|
|
|
|
private:
|
|
|
|
LinuxSandboxStarter() {}
|
|
|
|
friend mozilla::detail::UniqueSelector<LinuxSandboxStarter>::SingleObject
|
|
|
|
mozilla::MakeUnique<LinuxSandboxStarter>();
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-01-19 05:39:03 +03:00
|
|
|
public:
|
|
|
|
static UniquePtr<SandboxStarter> Make() {
|
|
|
|
if (mozilla::SandboxInfo::Get().CanSandboxMedia()) {
|
|
|
|
return MakeUnique<LinuxSandboxStarter>();
|
|
|
|
}
|
2017-02-09 12:31:55 +03:00
|
|
|
// Sandboxing isn't possible, but the parent has already
|
|
|
|
// checked that this plugin doesn't require it. (Bug 1074561)
|
2017-01-19 05:39:03 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
bool Start(const char* aLibPath) override {
|
|
|
|
mozilla::SetMediaPluginSandbox(aLibPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
2019-03-19 01:31:59 +03:00
|
|
|
#endif // XP_LINUX && MOZ_SANDBOX
|
2017-01-19 05:39:03 +03:00
|
|
|
|
|
|
|
static UniquePtr<SandboxStarter> MakeSandboxStarter() {
|
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
return mozilla::MakeUnique<WinSandboxStarter>();
|
2019-03-19 01:31:59 +03:00
|
|
|
#elif defined(XP_MACOSX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:39:03 +03:00
|
|
|
return mozilla::MakeUnique<MacSandboxStarter>();
|
2019-03-19 01:31:59 +03:00
|
|
|
#elif defined(XP_LINUX) && defined(MOZ_SANDBOX)
|
2017-01-19 05:39:03 +03:00
|
|
|
return LinuxSandboxStarter::Make();
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:44:54 +03:00
|
|
|
GMPLoader::GMPLoader() : mSandboxStarter(MakeSandboxStarter()) {}
|
|
|
|
|
|
|
|
bool GMPLoader::CanSandbox() const { return !!mSandboxStarter; }
|
2017-01-19 05:39:03 +03:00
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|