Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion obs-studio-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ if(WIN32)
"${PROJECT_BINARY_DIR}/version.rc"
"${PROJECT_SOURCE_DIR}/source/cef-sandbox-host.cpp"
"${PROJECT_SOURCE_DIR}/source/cef-sandbox-host.hpp"
"${PROJECT_SOURCE_DIR}/source/token-label-policy.cpp"
"${PROJECT_SOURCE_DIR}/source/token-label-policy.hpp"
)

endif()
Expand Down Expand Up @@ -691,7 +693,13 @@ if(BUILD_TESTING)
osn_target_uses_static_msvc_runtime(obs_studio_server_unit_tests)

if(WIN32)
target_sources(obs_studio_server_unit_tests PRIVATE "tests/test-cef-subprocess.cpp")
target_sources(
obs_studio_server_unit_tests
PRIVATE
"tests/test-cef-subprocess.cpp"
"tests/test-token-label-policy.cpp"
"${PROJECT_SOURCE_DIR}/source/token-label-policy.cpp"
)
endif()

if(APPLE)
Expand Down
51 changes: 49 additions & 2 deletions obs-studio-server/source/cef-sandbox-host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#ifdef _WIN32

#include "cef-subprocess.hpp"
#include "token-label-policy.hpp"

#define OBS_BROWSER_SANDBOX_HOST_IMPLEMENTATION
#include <obs-browser-sandbox.h>
#undef OBS_BROWSER_SANDBOX_HOST_IMPLEMENTATION
#include <include/cef_sandbox_win.h>

#include <util/base.h>

#include <ShlObj.h>
#include <Shellapi.h>
#include <Windows.h>
Expand Down Expand Up @@ -70,9 +73,53 @@ extern "C" __declspec(dllexport) uint32_t __cdecl obs_browser_sandbox_abi_versio
return OBS_BROWSER_SANDBOX_ABI_VERSION;
}

namespace osn::cef {

namespace {

bool IsProcessElevated(HANDLE token)
{
TOKEN_ELEVATION elevation = {};
DWORD size = 0;
return GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size) && elevation.TokenIsElevated;
}

} // namespace

} // namespace osn::cef

extern "C" __declspec(dllexport) void *__cdecl obs_browser_sandbox_info_create(void)
{
return cef_sandbox_info_create();
HANDLE token = nullptr;
std::optional<osn::cef::TokenLabelPolicy> original_policy;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | READ_CONTROL | WRITE_OWNER, &token)) {
if (osn::cef::IsProcessElevated(token))
original_policy = osn::cef::CaptureTokenLabelPolicy(token);
} else {
blog(LOG_WARNING, "[cef-sandbox] OpenProcessToken failed with Windows error %lu", GetLastError());
}

void *sandbox_info = cef_sandbox_info_create();

if (original_policy) {
const auto current_policy = osn::cef::CaptureTokenLabelPolicy(token);
if (current_policy && (current_policy->mask & SYSTEM_MANDATORY_LABEL_NO_READ_UP) &&
!(original_policy->mask & SYSTEM_MANDATORY_LABEL_NO_READ_UP)) {
// CEF's broker hardening adds NO_READ_UP to our token label; at High integrity that denies Windows Graphics Capture.
osn::cef::TokenLabelPolicy updated_policy = *current_policy;
updated_policy.mask &= ~static_cast<DWORD>(SYSTEM_MANDATORY_LABEL_NO_READ_UP);
if (osn::cef::SetTokenLabelPolicy(token, updated_policy))
blog(LOG_INFO, "[cef-sandbox] Cleared NO_READ_UP from token integrity label policy (0x%lx -> 0x%lx)", current_policy->mask,
updated_policy.mask);
else
blog(LOG_WARNING, "[cef-sandbox] Failed to clear NO_READ_UP from token integrity label policy, Windows error %lu",
GetLastError());
}
}

if (token)
CloseHandle(token);
return sandbox_info;
}

extern "C" __declspec(dllexport) void __cdecl obs_browser_sandbox_info_destroy(void *sandbox_info)
Expand Down Expand Up @@ -126,7 +173,7 @@ bool GetExecutablePath(std::filesystem::path &result)

