seccomp-tools 1.4.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +105 -17
  3. data/ext/ptrace/ptrace.c +56 -4
  4. data/lib/seccomp-tools/asm/asm.rb +8 -6
  5. data/lib/seccomp-tools/asm/compiler.rb +130 -224
  6. data/lib/seccomp-tools/asm/sasm.tab.rb +780 -0
  7. data/lib/seccomp-tools/asm/sasm.y +175 -0
  8. data/lib/seccomp-tools/asm/scalar.rb +129 -0
  9. data/lib/seccomp-tools/asm/scanner.rb +163 -0
  10. data/lib/seccomp-tools/asm/statement.rb +32 -0
  11. data/lib/seccomp-tools/asm/token.rb +29 -0
  12. data/lib/seccomp-tools/bpf.rb +31 -7
  13. data/lib/seccomp-tools/cli/asm.rb +3 -3
  14. data/lib/seccomp-tools/cli/base.rb +4 -4
  15. data/lib/seccomp-tools/cli/disasm.rb +27 -3
  16. data/lib/seccomp-tools/cli/dump.rb +4 -2
  17. data/lib/seccomp-tools/cli/emu.rb +1 -4
  18. data/lib/seccomp-tools/const.rb +37 -3
  19. data/lib/seccomp-tools/consts/sys_nr/aarch64.rb +284 -0
  20. data/lib/seccomp-tools/consts/sys_nr/amd64.rb +5 -1
  21. data/lib/seccomp-tools/consts/sys_nr/i386.rb +14 -14
  22. data/lib/seccomp-tools/consts/sys_nr/s390x.rb +365 -0
  23. data/lib/seccomp-tools/disasm/context.rb +2 -2
  24. data/lib/seccomp-tools/disasm/disasm.rb +16 -9
  25. data/lib/seccomp-tools/dumper.rb +12 -3
  26. data/lib/seccomp-tools/emulator.rb +5 -9
  27. data/lib/seccomp-tools/error.rb +31 -0
  28. data/lib/seccomp-tools/instruction/alu.rb +1 -1
  29. data/lib/seccomp-tools/instruction/base.rb +1 -1
  30. data/lib/seccomp-tools/instruction/jmp.rb +28 -10
  31. data/lib/seccomp-tools/instruction/ld.rb +5 -3
  32. data/lib/seccomp-tools/syscall.rb +23 -13
  33. data/lib/seccomp-tools/templates/asm.s390x.asm +26 -0
  34. data/lib/seccomp-tools/util.rb +3 -1
  35. data/lib/seccomp-tools/version.rb +1 -1
  36. metadata +38 -9
  37. data/lib/seccomp-tools/asm/tokenizer.rb +0 -169
@@ -10,7 +10,13 @@ module SeccompTools
10
10
  # Summary of this command.
11
11
  SUMMARY = 'Disassemble seccomp bpf.'
12
12
  # Usage of this command.
13
- USAGE = ('disasm - ' + SUMMARY + "\n\n" + 'Usage: seccomp-tools disasm BPF_FILE [options]').freeze
13
+ USAGE = "disasm - #{SUMMARY}\n\nUsage: seccomp-tools disasm BPF_FILE [options]"
14
+
15
+ def initialize(*)
16
+ super
17
+ option[:bpf] = true
18
+ option[:arg_infer] = true
19
+ end
14
20
 
15
21
  # Define option parser.
16
22
  # @return [OptionParser]
@@ -20,8 +26,23 @@ module SeccompTools
20
26
  opt.on('-o', '--output FILE', 'Output result into FILE instead of stdout.') do |o|
21
27
  option[:ofile] = o
22
28
  end
23
-
24
29
  option_arch(opt)
30
+ opt.on('--[no-]bpf', 'Display BPF bytes (code, jt, etc.).',
31
+ 'Default: true') do |f|
32
+ option[:bpf] = f
33
+ end
34
+ opt.on('--[no-]arg-infer', 'Display syscall arguments with parameter names when possible.',
35
+ 'Default: true') do |f|
36
+ option[:arg_infer] = f
37
+ end
38
+ opt.on('--asm-able', 'Output with this flag is a valid input of "seccomp-tools asm".',
39
+ 'By default, "seccomp-tools disasm" is in a human-readable format that easy for analysis.',
40
+ 'Passing this flag can have the output be simplified to a valid input for "seccomp-tools asm".',
41
+ 'This flag implies "--no-bpf --no-arg-infer".',
42
+ 'Default: false') do |_f|
43
+ option[:bpf] = false
44
+ option[:arg_infer] = false
45
+ end
25
46
  end
26
47
  end
27
48
 
@@ -33,7 +54,10 @@ module SeccompTools
33
54
  option[:ifile] = argv.shift
34
55
  return CLI.show(parser.help) if option[:ifile].nil?
35
56
 
36
- output { SeccompTools::Disasm.disasm(input, arch: option[:arch]) }
57
+ output do
58
+ SeccompTools::Disasm.disasm(input, arch: option[:arch], display_bpf: option[:bpf],
59
+ arg_infer: option[:arg_infer])
60
+ end
37
61
  end
38
62
  end
39
63
  end
@@ -14,7 +14,8 @@ module SeccompTools
14
14
  # Summary of this command.
15
15
  SUMMARY = 'Automatically dump seccomp bpf from execution file(s).'
16
16
  # Usage of this command.
17
- USAGE = ('dump - ' + SUMMARY + "\n\n" + 'Usage: seccomp-tools dump [exec] [options]').freeze
17
+ USAGE = "dump - #{SUMMARY}\nNOTE : This function is only available on Linux." \
18
+ "\n\nUsage: seccomp-tools dump [exec] [options]"
18
19
 
19
20
  def initialize(*)
20
21
  super
@@ -64,11 +65,12 @@ module SeccompTools
64
65
  # Handle options.
65
66
  # @return [void]
66
67
  def handle
68
+ return Logger.error('Dump is only available on Linux.') unless Dumper::SUPPORTED
67
69
  return unless super
68
70
 
69
71
  block = lambda do |bpf, arch|
70
72
  case option[:format]
71
- when :inspect then output { '"' + bpf.bytes.map { |b| format('\\x%02X', b) }.join + "\"\n" }
73
+ when :inspect then output { "\"#{bpf.bytes.map { |b| format('\\x%02X', b) }.join}\"\n" }
72
74
  when :raw then output { bpf }
73
75
  when :disasm then output { SeccompTools::Disasm.disasm(bpf, arch: arch) }
74
76
  end
@@ -15,10 +15,7 @@ module SeccompTools
15
15
  # Summary of this command.
16
16
  SUMMARY = 'Emulate seccomp rules.'
17
17
  # Usage of this command.
18
- USAGE = ('emu - ' +
19
- SUMMARY +
20
- "\n\n" \
21
- 'Usage: seccomp-tools emu [options] BPF_FILE [sys_nr [arg0 [arg1 ... arg5]]]').freeze
18
+ USAGE = "emu - #{SUMMARY}\n\nUsage: seccomp-tools emu [options] BPF_FILE [sys_nr [arg0 [arg1 ... arg5]]]"
22
19
 
23
20
  def initialize(*)
24
21
  super
@@ -59,6 +59,8 @@ module SeccompTools
59
59
  KILL: 0x00000000, # alias of KILL_THREAD
60
60
  TRAP: 0x00030000,
61
61
  ERRNO: 0x00050000,
62
+ USER_NOTIF: 0x7fc00000,
63
+ LOG: 0x7ffc0000,
62
64
  TRACE: 0x7ff00000,
63
65
  ALLOW: 0x7fff0000
64
66
  }.freeze
@@ -118,19 +120,51 @@ module SeccompTools
118
120
  filename = File.join(__dir__, 'consts', 'sys_nr', "#{arch}.rb")
119
121
  return unless File.exist?(filename)
120
122
 
121
- const_set(cons, instance_eval(IO.read(filename)))
123
+ const_set(cons, instance_eval(File.read(filename)))
124
+ end
125
+
126
+ # Helper for loading syscall prototypes from generated sys_arg.rb.
127
+ def load_args
128
+ hash = instance_eval(File.read(File.join(__dir__, 'consts', 'sys_arg.rb')))
129
+ Hash.new do |_h, k|
130
+ next hash[k] if hash[k]
131
+ next hash[k.to_s[4..].to_sym] if k.to_s.start_with?('x32_')
132
+
133
+ nil
134
+ end
122
135
  end
123
136
  end
124
137
 
125
138
  # The argument names of all syscalls.
126
- SYS_ARG = instance_eval(IO.read(File.join(__dir__, 'consts', 'sys_arg.rb'))).freeze
139
+ SYS_ARG = Syscall.load_args.freeze
127
140
 
128
141
  # Constants from https://github.com/torvalds/linux/blob/master/include/uapi/linux/audit.h.
129
142
  module Audit
143
+ # Maps arch name to {ARCH}'s key.
144
+ ARCH_NAME = {
145
+ amd64: 'ARCH_X86_64',
146
+ i386: 'ARCH_I386',
147
+ aarch64: 'ARCH_AARCH64',
148
+ s390x: 'ARCH_S390X'
149
+ }.freeze
150
+
130
151
  # AUDIT_ARCH_*
131
152
  ARCH = {
132
153
  'ARCH_X86_64' => 0xc000003e,
133
- 'ARCH_I386' => 0x40000003
154
+ 'ARCH_I386' => 0x40000003,
155
+ 'ARCH_AARCH64' => 0xc00000b7,
156
+ 'ARCH_S390X' => 0x80000016
157
+ }.freeze
158
+ end
159
+
160
+ # Endianess constants.
161
+ module Endian
162
+ # Defining default endianess of architectures.
163
+ ENDIAN = {
164
+ i386: '<',
165
+ amd64: '<',
166
+ aarch64: '<',
167
+ s390x: '>'
134
168
  }.freeze
135
169
  end
136
170
  end
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ {
4
+ io_setup: 0,
5
+ io_destroy: 1,
6
+ io_submit: 2,
7
+ io_cancel: 3,
8
+ io_getevents: 4,
9
+ setxattr: 5,
10
+ lsetxattr: 6,
11
+ fsetxattr: 7,
12
+ getxattr: 8,
13
+ lgetxattr: 9,
14
+ fgetxattr: 10,
15
+ listxattr: 11,
16
+ llistxattr: 12,
17
+ flistxattr: 13,
18
+ removexattr: 14,
19
+ lremovexattr: 15,
20
+ fremovexattr: 16,
21
+ getcwd: 17,
22
+ lookup_dcookie: 18,
23
+ eventfd2: 19,
24
+ epoll_create1: 20,
25
+ epoll_ctl: 21,
26
+ epoll_pwait: 22,
27
+ dup: 23,
28
+ dup3: 24,
29
+ fcntl: 25,
30
+ inotify_init1: 26,
31
+ inotify_add_watch: 27,
32
+ inotify_rm_watch: 28,
33
+ ioctl: 29,
34
+ ioprio_set: 30,
35
+ ioprio_get: 31,
36
+ flock: 32,
37
+ mknodat: 33,
38
+ mkdirat: 34,
39
+ unlinkat: 35,
40
+ symlinkat: 36,
41
+ linkat: 37,
42
+ renameat: 38,
43
+ umount2: 39,
44
+ mount: 40,
45
+ pivot_root: 41,
46
+ nfsservctl: 42,
47
+ statfs: 43,
48
+ fstatfs: 44,
49
+ truncate: 45,
50
+ ftruncate: 46,
51
+ fallocate: 47,
52
+ faccessat: 48,
53
+ chdir: 49,
54
+ fchdir: 50,
55
+ chroot: 51,
56
+ fchmod: 52,
57
+ fchmodat: 53,
58
+ fchownat: 54,
59
+ fchown: 55,
60
+ openat: 56,
61
+ close: 57,
62
+ vhangup: 58,
63
+ pipe2: 59,
64
+ quotactl: 60,
65
+ getdents: 61,
66
+ getdents64: 61,
67
+ lseek: 62,
68
+ read: 63,
69
+ write: 64,
70
+ readv: 65,
71
+ writev: 66,
72
+ pread: 67,
73
+ pread64: 67,
74
+ pwrite: 68,
75
+ pwrite64: 68,
76
+ preadv: 69,
77
+ pwritev: 70,
78
+ sendfile: 71,
79
+ pselect6: 72,
80
+ ppoll: 73,
81
+ signalfd4: 74,
82
+ vmsplice: 75,
83
+ splice: 76,
84
+ tee: 77,
85
+ readlinkat: 78,
86
+ newfstatat: 79,
87
+ fstat: 80,
88
+ newfstat: 80,
89
+ sync: 81,
90
+ fsync: 82,
91
+ fdatasync: 83,
92
+ sync_file_range: 84,
93
+ timerfd_create: 85,
94
+ timerfd_settime: 86,
95
+ timerfd_gettime: 87,
96
+ utimensat: 88,
97
+ acct: 89,
98
+ capget: 90,
99
+ capset: 91,
100
+ personality: 92,
101
+ exit: 93,
102
+ exit_group: 94,
103
+ waitid: 95,
104
+ set_tid_address: 96,
105
+ unshare: 97,
106
+ futex: 98,
107
+ set_robust_list: 99,
108
+ get_robust_list: 100,
109
+ nanosleep: 101,
110
+ getitimer: 102,
111
+ setitimer: 103,
112
+ kexec_load: 104,
113
+ init_module: 105,
114
+ delete_module: 106,
115
+ timer_create: 107,
116
+ timer_gettime: 108,
117
+ timer_getoverrun: 109,
118
+ timer_settime: 110,
119
+ timer_delete: 111,
120
+ clock_settime: 112,
121
+ clock_gettime: 113,
122
+ clock_getres: 114,
123
+ clock_nanosleep: 115,
124
+ syslog: 116,
125
+ ptrace: 117,
126
+ sched_setparam: 118,
127
+ sched_setscheduler: 119,
128
+ sched_getscheduler: 120,
129
+ sched_getparam: 121,
130
+ sched_setaffinity: 122,
131
+ sched_getaffinity: 123,
132
+ sched_yield: 124,
133
+ sched_get_priority_max: 125,
134
+ sched_get_priority_min: 126,
135
+ sched_rr_get_interval: 127,
136
+ restart_syscall: 128,
137
+ kill: 129,
138
+ tkill: 130,
139
+ tgkill: 131,
140
+ sigaltstack: 132,
141
+ rt_sigsuspend: 133,
142
+ rt_sigaction: 134,
143
+ rt_sigprocmask: 135,
144
+ rt_sigpending: 136,
145
+ rt_sigtimedwait: 137,
146
+ rt_sigqueueinfo: 138,
147
+ rt_sigreturn: 139,
148
+ setpriority: 140,
149
+ getpriority: 141,
150
+ reboot: 142,
151
+ setregid: 143,
152
+ setgid: 144,
153
+ setreuid: 145,
154
+ setuid: 146,
155
+ setresuid: 147,
156
+ getresuid: 148,
157
+ setresgid: 149,
158
+ getresgid: 150,
159
+ setfsuid: 151,
160
+ setfsgid: 152,
161
+ times: 153,
162
+ setpgid: 154,
163
+ getpgid: 155,
164
+ getsid: 156,
165
+ setsid: 157,
166
+ getgroups: 158,
167
+ setgroups: 159,
168
+ uname: 160,
169
+ sethostname: 161,
170
+ setdomainname: 162,
171
+ getrlimit: 163,
172
+ setrlimit: 164,
173
+ getrusage: 165,
174
+ umask: 166,
175
+ prctl: 167,
176
+ getcpu: 168,
177
+ gettimeofday: 169,
178
+ settimeofday: 170,
179
+ adjtimex: 171,
180
+ getpid: 172,
181
+ getppid: 173,
182
+ getuid: 174,
183
+ geteuid: 175,
184
+ getgid: 176,
185
+ getegid: 177,
186
+ gettid: 178,
187
+ sysinfo: 179,
188
+ mq_open: 180,
189
+ mq_unlink: 181,
190
+ mq_timedsend: 182,
191
+ mq_timedreceive: 183,
192
+ mq_notify: 184,
193
+ mq_getsetattr: 185,
194
+ msgget: 186,
195
+ msgctl: 187,
196
+ msgrcv: 188,
197
+ msgsnd: 189,
198
+ semget: 190,
199
+ semctl: 191,
200
+ semtimedop: 192,
201
+ semop: 193,
202
+ shmget: 194,
203
+ shmctl: 195,
204
+ shmat: 196,
205
+ shmdt: 197,
206
+ socket: 198,
207
+ socketpair: 199,
208
+ bind: 200,
209
+ listen: 201,
210
+ accept: 202,
211
+ connect: 203,
212
+ getsockname: 204,
213
+ getpeername: 205,
214
+ sendto: 206,
215
+ recvfrom: 207,
216
+ setsockopt: 208,
217
+ getsockopt: 209,
218
+ shutdown: 210,
219
+ sendmsg: 211,
220
+ recvmsg: 212,
221
+ readahead: 213,
222
+ brk: 214,
223
+ munmap: 215,
224
+ mremap: 216,
225
+ add_key: 217,
226
+ request_key: 218,
227
+ keyctl: 219,
228
+ clone: 220,
229
+ execve: 221,
230
+ mmap: 222,
231
+ fadvise64: 223,
232
+ swapon: 224,
233
+ swapoff: 225,
234
+ mprotect: 226,
235
+ msync: 227,
236
+ mlock: 228,
237
+ munlock: 229,
238
+ mlockall: 230,
239
+ munlockall: 231,
240
+ mincore: 232,
241
+ madvise: 233,
242
+ remap_file_pages: 234,
243
+ mbind: 235,
244
+ get_mempolicy: 236,
245
+ set_mempolicy: 237,
246
+ migrate_pages: 238,
247
+ move_pages: 239,
248
+ rt_tgsigqueueinfo: 240,
249
+ perf_event_open: 241,
250
+ accept4: 242,
251
+ recvmmsg: 243,
252
+ wait4: 260,
253
+ prlimit64: 261,
254
+ fanotify_init: 262,
255
+ fanotify_mark: 263,
256
+ name_to_handle_at: 264,
257
+ open_by_handle_at: 265,
258
+ clock_adjtime: 266,
259
+ syncfs: 267,
260
+ setns: 268,
261
+ sendmmsg: 269,
262
+ process_vm_readv: 270,
263
+ process_vm_writev: 271,
264
+ kcmp: 272,
265
+ finit_module: 273,
266
+ sched_setattr: 274,
267
+ sched_getattr: 275,
268
+ renameat2: 276,
269
+ seccomp: 277,
270
+ getrandom: 278,
271
+ memfd_create: 279,
272
+ bpf: 280,
273
+ execveat: 281,
274
+ userfaultfd: 282,
275
+ membarrier: 283,
276
+ mlock2: 284,
277
+ copy_file_range: 285,
278
+ preadv2: 286,
279
+ pwritev2: 287,
280
+ pkey_mprotect: 288,
281
+ pkey_alloc: 289,
282
+ pkey_free: 290,
283
+ statx: 291
284
+ }
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Denote a x32 syscall.
4
+ X32_MODE_BIT = 0x40000000
3
5
  {
4
6
  read: 0,
5
7
  write: 1,
@@ -18,7 +20,9 @@
18
20
  rt_sigprocmask: 14,
19
21
  rt_sigreturn: 15,
20
22
  ioctl: 16,
23
+ pread: 17,
21
24
  pread64: 17,
25
+ pwrite: 18,
22
26
  pwrite64: 18,
23
27
  readv: 19,
24
28
  writev: 20,
@@ -334,4 +338,4 @@
334
338
  pkey_alloc: 330,
335
339
  pkey_free: 331,
336
340
  statx: 332
337
- }
341
+ }.tap { |h| h.keys.each { |k| h["x32_#{k}".to_sym] = h[k] | X32_MODE_BIT } } # rubocop:disable Style/HashEachMethods
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  {
4
- setup: 0,
4
+ restart_syscall: 0,
5
5
  exit: 1,
6
6
  fork: 2,
7
7
  read: 3,
@@ -259,14 +259,14 @@
259
259
  remap_file_pages: 257,
260
260
  set_tid_address: 258,
261
261
  timer_create: 259,
262
- timer_settime: (259 + 1),
263
- timer_gettime: (259 + 2),
264
- timer_getoverrun: (259 + 3),
265
- timer_delete: (259 + 4),
266
- clock_settime: (259 + 5),
267
- clock_gettime: (259 + 6),
268
- clock_getres: (259 + 7),
269
- clock_nanosleep: (259 + 8),
262
+ timer_settime: 260,
263
+ timer_gettime: 261,
264
+ timer_getoverrun: 262,
265
+ timer_delete: 263,
266
+ clock_settime: 264,
267
+ clock_gettime: 265,
268
+ clock_getres: 266,
269
+ clock_nanosleep: 267,
270
270
  statfs64: 268,
271
271
  fstatfs64: 269,
272
272
  tgkill: 270,
@@ -277,11 +277,11 @@
277
277
  get_mempolicy: 275,
278
278
  set_mempolicy: 276,
279
279
  mq_open: 277,
280
- mq_unlink: (277 + 1),
281
- mq_timedsend: (277 + 2),
282
- mq_timedreceive: (277 + 3),
283
- mq_notify: (277 + 4),
284
- mq_getsetattr: (277 + 5),
280
+ mq_unlink: 278,
281
+ mq_timedsend: 279,
282
+ mq_timedreceive: 280,
283
+ mq_notify: 281,
284
+ mq_getsetattr: 282,
285
285
  sys_kexec_load: 283,
286
286
  waitid: 284,
287
287
  add_key: 286,