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

Set context timeouts on all exec functions #688

Merged
merged 2 commits into from
Dec 22, 2020
Merged
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
2 changes: 1 addition & 1 deletion pkg/osquery/table/mdfind_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func mdfind(args ...string) ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

we see a lot of timeouts for mdfind, will there significant perf impacts to devices by 10x-ing this timeout? Does mdfind use significant CPU? ( it seems like this is a potential CPU hotspot depending on the query)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No clue. I just upped the numbers.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough. I don't think we have significant safety nets for other performance sensitive areas, so this is probably in line with current art.

defer cancel()

path := "/usr/bin/mdfind"
Expand Down
19 changes: 13 additions & 6 deletions pkg/osquery/table/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"os/exec"
"strconv"
"time"

"github.com/go-kit/kit/log"
"github.com/groob/plist"
Expand All @@ -31,13 +32,13 @@ func MDMInfo(logger log.Logger) *table.Plugin {
}

func generateMDMInfo(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
profiles, err := getMDMProfile()
profiles, err := getMDMProfile(ctx)
if err != nil {
return nil, err
}

depEnrolled, userApproved := "unknown", "unknown"
status, err := getMDMProfileStatus()
status, err := getMDMProfileStatus(ctx)
if err == nil { // only supported on 10.13.4+
depEnrolled = strconv.FormatBool(status.DEPEnrolled)
userApproved = strconv.FormatBool(status.UserApproved)
Expand Down Expand Up @@ -84,8 +85,11 @@ func generateMDMInfo(ctx context.Context, queryContext table.QueryContext) ([]ma
return results, nil
}

func getMDMProfile() (*profilesOutput, error) {
cmd := exec.Command("/usr/bin/profiles", "-L", "-o", "stdout-xml")
func getMDMProfile(ctx context.Context) (*profilesOutput, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "/usr/bin/profiles", "-L", "-o", "stdout-xml")
out, err := cmd.Output()
if err != nil {
return nil, errors.Wrap(err, "calling /usr/bin/profiles to get MDM profile payload")
Expand Down Expand Up @@ -124,8 +128,11 @@ type payloadContent struct {
SignMessage bool
}

func getMDMProfileStatus() (profileStatus, error) {
cmd := exec.Command("/usr/bin/profiles", "status", "-type", "enrollment")
func getMDMProfileStatus(ctx context.Context) (profileStatus, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "/usr/bin/profiles", "status", "-type", "enrollment")
out, err := cmd.Output()
if err != nil {
return profileStatus{}, errors.Wrap(err, "calling /usr/bin/profiles to get MDM profile status")
Expand Down
5 changes: 3 additions & 2 deletions pkg/osquery/table/mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package table

import (
"context"
"testing"

"github.com/kolide/kit/env"
Expand All @@ -11,9 +12,9 @@ import (

func TestMDMProfileStatus(t *testing.T) {
if env.Bool("SKIP_TEST_MDM", true) {
t.Skip("No docker")
t.Skip("Skipping MDM Test")
}

_, err := getMDMProfileStatus()
_, err := getMDMProfileStatus(context.TODO())
require.Nil(t, err)
}
4 changes: 4 additions & 0 deletions pkg/osquery/table/touchid_system_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"regexp"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -44,6 +45,9 @@ type touchIDSystemConfig struct {

// TouchIDSystemConfigGenerate will be called whenever the table is queried.
func (t *touchIDSystemConfigTable) generate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

var results []map[string]string
var touchIDCompatible, secureEnclaveCPU, touchIDEnabled, touchIDUnlock string

Expand Down
4 changes: 4 additions & 0 deletions pkg/osquery/table/touchid_user_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -141,6 +142,9 @@ func (t *touchIDUserConfigTable) generate(ctx context.Context, queryContext tabl

// runCommand runs a given command and arguments as the supplied user
func runCommandContext(ctx context.Context, uid int, cmd string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

// Set up the command
var stdout bytes.Buffer
c := exec.CommandContext(ctx, cmd, args...)
Expand Down
4 changes: 4 additions & 0 deletions pkg/osquery/tables/dataflattentable/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"os/exec"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -57,6 +58,9 @@ func (t *Table) generateExec(ctx context.Context, queryContext table.QueryContex
}

func (t *Table) exec(ctx context.Context) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()

var stdout bytes.Buffer
var stderr bytes.Buffer

Expand Down
10 changes: 8 additions & 2 deletions pkg/osquery/tables/filevault/filevault.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,28 @@ func TablePlugin(client *osquery.ExtensionManagerClient, logger log.Logger) *tab
}

func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var results []map[string]string

// Read the system's fdesetup configuration
var stdout bytes.Buffer

cmd := exec.CommandContext(ctx, fdesetupPath, "status")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd.Stdout = &stdout

if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, "calling fdesetup")
}

output := string(stdout.Bytes())
status := strings.TrimSuffix(output, "\n")

result := map[string]string{
"status": status,
}

results = append(results, result)

return results, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/osquery/tables/ioreg/ioreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ func (t *Table) flattenOutput(dataQuery string, systemOutput []byte) ([]dataflat
}

func (t *Table) execIoreg(ctx context.Context, args []string) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

var stdout bytes.Buffer
var stderr bytes.Buffer

args = append(args, "-a")

ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, ioregPath, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down
8 changes: 8 additions & 0 deletions pkg/osquery/tables/systemprofiler/systemprofiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"context"
"os/exec"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -197,6 +198,13 @@ func (t *Table) getRowsFromOutput(dataQuery, detailLevel string, systemProfilerO
}

func (t *Table) execSystemProfiler(ctx context.Context, detailLevel string, subcommands []string) ([]byte, error) {
timeout := 30 * time.Second
if detailLevel == "full" {
timeout = 5 * time.Minute
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

var stdout bytes.Buffer
var stderr bytes.Buffer

Expand Down