Skip to content
Closed
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

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions op-devstack/dsl/l2_el_proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ethereum-optimism/optimism/op-core/predeploys"
"github.com/ethereum-optimism/optimism/op-devstack/stack"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -107,6 +108,73 @@ func (el *L2ELNode) WaitForProofsStoreBlock(target uint64, timeout time.Duration
return lastStatus
}

// WaitForProofsStorePrunedPast waits until this node's proofs-history store no longer serves
// block, because pruning moved the store's earliest block past it. The store prunes to its
// window behind the latest block it indexed, so the chain has to be more than a window past
// block first.
func (el *L2ELNode) WaitForProofsStorePrunedPast(block uint64, timeout time.Duration) ProofsSyncStatus {
ctx, cancel := context.WithTimeout(el.ctx, timeout)
defer cancel()
logger := el.log.With("name", el.inner.Name(), "chain", el.ChainID(), "block", block, "timeout", timeout)
logger.Info("Waiting for proofs store to prune past block")

var lastStatus ProofsSyncStatus
var lastErr error
err := wait.For(ctx, 200*time.Millisecond, func() (bool, error) {
status, err := el.ProofsSyncStatus()
if err != nil {
lastErr = err
logger.Warn("debug_proofsSyncStatus failed; will retry", "err", err)
return false, nil
}
lastStatus, lastErr = status, nil
if status.Earliest != nil && *status.Earliest > block {
logger.Info("Proofs store pruned past block", "window", status)
return true, nil
}
logger.Info("Proofs store still serves block", "window", status)
return false, nil
})
el.require.NoErrorf(err, "proofs store on %s did not prune past block %d within %s; last window %s; last error: %v",
el.inner.Name(), block, timeout, lastStatus, lastErr)
return lastStatus
}

// InitProofsHistory seeds this node's proofs-history store from its datadir with
// `op-reth proofs init`, the way an operator does for a node that synced without proofs history,
// and makes the node run with proofs history from its next Start. It fails the test if the node
// is running or the init fails; InitProofsHistoryRaw does neither.
func (el *L2ELNode) InitProofsHistory(opts ...func(*stack.ProofsHistoryInit)) stack.ProofsHistoryInitReport {
if control, ok := el.inner.(stack.ControlledLifecycle); ok {
el.require.Falsef(control.Running(), "stop %s before seeding its proofs-history store", el.inner.Name())
}
report, err := el.InitProofsHistoryRaw(opts...)
el.require.NoErrorf(err, "proofs init on %s must seed the store", el.inner.Name())
return report
}

// InitProofsHistoryRaw runs `op-reth proofs init` against this node's datadir whether or not the
// node is running and returns the outcome, failure included, for the test to assert on. The
// options start from op-reth's defaults: a backfill through a trie-state snapshot and the node's
// configured window.
func (el *L2ELNode) InitProofsHistoryRaw(opts ...func(*stack.ProofsHistoryInit)) (stack.ProofsHistoryInitReport, error) {
initializer, ok := el.inner.(stack.ProofsHistoryInitializer)
el.require.Truef(ok, "L2EL node %s cannot seed a proofs-history store", el.inner.Name())
var init stack.ProofsHistoryInit
for _, opt := range opts {
opt(&init)
}
logger := el.log.With("name", el.inner.Name(), "chain", el.ChainID(), "plain_backfill", init.PlainBackfill)
logger.Info("Seeding the proofs-history store from the datadir")
report, err := initializer.InitProofsHistory(init)
if err != nil {
logger.Warn("Proofs init did not seed the store", "duration", report.Duration)
return report, err
}
logger.Info("Proofs init seeded the store", "duration", report.Duration)
return report, nil
}

// VerifyHistoricalProofsMatch sweeps the blocks this node's proofs-history store serves and, at
// every sampled block, requires eth_getProof for each configured account to verify against the
// block's state root and to equal the proof served by reference. The L2ToL1MessagePasser proof is
Expand Down
13 changes: 10 additions & 3 deletions op-devstack/presets/rpc_frontends.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ type l2ELFrontend struct {
control stack.ControlledLifecycle
ungraceful stack.UngracefulLifecycle
logRecorder stack.LogRecorder
proofsInit stack.ProofsHistoryInitializer
}

var (
_ stack.L2ELNode = (*l2ELFrontend)(nil)
_ stack.LogRecorder = (*l2ELFrontend)(nil)
_ stack.UngracefulLifecycle = (*l2ELFrontend)(nil)
_ stack.L2ELNode = (*l2ELFrontend)(nil)
_ stack.LogRecorder = (*l2ELFrontend)(nil)
_ stack.ProofsHistoryInitializer = (*l2ELFrontend)(nil)
_ stack.UngracefulLifecycle = (*l2ELFrontend)(nil)
)

