Skip to content

Commit

Permalink
Suppress EOF errors when seeking empty result sets. (Velocidex#3593)
Browse files Browse the repository at this point in the history
  • Loading branch information
scudette authored Jul 4, 2024
1 parent 8d39908 commit bf3bd32
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api/tables/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func getTable(

// Seek to the row we need.
err = rs_reader.SeekToRow(int64(in.StartRow))
if err == io.EOF {
if errors.Is(err, io.EOF) {
return result, nil
}

Expand Down Expand Up @@ -207,7 +207,7 @@ func getStackTable(

// Seek to the row we need.
err = rs_reader.SeekToRow(int64(in.StartRow))
if err == io.EOF {
if errors.Is(err, io.EOF) {
return result, nil
}

Expand Down
7 changes: 7 additions & 0 deletions artifacts/definitions/Server/Utils/CreateMSI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ name: Server.Utils.CreateMSI
description: |
Build an MSI ready for deployment in the current org.
This artifact depends on the following tools:
* <velo-tool-viewer name="VelociraptorWindowsMSI" />
* <velo-tool-viewer name="VelociraptorWindows_x86MSI" />
You can replace those with suitable MSI builds.
type: SERVER

parameters:
Expand Down
9 changes: 7 additions & 2 deletions artifacts/definitions/Windows/Memory/Acquisition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ tools:
github_asset_regex: winpmem_mini_x64.+exe
serve_locally: true

precondition: SELECT OS From info() where OS = 'windows' AND Architecture = "amd64"
precondition: |
SELECT OS FROM info()
WHERE OS = 'windows'
AND Architecture = "amd64"
sources:
- query: |
LET Tempfile <= tempfile(extension=".raw")
SELECT * FROM foreach(
row={
SELECT OSPath, tempfile(extension=".raw", remove_last=TRUE) AS Tempfile
SELECT OSPath
FROM Artifact.Generic.Utils.FetchBinary(ToolName="WinPmem64")
},
query={
Expand Down
1 change: 1 addition & 0 deletions result_sets/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type TimedResultSetWriter interface {
}

type ResultSetReader interface {
// Returns EOF if the row does not exist in the result set.
SeekToRow(start int64) error
Rows(ctx context.Context) <-chan *ordereddict.Dict

Expand Down
9 changes: 9 additions & 0 deletions services/hunt_dispatcher/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package hunt_dispatcher

import (
"context"
"errors"
"io"
"time"

api_proto "www.velocidex.com/golang/velociraptor/api/proto"
Expand Down Expand Up @@ -133,6 +135,13 @@ func (self *HuntDispatcher) GetFlows(

// Seek to the row we need.
err = rs_reader.SeekToRow(int64(start))
if errors.Is(err, io.EOF) {
close(output_chan)
rs_reader.Close()

return output_chan, 0, nil
}

if err != nil {
close(output_chan)
rs_reader.Close()
Expand Down
6 changes: 6 additions & 0 deletions services/hunt_dispatcher/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package hunt_dispatcher

import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -249,6 +251,10 @@ func (self *HuntStorageManagerImpl) ListHunts(
defer rs_reader.Close()

err = rs_reader.SeekToRow(offset)
if errors.Is(err, io.EOF) {
return nil, 0, nil
}

if err != nil {
return nil, 0, err
}
Expand Down
16 changes: 12 additions & 4 deletions services/launcher/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package launcher
import (
"context"
"fmt"
"io"
"os"

"github.com/Velocidex/ordereddict"
Expand Down Expand Up @@ -101,30 +102,37 @@ func (self *FlowStorageManager) ListFlows(
// Try to rebuild the index
err = self.buildFlowIndexFromLegacy(ctx, config_obj, client_id)
if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("buildFlowIndexFromLegacy %w", err)
}

rs_reader, err = result_sets.NewResultSetReaderWithOptions(
ctx, config_obj, file_store_factory,
client_path_manager.FlowIndex(), options)
if err != nil {
return nil, 0, fmt.Errorf("NewResultSetReaderWithOptions %w", err)
}
}

if err != nil {
return nil, 0, err
}

result := []*services.FlowSummary{}
err = rs_reader.SeekToRow(offset)
if errors.Is(err, io.EOF) {
return result, 0, nil
}

if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("SeekToRow %v %w", offset, err)
}

// Highly optimized reader for speed.
json_chan, err := rs_reader.JSON(ctx)
if err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("JSON %w", err)
}

result := []*services.FlowSummary{}
for serialized := range json_chan {
summary := &services.FlowSummary{}
err = json.Unmarshal(serialized, summary)
Expand Down
5 changes: 5 additions & 0 deletions services/vfs_service/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vfs_service
import (
"context"
"errors"
"io"

"github.com/Velocidex/ordereddict"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -102,6 +103,10 @@ func renderDBVFS(
defer reader.Close()

err = reader.SeekToRow(int64(result.StartIdx))
if errors.Is(err, io.EOF) {
return nil, nil
}

if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions vql/server/flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"io"

"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/velociraptor/acls"
Expand Down Expand Up @@ -181,6 +182,10 @@ func (self SourcePlugin) Call(

if arg.StartRow > 0 {
err = result_set_reader.SeekToRow(arg.StartRow)
if errors.Is(err, io.EOF) {
return
}

if err != nil {
scope.Log("source: %v", err)
return
Expand Down

0 comments on commit bf3bd32

Please sign in to comment.