| 1 | /* |
| 2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include <common/credentials.h> |
| 10 | #include <common/error.h> |
| 11 | #include <common/hashtable/hashtable.h> |
| 12 | #include <common/hashtable/utils.h> |
| 13 | #include <common/macros.h> |
| 14 | #include <common/mi-lttng.h> |
| 15 | #include <common/payload-view.h> |
| 16 | #include <common/payload.h> |
| 17 | #include <common/runas.h> |
| 18 | #include <ctype.h> |
| 19 | #include <lttng/constant.h> |
| 20 | #include <lttng/event-rule/event-rule-internal.h> |
| 21 | #include <lttng/event-rule/event-rule.h> |
| 22 | #include <lttng/event-rule/kernel-kprobe-internal.h> |
| 23 | #include <lttng/kernel-probe-internal.h> |
| 24 | #include <lttng/kernel-probe.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #define IS_KPROBE_EVENT_RULE(rule) \ |
| 28 | (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE) |
| 29 | |
| 30 | #if (LTTNG_SYMBOL_NAME_LEN == 256) |
| 31 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" |
| 32 | #endif |
| 33 | |
| 34 | static void lttng_event_rule_kernel_kprobe_destroy(struct lttng_event_rule *rule) |
| 35 | { |
| 36 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 37 | |
| 38 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 39 | |
| 40 | lttng_kernel_probe_location_destroy(kprobe->location); |
| 41 | free(kprobe->name); |
| 42 | free(kprobe); |
| 43 | } |
| 44 | |
| 45 | static bool lttng_event_rule_kernel_kprobe_validate( |
| 46 | const struct lttng_event_rule *rule) |
| 47 | { |
| 48 | bool valid = false; |
| 49 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 50 | |
| 51 | if (!rule) { |
| 52 | goto end; |
| 53 | } |
| 54 | |
| 55 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 56 | |
| 57 | /* Required field. */ |
| 58 | if (!kprobe->name) { |
| 59 | ERR("Invalid name event rule: a name must be set."); |
| 60 | goto end; |
| 61 | } |
| 62 | |
| 63 | /* Required field. */ |
| 64 | if(!kprobe->location) { |
| 65 | ERR("Invalid name event rule: a location must be set."); |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | valid = true; |
| 70 | end: |
| 71 | return valid; |
| 72 | } |
| 73 | |
| 74 | static int lttng_event_rule_kernel_kprobe_serialize( |
| 75 | const struct lttng_event_rule *rule, |
| 76 | struct lttng_payload *payload) |
| 77 | { |
| 78 | int ret; |
| 79 | size_t name_len, header_offset, size_before_location; |
| 80 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 81 | struct lttng_event_rule_kernel_kprobe_comm kprobe_comm; |
| 82 | struct lttng_event_rule_kernel_kprobe_comm *header; |
| 83 | |
| 84 | if (!rule || !IS_KPROBE_EVENT_RULE(rule)) { |
| 85 | ret = -1; |
| 86 | goto end; |
| 87 | } |
| 88 | |
| 89 | header_offset = payload->buffer.size; |
| 90 | |
| 91 | DBG("Serializing kprobe event rule."); |
| 92 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 93 | |
| 94 | name_len = strlen(kprobe->name) + 1; |
| 95 | kprobe_comm.name_len = name_len; |
| 96 | |
| 97 | ret = lttng_dynamic_buffer_append( |
| 98 | &payload->buffer, &kprobe_comm, sizeof(kprobe_comm)); |
| 99 | if (ret) { |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | ret = lttng_dynamic_buffer_append(&payload->buffer, kprobe->name, name_len); |
| 104 | if (ret) { |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | size_before_location = payload->buffer.size; |
| 109 | |
| 110 | ret = lttng_kernel_probe_location_serialize(kprobe->location, payload); |
| 111 | if (ret < 0) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | /* Update the header regarding the probe size. */ |
| 116 | header = (struct lttng_event_rule_kernel_kprobe_comm*) ( |
| 117 | (char *) payload->buffer.data + header_offset); |
| 118 | header->location_len = payload->buffer.size - size_before_location; |
| 119 | |
| 120 | ret = 0; |
| 121 | |
| 122 | end: |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static bool lttng_event_rule_kernel_kprobe_is_equal(const struct lttng_event_rule *_a, |
| 127 | const struct lttng_event_rule *_b) |
| 128 | { |
| 129 | bool is_equal = false; |
| 130 | struct lttng_event_rule_kernel_kprobe *a, *b; |
| 131 | |
| 132 | a = container_of(_a, struct lttng_event_rule_kernel_kprobe, parent); |
| 133 | b = container_of(_b, struct lttng_event_rule_kernel_kprobe, parent); |
| 134 | |
| 135 | /* Quick checks */ |
| 136 | if (!!a->name != !!b->name) { |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | /* Long check */ |
| 141 | assert(a->name); |
| 142 | assert(b->name); |
| 143 | if (strcmp(a->name, b->name)) { |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | is_equal = lttng_kernel_probe_location_is_equal( |
| 148 | a->location, b->location); |
| 149 | end: |
| 150 | return is_equal; |
| 151 | } |
| 152 | |
| 153 | static enum lttng_error_code lttng_event_rule_kernel_kprobe_generate_filter_bytecode( |
| 154 | struct lttng_event_rule *rule, |
| 155 | const struct lttng_credentials *creds) |
| 156 | { |
| 157 | /* Nothing to do. */ |
| 158 | return LTTNG_OK; |
| 159 | } |
| 160 | |
| 161 | static const char *lttng_event_rule_kernel_kprobe_get_filter( |
| 162 | const struct lttng_event_rule *rule) |
| 163 | { |
| 164 | /* Not supported. */ |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | static const struct lttng_bytecode * |
| 169 | lttng_event_rule_kernel_kprobe_get_filter_bytecode(const struct lttng_event_rule *rule) |
| 170 | { |
| 171 | /* Not supported. */ |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | static enum lttng_event_rule_generate_exclusions_status |
| 176 | lttng_event_rule_kernel_kprobe_generate_exclusions(const struct lttng_event_rule *rule, |
| 177 | struct lttng_event_exclusion **exclusions) |
| 178 | { |
| 179 | /* Not supported. */ |
| 180 | *exclusions = NULL; |
| 181 | return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; |
| 182 | } |
| 183 | |
| 184 | static unsigned long |
| 185 | lttng_event_rule_kernel_kprobe_hash( |
| 186 | const struct lttng_event_rule *rule) |
| 187 | { |
| 188 | unsigned long hash; |
| 189 | struct lttng_event_rule_kernel_kprobe *krule = |
| 190 | container_of(rule, typeof(*krule), parent); |
| 191 | |
| 192 | hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE, |
| 193 | lttng_ht_seed); |
| 194 | hash ^= hash_key_str(krule->name, lttng_ht_seed); |
| 195 | hash ^= lttng_kernel_probe_location_hash(krule->location); |
| 196 | |
| 197 | return hash; |
| 198 | } |
| 199 | |
| 200 | static |
| 201 | int kernel_probe_set_location( |
| 202 | struct lttng_event_rule_kernel_kprobe *kprobe, |
| 203 | const struct lttng_kernel_probe_location *location) |
| 204 | { |
| 205 | int ret; |
| 206 | struct lttng_kernel_probe_location *location_copy = NULL; |
| 207 | |
| 208 | if (!kprobe || !location || kprobe->location) { |
| 209 | ret = -1; |
| 210 | goto end; |
| 211 | } |
| 212 | |
| 213 | location_copy = lttng_kernel_probe_location_copy(location); |
| 214 | if (!location_copy) { |
| 215 | ret = -1; |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | kprobe->location = location_copy; |
| 220 | location_copy = NULL; |
| 221 | ret = 0; |
| 222 | end: |
| 223 | lttng_kernel_probe_location_destroy(location_copy); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static |
| 228 | enum lttng_error_code lttng_event_rule_kernel_kprobe_mi_serialize( |
| 229 | const struct lttng_event_rule *rule, struct mi_writer *writer) |
| 230 | { |
| 231 | int ret; |
| 232 | enum lttng_error_code ret_code; |
| 233 | enum lttng_event_rule_status status; |
| 234 | const char *event_name = NULL; |
| 235 | const struct lttng_kernel_probe_location *location = NULL; |
| 236 | |
| 237 | assert(rule); |
| 238 | assert(writer); |
| 239 | assert(IS_KPROBE_EVENT_RULE(rule)); |
| 240 | |
| 241 | status = lttng_event_rule_kernel_kprobe_get_event_name( |
| 242 | rule, &event_name); |
| 243 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 244 | assert(event_name); |
| 245 | |
| 246 | status = lttng_event_rule_kernel_kprobe_get_location(rule, &location); |
| 247 | assert(status == LTTNG_EVENT_RULE_STATUS_OK); |
| 248 | assert(location); |
| 249 | |
| 250 | /* Open event rule kernel kprobe element. */ |
| 251 | ret = mi_lttng_writer_open_element( |
| 252 | writer, mi_lttng_element_event_rule_kernel_kprobe); |
| 253 | if (ret) { |
| 254 | goto mi_error; |
| 255 | } |
| 256 | |
| 257 | /* Name. */ |
| 258 | ret = mi_lttng_writer_write_element_string(writer, |
| 259 | mi_lttng_element_event_rule_event_name, event_name); |
| 260 | if (ret) { |
| 261 | goto mi_error; |
| 262 | } |
| 263 | |
| 264 | /* Probe location. */ |
| 265 | ret_code = lttng_kernel_probe_location_mi_serialize(location, writer); |
| 266 | if (ret_code != LTTNG_OK) { |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | /* Close event rule kernel kprobe element. */ |
| 271 | ret = mi_lttng_writer_close_element(writer); |
| 272 | if (ret) { |
| 273 | goto mi_error; |
| 274 | } |
| 275 | |
| 276 | ret_code = LTTNG_OK; |
| 277 | goto end; |
| 278 | |
| 279 | mi_error: |
| 280 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 281 | end: |
| 282 | return ret_code; |
| 283 | } |
| 284 | |
| 285 | struct lttng_event_rule *lttng_event_rule_kernel_kprobe_create( |
| 286 | const struct lttng_kernel_probe_location *location) |
| 287 | { |
| 288 | struct lttng_event_rule *rule = NULL; |
| 289 | struct lttng_event_rule_kernel_kprobe *krule; |
| 290 | |
| 291 | krule = zmalloc(sizeof(struct lttng_event_rule_kernel_kprobe)); |
| 292 | if (!krule) { |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | rule = &krule->parent; |
| 297 | lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE); |
| 298 | krule->parent.validate = lttng_event_rule_kernel_kprobe_validate; |
| 299 | krule->parent.serialize = lttng_event_rule_kernel_kprobe_serialize; |
| 300 | krule->parent.equal = lttng_event_rule_kernel_kprobe_is_equal; |
| 301 | krule->parent.destroy = lttng_event_rule_kernel_kprobe_destroy; |
| 302 | krule->parent.generate_filter_bytecode = |
| 303 | lttng_event_rule_kernel_kprobe_generate_filter_bytecode; |
| 304 | krule->parent.get_filter = lttng_event_rule_kernel_kprobe_get_filter; |
| 305 | krule->parent.get_filter_bytecode = |
| 306 | lttng_event_rule_kernel_kprobe_get_filter_bytecode; |
| 307 | krule->parent.generate_exclusions = |
| 308 | lttng_event_rule_kernel_kprobe_generate_exclusions; |
| 309 | krule->parent.hash = lttng_event_rule_kernel_kprobe_hash; |
| 310 | krule->parent.mi_serialize = lttng_event_rule_kernel_kprobe_mi_serialize; |
| 311 | |
| 312 | if (kernel_probe_set_location(krule, location)) { |
| 313 | lttng_event_rule_destroy(rule); |
| 314 | rule = NULL; |
| 315 | } |
| 316 | |
| 317 | end: |
| 318 | return rule; |
| 319 | } |
| 320 | |
| 321 | LTTNG_HIDDEN |
| 322 | ssize_t lttng_event_rule_kernel_kprobe_create_from_payload( |
| 323 | struct lttng_payload_view *view, |
| 324 | struct lttng_event_rule **_event_rule) |
| 325 | { |
| 326 | ssize_t ret, offset = 0; |
| 327 | enum lttng_event_rule_status status; |
| 328 | const struct lttng_event_rule_kernel_kprobe_comm *kprobe_comm; |
| 329 | const char *name; |
| 330 | struct lttng_buffer_view current_buffer_view; |
| 331 | struct lttng_event_rule *rule = NULL; |
| 332 | struct lttng_kernel_probe_location *location = NULL; |
| 333 | |
| 334 | if (!_event_rule) { |
| 335 | ret = -1; |
| 336 | goto end; |
| 337 | } |
| 338 | |
| 339 | current_buffer_view = lttng_buffer_view_from_view( |
| 340 | &view->buffer, offset, sizeof(*kprobe_comm)); |
| 341 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { |
| 342 | ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header."); |
| 343 | ret = -1; |
| 344 | goto end; |
| 345 | } |
| 346 | |
| 347 | kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data; |
| 348 | |
| 349 | /* Skip to payload */ |
| 350 | offset += current_buffer_view.size; |
| 351 | |
| 352 | { |
| 353 | /* Map the name. */ |
| 354 | struct lttng_payload_view current_payload_view = |
| 355 | lttng_payload_view_from_view(view, offset, |
| 356 | kprobe_comm->name_len); |
| 357 | |
| 358 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
| 359 | ret = -1; |
| 360 | goto end; |
| 361 | } |
| 362 | |
| 363 | name = current_payload_view.buffer.data; |
| 364 | if (!lttng_buffer_view_contains_string( |
| 365 | ¤t_payload_view.buffer, name, |
| 366 | kprobe_comm->name_len)) { |
| 367 | ret = -1; |
| 368 | goto end; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* Skip after the name. */ |
| 373 | offset += kprobe_comm->name_len; |
| 374 | |
| 375 | /* Map the kernel probe location. */ |
| 376 | { |
| 377 | struct lttng_payload_view current_payload_view = |
| 378 | lttng_payload_view_from_view(view, offset, |
| 379 | kprobe_comm->location_len); |
| 380 | |
| 381 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { |
| 382 | ret = -1; |
| 383 | goto end; |
| 384 | } |
| 385 | |
| 386 | ret = lttng_kernel_probe_location_create_from_payload( |
| 387 | ¤t_payload_view, &location); |
| 388 | if (ret < 0) { |
| 389 | ret = -1; |
| 390 | goto end; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (ret != kprobe_comm->location_len) { |
| 395 | ret = -1; |
| 396 | goto end; |
| 397 | } |
| 398 | |
| 399 | /* Skip after the location */ |
| 400 | offset += kprobe_comm->location_len; |
| 401 | |
| 402 | rule = lttng_event_rule_kernel_kprobe_create(location); |
| 403 | if (!rule) { |
| 404 | ERR("Failed to create event rule kprobe."); |
| 405 | ret = -1; |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | status = lttng_event_rule_kernel_kprobe_set_event_name(rule, name); |
| 410 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
| 411 | ERR("Failed to set event rule kprobe name."); |
| 412 | ret = -1; |
| 413 | goto end; |
| 414 | } |
| 415 | |
| 416 | *_event_rule = rule; |
| 417 | rule = NULL; |
| 418 | ret = offset; |
| 419 | end: |
| 420 | lttng_kernel_probe_location_destroy(location); |
| 421 | lttng_event_rule_destroy(rule); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_get_location( |
| 426 | const struct lttng_event_rule *rule, |
| 427 | const struct lttng_kernel_probe_location **location) |
| 428 | { |
| 429 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
| 430 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 431 | |
| 432 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { |
| 433 | status = LTTNG_EVENT_RULE_STATUS_INVALID; |
| 434 | goto end; |
| 435 | } |
| 436 | |
| 437 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 438 | *location = kprobe->location; |
| 439 | |
| 440 | if (!*location) { |
| 441 | status = LTTNG_EVENT_RULE_STATUS_UNSET; |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | end: |
| 446 | return status; |
| 447 | } |
| 448 | |
| 449 | enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_set_event_name( |
| 450 | struct lttng_event_rule *rule, const char *name) |
| 451 | { |
| 452 | char *name_copy = NULL; |
| 453 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 454 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
| 455 | |
| 456 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name || |
| 457 | strlen(name) == 0) { |
| 458 | status = LTTNG_EVENT_RULE_STATUS_INVALID; |
| 459 | goto end; |
| 460 | } |
| 461 | |
| 462 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 463 | name_copy = strdup(name); |
| 464 | if (!name_copy) { |
| 465 | status = LTTNG_EVENT_RULE_STATUS_ERROR; |
| 466 | goto end; |
| 467 | } |
| 468 | |
| 469 | free(kprobe->name); |
| 470 | |
| 471 | kprobe->name = name_copy; |
| 472 | name_copy = NULL; |
| 473 | end: |
| 474 | return status; |
| 475 | } |
| 476 | |
| 477 | enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_get_event_name( |
| 478 | const struct lttng_event_rule *rule, const char **name) |
| 479 | { |
| 480 | struct lttng_event_rule_kernel_kprobe *kprobe; |
| 481 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; |
| 482 | |
| 483 | if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) { |
| 484 | status = LTTNG_EVENT_RULE_STATUS_INVALID; |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent); |
| 489 | if (!kprobe->name) { |
| 490 | status = LTTNG_EVENT_RULE_STATUS_UNSET; |
| 491 | goto end; |
| 492 | } |
| 493 | |
| 494 | *name = kprobe->name; |
| 495 | end: |
| 496 | return status; |
| 497 | } |