-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Warn if
CAP_IPC_LOCK
capability is missing
- Loading branch information
Showing
5 changed files
with
74 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-secure-stdlib/mlock" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/syndtr/gocapability/capability" | ||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
// ExecWrapper mlocks the process memory (if supported) before our `run` functions, | ||
// and gracefully logs and exits afterwards. | ||
func ExecWrapper(f func(ctx *cli.Context) (int, error)) cli.ActionFunc { | ||
return func(ctx *cli.Context) error { | ||
caps, err := capability.NewPid2(0) | ||
if err != nil { | ||
return exit(1, fmt.Errorf("error getting capabilities: %w", err)) | ||
} | ||
|
||
if err = caps.Load(); err != nil { | ||
return exit(1, fmt.Errorf("error loading capabilities: %w", err)) | ||
} | ||
|
||
if caps.Get(capability.EFFECTIVE, capability.CAP_IPC_LOCK) { // mlock.Supported() is assumed | ||
if err = mlock.LockMemory(); err != nil { | ||
return exit(1, fmt.Errorf("error locking vac memory: %w", err)) | ||
} | ||
} else { | ||
log.Warn("unable to lock memory, missing CAP_IPC_LOCK capability") | ||
} | ||
|
||
return exit(f(ctx)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-secure-stdlib/mlock" | ||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
// ExecWrapper mlocks the process memory (if supported) before our `run` functions, | ||
// and gracefully logs and exits afterwards. | ||
func ExecWrapper(f func(ctx *cli.Context) (int, error)) cli.ActionFunc { | ||
return func(ctx *cli.Context) error { | ||
if mlock.Supported() { | ||
if err := mlock.LockMemory(); err != nil { | ||
return exit(1, fmt.Errorf("error locking vac memory: %w", err)) | ||
} | ||
} | ||
|
||
return exit(f(ctx)) | ||
} | ||
} |