shard: add more checks for GetRange parameters

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-07-29 11:11:55 +03:00
parent 7570677e3b
commit a46f20064c
4 changed files with 10 additions and 6 deletions

View file

@ -628,7 +628,7 @@ func (b *blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm GetRange
to := from + prm.rng.GetLength() to := from + prm.rng.GetLength()
payload := obj.Payload() payload := obj.Payload()
if uint64(len(payload)) < to { if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
var errOutOfRange apistatus.ObjectOutOfRange var errOutOfRange apistatus.ObjectOutOfRange
return GetRangeSmallRes{}, errOutOfRange return GetRangeSmallRes{}, errOutOfRange

View file

@ -54,7 +54,7 @@ func (b *BlobStor) GetRangeBig(prm GetRangeBigPrm) (GetRangeBigRes, error) {
payload := obj.Payload() payload := obj.Payload()
ln, off := prm.rng.GetLength(), prm.rng.GetOffset() ln, off := prm.rng.GetLength(), prm.rng.GetOffset()
if pLen := uint64(len(payload)); pLen < ln+off { if pLen := uint64(len(payload)); ln+off < off || pLen < off || pLen < ln+off {
var errOutOfRange apistatus.ObjectOutOfRange var errOutOfRange apistatus.ObjectOutOfRange
return GetRangeBigRes{}, errOutOfRange return GetRangeBigRes{}, errOutOfRange

View file

@ -113,7 +113,7 @@ func (s *Shard) GetRange(prm RngPrm) (RngRes, error) {
payload := res.Payload() payload := res.Payload()
from := rng.GetOffset() from := rng.GetOffset()
to := from + rng.GetLength() to := from + rng.GetLength()
if uint64(len(payload)) < to { if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
return nil, apistatus.ObjectOutOfRange{} return nil, apistatus.ObjectOutOfRange{}
} }

View file

@ -1,6 +1,7 @@
package shard_test package shard_test
import ( import (
"math"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/util/slice"
@ -45,15 +46,18 @@ func testShardGetRange(t *testing.T, hasWriteCache bool) {
testCases := []testCase{ testCases := []testCase{
{false, "small object, good", 1024, newRange(11, 123)}, {false, "small object, good", 1024, newRange(11, 123)},
{true, "small object, out of range", 1024, newRange(10, 1020)}, {true, "small object, out of range, big len", 1024, newRange(10, 1020)},
{true, "small object, out of range, big offset", 1024, newRange(1025, math.MaxUint64-10)},
{false, "big object, good", 2048, newRange(11, 123)}, {false, "big object, good", 2048, newRange(11, 123)},
{true, "big object, out of range", 2048, newRange(100, 2000)}, {true, "big object, out of range, big len", 2048, newRange(100, 2000)},
{true, "big object, out of range, big offset", 2048, newRange(2048, math.MaxUint64-10)},
} }
if hasWriteCache { if hasWriteCache {
testCases = append(testCases, testCases = append(testCases,
testCase{false, "object in write-cache, good", 100, newRange(2, 18)}, testCase{false, "object in write-cache, good", 100, newRange(2, 18)},
testCase{true, "object in write-cache, out of range", 100, newRange(4, 99)}) testCase{true, "object in write-cache, out of range, big len", 100, newRange(4, 99)},
testCase{true, "object in write-cache, out of range, big offset", 100, newRange(101, math.MaxUint64-10)})
} }
sh := newCustomShard(t, t.TempDir(), hasWriteCache, sh := newCustomShard(t, t.TempDir(), hasWriteCache,