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

Memory is not secure and may be vulnerable to attacks #376

Open
blacksun1977 opened this issue Aug 26, 2024 · 2 comments
Open

Memory is not secure and may be vulnerable to attacks #376

blacksun1977 opened this issue Aug 26, 2024 · 2 comments

Comments

@blacksun1977
Copy link

Memory is not secure and may be vulnerable to attacks.
see the code:

file : decode_slice.go

func (d *Decoder) decodeSlice(c byte) ([]interface{}, error) {
	n, err := d.arrayLen(c)
	if err != nil {
		return nil, err
	}
	if n == -1 {
		return nil, nil
	}

	s := make([]interface{}, 0, n) // dangerous code
	for i := 0; i < n; i++ {
		v, err := d.decodeInterfaceCond()
		if err != nil {
			return nil, err
		}
		s = append(s, v)
	}

	return s, nil
}

If someone modifies the length of the array to 1m, they will request at least 1M of memory. If it is a N dimensional array, N*1M of memory will be required, which can easily lead to memory request attacks
I think safe code should be like this:

var sliceAllocLen = 64 // configurable or suggested length
func (d *Decoder) decodeSlice(c byte) ([]interface{}, error) {
	n, err := d.arrayLen(c)
	if err != nil {
		return nil, err
	}
	if n == -1 {
		return nil, nil
	}
	if n > sliceAllocLen {
		n = sliceAllocLen
	}
	s := make([]interface{}, 0, n) // dangerous code
	for i := 0; i < n; i++ {
		v, err := d.decodeInterfaceCond()
		if err != nil {
			return nil, err
		}
		s = append(s, v)
	}

	return s, nil
}

I don't think we should trust the length of arrays in data stream,
it is necessary to limit the length of the array and also limit its recursive depth.

If we can determine the remaining length of the input stream, it can be easily determined to make it more efficient. For example, if there are 1024 bytes left, the length of the array will not exceed 1024

I used translation software, please forgive any unclear descriptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@blacksun1977 and others