func newPresetL2ELNode(t devtest.T, name string, chainID eth.ChainID, userRPCCl opclient.RPC, userRPC string, engineRPCCl opclient.RPC, rollupCfg *rollup.Config) *l2ELFrontend {
Expand Down Expand Up @@ -210,6 +212,11 @@ func (r *l2ELFrontend) RecordLogLines(substring string) *logpipe.LogRecord {
return r.logRecorder.RecordLogLines(substring)
}

func (r *l2ELFrontend) InitProofsHistory(init stack.ProofsHistoryInit) (stack.ProofsHistoryInitReport, error) {
r.require().NotNil(r.proofsInit, "L2EL node %s cannot seed a proofs-history store", r.Name())
return r.proofsInit.InitProofsHistory(init)
}

func (r *l2ELFrontend) StartControlled(ctx context.Context) error {
if r.control == nil {
return fmt.Errorf("L2EL node %s is not controllable", r.Name())
Expand Down
3 changes: 3 additions & 0 deletions op-devstack/presets/sysgo_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func newL2ELFrontend(t devtest.T, name string, chainID eth.ChainID, userRPC stri
if recorder, ok := lifecycle[0].(stack.LogRecorder); ok {
l2EL.logRecorder = recorder
}
if initializer, ok := lifecycle[0].(stack.ProofsHistoryInitializer); ok {
l2EL.proofsInit = initializer
}
}
return l2EL
}
Expand Down
34 changes: 34 additions & 0 deletions op-devstack/stack/proofs_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package stack

import "time"

// ProofsHistoryInit is one run of `op-reth proofs init` against a node's datadir: it anchors a
// proofs-history store at the datadir's tip, backfills the store Window blocks behind the anchor,
// and makes the node run with proofs history from its next Start on. The zero value is the
// operator's plain invocation.
type ProofsHistoryInit struct {
// PlainBackfill reads every backfilled block's changes from reth's database instead of going
// through the trie-state snapshot op-reth builds by default.
PlainBackfill bool
// Window is the number of blocks the backfill extends behind the anchor and the window the
// node keeps once proofs history is on. Zero keeps the window the node was configured with.
Window uint64
}

// ProofsHistoryInitReport is what a proofs-history init run produced.
type ProofsHistoryInitReport struct {
// Duration is how long the subcommand ran, backfill included.
Duration time.Duration
// Output is the subcommand's combined stdout and stderr.
Output string
}

// ProofsHistoryInitializer is implemented by execution-layer nodes whose proofs-history store can
// be seeded from their datadir after they were built, the way an operator seeds one for a node
// that has synced without proofs history.
type ProofsHistoryInitializer interface {
// InitProofsHistory runs the init against the node's datadir, whether or not the node is
// running, and enables proofs history on the node's next Start when it succeeds. The error of
// a failed run carries the subcommand's output.
InitProofsHistory(init ProofsHistoryInit) (ProofsHistoryInitReport, error)
}
117 changes: 106 additions & 11 deletions op-devstack/sysgo/l2_el_opreth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"fmt"
"net/url"
"os/exec"
"strconv"
"strings"
"sync"
"time"

"github.com/ethereum-optimism/optimism/op-devstack/devtest"
"github.com/ethereum-optimism/optimism/op-devstack/stack"
Expand All @@ -24,9 +27,10 @@ type OpRethConfig struct {
// (any binary that accepts every op-reth subcommand/flag, plus optionally its own additive
// flags) may be selected via OpRethWithBinary.
Binary string
// DisableProofsHistory skips the proofs-history init + runtime flags for this node. The mixed
// runtime otherwise enables proofs-history on every op-reth node; some CLI-superset binaries
// reject --proofs-history in the mode under test.
// DisableProofsHistory builds the node without a seeded proofs-history store and without the
// proofs-history runtime flags, until InitProofsHistory seeds the store. The mixed runtime
// otherwise enables proofs history on every op-reth node; some CLI-superset binaries reject
// --proofs-history in the mode under test.
DisableProofsHistory bool
// ProofsHistoryWindow is the number of blocks the proofs-history store keeps. Zero keeps the
// mixed runtime's default window.
Expand Down Expand Up @@ -84,9 +88,9 @@ func OpRethWithBinary(binary string) OpRethOption {
})
}

// OpRethWithoutProofsHistory disables the proofs-history subsystem for this node. The mixed runtime
// enables proofs-history on every op-reth node by default; use this for a CLI-superset binary that
// rejects --proofs-history in the mode under test.
// OpRethWithoutProofsHistory builds the node without proofs history, which InitProofsHistory can
// turn on later. The mixed runtime enables it on every op-reth node by default; use this for a
// CLI-superset binary that rejects --proofs-history in the mode under test.
func OpRethWithoutProofsHistory() OpRethOption {
return OpRethOptionFn(func(p devtest.T, _ ComponentTarget, cfg *OpRethConfig) {
cfg.DisableProofsHistory = true
Expand Down Expand Up @@ -134,10 +138,23 @@ type OpReth struct {
userProxy *tcpproxy.Proxy

execPath string
args []string
// args are the `node` arguments without the proofs-history flags, which Start appends while
// proofsHistoryEnabled is set.
args []string
// Each entry is of the form "key=value".
env []string

chainConfigPath string
dataDirPath string

// proofsHistoryEnabled says the store at proofsHistoryStoragePath is seeded, so the node runs
// with proofs history from its next start.
proofsHistoryEnabled bool
proofsHistoryStoragePath string
// proofsHistoryWindow is the window proofs init backfills and the node keeps; zero means the
// mixed runtime's default.
proofsHistoryWindow uint64

p devtest.T

sub *SubProcess
Expand All @@ -150,9 +167,10 @@ type OpReth struct {
}

var (
_ L2ELNode = (*OpReth)(nil)
_ stack.LogRecorder = (*OpReth)(nil)
_ stack.UngracefulLifecycle = (*OpReth)(nil)
_ L2ELNode = (*OpReth)(nil)
_ stack.LogRecorder = (*OpReth)(nil)
_ stack.ProofsHistoryInitializer = (*OpReth)(nil)
_ stack.UngracefulLifecycle = (*OpReth)(nil)
)

// RecordLogLines returns a record of every line op-reth writes to stdout or stderr from now on,
Expand Down Expand Up @@ -245,7 +263,7 @@ func (n *OpReth) Start() {
})
n.sub = NewSubProcess(n.p, stdOutLogs, stdErrLogs)

err := n.sub.Start(n.execPath, n.args, n.env)
err := n.sub.Start(n.execPath, n.nodeArgs(), n.env)
n.p.Require().NoError(err, "Must start")

var userRPCAddr, authRPCAddr string
Expand Down Expand Up @@ -292,6 +310,83 @@ func (n *OpReth) StopUngracefully() {
n.sub = nil
}

// nodeArgs are the `node` arguments of the next start. Callers must hold n.mu.
func (n *OpReth) nodeArgs() []string {
if !n.proofsHistoryEnabled {
return n.args
}
args := append([]string{}, n.args...)
return append(args, proofsHistoryNodeArgs(n.proofsHistoryWindow, n.proofsHistoryStoragePath)...)
}

// InitProofsHistory runs `op-reth proofs init` against this node's datadir and, when it succeeds,
// makes the next Start run with proofs history on the seeded store. It does not stop the node:
// what init does to a datadir the node is still writing to is the caller's concern.
func (n *OpReth) InitProofsHistory(init stack.ProofsHistoryInit) (stack.ProofsHistoryInitReport, error) {
n.mu.Lock()
defer n.mu.Unlock()
window := n.proofsHistoryWindow
if init.Window != 0 {
window = init.Window
}
args := proofsHistoryInitArgs(n.chainConfigPath, n.dataDirPath, n.proofsHistoryStoragePath, window, init.PlainBackfill)
logger := n.p.Logger().New("name", n.name, "chain", n.chainID, "running", n.sub != nil)
logger.Info("Running op-reth proofs init", "cmd", n.execPath, "args", args)
started := time.Now()
output, err := exec.CommandContext(n.p.Ctx(), n.execPath, args...).CombinedOutput()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Medium SAST Finding

OS Command Injection (CWE-78)

More Details

OS command injection is a critical vulnerability that allows an attacker to execute arbitrary commands on the system. This can lead to a full system compromise, as the attacker can potentially gain complete control over the application and the underlying system.

The vulnerability arises when user input is used to construct commands or command arguments that are then executed by the application. This can occur when user-supplied data is passed directly to functions that execute OS commands, such as exec.Command, exec.CommandContext, syscall.ForkExec, or syscall.StartProcess.

If an attacker can inject malicious code into these commands, they can potentially execute any command on the system, including installing malware, stealing data, or causing a denial of service. This vulnerability can have severe consequences, including data breaches, system compromise, and unauthorized access to sensitive information.

To avoid this vulnerability, user input should never be used directly in constructing commands or command arguments. Instead, the application should use a hardcoded set of arguments and validate any user input to ensure it does not contain malicious code.

Attribute Value
Impact Medium
Likelihood Medium

Remediation

OS command injection is a critical vulnerability that allows an attacker to execute arbitrary commands on the system. This can lead to a full system compromise, as the attacker can potentially gain complete control over the application and the underlying system. The vulnerability arises when user input is used to construct commands or command arguments that are then executed by the application.

To fix this issue, user input should never be used directly in constructing commands or command arguments. Instead, the application should use a hardcoded set of arguments and validate any user input to ensure it does not contain malicious code. Additionally, it is recommended to use language-specific facilities for executing commands, which provide better input validation and sanitization.

Code examples

// VULNERABLE CODE - User input is directly used in constructing the command
userInput := getInput() // Assume this comes from user input
cmd := exec.Command("/bin/sh", "-c", userInput)
err := cmd.Run()
// SECURE CODE - Command and arguments are hardcoded, and user input is validated
userInput := getInput() // Assume this comes from user input
if !isValidInput(userInput) {
    // Handle invalid input
    return
}
cmd := exec.Command("/bin/sh", "-c", "echo", userInput)
err := cmd.Run()

Additional recommendations

  • Follow the principle of least privilege and run the application with the minimum required permissions.
  • Use input validation and sanitization techniques to ensure that user input does not contain malicious code.
  • Implement secure coding practices and follow security standards such as OWASP Top 10 and CWE.
  • Consider using language-specific facilities for executing commands, such as os/exec in Go, which provide better input validation and sanitization.
  • Regularly update and patch the application and its dependencies to address known vulnerabilities.
  • Implement logging and monitoring mechanisms to detect and respond to potential security incidents.

Rule ID: WS-I011-GO-00026


To ignore this finding as an exception, reply to this conversation with #wiz_ignore reason

If you'd like to ignore this finding in all future scans, add an exception in the .wiz file (learn more) or create an Ignore Rule (learn more).


To get more details on how to remediate this issue using AI, reply to this conversation with #wiz remediate

report := stack.ProofsHistoryInitReport{Duration: time.Since(started), Output: string(output)}
if err != nil {
logger.Warn("op-reth proofs init failed", "duration", report.Duration, "err", err, "output", report.Output)
return report, fmt.Errorf("op-reth proofs init failed after %s: %w\n%s", report.Duration, err, report.Output)
}
n.proofsHistoryEnabled = true
n.proofsHistoryWindow = window
logger.Info("op-reth proofs init succeeded; proofs history is on from the next start",
"duration", report.Duration, "window", resolveProofsHistoryWindow(window), "storage_path", n.proofsHistoryStoragePath)
logger.Debug("op-reth proofs init output", "output", report.Output)
return report, nil
}

// defaultMixedProofsHistoryWindow is the proofs-history window an op-reth node gets unless
// OpRethWithProofsHistoryWindow or a ProofsHistoryInit overrides it.
const defaultMixedProofsHistoryWindow uint64 = 10_000

func resolveProofsHistoryWindow(window uint64) uint64 {
if window == 0 {
return defaultMixedProofsHistoryWindow
}
return window
}

// proofsHistoryNodeArgs returns the node flags that enable proofs history on the store at
// storagePath.
func proofsHistoryNodeArgs(window uint64, storagePath string) []string {
return []string{
"--proofs-history",
"--proofs-history.storage-path=" + storagePath,
"--proofs-history.window=" + strconv.FormatUint(resolveProofsHistoryWindow(window), 10),
}
}

// proofsHistoryInitArgs returns the `proofs init` invocation that seeds the store at storagePath
// from the datadir and backfills it window blocks behind the datadir's tip. op-reth backfills
// through a trie-state snapshot unless told not to, so the snapshot path gets the operator's
// invocation with no flag.
func proofsHistoryInitArgs(chainConfigPath, dataDirPath, storagePath string, window uint64, plainBackfill bool) []string {
args := []string{
"proofs",
"init",
"--chain=" + chainConfigPath,
"--datadir=" + dataDirPath,
"--proofs-history.storage-path=" + storagePath,
"--proofs-history.window=" + strconv.FormatUint(resolveProofsHistoryWindow(window), 10),
}
if plainBackfill {
args = append(args, "--proofs-history.use-snapshot=false")
}
return args
}

// Callers must hold n.mu.
func (n *OpReth) clearProxyUpstreams() {
if n.userProxy != nil {
Expand Down
Loading
Loading