-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: sandboxing for unified exec #4995
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
jif-oai
wants to merge
17
commits into
main
Choose a base branch
from
jif/sandbox-unified-exec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3aadc93
feat: sandboxing for unified exec
jif-oai 29a2305
V2
jif-oai b47d54d
V3
jif-oai 30ae983
Fix 1
jif-oai f5c626b
Fix 2
jif-oai b3cf2a4
Fix 3
jif-oai ecac194
Merge branch 'main' into jif/sandbox-unified-exec
jif-oai f61c7aa
RV1
jif-oai 703c382
RV2
jif-oai 1e2f6f2
RV3
jif-oai 53f07c3
RV4
jif-oai b622a1c
RV5
jif-oai a55fc6a
RV6
jif-oai 6755b3b
Merge remote-tracking branch 'origin/main' into jif/sandbox-unified-exec
jif-oai 9865a0c
RV7
jif-oai b7e834a
Fix tests
jif-oai 65be622
Fix tests 2
jif-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,99 @@ use crate::executor::ExecutionMode; | |
use crate::executor::ExecutionRequest; | ||
use crate::executor::ExecutorConfig; | ||
use crate::executor::errors::ExecError; | ||
use crate::landlock::create_linux_sandbox_command_args; | ||
use crate::protocol::SandboxPolicy; | ||
use crate::safety::SafetyCheck; | ||
use crate::safety::assess_command_safety; | ||
use crate::safety::assess_patch_safety; | ||
use crate::seatbelt::MACOS_PATH_TO_SEATBELT_EXECUTABLE; | ||
use crate::seatbelt::create_seatbelt_command_args; | ||
use crate::spawn::CODEX_SANDBOX_ENV_VAR; | ||
use crate::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR; | ||
use codex_otel::otel_event_manager::OtelEventManager; | ||
use codex_otel::otel_event_manager::ToolDecisionSource; | ||
use codex_protocol::protocol::AskForApproval; | ||
use codex_protocol::protocol::ReviewDecision; | ||
use std::collections::HashMap; | ||
use std::collections::HashSet; | ||
use std::path::Path; | ||
use std::path::PathBuf; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct SandboxLaunch { | ||
pub program: String, | ||
pub args: Vec<String>, | ||
pub env: HashMap<String, String>, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub(crate) enum SandboxLaunchError { | ||
#[error("missing command line for sandbox launch")] | ||
MissingCommandLine, | ||
#[error("missing codex-linux-sandbox executable path")] | ||
MissingLinuxSandboxExecutable, | ||
} | ||
|
||
pub(crate) fn build_launch_for_sandbox( | ||
sandbox: SandboxType, | ||
command: &[String], | ||
sandbox_policy: &SandboxPolicy, | ||
sandbox_policy_cwd: &Path, | ||
codex_linux_sandbox_exe: Option<&PathBuf>, | ||
) -> Result<SandboxLaunch, SandboxLaunchError> { | ||
let mut env = HashMap::new(); | ||
if !sandbox_policy.has_full_network_access() { | ||
env.insert( | ||
CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR.to_string(), | ||
"1".to_string(), | ||
); | ||
} | ||
|
||
match sandbox { | ||
SandboxType::None => { | ||
let (program, args) = command | ||
.split_first() | ||
.ok_or(SandboxLaunchError::MissingCommandLine)?; | ||
Ok(SandboxLaunch { | ||
program: program.clone(), | ||
args: args.to_vec(), | ||
env, | ||
}) | ||
} | ||
SandboxType::MacosSeatbelt => { | ||
env.insert(CODEX_SANDBOX_ENV_VAR.to_string(), "seatbelt".to_string()); | ||
let args = | ||
create_seatbelt_command_args(command.to_vec(), sandbox_policy, sandbox_policy_cwd); | ||
Ok(SandboxLaunch { | ||
program: MACOS_PATH_TO_SEATBELT_EXECUTABLE.to_string(), | ||
args, | ||
env, | ||
}) | ||
} | ||
SandboxType::LinuxSeccomp => { | ||
let exe = | ||
codex_linux_sandbox_exe.ok_or(SandboxLaunchError::MissingLinuxSandboxExecutable)?; | ||
let args = create_linux_sandbox_command_args( | ||
command.to_vec(), | ||
sandbox_policy, | ||
sandbox_policy_cwd, | ||
); | ||
Ok(SandboxLaunch { | ||
program: exe.to_string_lossy().to_string(), | ||
args, | ||
env, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct RetrySandboxContext<'a> { | ||
pub sub_id: &'a str, | ||
pub call_id: &'a str, | ||
pub tool_name: &'a str, | ||
pub otel_event_manager: &'a OtelEventManager, | ||
} | ||
|
||
/// Sandbox placement options selected for an execution run, including whether | ||
/// to escalate after failures and whether approvals should persist. | ||
|
@@ -50,6 +135,53 @@ fn should_escalate_on_failure(approval: AskForApproval, sandbox: SandboxType) -> | |
) | ||
} | ||
|
||
pub(crate) async fn request_retry_without_sandbox( | ||
session: &Session, | ||
failure_message: impl Into<String>, | ||
command: &[String], | ||
cwd: PathBuf, | ||
ctx: RetrySandboxContext<'_>, | ||
) -> Option<ReviewDecision> { | ||
session | ||
.notify_background_event(ctx.sub_id, failure_message.into()) | ||
.await; | ||
|
||
let approval_command = command.to_vec(); | ||
let decision = session | ||
.request_command_approval( | ||
ctx.sub_id.to_string(), | ||
ctx.call_id.to_string(), | ||
approval_command.clone(), | ||
cwd, | ||
Some("command failed; retry without sandbox?".to_string()), | ||
) | ||
.await; | ||
|
||
ctx.otel_event_manager.tool_decision( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we push this log ionto |
||
ctx.tool_name, | ||
ctx.call_id, | ||
decision, | ||
ToolDecisionSource::User, | ||
); | ||
|
||
match decision { | ||
ReviewDecision::Approved | ReviewDecision::ApprovedForSession => { | ||
if matches!(decision, ReviewDecision::ApprovedForSession) { | ||
session | ||
.services | ||
.executor | ||
.record_session_approval(approval_command); | ||
} | ||
|
||
session | ||
.notify_background_event(ctx.sub_id, "retrying command without sandbox") | ||
.await; | ||
Some(decision) | ||
} | ||
ReviewDecision::Denied | ReviewDecision::Abort => None, | ||
} | ||
} | ||
|
||
/// Determines how a command should be sandboxed, prompting the user when | ||
/// policy requires explicit approval. | ||
#[allow(clippy::too_many_arguments)] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we put this method on the main path of all exect commands. I really don't like that we are forking an already complicated logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in remove spawn_command_under_* and send all invocations via build_launch_for_sandbox + spawn?