forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/unix/cap_freebsd.go

327 lines
5.1 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2017 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
//go:build freebsd
package unix
import (
"errors"
"fmt"
)
// Go implementation of C mostly found in /usr/src/sys/kern/subr_capability.c
const (
2024-02-18 10:42:21 +00:00
// This is the version of CapRights this package understands. See C implementation for parallels.
2024-02-18 10:42:21 +00:00
capRightsGoVersion = CAP_RIGHTS_VERSION_00
capArSizeMin = CAP_RIGHTS_VERSION_00 + 2
capArSizeMax = capRightsGoVersion + 2
2024-02-18 10:42:21 +00:00
)
var (
bit2idx = []int{
2024-02-18 10:42:21 +00:00
-1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1,
2024-02-18 10:42:21 +00:00
4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
}
)
func capidxbit(right uint64) int {
2024-02-18 10:42:21 +00:00
return int((right >> 57) & 0x1f)
2024-02-18 10:42:21 +00:00
}
func rightToIndex(right uint64) (int, error) {
2024-02-18 10:42:21 +00:00
idx := capidxbit(right)
2024-02-18 10:42:21 +00:00
if idx < 0 || idx >= len(bit2idx) {
2024-02-18 10:42:21 +00:00
return -2, fmt.Errorf("index for right 0x%x out of range", right)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return bit2idx[idx], nil
2024-02-18 10:42:21 +00:00
}
func caprver(right uint64) int {
2024-02-18 10:42:21 +00:00
return int(right >> 62)
2024-02-18 10:42:21 +00:00
}
func capver(rights *CapRights) int {
2024-02-18 10:42:21 +00:00
return caprver(rights.Rights[0])
2024-02-18 10:42:21 +00:00
}
func caparsize(rights *CapRights) int {
2024-02-18 10:42:21 +00:00
return capver(rights) + 2
2024-02-18 10:42:21 +00:00
}
// CapRightsSet sets the permissions in setrights in rights.
2024-02-18 10:42:21 +00:00
func CapRightsSet(rights *CapRights, setrights []uint64) error {
2024-02-18 10:42:21 +00:00
// This is essentially a copy of cap_rights_vset()
2024-02-18 10:42:21 +00:00
if capver(rights) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("bad rights version %d", capver(rights))
2024-02-18 10:42:21 +00:00
}
n := caparsize(rights)
2024-02-18 10:42:21 +00:00
if n < capArSizeMin || n > capArSizeMax {
2024-02-18 10:42:21 +00:00
return errors.New("bad rights size")
2024-02-18 10:42:21 +00:00
}
for _, right := range setrights {
2024-02-18 10:42:21 +00:00
if caprver(right) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return errors.New("bad right version")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i, err := rightToIndex(right)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i >= n {
2024-02-18 10:42:21 +00:00
return errors.New("index overflow")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if capidxbit(rights.Rights[i]) != capidxbit(right) {
2024-02-18 10:42:21 +00:00
return errors.New("index mismatch")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rights.Rights[i] |= right
2024-02-18 10:42:21 +00:00
if capidxbit(rights.Rights[i]) != capidxbit(right) {
2024-02-18 10:42:21 +00:00
return errors.New("index mismatch (after assign)")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// CapRightsClear clears the permissions in clearrights from rights.
2024-02-18 10:42:21 +00:00
func CapRightsClear(rights *CapRights, clearrights []uint64) error {
2024-02-18 10:42:21 +00:00
// This is essentially a copy of cap_rights_vclear()
2024-02-18 10:42:21 +00:00
if capver(rights) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("bad rights version %d", capver(rights))
2024-02-18 10:42:21 +00:00
}
n := caparsize(rights)
2024-02-18 10:42:21 +00:00
if n < capArSizeMin || n > capArSizeMax {
2024-02-18 10:42:21 +00:00
return errors.New("bad rights size")
2024-02-18 10:42:21 +00:00
}
for _, right := range clearrights {
2024-02-18 10:42:21 +00:00
if caprver(right) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return errors.New("bad right version")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i, err := rightToIndex(right)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i >= n {
2024-02-18 10:42:21 +00:00
return errors.New("index overflow")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if capidxbit(rights.Rights[i]) != capidxbit(right) {
2024-02-18 10:42:21 +00:00
return errors.New("index mismatch")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rights.Rights[i] &= ^(right & 0x01FFFFFFFFFFFFFF)
2024-02-18 10:42:21 +00:00
if capidxbit(rights.Rights[i]) != capidxbit(right) {
2024-02-18 10:42:21 +00:00
return errors.New("index mismatch (after assign)")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// CapRightsIsSet checks whether all the permissions in setrights are present in rights.
2024-02-18 10:42:21 +00:00
func CapRightsIsSet(rights *CapRights, setrights []uint64) (bool, error) {
2024-02-18 10:42:21 +00:00
// This is essentially a copy of cap_rights_is_vset()
2024-02-18 10:42:21 +00:00
if capver(rights) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return false, fmt.Errorf("bad rights version %d", capver(rights))
2024-02-18 10:42:21 +00:00
}
n := caparsize(rights)
2024-02-18 10:42:21 +00:00
if n < capArSizeMin || n > capArSizeMax {
2024-02-18 10:42:21 +00:00
return false, errors.New("bad rights size")
2024-02-18 10:42:21 +00:00
}
for _, right := range setrights {
2024-02-18 10:42:21 +00:00
if caprver(right) != CAP_RIGHTS_VERSION_00 {
2024-02-18 10:42:21 +00:00
return false, errors.New("bad right version")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i, err := rightToIndex(right)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return false, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i >= n {
2024-02-18 10:42:21 +00:00
return false, errors.New("index overflow")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if capidxbit(rights.Rights[i]) != capidxbit(right) {
2024-02-18 10:42:21 +00:00
return false, errors.New("index mismatch")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if (rights.Rights[i] & right) != right {
2024-02-18 10:42:21 +00:00
return false, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return true, nil
2024-02-18 10:42:21 +00:00
}
func capright(idx uint64, bit uint64) uint64 {
2024-02-18 10:42:21 +00:00
return ((1 << (57 + idx)) | bit)
2024-02-18 10:42:21 +00:00
}
// CapRightsInit returns a pointer to an initialised CapRights structure filled with rights.
2024-02-18 10:42:21 +00:00
// See man cap_rights_init(3) and rights(4).
2024-02-18 10:42:21 +00:00
func CapRightsInit(rights []uint64) (*CapRights, error) {
2024-02-18 10:42:21 +00:00
var r CapRights
2024-02-18 10:42:21 +00:00
r.Rights[0] = (capRightsGoVersion << 62) | capright(0, 0)
2024-02-18 10:42:21 +00:00
r.Rights[1] = capright(1, 0)
err := CapRightsSet(&r, rights)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &r, nil
2024-02-18 10:42:21 +00:00
}
// CapRightsLimit reduces the operations permitted on fd to at most those contained in rights.
2024-02-18 10:42:21 +00:00
// The capability rights on fd can never be increased by CapRightsLimit.
2024-02-18 10:42:21 +00:00
// See man cap_rights_limit(2) and rights(4).
2024-02-18 10:42:21 +00:00
func CapRightsLimit(fd uintptr, rights *CapRights) error {
2024-02-18 10:42:21 +00:00
return capRightsLimit(int(fd), rights)
2024-02-18 10:42:21 +00:00
}
// CapRightsGet returns a CapRights structure containing the operations permitted on fd.
2024-02-18 10:42:21 +00:00
// See man cap_rights_get(3) and rights(4).
2024-02-18 10:42:21 +00:00
func CapRightsGet(fd uintptr) (*CapRights, error) {
2024-02-18 10:42:21 +00:00
r, err := CapRightsInit(nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = capRightsGet(capRightsGoVersion, int(fd), r)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return r, nil
2024-02-18 10:42:21 +00:00
}