-
Notifications
You must be signed in to change notification settings - Fork 1
/
fwmark.py
executable file
·288 lines (236 loc) · 7.89 KB
/
fwmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
import ctypes.util
import os
import pathlib
class _align_8(ctypes.Structure):
_fields_ = [
('_dummy', ctypes.c_uint64 * 0),
]
## union bpf_attr member structures
class bpf_prog_load_attr(ctypes.Structure):
_anonymous_ = ['_align']
_fields_ = [
('_align', _align_8),
('prog_type', ctypes.c_uint32),
('insn_cnt', ctypes.c_uint32),
('insns', ctypes.c_uint64),
('license', ctypes.c_uint64),
# remaining fields are zeroed by bpf syscall
]
class bpf_prog_attach_attr(ctypes.Structure):
_anonymous_ = ['_align']
_fields_ = [
('_align', _align_8),
('target_fd', ctypes.c_uint32),
('attach_bpf_fd', ctypes.c_uint32),
('attach_type', ctypes.c_uint32),
# remaining fields are zeroed by bpf syscall
]
## bpf syscall
if os.uname()[4] == 'x86_64':
__NR_bpf = 321
else:
raise NotImplementedError
# if you want to port this script, add syscall number here
# and make sure bit order in bpf_insn matches
syscall = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True).syscall
def bpf(cmd, attr):
return syscall(__NR_bpf, cmd, ctypes.byref(attr), ctypes.sizeof(attr))
## bpf command constants
BPF_PROG_LOAD = 5
BPF_PROG_ATTACH = 8
## bpf attach type constants
BPF_CGROUP_INET_SOCK_CREATE = 2
## bpf syscall helpers
def bpf_prog_load(prog_type, instructions, license='GPL'):
bpf_prog = bytearray().join(bytes(insn) for insn in instructions)
insns = (ctypes.c_ubyte * len(bpf_prog)).from_buffer(bpf_prog)
if isinstance(license, str):
license = license.encode()
license = ctypes.create_string_buffer(license)
attr = bpf_prog_load_attr(
prog_type=prog_type,
insn_cnt=len(instructions),
insns=ctypes.addressof(insns),
license=ctypes.addressof(license),
)
ret = bpf(BPF_PROG_LOAD, attr)
if ret == -1:
errno = ctypes.get_errno()
raise OSError(errno, os.strerror(errno))
return ret
def bpf_prog_attach(prog_fd, target_fd, attach_type):
attr = bpf_prog_attach_attr(
target_fd=target_fd,
attach_bpf_fd=prog_fd,
attach_type=attach_type,
)
ret = bpf(BPF_PROG_ATTACH, attr)
if ret == -1:
errno = ctypes.get_errno()
raise OSError(errno, os.strerror(errno))
## BPF register constants
BPF_REG_0 = 0
BPF_REG_1 = 1
## BPF instruction classes
BPF_STX = 0x03
BPF_ALU = 0x04
BPF_JMP = 0x05
## BPF_SIZE
BPF_W = 0x00
## BPF_MODE
BPF_MEM = 0x60
## BPF_SRC
BPF_K = 0x00
BPF_X = 0x08
## ???
BPF_EXIT = 0x90
BPF_MOV = 0xb0
## BPF instructions
class bpf_insn(ctypes.Structure):
_pack_ = 1
_fields_ = [
('code', ctypes.c_ubyte),
('dst_reg', ctypes.c_ubyte, 4), # bit-order only tested on amd64
('src_reg', ctypes.c_ubyte, 4),
('off', ctypes.c_int16),
('imm', ctypes.c_int32),
]
BPF_EXIT_INSN = lambda: bpf_insn(
code=BPF_JMP | BPF_EXIT,
)
BPF_MOV32_IMM = lambda dst, imm: bpf_insn(
code=BPF_ALU | BPF_MOV | BPF_K,
dst_reg=dst,
imm=imm,
)
BPF_STX_MEM = lambda size, dst, src, off: bpf_insn(
code=BPF_STX | size | BPF_MEM,
dst_reg=dst,
src_reg=src,
off=off,
)
## BPF program types
BPF_PROG_TYPE_CGROUP_SOCK = 9
## BPF context
class bpf_sock(ctypes.Structure):
_fields_ = [
('bound_dev_if', ctypes.c_uint32),
('family', ctypes.c_uint32),
('type', ctypes.c_uint32),
('protocol', ctypes.c_uint32),
('mark', ctypes.c_uint32),
('priority', ctypes.c_uint32),
('src_ip4', ctypes.c_uint32),
('src_ip6', ctypes.c_uint32 * 4),
('src_port', ctypes.c_uint32),
('dst_port', ctypes.c_uint32),
('dst_ip4', ctypes.c_uint32),
('dst_ip6', ctypes.c_uint32 * 4),
('state', ctypes.c_uint32),
('rx_queue_mapping', ctypes.c_int32),
]
## /proc/mounts
def read_mangled_path(s, escape=b' \t\n\\'):
if not isinstance(s, bytes):
raise TypeError('mangled path should be byte string')
if not isinstance(escape, bytes):
raise TypeError('escape charset should be byte string')
if b'\\' not in escape:
raise ValueError('escape charset does not contain backslash')
result = bytearray()
it = iter(s)
for x in it:
if x in escape:
break
if x == 0x5C: # backslash
try:
escape_sequence = bytes(next(it) for i in range(3))
except StopIteration:
raise ValueError('short read in escape sequence')
for x in escape_sequence:
if x not in b'01234567':
raise ValueError('invalid escape sequence')
x = int(escape_sequence, 8)
if x > 255:
raise ValueError('invalid escape sequence')
result.append(x)
return bytes(result)
def find_all_cgroup2_mounts():
with open('/proc/mounts', 'rb') as f:
return [
pathlib.Path(os.fsdecode(read_mangled_path(line[8:])))
for line in f
if line.startswith(b'cgroup2 ')
]
## /proc/self/cgroup
def get_self_cgroup():
with open('/proc/self/cgroup', 'rb') as f:
for line in f:
if line.startswith(b'0::/'):
return pathlib.PurePath(os.fsdecode(line[4:-1]))
raise RuntimeError('unable to get cgroup hierarchy for current process')
def get_self_slice():
dot = pathlib.PurePath('.')
path = get_self_cgroup()
while path.suffix != '.slice':
if path == dot:
raise RuntimeError('unable to find systemd.slice for current process')
path = path.parent
return path
## main
def main():
import argparse
## parse arguments
parser = argparse.ArgumentParser(description='run program in a new cgroup with specified fwmark')
parser.add_argument('--cgroup2', type=pathlib.Path, default=None, help='cgroup2 mountpoint, usually under /sys/fs/cgroup')
parser.add_argument('fwmark', type=lambda s: int(s, 0), help='fwmark to be applied')
parser.add_argument('command', nargs=argparse.REMAINDER, help='command to be executed (default to $SHELL if omitted)')
args = parser.parse_args()
## check arguments
if not 0 <= args.fwmark <= 2**32-1:
raise ValueError('invalid fwmark')
if not args.command:
args.command = [os.environ['SHELL']]
## determine cgroup2 mountpoint
mounts = find_all_cgroup2_mounts()
if args.cgroup2 is None:
if len(mounts) == 1:
args.cgroup2 = mounts[0]
elif len(mounts) == 0:
raise RuntimeError('no cgroup2 mountpoint available')
else:
raise RuntimeError('multiple cgroup2 mountpoint available, choose one via command line argument')
else:
if args.cgroup2 not in mounts:
raise RuntimeError(f'{arg.cgroup2} is not a cgroup2 mountpoint')
## create cgroup (random systemd.scope under current systemd.slice)
systemd_scope_name = f'fwmark-{args.fwmark:08X}-{os.urandom(8).hex()}.scope'
systemd_slice_path = get_self_slice()
cgroup_path = args.cgroup2 / systemd_slice_path / systemd_scope_name
cgroup_path.mkdir(mode=0o755)
cgroup_fd = os.open(cgroup_path, os.O_RDONLY | os.O_DIRECTORY)
## load bpf program
bpf_prog = [
BPF_MOV32_IMM(BPF_REG_0, args.fwmark),
BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, bpf_sock.mark.offset),
BPF_MOV32_IMM(BPF_REG_0, 1),
BPF_EXIT_INSN(),
]
bpf_prog_fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, bpf_prog)
## attach bpf program to cgroup
bpf_prog_attach(bpf_prog_fd, cgroup_fd, BPF_CGROUP_INET_SOCK_CREATE)
## execute command
os.environ['FWMARK'] = str(args.fwmark)
os.execlp(
'systemd-run',
'systemd-run',
'--quiet',
'--collect',
f'--slice={systemd_slice_path.name}',
'--scope',
f'--unit={systemd_scope_name}',
*args.command,
)
if __name__ == '__main__':
main()