int ExecuteSubprocess()
{
void *sandbox_info = obs_browser_sandbox_info_create();
void *sandbox_info = cef_sandbox_info_create();
if (!sandbox_info) {
std::cerr << "cef_sandbox_info_create returned null" << std::endl;
return dispatch_failure;
Expand Down
59 changes: 59 additions & 0 deletions obs-studio-server/source/token-label-policy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "token-label-policy.hpp"

#ifdef _WIN32

#include <aclapi.h>

namespace osn::cef {

std::optional<TokenLabelPolicy> CaptureTokenLabelPolicy(HANDLE token)
{
PACL sacl = nullptr;
PSECURITY_DESCRIPTOR descriptor = nullptr;
if (GetSecurityInfo(token, SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, nullptr, nullptr, nullptr, &sacl, &descriptor) != ERROR_SUCCESS)
return std::nullopt;

std::optional<TokenLabelPolicy> policy;
for (DWORD index = 0; sacl && index < sacl->AceCount && !policy; ++index) {
void *ace = nullptr;
if (!GetAce(sacl, index, &ace))
break;

if (static_cast<ACE_HEADER *>(ace)->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE)
continue;

const auto label = static_cast<SYSTEM_MANDATORY_LABEL_ACE *>(ace);
const PSID sid = reinterpret_cast<PSID>(&label->SidStart);
const BYTE *sid_bytes = static_cast<const BYTE *>(sid);
policy = TokenLabelPolicy{std::vector<BYTE>(sid_bytes, sid_bytes + GetLengthSid(sid)), label->Mask};
}

LocalFree(descriptor);
return policy;
}

bool SetTokenLabelPolicy(HANDLE token, const TokenLabelPolicy &policy)
{
if (policy.sid.empty())
return false;

const PSID sid = const_cast<BYTE *>(policy.sid.data());
std::vector<BYTE> buffer(sizeof(ACL) + sizeof(SYSTEM_MANDATORY_LABEL_ACE) + GetLengthSid(sid));
const PACL sacl = reinterpret_cast<PACL>(buffer.data());
if (!InitializeAcl(sacl, static_cast<DWORD>(buffer.size()), ACL_REVISION))
return false;
if (!AddMandatoryAce(sacl, ACL_REVISION, 0, policy.mask, sid))
return false;

// SetSecurityInfo returns the error code without setting last-error.
const DWORD status = SetSecurityInfo(token, SE_KERNEL_OBJECT, LABEL_SECURITY_INFORMATION, nullptr, nullptr, nullptr, sacl);
if (status != ERROR_SUCCESS) {
SetLastError(status);
return false;
}
return true;
}

} // namespace osn::cef

#endif
22 changes: 22 additions & 0 deletions obs-studio-server/source/token-label-policy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#ifdef _WIN32

#include <optional>
#include <vector>

#include <Windows.h>

namespace osn::cef {

struct TokenLabelPolicy {
std::vector<BYTE> sid;
DWORD mask = 0;
};

std::optional<TokenLabelPolicy> CaptureTokenLabelPolicy(HANDLE token);
bool SetTokenLabelPolicy(HANDLE token, const TokenLabelPolicy &policy);

} // namespace osn::cef

#endif
88 changes: 88 additions & 0 deletions obs-studio-server/tests/test-token-label-policy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "token-label-policy.hpp"

#include <catch2/catch_test_macros.hpp>

namespace {

class ScopedDuplicatedToken {
public:
explicit ScopedDuplicatedToken(DWORD access = MAXIMUM_ALLOWED)
{
HANDLE process_token = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &process_token))
return;

DuplicateTokenEx(process_token, access, nullptr, SecurityImpersonation, TokenImpersonation, &token);
CloseHandle(process_token);
}

~ScopedDuplicatedToken()
{
if (token)
CloseHandle(token);
}

HANDLE token = nullptr;
};

osn::cef::TokenLabelPolicy WithMask(osn::cef::TokenLabelPolicy policy, DWORD mask)
{
policy.mask = mask;
return policy;
}

} // namespace

TEST_CASE("Clearing NO_READ_UP leaves the other label bits and the SID intact", "[cef-sandbox]")
{
ScopedDuplicatedToken duplicated;
REQUIRE(duplicated.token != nullptr);

const auto original = osn::cef::CaptureTokenLabelPolicy(duplicated.token);
REQUIRE(original.has_value());
REQUIRE_FALSE(original->sid.empty());

constexpr DWORD hardened_mask = SYSTEM_MANDATORY_LABEL_NO_WRITE_UP | SYSTEM_MANDATORY_LABEL_NO_READ_UP | SYSTEM_MANDATORY_LABEL_NO_EXECUTE_UP;
REQUIRE(osn::cef::SetTokenLabelPolicy(duplicated.token, WithMask(*original, hardened_mask)));

const auto hardened = osn::cef::CaptureTokenLabelPolicy(duplicated.token);
REQUIRE(hardened.has_value());
CHECK(hardened->mask == hardened_mask);

REQUIRE(osn::cef::SetTokenLabelPolicy(duplicated.token, WithMask(*hardened, hardened->mask & ~static_cast<DWORD>(SYSTEM_MANDATORY_LABEL_NO_READ_UP))));

const auto cleared = osn::cef::CaptureTokenLabelPolicy(duplicated.token);
REQUIRE(cleared.has_value());
CHECK(cleared->mask == (SYSTEM_MANDATORY_LABEL_NO_WRITE_UP | SYSTEM_MANDATORY_LABEL_NO_EXECUTE_UP));
CHECK(cleared->sid == original->sid);

REQUIRE(osn::cef::SetTokenLabelPolicy(duplicated.token, *original));

const auto restored = osn::cef::CaptureTokenLabelPolicy(duplicated.token);
REQUIRE(restored.has_value());
CHECK(restored->mask == original->mask);
CHECK(restored->sid == original->sid);
}

TEST_CASE("Token label policy set rejects an empty policy", "[cef-sandbox]")
{
ScopedDuplicatedToken duplicated;
REQUIRE(duplicated.token != nullptr);

CHECK_FALSE(osn::cef::SetTokenLabelPolicy(duplicated.token, osn::cef::TokenLabelPolicy{}));
}

TEST_CASE("Token label policy set reports the failure through last-error", "[cef-sandbox]")
{
ScopedDuplicatedToken writable;
REQUIRE(writable.token != nullptr);
const auto original = osn::cef::CaptureTokenLabelPolicy(writable.token);
REQUIRE(original.has_value());

ScopedDuplicatedToken read_only(TOKEN_QUERY | READ_CONTROL);
REQUIRE(read_only.token != nullptr);

SetLastError(ERROR_SUCCESS);
CHECK_FALSE(osn::cef::SetTokenLabelPolicy(read_only.token, *original));
CHECK(GetLastError() == ERROR_ACCESS_DENIED);
}
Loading