Debian Codegraph @

capability/syscall_linux.go

raw symbols SCIP
1 // Copyright 2024 The Capability Authors.
2 // Copyright 2013 Suryandaru Triandana <[email protected]>
3 // All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package capability
9
10 import (
11 "syscall"
12 "unsafe"
13 )
14
15 type capHeader struct {
16 version uint32
17 pid int32
18 }
19
20 type capData struct {
21 effective uint32
22 permitted uint32
23 inheritable uint32
24 }
25
26 func capget(hdr *capHeader, data *capData) (err error) {
27 _, _, e1 := syscall.RawSyscall(syscall.SYS_CAPGET, uintptr(unsafe.Pointer(hdr)), uintptr(unsafe.Pointer(data)), 0)
28 if e1 != 0 {
29 err = e1
30 }
31 return
32 }
33
34 func capset(hdr *capHeader, data *capData) (err error) {
35 _, _, e1 := syscall.RawSyscall(syscall.SYS_CAPSET, uintptr(unsafe.Pointer(hdr)), uintptr(unsafe.Pointer(data)), 0)
36 if e1 != 0 {
37 err = e1
38 }
39 return
40 }
41
42 // not yet in syscall
43 const (
44 pr_CAP_AMBIENT = 47
45 pr_CAP_AMBIENT_IS_SET = uintptr(1)
46 pr_CAP_AMBIENT_RAISE = uintptr(2)
47 pr_CAP_AMBIENT_LOWER = uintptr(3)
48 pr_CAP_AMBIENT_CLEAR_ALL = uintptr(4)
49 )
50
51 func prctl(option int, arg2, arg3 uintptr) (err error) {
52 _, _, e1 := syscall.RawSyscall(syscall.SYS_PRCTL, uintptr(option), arg2, arg3)
53 if e1 != 0 {
54 err = e1
55 }
56 return
57 }
58
59 func prctlRetInt(option int, arg2, arg3 uintptr) (int, error) {
60 ret, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, uintptr(option), arg2, arg3)
61 if err != 0 {
62 return 0, err
63 }
64 return int(ret), nil
65 }
66
67 const (
68 vfsXattrName = "security.capability"
69
70 vfsCapVerMask = 0xff000000
71 vfsCapVer1 = 0x01000000
72 vfsCapVer2 = 0x02000000
73
74 vfsCapFlagMask = ^vfsCapVerMask
75 vfsCapFlageffective = 0x000001
76
77 vfscapDataSizeV1 = 4 * (1 + 2*1)
78 vfscapDataSizeV2 = 4 * (1 + 2*2)
79 )
80
81 type vfscapData struct {
82 magic uint32
83 data [2]struct {
84 permitted uint32
85 inheritable uint32
86 }
87 effective [2]uint32
88 version int8
89 }
90
91 var _vfsXattrName *byte
92
93 func init() {
94 _vfsXattrName, _ = syscall.BytePtrFromString(vfsXattrName)
95 }
96
97 func getVfsCap(path string, dest *vfscapData) (err error) {
98 var _p0 *byte
99 _p0, err = syscall.BytePtrFromString(path)
100 if err != nil {
101 return
102 }
103 r0, _, e1 := syscall.RawSyscall6(syscall.SYS_GETXATTR, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_vfsXattrName)), uintptr(unsafe.Pointer(dest)), vfscapDataSizeV2, 0, 0)
104 if e1 != 0 {
105 if e1 == syscall.ENODATA {
106 dest.version = 2
107 return
108 }
109 err = e1
110 }
111 switch dest.magic & vfsCapVerMask {
112 case vfsCapVer1:
113 dest.version = 1
114 if r0 != vfscapDataSizeV1 {
115 return syscall.EINVAL
116 }
117 dest.data[1].permitted = 0
118 dest.data[1].inheritable = 0
119 case vfsCapVer2:
120 dest.version = 2
121 if r0 != vfscapDataSizeV2 {
122 return syscall.EINVAL
123 }
124 default:
125 return syscall.EINVAL
126 }
127 if dest.magic&vfsCapFlageffective != 0 {
128 dest.effective[0] = dest.data[0].permitted | dest.data[0].inheritable
129 dest.effective[1] = dest.data[1].permitted | dest.data[1].inheritable
130 } else {
131 dest.effective[0] = 0
132 dest.effective[1] = 0
133 }
134 return
135 }
136
137 func setVfsCap(path string, data *vfscapData) (err error) {
138 var _p0 *byte
139 _p0, err = syscall.BytePtrFromString(path)
140 if err != nil {
141 return
142 }
143 var size uintptr
144 switch data.version {
145 case 1:
146 data.magic = vfsCapVer1
147 size = vfscapDataSizeV1
148 case 2:
149 data.magic = vfsCapVer2
150 if data.effective[0] != 0 || data.effective[1] != 0 {
151 data.magic |= vfsCapFlageffective
152 }
153 size = vfscapDataSizeV2
154 default:
155 return syscall.EINVAL
156 }
157 _, _, e1 := syscall.RawSyscall6(syscall.SYS_SETXATTR, uintptr(unsafe.Pointer(_p0)), uintptr(unsafe.Pointer(_vfsXattrName)), uintptr(unsafe.Pointer(data)), size, 0, 0)
158 if e1 != 0 {
159 err = e1
160 }
161 return
162 }