-
Notifications
You must be signed in to change notification settings - Fork 17
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
[CRE-40] Check binary size before decompression #994
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ var ( | |
defaultMinMemoryMBs = 128 | ||
DefaultInitialFuel = uint64(100_000_000) | ||
defaultMaxFetchRequests = 5 | ||
defaultMaxBinarySize = 10 * 1024 * 1024 // 10 MB | ||
) | ||
|
||
type DeterminismConfig struct { | ||
|
@@ -91,6 +92,7 @@ type ModuleConfig struct { | |
IsUncompressed bool | ||
Fetch func(ctx context.Context, req *wasmpb.FetchRequest) (*wasmpb.FetchResponse, error) | ||
MaxFetchRequests int | ||
MaxBinarySize int64 | ||
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.
|
||
|
||
// Labeler is used to emit messages from the module. | ||
Labeler custmsg.MessageEmitter | ||
|
@@ -166,6 +168,10 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig)) | |
modCfg.MinMemoryMBs = int64(defaultMinMemoryMBs) | ||
} | ||
|
||
if modCfg.MaxBinarySize == 0 { | ||
modCfg.MaxBinarySize = int64(defaultMaxBinarySize) | ||
} | ||
|
||
// Take the max of the min and the configured max memory mbs. | ||
// We do this because Go requires a minimum of 16 megabytes to run, | ||
// and local testing has shown that with less than the min, some | ||
|
@@ -183,6 +189,12 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig)) | |
|
||
engine := wasmtime.NewEngineWithConfig(cfg) | ||
if !modCfg.IsUncompressed { | ||
// validate the binary size before decompressing | ||
// this is to prevent decompression bombs | ||
if int64(len(binary)) > modCfg.MaxBinarySize { | ||
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 this type be |
||
return nil, fmt.Errorf("binary size exceeds the maximum allowed size of %d bytes", modCfg.MaxBinarySize) | ||
} | ||
|
||
rdr := brotli.NewReader(bytes.NewBuffer(binary)) | ||
decompedBinary, err := io.ReadAll(rdr) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -903,6 +903,48 @@ func TestModule_Sandbox_Memory(t *testing.T) { | |||||
assert.ErrorContains(t, err, "exit status 2") | ||||||
} | ||||||
|
||||||
func TestModule_CompressedBinarySize(t *testing.T) { | ||||||
t.Parallel() | ||||||
|
||||||
t.Run("compressed binary size is smaller than the default 10mb limit", func(t *testing.T) { | ||||||
binary := createTestBinary(successBinaryCmd, successBinaryLocation, false, t) | ||||||
|
||||||
_, err := NewModule(&ModuleConfig{IsUncompressed: false, Logger: logger.Test(t)}, binary) | ||||||
require.NoError(t, err) | ||||||
}) | ||||||
|
||||||
t.Run("compressed binary size is bigger than the default 10mb limit", func(t *testing.T) { | ||||||
// 11mb binary | ||||||
binary := make([]byte, 11*1024*1024) | ||||||
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.
Suggested change
nit |
||||||
|
||||||
var b bytes.Buffer | ||||||
bwr := brotli.NewWriter(&b) | ||||||
_, err := bwr.Write(binary) | ||||||
require.NoError(t, err) | ||||||
require.NoError(t, bwr.Close()) | ||||||
|
||||||
_, err = NewModule(&ModuleConfig{IsUncompressed: false, Logger: logger.Test(t)}, binary) | ||||||
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", 10*1024*1024) | ||||||
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.
Suggested change
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. ahh true, sorry forgot about that one. should be good now thanks |
||||||
require.ErrorContains(t, err, default10mbLimit) | ||||||
}) | ||||||
|
||||||
t.Run("compressed binary size is bigger than the custom limit", func(t *testing.T) { | ||||||
// 2mb binary | ||||||
binary := make([]byte, 2*1024*1024) | ||||||
|
||||||
var b bytes.Buffer | ||||||
bwr := brotli.NewWriter(&b) | ||||||
_, err := bwr.Write(binary) | ||||||
require.NoError(t, err) | ||||||
require.NoError(t, bwr.Close()) | ||||||
|
||||||
customMaxBinarySize := int64(1 * 1024 * 1024) | ||||||
_, err = NewModule(&ModuleConfig{IsUncompressed: false, MaxBinarySize: customMaxBinarySize, Logger: logger.Test(t)}, binary) | ||||||
default10mbLimit := fmt.Sprintf("binary size exceeds the maximum allowed size of %d bytes", customMaxBinarySize) | ||||||
require.ErrorContains(t, err, default10mbLimit) | ||||||
}) | ||||||
} | ||||||
|
||||||
func TestModule_Sandbox_SleepIsStubbedOut(t *testing.T) { | ||||||
t.Parallel() | ||||||
ctx := tests.Context(t) | ||||||
|
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.
@agparadiso Could we call this MaxUncompressedBinarySize -- since you're only applying it to the uncompressed size
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.
this is used to check the size before decompressing it. Should it be
MaxCompressedBinarySize
? 🤔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.
Hah yes it should be :)