implemented pledge and unveil

This commit is contained in:
ston1th 2023-12-21 14:30:04 +01:00
commit 37ecf9a192
3 changed files with 39 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import (
"syscall" "syscall"
"time" "time"
"git.giftfish.de/ston1th/godrop/v2"
"git.giftfish.de/ston1th/keyctl/pkg/api" "git.giftfish.de/ston1th/keyctl/pkg/api"
"git.giftfish.de/ston1th/keyctl/pkg/cli" "git.giftfish.de/ston1th/keyctl/pkg/cli"
"git.giftfish.de/ston1th/keyctl/pkg/db" "git.giftfish.de/ston1th/keyctl/pkg/db"
@ -71,10 +72,16 @@ func server() {
log = klogr.New().WithName("main") log = klogr.New().WithName("main")
log.Info("starting keyctl", "version", version) log.Info("starting keyctl", "version", version)
err := godrop.PledgePromises("stdio rpath wpath cpath inet fattr flock unix unveil")
if err != nil {
klog.Fatalf("init failed: %s", err)
}
dbstore, err := db.New(klogr.New().WithName("db"), path) dbstore, err := db.New(klogr.New().WithName("db"), path)
if err != nil { if err != nil {
klog.Fatalf("init failed: %s", err) klog.Fatalf("init failed: %s", err)
} }
srv, err := api.NewServer(klogr.New().WithName("srv"), listen, filepath.Join(path, socket), dbstore) srv, err := api.NewServer(klogr.New().WithName("srv"), listen, filepath.Join(path, socket), dbstore)
if err != nil { if err != nil {
klog.Fatalf("init failed: %s", err) klog.Fatalf("init failed: %s", err)

View file

@ -22,6 +22,7 @@ import (
type Client struct { type Client struct {
endpoint string endpoint string
unix string
httpClient *http.Client httpClient *http.Client
dialer *net.Dialer dialer *net.Dialer
dialerFunc func(ctx context.Context, network, addr string) (net.Conn, error) dialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
@ -50,6 +51,7 @@ func WithEndpoint(endpoint string) ClientOption {
return client.dialer.DialContext(ctx, "unix", u.Path) return client.dialer.DialContext(ctx, "unix", u.Path)
} }
client.endpoint = "http://unix" client.endpoint = "http://unix"
client.unix = u.Path
return return
} }
client.dialerFunc = client.dialer.DialContext client.dialerFunc = client.dialer.DialContext
@ -189,6 +191,10 @@ func NewClient(options ...ClientOption) *Client {
return client return client
} }
func (c *Client) IsUnix() (socket string, ok bool) {
return c.unix, c.unix != ""
}
func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) { func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
url := c.endpoint + path url := c.endpoint + path
req, err := http.NewRequest(method, url, body) req, err := http.NewRequest(method, url, body)

View file

@ -14,6 +14,7 @@ import (
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"git.giftfish.de/ston1th/godrop/v2"
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client" apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
clientv1 "git.giftfish.de/ston1th/keyctl/pkg/api/v1/client" clientv1 "git.giftfish.de/ston1th/keyctl/pkg/api/v1/client"
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema" "git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
@ -29,6 +30,17 @@ func exit(err error) {
func Run(arg string) { func Run(arg string) {
ctx := context.Background() ctx := context.Background()
pledge := "stdio unveil"
switch arg {
case "new", "ls", "req", "approve", "reject", "del":
pledge += " unix"
case "get":
pledge += " unix inet dns"
}
err := godrop.PledgePromises(pledge)
if err != nil {
exit(err)
}
switch arg { switch arg {
case "new": case "new":
name, existing, encoding, t, size, err := newKeyFlags() name, existing, encoding, t, size, err := newKeyFlags()
@ -148,6 +160,12 @@ func client() (c *clientv1.Client) {
exit(err) exit(err)
} }
apic := apiclient.NewClient(opts...) apic := apiclient.NewClient(opts...)
if socket, ok := apic.IsUnix(); ok {
err = unveil(socket)
if err != nil {
exit(err)
}
}
c, err = clientv1.NewClient(apic) c, err = clientv1.NewClient(apic)
if err != nil { if err != nil {
exit(err) exit(err)
@ -155,6 +173,14 @@ func client() (c *clientv1.Client) {
return return
} }
func unveil(socket string) error {
err := godrop.Unveil(socket, "rwc")
if err != nil {
return err
}
return godrop.UnveilBlock()
}
func jsonOutput(v any) { func jsonOutput(v any) {
b, err := json.Marshal(v) b, err := json.Marshal(v)
if err != nil { if err != nil {