| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 6 | * |
| 7 | * LTTng UST namespaced effective group ID context. |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <stddef.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <unistd.h> |
| 15 | #include <lttng/ust-events.h> |
| 16 | #include <lttng/ust-tracer.h> |
| 17 | #include <lttng/ringbuffer-config.h> |
| 18 | #include "creds.h" |
| 19 | |
| 20 | |
| 21 | /* |
| 22 | * At the kernel level, user IDs and group IDs are a per-thread attribute. |
| 23 | * However, POSIX requires that all threads in a process share the same |
| 24 | * credentials. The NPTL threading implementation handles the POSIX |
| 25 | * requirements by providing wrapper functions for the various system calls |
| 26 | * that change process UIDs and GIDs. These wrapper functions (including those |
| 27 | * for setreuid() and setregid()) employ a signal-based technique to ensure |
| 28 | * that when one thread changes credentials, all of the other threads in the |
| 29 | * process also change their credentials. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * We cache the result to ensure we don't trigger a system call for |
| 34 | * each event. User / group IDs are global to the process. |
| 35 | */ |
| 36 | static gid_t cached_vegid = INVALID_GID; |
| 37 | |
| 38 | static |
| 39 | gid_t get_vegid(void) |
| 40 | { |
| 41 | gid_t vegid; |
| 42 | |
| 43 | vegid = CMM_LOAD_SHARED(cached_vegid); |
| 44 | |
| 45 | if (caa_unlikely(vegid == INVALID_GID)) { |
| 46 | vegid = getegid(); |
| 47 | CMM_STORE_SHARED(cached_vegid, vegid); |
| 48 | } |
| 49 | |
| 50 | return vegid; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * The vegid can change on setuid, setreuid, setresuid and seteuid. |
| 55 | */ |
| 56 | void lttng_context_vegid_reset(void) |
| 57 | { |
| 58 | CMM_STORE_SHARED(cached_vegid, INVALID_GID); |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | size_t vegid_get_size(struct lttng_ctx_field *field, size_t offset) |
| 63 | { |
| 64 | size_t size = 0; |
| 65 | |
| 66 | size += lib_ring_buffer_align(offset, lttng_alignof(gid_t)); |
| 67 | size += sizeof(gid_t); |
| 68 | return size; |
| 69 | } |
| 70 | |
| 71 | static |
| 72 | void vegid_record(struct lttng_ctx_field *field, |
| 73 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 74 | struct lttng_channel *chan) |
| 75 | { |
| 76 | gid_t vegid; |
| 77 | |
| 78 | vegid = get_vegid(); |
| 79 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vegid)); |
| 80 | chan->ops->event_write(ctx, &vegid, sizeof(vegid)); |
| 81 | } |
| 82 | |
| 83 | static |
| 84 | void vegid_get_value(struct lttng_ctx_field *field, |
| 85 | struct lttng_ctx_value *value) |
| 86 | { |
| 87 | value->u.s64 = get_vegid(); |
| 88 | } |
| 89 | |
| 90 | int lttng_add_vegid_to_ctx(struct lttng_ctx **ctx) |
| 91 | { |
| 92 | struct lttng_ctx_field *field; |
| 93 | |
| 94 | field = lttng_append_context(ctx); |
| 95 | if (!field) |
| 96 | return -ENOMEM; |
| 97 | if (lttng_find_context(*ctx, "vegid")) { |
| 98 | lttng_remove_context_field(ctx, field); |
| 99 | return -EEXIST; |
| 100 | } |
| 101 | field->event_field.name = "vegid"; |
| 102 | field->event_field.type.atype = atype_integer; |
| 103 | field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT; |
| 104 | field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT; |
| 105 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t); |
| 106 | field->event_field.type.u.integer.reverse_byte_order = 0; |
| 107 | field->event_field.type.u.integer.base = 10; |
| 108 | field->event_field.type.u.integer.encoding = lttng_encode_none; |
| 109 | field->get_size = vegid_get_size; |
| 110 | field->record = vegid_record; |
| 111 | field->get_value = vegid_get_value; |
| 112 | lttng_context_update(*ctx); |
| 113 | return 0; |
| 114 | } |