fix: prevent node mode to be used as script runner by other apps (#40579)
This commit is contained in:
parent
9aa73abe78
commit
cb0da6ff34
8 changed files with 301 additions and 88 deletions
|
@ -13,6 +13,7 @@
|
|||
#include "base/base_switches.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/containers/fixed_flat_set.h"
|
||||
#include "base/environment.h"
|
||||
#include "base/feature_list.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
|
@ -20,6 +21,7 @@
|
|||
#include "base/task/thread_pool/thread_pool_instance.h"
|
||||
#include "content/public/common/content_switches.h"
|
||||
#include "electron/electron_version.h"
|
||||
#include "electron/fuses.h"
|
||||
#include "gin/array_buffer.h"
|
||||
#include "gin/public/isolate_holder.h"
|
||||
#include "gin/v8_initializer.h"
|
||||
|
@ -35,13 +37,16 @@
|
|||
#endif
|
||||
|
||||
#if BUILDFLAG(IS_LINUX)
|
||||
#include "base/environment.h"
|
||||
#include "base/posix/global_descriptors.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "components/crash/core/app/crash_switches.h" // nogncheck
|
||||
#include "content/public/common/content_descriptors.h"
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
#include "shell/common/mac/codesign_util.h"
|
||||
#endif
|
||||
|
||||
#if !IS_MAS_BUILD()
|
||||
#include "components/crash/core/app/crashpad.h" // nogncheck
|
||||
#include "shell/app/electron_crash_reporter_client.h"
|
||||
|
@ -100,12 +105,36 @@ int NodeMain(int argc, char* argv[]) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
auto os_env = base::Environment::Create();
|
||||
bool node_options_enabled = electron::fuses::IsNodeOptionsEnabled();
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
if (node_options_enabled && os_env->HasVar("NODE_OPTIONS")) {
|
||||
// On macOS, it is forbidden to run sandboxed app with custom arguments
|
||||
// from another app, i.e. args are discarded in following call:
|
||||
// exec("Sandboxed.app", ["--custom-args-will-be-discarded"])
|
||||
// However it is possible to bypass the restriction by abusing the node mode
|
||||
// of Electron apps:
|
||||
// exec("Electron.app", {env: {ELECTRON_RUN_AS_NODE: "1",
|
||||
// NODE_OPTIONS: "--require 'bad.js'"}})
|
||||
// To prevent Electron apps from being used to work around macOS security
|
||||
// restrictions, when NODE_OPTIONS is passed it will be checked whether
|
||||
// this process is invoked by its own app.
|
||||
if (!ProcessBelongToCurrentApp(getppid())) {
|
||||
LOG(ERROR) << "NODE_OPTIONS is disabled because this process is invoked "
|
||||
"by other apps.";
|
||||
node_options_enabled = false;
|
||||
}
|
||||
}
|
||||
#endif // BUILDFLAG(IS_MAC)
|
||||
if (!node_options_enabled) {
|
||||
os_env->UnSetVar("NODE_OPTIONS");
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
v8_crashpad_support::SetUp();
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(IS_LINUX)
|
||||
auto os_env = base::Environment::Create();
|
||||
std::string fd_string, pid_string;
|
||||
if (os_env->GetVar("CRASHDUMP_SIGNAL_FD", &fd_string) &&
|
||||
os_env->GetVar("CRASHPAD_HANDLER_PID", &pid_string)) {
|
||||
|
|
55
shell/common/mac/codesign_util.cc
Normal file
55
shell/common/mac/codesign_util.cc
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Copyright 2023 Microsoft, Inc.
|
||||
// Copyright 2013 The Chromium Authors
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/mac/codesign_util.h"
|
||||
|
||||
#include "base/apple/osstatus_logging.h"
|
||||
#include "base/apple/scoped_cftyperef.h"
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <Security/Security.h>
|
||||
|
||||
namespace electron {
|
||||
|
||||
bool ProcessBelongToCurrentApp(pid_t pid) {
|
||||
// Get and check the code signature of current app.
|
||||
base::apple::ScopedCFTypeRef<SecCodeRef> self_code;
|
||||
OSStatus status =
|
||||
SecCodeCopySelf(kSecCSDefaultFlags, self_code.InitializeInto());
|
||||
if (status != errSecSuccess) {
|
||||
OSSTATUS_LOG(ERROR, status) << "SecCodeCopyGuestWithAttributes";
|
||||
return false;
|
||||
}
|
||||
// Get the code signature of process.
|
||||
base::apple::ScopedCFTypeRef<CFNumberRef> process_cf(
|
||||
CFNumberCreate(nullptr, kCFNumberIntType, &pid));
|
||||
const void* attribute_keys[] = {kSecGuestAttributePid};
|
||||
const void* attribute_values[] = {process_cf.get()};
|
||||
base::apple::ScopedCFTypeRef<CFDictionaryRef> attributes(CFDictionaryCreate(
|
||||
nullptr, attribute_keys, attribute_values, std::size(attribute_keys),
|
||||
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
|
||||
base::apple::ScopedCFTypeRef<SecCodeRef> process_code;
|
||||
status = SecCodeCopyGuestWithAttributes(nullptr, attributes.get(),
|
||||
kSecCSDefaultFlags,
|
||||
process_code.InitializeInto());
|
||||
if (status != errSecSuccess) {
|
||||
OSSTATUS_LOG(ERROR, status) << "SecCodeCopyGuestWithAttributes";
|
||||
return false;
|
||||
}
|
||||
// Get the requirement of current app's code signature.
|
||||
base::apple::ScopedCFTypeRef<SecRequirementRef> self_requirement;
|
||||
status = SecCodeCopyDesignatedRequirement(self_code.get(), kSecCSDefaultFlags,
|
||||
self_requirement.InitializeInto());
|
||||
if (status != errSecSuccess) {
|
||||
OSSTATUS_LOG(ERROR, status) << "SecCodeCopyDesignatedRequirement";
|
||||
return false;
|
||||
}
|
||||
// Check whether the process meets the signature requirement of current app.
|
||||
status = SecCodeCheckValidity(process_code.get(), kSecCSDefaultFlags,
|
||||
self_requirement.get());
|
||||
return status == errSecSuccess;
|
||||
}
|
||||
|
||||
} // namespace electron
|
19
shell/common/mac/codesign_util.h
Normal file
19
shell/common/mac/codesign_util.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2023 Microsoft, Inc.
|
||||
// Copyright 2013 The Chromium Authors
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_MAC_CODESIGN_UTIL_H_
|
||||
#define SHELL_COMMON_MAC_CODESIGN_UTIL_H_
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
namespace electron {
|
||||
|
||||
// Given a pid, check if the process belongs to current app by comparing its
|
||||
// code signature with current app.
|
||||
bool ProcessBelongToCurrentApp(pid_t pid);
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_COMMON_MAC_CODESIGN_UTIL_H_
|
Loading…
Add table
Add a link
Reference in a new issue