Debian Codegraph @

capability/syscall_linux.go

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