k3s/vendor/github.com/opencontainers/runc/libcontainer/keys/keyctl.go

48 lines
1.0 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
// +build linux
package keys
import (
"strconv"
"strings"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
type KeySerial uint32
func JoinSessionKeyring(name string) (KeySerial, error) {
sessKeyID, err := unix.KeyctlJoinSessionKeyring(name)
2019-01-12 04:58:27 +00:00
if err != nil {
return 0, errors.Wrap(err, "create session key")
}
return KeySerial(sessKeyID), nil
2019-01-12 04:58:27 +00:00
}
// ModKeyringPerm modifies permissions on a keyring by reading the current permissions,
// anding the bits with the given mask (clearing permissions) and setting
// additional permission bits
func ModKeyringPerm(ringID KeySerial, mask, setbits uint32) error {
dest, err := unix.KeyctlString(unix.KEYCTL_DESCRIBE, int(ringID))
2019-01-12 04:58:27 +00:00
if err != nil {
return err
}
res := strings.Split(dest, ";")
if len(res) < 5 {
2020-08-10 17:43:49 +00:00
return errors.New("Destination buffer for key description is too small")
2019-01-12 04:58:27 +00:00
}
// parse permissions
perm64, err := strconv.ParseUint(res[3], 16, 32)
if err != nil {
return err
}
perm := (uint32(perm64) & mask) | setbits
return unix.KeyctlSetperm(int(ringID), perm)
2019-01-12 04:58:27 +00:00
}