Skip to content

src: prepare for v8 sandboxing #58376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented May 18, 2025

When the v8 sandbox is enabled (possibly at some point in the future) all ArrayBuffer allocations must be done within the isolate. Externally allocated backing stores are not permitted. This starts to prepare for that by adding some compile time branches to copy some external buffers rather than wrapping them. We also disable the --secure-heap feature since that fundamentally doesn't make sense and can't be supported with v8 sandbox enabled.

This PR cannot yet include additional tests for the new branches largely because we cannot yet build node with v8 sandbox enabled.

@jasnell jasnell requested review from anonrig, targos and joyeecheung May 18, 2025 17:52
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/config
  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels May 18, 2025
@jasnell jasnell force-pushed the jasnell/some-v8-sandbox-preparations branch from d21804d to 474cba4 Compare May 18, 2025 17:54
@nodejs-github-bot

This comment was marked as outdated.

@panva panva requested a review from tniessen May 18, 2025 18:33

This comment was marked as outdated.

@jasnell jasnell force-pushed the jasnell/some-v8-sandbox-preparations branch from 474cba4 to fd4f0e7 Compare May 18, 2025 18:56
@nodejs-github-bot

This comment was marked as outdated.

std::unique_ptr<BackingStore> ptr = ArrayBuffer::NewBackingStore(
allocated_data_,
size(),
[](void* data, size_t length, void* deleter_data) {
OPENSSL_clear_free(deleter_data, length);
}, allocated_data_);
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it would be a good idea to add a helper to e.g. util.h that switches between the two variants in a single location rather than having #ifdefs for every call, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely yes. I'm wanting to minimize the individual changes in this PR so we can make sure they are correct, but then we can likely extract out some utilities (maybe in node_buffer.h) that handle this in a more general way separately. That can be done separately I think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a TODO

@jasnell jasnell force-pushed the jasnell/some-v8-sandbox-preparations branch from fd4f0e7 to d9af9b2 Compare May 19, 2025 00:09
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@jasnell jasnell added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 19, 2025
@nodejs-github-bot
Copy link
Collaborator

Comment on lines +668 to +676
// The v8 sandbox is enabled, so we cannot use the secure heap because
// the sandbox requires that all array buffers be allocated via the isolate.
// That is fundamentally incompatible with the secure heap which allocates
// in openssl's secure heap area. Instead we'll just throw an error here.
//
// That said, we really shouldn't get here in the first place since the
// option to enable the secure heap is only available when the sandbox
// is disabled.
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be

Suggested change
// The v8 sandbox is enabled, so we cannot use the secure heap because
// the sandbox requires that all array buffers be allocated via the isolate.
// That is fundamentally incompatible with the secure heap which allocates
// in openssl's secure heap area. Instead we'll just throw an error here.
//
// That said, we really shouldn't get here in the first place since the
// option to enable the secure heap is only available when the sandbox
// is disabled.
THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(env);
// Secure heap is only available when the sandbox is disabled.
UNREACHABLE();

auto backing = ArrayBuffer::NewBackingStore(
env->isolate(),
data.size(),
BackingStoreInitializationMode::kUninitialized);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these use the default BackingStoreOnFailureMode::kOutOfMemory? I think previously the caller would throw catchable errors instead of memory allocation on the OpenSSL side fails. Now this would just crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants