| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-syscalls.c |
| 4 | * |
| 5 | * LTTng syscall probes. |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/compat.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/bitmap.h> |
| 15 | #include <linux/in.h> |
| 16 | #include <linux/in6.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/stringify.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/anon_inodes.h> |
| 21 | #include <linux/fcntl.h> |
| 22 | #include <asm/ptrace.h> |
| 23 | #include <asm/syscall.h> |
| 24 | |
| 25 | #include <lttng/bitfield.h> |
| 26 | #include <wrapper/tracepoint.h> |
| 27 | #include <wrapper/file.h> |
| 28 | #include <wrapper/rcu.h> |
| 29 | #include <wrapper/syscall.h> |
| 30 | #include <lttng/events.h> |
| 31 | |
| 32 | #ifndef CONFIG_COMPAT |
| 33 | # ifndef is_compat_task |
| 34 | # define is_compat_task() (0) |
| 35 | # endif |
| 36 | #endif |
| 37 | |
| 38 | /* in_compat_syscall appears in kernel 4.6. */ |
| 39 | #ifndef in_compat_syscall |
| 40 | #define in_compat_syscall() is_compat_task() |
| 41 | #endif |
| 42 | |
| 43 | enum sc_type { |
| 44 | SC_TYPE_ENTRY, |
| 45 | SC_TYPE_EXIT, |
| 46 | SC_TYPE_COMPAT_ENTRY, |
| 47 | SC_TYPE_COMPAT_EXIT, |
| 48 | }; |
| 49 | |
| 50 | #define SYSCALL_ENTRY_TOK syscall_entry_ |
| 51 | #define COMPAT_SYSCALL_ENTRY_TOK compat_syscall_entry_ |
| 52 | #define SYSCALL_EXIT_TOK syscall_exit_ |
| 53 | #define COMPAT_SYSCALL_EXIT_TOK compat_syscall_exit_ |
| 54 | |
| 55 | #define SYSCALL_ENTRY_STR __stringify(SYSCALL_ENTRY_TOK) |
| 56 | #define COMPAT_SYSCALL_ENTRY_STR __stringify(COMPAT_SYSCALL_ENTRY_TOK) |
| 57 | #define SYSCALL_EXIT_STR __stringify(SYSCALL_EXIT_TOK) |
| 58 | #define COMPAT_SYSCALL_EXIT_STR __stringify(COMPAT_SYSCALL_EXIT_TOK) |
| 59 | |
| 60 | static |
| 61 | void syscall_entry_probe(void *__data, struct pt_regs *regs, long id); |
| 62 | static |
| 63 | void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret); |
| 64 | |
| 65 | /* |
| 66 | * Forward declarations for old kernels. |
| 67 | */ |
| 68 | struct mmsghdr; |
| 69 | struct rlimit64; |
| 70 | struct oldold_utsname; |
| 71 | struct old_utsname; |
| 72 | struct sel_arg_struct; |
| 73 | struct mmap_arg_struct; |
| 74 | struct file_handle; |
| 75 | struct user_msghdr; |
| 76 | |
| 77 | /* |
| 78 | * Forward declaration for kernels >= 5.6 |
| 79 | */ |
| 80 | struct timex; |
| 81 | struct timeval; |
| 82 | struct itimerval; |
| 83 | struct itimerspec; |
| 84 | |
| 85 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) |
| 86 | typedef __kernel_old_time_t time_t; |
| 87 | #endif |
| 88 | |
| 89 | #ifdef IA32_NR_syscalls |
| 90 | #define NR_compat_syscalls IA32_NR_syscalls |
| 91 | #else |
| 92 | #define NR_compat_syscalls NR_syscalls |
| 93 | #endif |
| 94 | |
| 95 | /* |
| 96 | * Create LTTng tracepoint probes. |
| 97 | */ |
| 98 | #define LTTNG_PACKAGE_BUILD |
| 99 | #define CREATE_TRACE_POINTS |
| 100 | #define TP_MODULE_NOINIT |
| 101 | #define TRACE_INCLUDE_PATH instrumentation/syscalls/headers |
| 102 | |
| 103 | #define PARAMS(args...) args |
| 104 | |
| 105 | /* Handle unknown syscalls */ |
| 106 | #undef TRACE_SYSTEM |
| 107 | #define TRACE_SYSTEM syscalls_unknown |
| 108 | #include <instrumentation/syscalls/headers/syscalls_unknown.h> |
| 109 | #undef TRACE_SYSTEM |
| 110 | |
| 111 | #define SC_ENTER |
| 112 | |
| 113 | #undef sc_exit |
| 114 | #define sc_exit(...) |
| 115 | #undef sc_in |
| 116 | #define sc_in(...) __VA_ARGS__ |
| 117 | #undef sc_out |
| 118 | #define sc_out(...) |
| 119 | #undef sc_inout |
| 120 | #define sc_inout(...) __VA_ARGS__ |
| 121 | |
| 122 | /* Hijack probe callback for system call enter */ |
| 123 | #undef TP_PROBE_CB |
| 124 | #define TP_PROBE_CB(_template) &syscall_entry_probe |
| 125 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
| 126 | LTTNG_TRACEPOINT_EVENT(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 127 | PARAMS(_fields)) |
| 128 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
| 129 | LTTNG_TRACEPOINT_EVENT_CODE(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 130 | PARAMS(_locvar), PARAMS(_code_pre), \ |
| 131 | PARAMS(_fields), PARAMS(_code_post)) |
| 132 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
| 133 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_entry_##_name, PARAMS(_fields)) |
| 134 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
| 135 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_entry_##_template, syscall_entry_##_name) |
| 136 | /* Enumerations only defined at first inclusion. */ |
| 137 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) \ |
| 138 | LTTNG_TRACEPOINT_ENUM(_name, PARAMS(_values)) |
| 139 | #undef TRACE_SYSTEM |
| 140 | #define TRACE_SYSTEM syscall_entry_integers |
| 141 | #define TRACE_INCLUDE_FILE syscalls_integers |
| 142 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
| 143 | #undef TRACE_INCLUDE_FILE |
| 144 | #undef TRACE_SYSTEM |
| 145 | #define TRACE_SYSTEM syscall_entry_pointers |
| 146 | #define TRACE_INCLUDE_FILE syscalls_pointers |
| 147 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
| 148 | #undef TRACE_INCLUDE_FILE |
| 149 | #undef TRACE_SYSTEM |
| 150 | #undef SC_LTTNG_TRACEPOINT_ENUM |
| 151 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
| 152 | #undef SC_LTTNG_TRACEPOINT_EVENT |
| 153 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS |
| 154 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS |
| 155 | #undef TP_PROBE_CB |
| 156 | #undef _TRACE_SYSCALLS_INTEGERS_H |
| 157 | #undef _TRACE_SYSCALLS_POINTERS_H |
| 158 | |
| 159 | /* Hijack probe callback for compat system call enter */ |
| 160 | #define TP_PROBE_CB(_template) &syscall_entry_probe |
| 161 | #define LTTNG_SC_COMPAT |
| 162 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
| 163 | LTTNG_TRACEPOINT_EVENT(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 164 | PARAMS(_fields)) |
| 165 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
| 166 | LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 167 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
| 168 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
| 169 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_entry_##_name, PARAMS(_fields)) |
| 170 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
| 171 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_entry_##_template, \ |
| 172 | compat_syscall_entry_##_name) |
| 173 | /* Enumerations only defined at inital inclusion (not here). */ |
| 174 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) |
| 175 | #define TRACE_SYSTEM compat_syscall_entry_integers |
| 176 | #define TRACE_INCLUDE_FILE compat_syscalls_integers |
| 177 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
| 178 | #undef TRACE_INCLUDE_FILE |
| 179 | #undef TRACE_SYSTEM |
| 180 | #define TRACE_SYSTEM compat_syscall_entry_pointers |
| 181 | #define TRACE_INCLUDE_FILE compat_syscalls_pointers |
| 182 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
| 183 | #undef TRACE_INCLUDE_FILE |
| 184 | #undef TRACE_SYSTEM |
| 185 | #undef SC_LTTNG_TRACEPOINT_ENUM |
| 186 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
| 187 | #undef SC_LTTNG_TRACEPOINT_EVENT |
| 188 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS |
| 189 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS |
| 190 | #undef TP_PROBE_CB |
| 191 | #undef _TRACE_SYSCALLS_INTEGERS_H |
| 192 | #undef _TRACE_SYSCALLS_POINTERS_H |
| 193 | #undef LTTNG_SC_COMPAT |
| 194 | |
| 195 | #undef SC_ENTER |
| 196 | |
| 197 | #define SC_EXIT |
| 198 | |
| 199 | #undef sc_exit |
| 200 | #define sc_exit(...) __VA_ARGS__ |
| 201 | #undef sc_in |
| 202 | #define sc_in(...) |
| 203 | #undef sc_out |
| 204 | #define sc_out(...) __VA_ARGS__ |
| 205 | #undef sc_inout |
| 206 | #define sc_inout(...) __VA_ARGS__ |
| 207 | |
| 208 | /* Hijack probe callback for system call exit */ |
| 209 | #define TP_PROBE_CB(_template) &syscall_exit_probe |
| 210 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
| 211 | LTTNG_TRACEPOINT_EVENT(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 212 | PARAMS(_fields)) |
| 213 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
| 214 | LTTNG_TRACEPOINT_EVENT_CODE(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 215 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
| 216 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
| 217 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_exit_##_name, PARAMS(_fields)) |
| 218 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
| 219 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_exit_##_template, \ |
| 220 | syscall_exit_##_name) |
| 221 | /* Enumerations only defined at inital inclusion (not here). */ |
| 222 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) |
| 223 | #define TRACE_SYSTEM syscall_exit_integers |
| 224 | #define TRACE_INCLUDE_FILE syscalls_integers |
| 225 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
| 226 | #undef TRACE_INCLUDE_FILE |
| 227 | #undef TRACE_SYSTEM |
| 228 | #define TRACE_SYSTEM syscall_exit_pointers |
| 229 | #define TRACE_INCLUDE_FILE syscalls_pointers |
| 230 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
| 231 | #undef TRACE_INCLUDE_FILE |
| 232 | #undef TRACE_SYSTEM |
| 233 | #undef SC_LTTNG_TRACEPOINT_ENUM |
| 234 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
| 235 | #undef SC_LTTNG_TRACEPOINT_EVENT |
| 236 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS |
| 237 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS |
| 238 | #undef TP_PROBE_CB |
| 239 | #undef _TRACE_SYSCALLS_INTEGERS_H |
| 240 | #undef _TRACE_SYSCALLS_POINTERS_H |
| 241 | |
| 242 | |
| 243 | /* Hijack probe callback for compat system call exit */ |
| 244 | #define TP_PROBE_CB(_template) &syscall_exit_probe |
| 245 | #define LTTNG_SC_COMPAT |
| 246 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
| 247 | LTTNG_TRACEPOINT_EVENT(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 248 | PARAMS(_fields)) |
| 249 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
| 250 | LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
| 251 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
| 252 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
| 253 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_exit_##_name, PARAMS(_fields)) |
| 254 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
| 255 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_exit_##_template, \ |
| 256 | compat_syscall_exit_##_name) |
| 257 | /* Enumerations only defined at inital inclusion (not here). */ |
| 258 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) |
| 259 | #define TRACE_SYSTEM compat_syscall_exit_integers |
| 260 | #define TRACE_INCLUDE_FILE compat_syscalls_integers |
| 261 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
| 262 | #undef TRACE_INCLUDE_FILE |
| 263 | #undef TRACE_SYSTEM |
| 264 | #define TRACE_SYSTEM compat_syscall_exit_pointers |
| 265 | #define TRACE_INCLUDE_FILE compat_syscalls_pointers |
| 266 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
| 267 | #undef TRACE_INCLUDE_FILE |
| 268 | #undef TRACE_SYSTEM |
| 269 | #undef SC_LTTNG_TRACEPOINT_ENUM |
| 270 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
| 271 | #undef SC_LTTNG_TRACEPOINT_EVENT |
| 272 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS |
| 273 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS |
| 274 | #undef TP_PROBE_CB |
| 275 | #undef _TRACE_SYSCALLS_INTEGERS_H |
| 276 | #undef _TRACE_SYSCALLS_POINTERS_H |
| 277 | #undef LTTNG_SC_COMPAT |
| 278 | |
| 279 | #undef SC_EXIT |
| 280 | |
| 281 | #undef TP_MODULE_NOINIT |
| 282 | #undef LTTNG_PACKAGE_BUILD |
| 283 | #undef CREATE_TRACE_POINTS |
| 284 | |
| 285 | struct trace_syscall_entry { |
| 286 | void *func; |
| 287 | const struct lttng_event_desc *desc; |
| 288 | const struct lttng_event_field *fields; |
| 289 | unsigned int nrargs; |
| 290 | }; |
| 291 | |
| 292 | #define CREATE_SYSCALL_TABLE |
| 293 | |
| 294 | #define SC_ENTER |
| 295 | |
| 296 | #undef sc_exit |
| 297 | #define sc_exit(...) |
| 298 | |
| 299 | #undef TRACE_SYSCALL_TABLE |
| 300 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ |
| 301 | [ _nr ] = { \ |
| 302 | .func = __event_probe__syscall_entry_##_template, \ |
| 303 | .nrargs = (_nrargs), \ |
| 304 | .fields = __event_fields___syscall_entry_##_template, \ |
| 305 | .desc = &__event_desc___syscall_entry_##_name, \ |
| 306 | }, |
| 307 | |
| 308 | /* Syscall enter tracing table */ |
| 309 | static const struct trace_syscall_entry sc_table[] = { |
| 310 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
| 311 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
| 312 | }; |
| 313 | |
| 314 | #undef TRACE_SYSCALL_TABLE |
| 315 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ |
| 316 | [ _nr ] = { \ |
| 317 | .func = __event_probe__compat_syscall_entry_##_template, \ |
| 318 | .nrargs = (_nrargs), \ |
| 319 | .fields = __event_fields___compat_syscall_entry_##_template, \ |
| 320 | .desc = &__event_desc___compat_syscall_entry_##_name, \ |
| 321 | }, |
| 322 | |
| 323 | /* Compat syscall enter table */ |
| 324 | const struct trace_syscall_entry compat_sc_table[] = { |
| 325 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
| 326 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
| 327 | }; |
| 328 | |
| 329 | #undef SC_ENTER |
| 330 | |
| 331 | #define SC_EXIT |
| 332 | |
| 333 | #undef sc_exit |
| 334 | #define sc_exit(...) __VA_ARGS__ |
| 335 | |
| 336 | #undef TRACE_SYSCALL_TABLE |
| 337 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ |
| 338 | [ _nr ] = { \ |
| 339 | .func = __event_probe__syscall_exit_##_template, \ |
| 340 | .nrargs = (_nrargs), \ |
| 341 | .fields = __event_fields___syscall_exit_##_template, \ |
| 342 | .desc = &__event_desc___syscall_exit_##_name, \ |
| 343 | }, |
| 344 | |
| 345 | /* Syscall exit table */ |
| 346 | static const struct trace_syscall_entry sc_exit_table[] = { |
| 347 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
| 348 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
| 349 | }; |
| 350 | |
| 351 | #undef TRACE_SYSCALL_TABLE |
| 352 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ |
| 353 | [ _nr ] = { \ |
| 354 | .func = __event_probe__compat_syscall_exit_##_template, \ |
| 355 | .nrargs = (_nrargs), \ |
| 356 | .fields = __event_fields___compat_syscall_exit_##_template, \ |
| 357 | .desc = &__event_desc___compat_syscall_exit_##_name, \ |
| 358 | }, |
| 359 | |
| 360 | /* Compat syscall exit table */ |
| 361 | const struct trace_syscall_entry compat_sc_exit_table[] = { |
| 362 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
| 363 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
| 364 | }; |
| 365 | |
| 366 | #undef SC_EXIT |
| 367 | |
| 368 | #undef CREATE_SYSCALL_TABLE |
| 369 | |
| 370 | struct lttng_syscall_filter { |
| 371 | DECLARE_BITMAP(sc, NR_syscalls); |
| 372 | DECLARE_BITMAP(sc_compat, NR_compat_syscalls); |
| 373 | }; |
| 374 | |
| 375 | static void syscall_entry_unknown(struct lttng_event *event, |
| 376 | struct pt_regs *regs, unsigned int id) |
| 377 | { |
| 378 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 379 | |
| 380 | lttng_syscall_get_arguments(current, regs, args); |
| 381 | if (unlikely(in_compat_syscall())) |
| 382 | __event_probe__compat_syscall_entry_unknown(event, id, args); |
| 383 | else |
| 384 | __event_probe__syscall_entry_unknown(event, id, args); |
| 385 | } |
| 386 | |
| 387 | void syscall_entry_probe(void *__data, struct pt_regs *regs, long id) |
| 388 | { |
| 389 | struct lttng_channel *chan = __data; |
| 390 | struct lttng_event *event, *unknown_event; |
| 391 | const struct trace_syscall_entry *table, *entry; |
| 392 | size_t table_len; |
| 393 | |
| 394 | if (unlikely(in_compat_syscall())) { |
| 395 | struct lttng_syscall_filter *filter; |
| 396 | |
| 397 | filter = lttng_rcu_dereference(chan->sc_filter); |
| 398 | if (filter) { |
| 399 | if (id < 0 || id >= NR_compat_syscalls |
| 400 | || !test_bit(id, filter->sc_compat)) { |
| 401 | /* System call filtered out. */ |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | table = compat_sc_table; |
| 406 | table_len = ARRAY_SIZE(compat_sc_table); |
| 407 | unknown_event = chan->sc_compat_unknown; |
| 408 | } else { |
| 409 | struct lttng_syscall_filter *filter; |
| 410 | |
| 411 | filter = lttng_rcu_dereference(chan->sc_filter); |
| 412 | if (filter) { |
| 413 | if (id < 0 || id >= NR_syscalls |
| 414 | || !test_bit(id, filter->sc)) { |
| 415 | /* System call filtered out. */ |
| 416 | return; |
| 417 | } |
| 418 | } |
| 419 | table = sc_table; |
| 420 | table_len = ARRAY_SIZE(sc_table); |
| 421 | unknown_event = chan->sc_unknown; |
| 422 | } |
| 423 | if (unlikely(id < 0 || id >= table_len)) { |
| 424 | syscall_entry_unknown(unknown_event, regs, id); |
| 425 | return; |
| 426 | } |
| 427 | if (unlikely(in_compat_syscall())) |
| 428 | event = chan->compat_sc_table[id]; |
| 429 | else |
| 430 | event = chan->sc_table[id]; |
| 431 | if (unlikely(!event)) { |
| 432 | syscall_entry_unknown(unknown_event, regs, id); |
| 433 | return; |
| 434 | } |
| 435 | entry = &table[id]; |
| 436 | WARN_ON_ONCE(!entry); |
| 437 | |
| 438 | switch (entry->nrargs) { |
| 439 | case 0: |
| 440 | { |
| 441 | void (*fptr)(void *__data) = entry->func; |
| 442 | |
| 443 | fptr(event); |
| 444 | break; |
| 445 | } |
| 446 | case 1: |
| 447 | { |
| 448 | void (*fptr)(void *__data, unsigned long arg0) = entry->func; |
| 449 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 450 | |
| 451 | lttng_syscall_get_arguments(current, regs, args); |
| 452 | fptr(event, args[0]); |
| 453 | break; |
| 454 | } |
| 455 | case 2: |
| 456 | { |
| 457 | void (*fptr)(void *__data, |
| 458 | unsigned long arg0, |
| 459 | unsigned long arg1) = entry->func; |
| 460 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 461 | |
| 462 | lttng_syscall_get_arguments(current, regs, args); |
| 463 | fptr(event, args[0], args[1]); |
| 464 | break; |
| 465 | } |
| 466 | case 3: |
| 467 | { |
| 468 | void (*fptr)(void *__data, |
| 469 | unsigned long arg0, |
| 470 | unsigned long arg1, |
| 471 | unsigned long arg2) = entry->func; |
| 472 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 473 | |
| 474 | lttng_syscall_get_arguments(current, regs, args); |
| 475 | fptr(event, args[0], args[1], args[2]); |
| 476 | break; |
| 477 | } |
| 478 | case 4: |
| 479 | { |
| 480 | void (*fptr)(void *__data, |
| 481 | unsigned long arg0, |
| 482 | unsigned long arg1, |
| 483 | unsigned long arg2, |
| 484 | unsigned long arg3) = entry->func; |
| 485 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 486 | |
| 487 | lttng_syscall_get_arguments(current, regs, args); |
| 488 | fptr(event, args[0], args[1], args[2], args[3]); |
| 489 | break; |
| 490 | } |
| 491 | case 5: |
| 492 | { |
| 493 | void (*fptr)(void *__data, |
| 494 | unsigned long arg0, |
| 495 | unsigned long arg1, |
| 496 | unsigned long arg2, |
| 497 | unsigned long arg3, |
| 498 | unsigned long arg4) = entry->func; |
| 499 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 500 | |
| 501 | lttng_syscall_get_arguments(current, regs, args); |
| 502 | fptr(event, args[0], args[1], args[2], args[3], args[4]); |
| 503 | break; |
| 504 | } |
| 505 | case 6: |
| 506 | { |
| 507 | void (*fptr)(void *__data, |
| 508 | unsigned long arg0, |
| 509 | unsigned long arg1, |
| 510 | unsigned long arg2, |
| 511 | unsigned long arg3, |
| 512 | unsigned long arg4, |
| 513 | unsigned long arg5) = entry->func; |
| 514 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 515 | |
| 516 | lttng_syscall_get_arguments(current, regs, args); |
| 517 | fptr(event, args[0], args[1], args[2], |
| 518 | args[3], args[4], args[5]); |
| 519 | break; |
| 520 | } |
| 521 | default: |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static void syscall_exit_unknown(struct lttng_event *event, |
| 527 | struct pt_regs *regs, int id, long ret) |
| 528 | { |
| 529 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 530 | |
| 531 | lttng_syscall_get_arguments(current, regs, args); |
| 532 | if (unlikely(in_compat_syscall())) |
| 533 | __event_probe__compat_syscall_exit_unknown(event, id, ret, |
| 534 | args); |
| 535 | else |
| 536 | __event_probe__syscall_exit_unknown(event, id, ret, args); |
| 537 | } |
| 538 | |
| 539 | void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret) |
| 540 | { |
| 541 | struct lttng_channel *chan = __data; |
| 542 | struct lttng_event *event, *unknown_event; |
| 543 | const struct trace_syscall_entry *table, *entry; |
| 544 | size_t table_len; |
| 545 | long id; |
| 546 | |
| 547 | id = syscall_get_nr(current, regs); |
| 548 | if (unlikely(in_compat_syscall())) { |
| 549 | struct lttng_syscall_filter *filter; |
| 550 | |
| 551 | filter = lttng_rcu_dereference(chan->sc_filter); |
| 552 | if (filter) { |
| 553 | if (id < 0 || id >= NR_compat_syscalls |
| 554 | || !test_bit(id, filter->sc_compat)) { |
| 555 | /* System call filtered out. */ |
| 556 | return; |
| 557 | } |
| 558 | } |
| 559 | table = compat_sc_exit_table; |
| 560 | table_len = ARRAY_SIZE(compat_sc_exit_table); |
| 561 | unknown_event = chan->compat_sc_exit_unknown; |
| 562 | } else { |
| 563 | struct lttng_syscall_filter *filter; |
| 564 | |
| 565 | filter = lttng_rcu_dereference(chan->sc_filter); |
| 566 | if (filter) { |
| 567 | if (id < 0 || id >= NR_syscalls |
| 568 | || !test_bit(id, filter->sc)) { |
| 569 | /* System call filtered out. */ |
| 570 | return; |
| 571 | } |
| 572 | } |
| 573 | table = sc_exit_table; |
| 574 | table_len = ARRAY_SIZE(sc_exit_table); |
| 575 | unknown_event = chan->sc_exit_unknown; |
| 576 | } |
| 577 | if (unlikely(id < 0 || id >= table_len)) { |
| 578 | syscall_exit_unknown(unknown_event, regs, id, ret); |
| 579 | return; |
| 580 | } |
| 581 | if (unlikely(in_compat_syscall())) |
| 582 | event = chan->compat_sc_exit_table[id]; |
| 583 | else |
| 584 | event = chan->sc_exit_table[id]; |
| 585 | if (unlikely(!event)) { |
| 586 | syscall_exit_unknown(unknown_event, regs, id, ret); |
| 587 | return; |
| 588 | } |
| 589 | entry = &table[id]; |
| 590 | WARN_ON_ONCE(!entry); |
| 591 | |
| 592 | switch (entry->nrargs) { |
| 593 | case 0: |
| 594 | { |
| 595 | void (*fptr)(void *__data, long ret) = entry->func; |
| 596 | |
| 597 | fptr(event, ret); |
| 598 | break; |
| 599 | } |
| 600 | case 1: |
| 601 | { |
| 602 | void (*fptr)(void *__data, |
| 603 | long ret, |
| 604 | unsigned long arg0) = entry->func; |
| 605 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 606 | |
| 607 | lttng_syscall_get_arguments(current, regs, args); |
| 608 | fptr(event, ret, args[0]); |
| 609 | break; |
| 610 | } |
| 611 | case 2: |
| 612 | { |
| 613 | void (*fptr)(void *__data, |
| 614 | long ret, |
| 615 | unsigned long arg0, |
| 616 | unsigned long arg1) = entry->func; |
| 617 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 618 | |
| 619 | lttng_syscall_get_arguments(current, regs, args); |
| 620 | fptr(event, ret, args[0], args[1]); |
| 621 | break; |
| 622 | } |
| 623 | case 3: |
| 624 | { |
| 625 | void (*fptr)(void *__data, |
| 626 | long ret, |
| 627 | unsigned long arg0, |
| 628 | unsigned long arg1, |
| 629 | unsigned long arg2) = entry->func; |
| 630 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 631 | |
| 632 | lttng_syscall_get_arguments(current, regs, args); |
| 633 | fptr(event, ret, args[0], args[1], args[2]); |
| 634 | break; |
| 635 | } |
| 636 | case 4: |
| 637 | { |
| 638 | void (*fptr)(void *__data, |
| 639 | long ret, |
| 640 | unsigned long arg0, |
| 641 | unsigned long arg1, |
| 642 | unsigned long arg2, |
| 643 | unsigned long arg3) = entry->func; |
| 644 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 645 | |
| 646 | lttng_syscall_get_arguments(current, regs, args); |
| 647 | fptr(event, ret, args[0], args[1], args[2], args[3]); |
| 648 | break; |
| 649 | } |
| 650 | case 5: |
| 651 | { |
| 652 | void (*fptr)(void *__data, |
| 653 | long ret, |
| 654 | unsigned long arg0, |
| 655 | unsigned long arg1, |
| 656 | unsigned long arg2, |
| 657 | unsigned long arg3, |
| 658 | unsigned long arg4) = entry->func; |
| 659 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 660 | |
| 661 | lttng_syscall_get_arguments(current, regs, args); |
| 662 | fptr(event, ret, args[0], args[1], args[2], args[3], args[4]); |
| 663 | break; |
| 664 | } |
| 665 | case 6: |
| 666 | { |
| 667 | void (*fptr)(void *__data, |
| 668 | long ret, |
| 669 | unsigned long arg0, |
| 670 | unsigned long arg1, |
| 671 | unsigned long arg2, |
| 672 | unsigned long arg3, |
| 673 | unsigned long arg4, |
| 674 | unsigned long arg5) = entry->func; |
| 675 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
| 676 | |
| 677 | lttng_syscall_get_arguments(current, regs, args); |
| 678 | fptr(event, ret, args[0], args[1], args[2], |
| 679 | args[3], args[4], args[5]); |
| 680 | break; |
| 681 | } |
| 682 | default: |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * noinline to diminish caller stack size. |
| 689 | * Should be called with sessions lock held. |
| 690 | */ |
| 691 | static |
| 692 | int fill_table(const struct trace_syscall_entry *table, size_t table_len, |
| 693 | struct lttng_event **chan_table, struct lttng_channel *chan, |
| 694 | void *filter, enum sc_type type) |
| 695 | { |
| 696 | const struct lttng_event_desc *desc; |
| 697 | unsigned int i; |
| 698 | |
| 699 | /* Allocate events for each syscall, insert into table */ |
| 700 | for (i = 0; i < table_len; i++) { |
| 701 | struct lttng_kernel_event ev; |
| 702 | desc = table[i].desc; |
| 703 | |
| 704 | if (!desc) { |
| 705 | /* Unknown syscall */ |
| 706 | continue; |
| 707 | } |
| 708 | /* |
| 709 | * Skip those already populated by previous failed |
| 710 | * register for this channel. |
| 711 | */ |
| 712 | if (chan_table[i]) |
| 713 | continue; |
| 714 | memset(&ev, 0, sizeof(ev)); |
| 715 | switch (type) { |
| 716 | case SC_TYPE_ENTRY: |
| 717 | strncpy(ev.name, SYSCALL_ENTRY_STR, |
| 718 | LTTNG_KERNEL_SYM_NAME_LEN); |
| 719 | break; |
| 720 | case SC_TYPE_EXIT: |
| 721 | strncpy(ev.name, SYSCALL_EXIT_STR, |
| 722 | LTTNG_KERNEL_SYM_NAME_LEN); |
| 723 | break; |
| 724 | case SC_TYPE_COMPAT_ENTRY: |
| 725 | strncpy(ev.name, COMPAT_SYSCALL_ENTRY_STR, |
| 726 | LTTNG_KERNEL_SYM_NAME_LEN); |
| 727 | break; |
| 728 | case SC_TYPE_COMPAT_EXIT: |
| 729 | strncpy(ev.name, COMPAT_SYSCALL_EXIT_STR, |
| 730 | LTTNG_KERNEL_SYM_NAME_LEN); |
| 731 | break; |
| 732 | default: |
| 733 | BUG_ON(1); |
| 734 | break; |
| 735 | } |
| 736 | strncat(ev.name, desc->name, |
| 737 | LTTNG_KERNEL_SYM_NAME_LEN - strlen(ev.name) - 1); |
| 738 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 739 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
| 740 | chan_table[i] = _lttng_event_create(chan, &ev, filter, |
| 741 | desc, ev.instrumentation); |
| 742 | WARN_ON_ONCE(!chan_table[i]); |
| 743 | if (IS_ERR(chan_table[i])) { |
| 744 | /* |
| 745 | * If something goes wrong in event registration |
| 746 | * after the first one, we have no choice but to |
| 747 | * leave the previous events in there, until |
| 748 | * deleted by session teardown. |
| 749 | */ |
| 750 | return PTR_ERR(chan_table[i]); |
| 751 | } |
| 752 | } |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * Should be called with sessions lock held. |
| 758 | */ |
| 759 | int lttng_syscalls_register(struct lttng_channel *chan, void *filter) |
| 760 | { |
| 761 | struct lttng_kernel_event ev; |
| 762 | int ret; |
| 763 | |
| 764 | wrapper_vmalloc_sync_mappings(); |
| 765 | |
| 766 | if (!chan->sc_table) { |
| 767 | /* create syscall table mapping syscall to events */ |
| 768 | chan->sc_table = kzalloc(sizeof(struct lttng_event *) |
| 769 | * ARRAY_SIZE(sc_table), GFP_KERNEL); |
| 770 | if (!chan->sc_table) |
| 771 | return -ENOMEM; |
| 772 | } |
| 773 | if (!chan->sc_exit_table) { |
| 774 | /* create syscall table mapping syscall to events */ |
| 775 | chan->sc_exit_table = kzalloc(sizeof(struct lttng_event *) |
| 776 | * ARRAY_SIZE(sc_exit_table), GFP_KERNEL); |
| 777 | if (!chan->sc_exit_table) |
| 778 | return -ENOMEM; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | #ifdef CONFIG_COMPAT |
| 783 | if (!chan->compat_sc_table) { |
| 784 | /* create syscall table mapping compat syscall to events */ |
| 785 | chan->compat_sc_table = kzalloc(sizeof(struct lttng_event *) |
| 786 | * ARRAY_SIZE(compat_sc_table), GFP_KERNEL); |
| 787 | if (!chan->compat_sc_table) |
| 788 | return -ENOMEM; |
| 789 | } |
| 790 | |
| 791 | if (!chan->compat_sc_exit_table) { |
| 792 | /* create syscall table mapping compat syscall to events */ |
| 793 | chan->compat_sc_exit_table = kzalloc(sizeof(struct lttng_event *) |
| 794 | * ARRAY_SIZE(compat_sc_exit_table), GFP_KERNEL); |
| 795 | if (!chan->compat_sc_exit_table) |
| 796 | return -ENOMEM; |
| 797 | } |
| 798 | #endif |
| 799 | if (!chan->sc_unknown) { |
| 800 | const struct lttng_event_desc *desc = |
| 801 | &__event_desc___syscall_entry_unknown; |
| 802 | |
| 803 | memset(&ev, 0, sizeof(ev)); |
| 804 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 805 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 806 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
| 807 | chan->sc_unknown = _lttng_event_create(chan, &ev, filter, |
| 808 | desc, |
| 809 | ev.instrumentation); |
| 810 | WARN_ON_ONCE(!chan->sc_unknown); |
| 811 | if (IS_ERR(chan->sc_unknown)) { |
| 812 | return PTR_ERR(chan->sc_unknown); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (!chan->sc_compat_unknown) { |
| 817 | const struct lttng_event_desc *desc = |
| 818 | &__event_desc___compat_syscall_entry_unknown; |
| 819 | |
| 820 | memset(&ev, 0, sizeof(ev)); |
| 821 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 822 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 823 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
| 824 | chan->sc_compat_unknown = _lttng_event_create(chan, &ev, filter, |
| 825 | desc, |
| 826 | ev.instrumentation); |
| 827 | WARN_ON_ONCE(!chan->sc_unknown); |
| 828 | if (IS_ERR(chan->sc_compat_unknown)) { |
| 829 | return PTR_ERR(chan->sc_compat_unknown); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (!chan->compat_sc_exit_unknown) { |
| 834 | const struct lttng_event_desc *desc = |
| 835 | &__event_desc___compat_syscall_exit_unknown; |
| 836 | |
| 837 | memset(&ev, 0, sizeof(ev)); |
| 838 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 839 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 840 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
| 841 | chan->compat_sc_exit_unknown = _lttng_event_create(chan, &ev, |
| 842 | filter, desc, |
| 843 | ev.instrumentation); |
| 844 | WARN_ON_ONCE(!chan->compat_sc_exit_unknown); |
| 845 | if (IS_ERR(chan->compat_sc_exit_unknown)) { |
| 846 | return PTR_ERR(chan->compat_sc_exit_unknown); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | if (!chan->sc_exit_unknown) { |
| 851 | const struct lttng_event_desc *desc = |
| 852 | &__event_desc___syscall_exit_unknown; |
| 853 | |
| 854 | memset(&ev, 0, sizeof(ev)); |
| 855 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
| 856 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 857 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
| 858 | chan->sc_exit_unknown = _lttng_event_create(chan, &ev, filter, |
| 859 | desc, ev.instrumentation); |
| 860 | WARN_ON_ONCE(!chan->sc_exit_unknown); |
| 861 | if (IS_ERR(chan->sc_exit_unknown)) { |
| 862 | return PTR_ERR(chan->sc_exit_unknown); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | ret = fill_table(sc_table, ARRAY_SIZE(sc_table), |
| 867 | chan->sc_table, chan, filter, SC_TYPE_ENTRY); |
| 868 | if (ret) |
| 869 | return ret; |
| 870 | ret = fill_table(sc_exit_table, ARRAY_SIZE(sc_exit_table), |
| 871 | chan->sc_exit_table, chan, filter, SC_TYPE_EXIT); |
| 872 | if (ret) |
| 873 | return ret; |
| 874 | |
| 875 | #ifdef CONFIG_COMPAT |
| 876 | ret = fill_table(compat_sc_table, ARRAY_SIZE(compat_sc_table), |
| 877 | chan->compat_sc_table, chan, filter, |
| 878 | SC_TYPE_COMPAT_ENTRY); |
| 879 | if (ret) |
| 880 | return ret; |
| 881 | ret = fill_table(compat_sc_exit_table, ARRAY_SIZE(compat_sc_exit_table), |
| 882 | chan->compat_sc_exit_table, chan, filter, |
| 883 | SC_TYPE_COMPAT_EXIT); |
| 884 | if (ret) |
| 885 | return ret; |
| 886 | #endif |
| 887 | if (!chan->sys_enter_registered) { |
| 888 | ret = lttng_wrapper_tracepoint_probe_register("sys_enter", |
| 889 | (void *) syscall_entry_probe, chan); |
| 890 | if (ret) |
| 891 | return ret; |
| 892 | chan->sys_enter_registered = 1; |
| 893 | } |
| 894 | /* |
| 895 | * We change the name of sys_exit tracepoint due to namespace |
| 896 | * conflict with sys_exit syscall entry. |
| 897 | */ |
| 898 | if (!chan->sys_exit_registered) { |
| 899 | ret = lttng_wrapper_tracepoint_probe_register("sys_exit", |
| 900 | (void *) syscall_exit_probe, chan); |
| 901 | if (ret) { |
| 902 | WARN_ON_ONCE(lttng_wrapper_tracepoint_probe_unregister("sys_enter", |
| 903 | (void *) syscall_entry_probe, chan)); |
| 904 | return ret; |
| 905 | } |
| 906 | chan->sys_exit_registered = 1; |
| 907 | } |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Only called at session destruction. |
| 913 | */ |
| 914 | int lttng_syscalls_unregister(struct lttng_channel *chan) |
| 915 | { |
| 916 | int ret; |
| 917 | |
| 918 | if (!chan->sc_table) |
| 919 | return 0; |
| 920 | if (chan->sys_enter_registered) { |
| 921 | ret = lttng_wrapper_tracepoint_probe_unregister("sys_enter", |
| 922 | (void *) syscall_entry_probe, chan); |
| 923 | if (ret) |
| 924 | return ret; |
| 925 | chan->sys_enter_registered = 0; |
| 926 | } |
| 927 | if (chan->sys_exit_registered) { |
| 928 | ret = lttng_wrapper_tracepoint_probe_unregister("sys_exit", |
| 929 | (void *) syscall_exit_probe, chan); |
| 930 | if (ret) |
| 931 | return ret; |
| 932 | chan->sys_exit_registered = 0; |
| 933 | } |
| 934 | /* lttng_event destroy will be performed by lttng_session_destroy() */ |
| 935 | kfree(chan->sc_table); |
| 936 | kfree(chan->sc_exit_table); |
| 937 | #ifdef CONFIG_COMPAT |
| 938 | kfree(chan->compat_sc_table); |
| 939 | kfree(chan->compat_sc_exit_table); |
| 940 | #endif |
| 941 | kfree(chan->sc_filter); |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static |
| 946 | int get_syscall_nr(const char *syscall_name) |
| 947 | { |
| 948 | int syscall_nr = -1; |
| 949 | int i; |
| 950 | |
| 951 | for (i = 0; i < ARRAY_SIZE(sc_table); i++) { |
| 952 | const struct trace_syscall_entry *entry; |
| 953 | const char *it_name; |
| 954 | |
| 955 | entry = &sc_table[i]; |
| 956 | if (!entry->desc) |
| 957 | continue; |
| 958 | it_name = entry->desc->name; |
| 959 | it_name += strlen(SYSCALL_ENTRY_STR); |
| 960 | if (!strcmp(syscall_name, it_name)) { |
| 961 | syscall_nr = i; |
| 962 | break; |
| 963 | } |
| 964 | } |
| 965 | return syscall_nr; |
| 966 | } |
| 967 | |
| 968 | static |
| 969 | int get_compat_syscall_nr(const char *syscall_name) |
| 970 | { |
| 971 | int syscall_nr = -1; |
| 972 | int i; |
| 973 | |
| 974 | for (i = 0; i < ARRAY_SIZE(compat_sc_table); i++) { |
| 975 | const struct trace_syscall_entry *entry; |
| 976 | const char *it_name; |
| 977 | |
| 978 | entry = &compat_sc_table[i]; |
| 979 | if (!entry->desc) |
| 980 | continue; |
| 981 | it_name = entry->desc->name; |
| 982 | it_name += strlen(COMPAT_SYSCALL_ENTRY_STR); |
| 983 | if (!strcmp(syscall_name, it_name)) { |
| 984 | syscall_nr = i; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | return syscall_nr; |
| 989 | } |
| 990 | |
| 991 | static |
| 992 | uint32_t get_sc_tables_len(void) |
| 993 | { |
| 994 | return ARRAY_SIZE(sc_table) + ARRAY_SIZE(compat_sc_table); |
| 995 | } |
| 996 | |
| 997 | int lttng_syscall_filter_enable(struct lttng_channel *chan, |
| 998 | const char *name) |
| 999 | { |
| 1000 | int syscall_nr, compat_syscall_nr, ret; |
| 1001 | struct lttng_syscall_filter *filter; |
| 1002 | |
| 1003 | WARN_ON_ONCE(!chan->sc_table); |
| 1004 | |
| 1005 | if (!name) { |
| 1006 | /* Enable all system calls by removing filter */ |
| 1007 | if (chan->sc_filter) { |
| 1008 | filter = chan->sc_filter; |
| 1009 | rcu_assign_pointer(chan->sc_filter, NULL); |
| 1010 | synchronize_trace(); |
| 1011 | kfree(filter); |
| 1012 | } |
| 1013 | chan->syscall_all = 1; |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | if (!chan->sc_filter) { |
| 1018 | if (chan->syscall_all) { |
| 1019 | /* |
| 1020 | * All syscalls are already enabled. |
| 1021 | */ |
| 1022 | return -EEXIST; |
| 1023 | } |
| 1024 | filter = kzalloc(sizeof(struct lttng_syscall_filter), |
| 1025 | GFP_KERNEL); |
| 1026 | if (!filter) |
| 1027 | return -ENOMEM; |
| 1028 | } else { |
| 1029 | filter = chan->sc_filter; |
| 1030 | } |
| 1031 | syscall_nr = get_syscall_nr(name); |
| 1032 | compat_syscall_nr = get_compat_syscall_nr(name); |
| 1033 | if (syscall_nr < 0 && compat_syscall_nr < 0) { |
| 1034 | ret = -ENOENT; |
| 1035 | goto error; |
| 1036 | } |
| 1037 | if (syscall_nr >= 0) { |
| 1038 | if (test_bit(syscall_nr, filter->sc)) { |
| 1039 | ret = -EEXIST; |
| 1040 | goto error; |
| 1041 | } |
| 1042 | bitmap_set(filter->sc, syscall_nr, 1); |
| 1043 | } |
| 1044 | if (compat_syscall_nr >= 0) { |
| 1045 | if (test_bit(compat_syscall_nr, filter->sc_compat)) { |
| 1046 | ret = -EEXIST; |
| 1047 | goto error; |
| 1048 | } |
| 1049 | bitmap_set(filter->sc_compat, compat_syscall_nr, 1); |
| 1050 | } |
| 1051 | if (!chan->sc_filter) |
| 1052 | rcu_assign_pointer(chan->sc_filter, filter); |
| 1053 | return 0; |
| 1054 | |
| 1055 | error: |
| 1056 | if (!chan->sc_filter) |
| 1057 | kfree(filter); |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | int lttng_syscall_filter_disable(struct lttng_channel *chan, |
| 1062 | const char *name) |
| 1063 | { |
| 1064 | int syscall_nr, compat_syscall_nr, ret; |
| 1065 | struct lttng_syscall_filter *filter; |
| 1066 | |
| 1067 | WARN_ON_ONCE(!chan->sc_table); |
| 1068 | |
| 1069 | if (!chan->sc_filter) { |
| 1070 | if (!chan->syscall_all) |
| 1071 | return -EEXIST; |
| 1072 | filter = kzalloc(sizeof(struct lttng_syscall_filter), |
| 1073 | GFP_KERNEL); |
| 1074 | if (!filter) |
| 1075 | return -ENOMEM; |
| 1076 | /* Trace all system calls, then apply disable. */ |
| 1077 | bitmap_set(filter->sc, 0, NR_syscalls); |
| 1078 | bitmap_set(filter->sc_compat, 0, NR_compat_syscalls); |
| 1079 | } else { |
| 1080 | filter = chan->sc_filter; |
| 1081 | } |
| 1082 | |
| 1083 | if (!name) { |
| 1084 | /* Fail if all syscalls are already disabled. */ |
| 1085 | if (bitmap_empty(filter->sc, NR_syscalls) |
| 1086 | && bitmap_empty(filter->sc_compat, |
| 1087 | NR_compat_syscalls)) { |
| 1088 | ret = -EEXIST; |
| 1089 | goto error; |
| 1090 | } |
| 1091 | |
| 1092 | /* Disable all system calls */ |
| 1093 | bitmap_clear(filter->sc, 0, NR_syscalls); |
| 1094 | bitmap_clear(filter->sc_compat, 0, NR_compat_syscalls); |
| 1095 | goto apply_filter; |
| 1096 | } |
| 1097 | syscall_nr = get_syscall_nr(name); |
| 1098 | compat_syscall_nr = get_compat_syscall_nr(name); |
| 1099 | if (syscall_nr < 0 && compat_syscall_nr < 0) { |
| 1100 | ret = -ENOENT; |
| 1101 | goto error; |
| 1102 | } |
| 1103 | if (syscall_nr >= 0) { |
| 1104 | if (!test_bit(syscall_nr, filter->sc)) { |
| 1105 | ret = -EEXIST; |
| 1106 | goto error; |
| 1107 | } |
| 1108 | bitmap_clear(filter->sc, syscall_nr, 1); |
| 1109 | } |
| 1110 | if (compat_syscall_nr >= 0) { |
| 1111 | if (!test_bit(compat_syscall_nr, filter->sc_compat)) { |
| 1112 | ret = -EEXIST; |
| 1113 | goto error; |
| 1114 | } |
| 1115 | bitmap_clear(filter->sc_compat, compat_syscall_nr, 1); |
| 1116 | } |
| 1117 | apply_filter: |
| 1118 | if (!chan->sc_filter) |
| 1119 | rcu_assign_pointer(chan->sc_filter, filter); |
| 1120 | chan->syscall_all = 0; |
| 1121 | return 0; |
| 1122 | |
| 1123 | error: |
| 1124 | if (!chan->sc_filter) |
| 1125 | kfree(filter); |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | static |
| 1130 | const struct trace_syscall_entry *syscall_list_get_entry(loff_t *pos) |
| 1131 | { |
| 1132 | const struct trace_syscall_entry *entry; |
| 1133 | int iter = 0; |
| 1134 | |
| 1135 | for (entry = sc_table; |
| 1136 | entry < sc_table + ARRAY_SIZE(sc_table); |
| 1137 | entry++) { |
| 1138 | if (iter++ >= *pos) |
| 1139 | return entry; |
| 1140 | } |
| 1141 | for (entry = compat_sc_table; |
| 1142 | entry < compat_sc_table + ARRAY_SIZE(compat_sc_table); |
| 1143 | entry++) { |
| 1144 | if (iter++ >= *pos) |
| 1145 | return entry; |
| 1146 | } |
| 1147 | /* End of list */ |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | |
| 1151 | static |
| 1152 | void *syscall_list_start(struct seq_file *m, loff_t *pos) |
| 1153 | { |
| 1154 | return (void *) syscall_list_get_entry(pos); |
| 1155 | } |
| 1156 | |
| 1157 | static |
| 1158 | void *syscall_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 1159 | { |
| 1160 | (*ppos)++; |
| 1161 | return (void *) syscall_list_get_entry(ppos); |
| 1162 | } |
| 1163 | |
| 1164 | static |
| 1165 | void syscall_list_stop(struct seq_file *m, void *p) |
| 1166 | { |
| 1167 | } |
| 1168 | |
| 1169 | static |
| 1170 | int get_sc_table(const struct trace_syscall_entry *entry, |
| 1171 | const struct trace_syscall_entry **table, |
| 1172 | unsigned int *bitness) |
| 1173 | { |
| 1174 | if (entry >= sc_table && entry < sc_table + ARRAY_SIZE(sc_table)) { |
| 1175 | if (bitness) |
| 1176 | *bitness = BITS_PER_LONG; |
| 1177 | if (table) |
| 1178 | *table = sc_table; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | if (!(entry >= compat_sc_table |
| 1182 | && entry < compat_sc_table + ARRAY_SIZE(compat_sc_table))) { |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | if (bitness) |
| 1186 | *bitness = 32; |
| 1187 | if (table) |
| 1188 | *table = compat_sc_table; |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | static |
| 1193 | int syscall_list_show(struct seq_file *m, void *p) |
| 1194 | { |
| 1195 | const struct trace_syscall_entry *table, *entry = p; |
| 1196 | unsigned int bitness; |
| 1197 | unsigned long index; |
| 1198 | int ret; |
| 1199 | const char *name; |
| 1200 | |
| 1201 | ret = get_sc_table(entry, &table, &bitness); |
| 1202 | if (ret) |
| 1203 | return ret; |
| 1204 | if (!entry->desc) |
| 1205 | return 0; |
| 1206 | if (table == sc_table) { |
| 1207 | index = entry - table; |
| 1208 | name = &entry->desc->name[strlen(SYSCALL_ENTRY_STR)]; |
| 1209 | } else { |
| 1210 | index = (entry - table) + ARRAY_SIZE(sc_table); |
| 1211 | name = &entry->desc->name[strlen(COMPAT_SYSCALL_ENTRY_STR)]; |
| 1212 | } |
| 1213 | seq_printf(m, "syscall { index = %lu; name = %s; bitness = %u; };\n", |
| 1214 | index, name, bitness); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static |
| 1219 | const struct seq_operations lttng_syscall_list_seq_ops = { |
| 1220 | .start = syscall_list_start, |
| 1221 | .next = syscall_list_next, |
| 1222 | .stop = syscall_list_stop, |
| 1223 | .show = syscall_list_show, |
| 1224 | }; |
| 1225 | |
| 1226 | static |
| 1227 | int lttng_syscall_list_open(struct inode *inode, struct file *file) |
| 1228 | { |
| 1229 | return seq_open(file, <tng_syscall_list_seq_ops); |
| 1230 | } |
| 1231 | |
| 1232 | const struct file_operations lttng_syscall_list_fops = { |
| 1233 | .owner = THIS_MODULE, |
| 1234 | .open = lttng_syscall_list_open, |
| 1235 | .read = seq_read, |
| 1236 | .llseek = seq_lseek, |
| 1237 | .release = seq_release, |
| 1238 | }; |
| 1239 | |
| 1240 | long lttng_channel_syscall_mask(struct lttng_channel *channel, |
| 1241 | struct lttng_kernel_syscall_mask __user *usyscall_mask) |
| 1242 | { |
| 1243 | uint32_t len, sc_tables_len, bitmask_len; |
| 1244 | int ret = 0, bit; |
| 1245 | char *tmp_mask; |
| 1246 | struct lttng_syscall_filter *filter; |
| 1247 | |
| 1248 | ret = get_user(len, &usyscall_mask->len); |
| 1249 | if (ret) |
| 1250 | return ret; |
| 1251 | sc_tables_len = get_sc_tables_len(); |
| 1252 | bitmask_len = ALIGN(sc_tables_len, 8) >> 3; |
| 1253 | if (len < sc_tables_len) { |
| 1254 | return put_user(sc_tables_len, &usyscall_mask->len); |
| 1255 | } |
| 1256 | /* Array is large enough, we can copy array to user-space. */ |
| 1257 | tmp_mask = kzalloc(bitmask_len, GFP_KERNEL); |
| 1258 | if (!tmp_mask) |
| 1259 | return -ENOMEM; |
| 1260 | filter = channel->sc_filter; |
| 1261 | |
| 1262 | for (bit = 0; bit < ARRAY_SIZE(sc_table); bit++) { |
| 1263 | char state; |
| 1264 | |
| 1265 | if (channel->sc_table) { |
| 1266 | if (filter) |
| 1267 | state = test_bit(bit, filter->sc); |
| 1268 | else |
| 1269 | state = 1; |
| 1270 | } else { |
| 1271 | state = 0; |
| 1272 | } |
| 1273 | bt_bitfield_write_be(tmp_mask, char, bit, 1, state); |
| 1274 | } |
| 1275 | for (; bit < sc_tables_len; bit++) { |
| 1276 | char state; |
| 1277 | |
| 1278 | if (channel->compat_sc_table) { |
| 1279 | if (filter) |
| 1280 | state = test_bit(bit - ARRAY_SIZE(sc_table), |
| 1281 | filter->sc_compat); |
| 1282 | else |
| 1283 | state = 1; |
| 1284 | } else { |
| 1285 | state = 0; |
| 1286 | } |
| 1287 | bt_bitfield_write_be(tmp_mask, char, bit, 1, state); |
| 1288 | } |
| 1289 | if (copy_to_user(usyscall_mask->mask, tmp_mask, bitmask_len)) |
| 1290 | ret = -EFAULT; |
| 1291 | kfree(tmp_mask); |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | int lttng_abi_syscall_list(void) |
| 1296 | { |
| 1297 | struct file *syscall_list_file; |
| 1298 | int file_fd, ret; |
| 1299 | |
| 1300 | file_fd = lttng_get_unused_fd(); |
| 1301 | if (file_fd < 0) { |
| 1302 | ret = file_fd; |
| 1303 | goto fd_error; |
| 1304 | } |
| 1305 | |
| 1306 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", |
| 1307 | <tng_syscall_list_fops, |
| 1308 | NULL, O_RDWR); |
| 1309 | if (IS_ERR(syscall_list_file)) { |
| 1310 | ret = PTR_ERR(syscall_list_file); |
| 1311 | goto file_error; |
| 1312 | } |
| 1313 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); |
| 1314 | if (ret < 0) |
| 1315 | goto open_error; |
| 1316 | fd_install(file_fd, syscall_list_file); |
| 1317 | return file_fd; |
| 1318 | |
| 1319 | open_error: |
| 1320 | fput(syscall_list_file); |
| 1321 | file_error: |
| 1322 | put_unused_fd(file_fd); |
| 1323 | fd_error: |
| 1324 | return ret; |
| 1325 | } |