Skip to content

Commit

Permalink
move global variables into per_process namespace. fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rdw-msft committed Jan 23, 2025
1 parent 37b7adc commit 7560a46
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/code_integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ let alreadyQueriedSystemCodeEnforcmentMode = false;
// Binding stub for non-Windows platforms
let binding = {
isFileTrustedBySystemCodeIntegrityPolicy: () => true,
isInteractiveModeDisabledInternal:() => false,
isInteractiveModeDisabledInternal: () => false,
isSystemEnforcingCodeIntegrity: () => false,
}
};
// Load the actual binding if on Windows
if (isWindows) {
binding = internalBinding('code_integrity');
Expand Down
45 changes: 24 additions & 21 deletions src/node_code_integrity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ using v8::Local;
using v8::Object;
using v8::Value;

namespace per_process {
bool isWldpInitialized = false;
pfnWldpCanExecuteFile WldpCanExecuteFile;
pfnWldpGetApplicationSettingBoolean WldpGetApplicationSettingBoolean;
pfnWldpQuerySecurityPolicy WldpQuerySecurityPolicy;
}

namespace codeintegrity {

static bool isWldpInitialized = false;
static pfnWldpCanExecuteFile WldpCanExecuteFile;
static pfnWldpGetApplicationSettingBoolean WldpGetApplicationSettingBoolean;
static pfnWldpQuerySecurityPolicy WldpQuerySecurityPolicy;
static PCWSTR NODEJS = L"Node.js";
static PCWSTR ENFORCE_CODE_INTEGRITY_SETTING_NAME = L"EnforceCodeIntegrity";
static PCWSTR DISABLE_INTERPRETIVE_MODE_SETTING_NAME =
L"DisableInteractiveMode";

void InitWldp(Environment* env) {
if (isWldpInitialized) {
if (per_process::isWldpInitialized) {
return;
}

Expand All @@ -38,22 +41,22 @@ void InitWldp(Environment* env) {
return env->ThrowError("Unable to load wldp.dll");
}

WldpCanExecuteFile =
per_process::WldpCanExecuteFile =
(pfnWldpCanExecuteFile)GetProcAddress(
wldp_module,
"WldpCanExecuteFile");

WldpGetApplicationSettingBoolean =
per_process::WldpGetApplicationSettingBoolean =
(pfnWldpGetApplicationSettingBoolean)GetProcAddress(
wldp_module,
"WldpGetApplicationSettingBoolean");

WldpQuerySecurityPolicy =
per_process::WldpQuerySecurityPolicy =
(pfnWldpQuerySecurityPolicy)GetProcAddress(
wldp_module,
"WldpQuerySecurityPolicy");

isWldpInitialized = true;
per_process::isWldpInitialized = true;
}

static void IsFileTrustedBySystemCodeIntegrityPolicy(
Expand All @@ -62,7 +65,7 @@ static void IsFileTrustedBySystemCodeIntegrityPolicy(
CHECK(args[0]->IsString());

Environment* env = Environment::GetCurrent(args);
if (!isWldpInitialized) {
if (!per_process::isWldpInitialized) {
InitWldp(env);
}

Expand All @@ -86,7 +89,7 @@ static void IsFileTrustedBySystemCodeIntegrityPolicy(

const GUID wldp_host_other = WLDP_HOST_OTHER;
WLDP_EXECUTION_POLICY result;
HRESULT hr = WldpCanExecuteFile(
HRESULT hr = per_process::WldpCanExecuteFile(
wldp_host_other,
WLDP_EXECUTION_EVALUATION_OPTION_NONE,
hFile,
Expand All @@ -108,13 +111,13 @@ static void IsInteractiveModeDisabledInternal(

Environment* env = Environment::GetCurrent(args);

if (!isWldpInitialized) {
if (!per_process::isWldpInitialized) {
InitWldp(env);
}

if (WldpGetApplicationSettingBoolean != nullptr) {
if (per_process::WldpGetApplicationSettingBoolean != nullptr) {
BOOL ret;
HRESULT hr = WldpGetApplicationSettingBoolean(
HRESULT hr = per_process::WldpGetApplicationSettingBoolean(
NODEJS,
DISABLE_INTERPRETIVE_MODE_SETTING_NAME,
&ret);
Expand All @@ -137,15 +140,15 @@ static void IsInteractiveModeDisabledInternal(
// versions going back to circa Win10 2023H2. In order to support systems
// older than that (down to Win10RS2), we can use the deprecated
// WldpQuerySecurityPolicy
if (WldpQuerySecurityPolicy != nullptr) {
if (per_process::WldpQuerySecurityPolicy != nullptr) {
DECLARE_CONST_UNICODE_STRING(providerName, L"Node.js");
DECLARE_CONST_UNICODE_STRING(keyName, L"Settings");
DECLARE_CONST_UNICODE_STRING(valueName, L"DisableInteractiveMode");
WLDP_SECURE_SETTING_VALUE_TYPE valueType =
WLDP_SECURE_SETTING_VALUE_TYPE_BOOLEAN;
ULONG valueSize = sizeof(int);
int ret = 0;
HRESULT hr = WldpQuerySecurityPolicy(
HRESULT hr = per_process::WldpQuerySecurityPolicy(
&providerName,
&keyName,
&valueName,
Expand All @@ -168,13 +171,13 @@ static void IsSystemEnforcingCodeIntegrity(

Environment* env = Environment::GetCurrent(args);

if (!isWldpInitialized) {
if (!per_process::isWldpInitialized) {
InitWldp(env);
}

if (WldpGetApplicationSettingBoolean != nullptr) {
if (per_process::WldpGetApplicationSettingBoolean != nullptr) {
BOOL ret;
HRESULT hr = WldpGetApplicationSettingBoolean(
HRESULT hr = per_process::WldpGetApplicationSettingBoolean(
NODEJS,
ENFORCE_CODE_INTEGRITY_SETTING_NAME,
&ret);
Expand All @@ -197,15 +200,15 @@ static void IsSystemEnforcingCodeIntegrity(
// versions going back to circa Win10 2023H2. In order to support systems
// older than that (down to Win10RS2), we can use the deprecated
// WldpQuerySecurityPolicy
if (WldpQuerySecurityPolicy != nullptr) {
if (per_process::WldpQuerySecurityPolicy != nullptr) {
DECLARE_CONST_UNICODE_STRING(providerName, L"Node.js");
DECLARE_CONST_UNICODE_STRING(keyName, L"Settings");
DECLARE_CONST_UNICODE_STRING(valueName, L"EnforceCodeIntegrity");
WLDP_SECURE_SETTING_VALUE_TYPE valueType =
WLDP_SECURE_SETTING_VALUE_TYPE_BOOLEAN;
ULONG valueSize = sizeof(int);
int ret = 0;
HRESULT hr = WldpQuerySecurityPolicy(
HRESULT hr = per_process::WldpQuerySecurityPolicy(
&providerName,
&keyName,
&valueName,
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-code-integrity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { describe, it } = require('node:test');
const ci = require('code_integrity');
Expand Down

0 comments on commit 7560a46

Please sign in to comment.