| 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-context.h> |
| 18 | |
| 19 | #include "context-internal.h" |
| 20 | #include "creds.h" |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * At the kernel level, user IDs and group IDs are a per-thread attribute. |
| 25 | * However, POSIX requires that all threads in a process share the same |
| 26 | * credentials. The NPTL threading implementation handles the POSIX |
| 27 | * requirements by providing wrapper functions for the various system calls |
| 28 | * that change process UIDs and GIDs. These wrapper functions (including those |
| 29 | * for setreuid() and setregid()) employ a signal-based technique to ensure |
| 30 | * that when one thread changes credentials, all of the other threads in the |
| 31 | * process also change their credentials. |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * We cache the result to ensure we don't trigger a system call for |
| 36 | * each event. User / group IDs are global to the process. |
| 37 | */ |
| 38 | static gid_t cached_vegid = INVALID_GID; |
| 39 | |
| 40 | static |
| 41 | gid_t get_vegid(void) |
| 42 | { |
| 43 | gid_t vegid; |
| 44 | |
| 45 | vegid = CMM_LOAD_SHARED(cached_vegid); |
| 46 | |
| 47 | if (caa_unlikely(vegid == INVALID_GID)) { |
| 48 | vegid = getegid(); |
| 49 | CMM_STORE_SHARED(cached_vegid, vegid); |
| 50 | } |
| 51 | |
| 52 | return vegid; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * The vegid can change on setuid, setreuid, setresuid and seteuid. |
| 57 | */ |
| 58 | void lttng_context_vegid_reset(void) |
| 59 | { |
| 60 | CMM_STORE_SHARED(cached_vegid, INVALID_GID); |
| 61 | } |
| 62 | |
| 63 | static |
| 64 | size_t vegid_get_size(struct lttng_ust_ctx_field *field, size_t offset) |
| 65 | { |
| 66 | size_t size = 0; |
| 67 | |
| 68 | size += lib_ring_buffer_align(offset, lttng_alignof(gid_t)); |
| 69 | size += sizeof(gid_t); |
| 70 | return size; |
| 71 | } |
| 72 | |
| 73 | static |
| 74 | void vegid_record(struct lttng_ust_ctx_field *field, |
| 75 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 76 | struct lttng_channel *chan) |
| 77 | { |
| 78 | gid_t vegid; |
| 79 | |
| 80 | vegid = get_vegid(); |
| 81 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vegid)); |
| 82 | chan->ops->event_write(ctx, &vegid, sizeof(vegid)); |
| 83 | } |
| 84 | |
| 85 | static |
| 86 | void vegid_get_value(struct lttng_ust_ctx_field *field, |
| 87 | struct lttng_ust_ctx_value *value) |
| 88 | { |
| 89 | value->u.s64 = get_vegid(); |
| 90 | } |
| 91 | |
| 92 | int lttng_add_vegid_to_ctx(struct lttng_ust_ctx **ctx) |
| 93 | { |
| 94 | struct lttng_ust_ctx_field *field; |
| 95 | struct lttng_ust_type_common *type; |
| 96 | int ret; |
| 97 | |
| 98 | type = lttng_ust_create_type_integer(sizeof(gid_t) * CHAR_BIT, |
| 99 | lttng_alignof(gid_t) * CHAR_BIT, |
| 100 | lttng_is_signed_type(gid_t), |
| 101 | BYTE_ORDER, 10); |
| 102 | if (!type) |
| 103 | return -ENOMEM; |
| 104 | field = lttng_append_context(ctx); |
| 105 | if (!field) { |
| 106 | ret = -ENOMEM; |
| 107 | goto error_context; |
| 108 | } |
| 109 | if (lttng_find_context(*ctx, "vegid")) { |
| 110 | ret = -EEXIST; |
| 111 | goto error_find_context; |
| 112 | } |
| 113 | field->event_field->name = strdup("vegid"); |
| 114 | if (!field->event_field->name) { |
| 115 | ret = -ENOMEM; |
| 116 | goto error_name; |
| 117 | } |
| 118 | field->event_field->type = type; |
| 119 | field->get_size = vegid_get_size; |
| 120 | field->record = vegid_record; |
| 121 | field->get_value = vegid_get_value; |
| 122 | lttng_context_update(*ctx); |
| 123 | return 0; |
| 124 | |
| 125 | error_name: |
| 126 | error_find_context: |
| 127 | lttng_remove_context_field(ctx, field); |
| 128 | error_context: |
| 129 | lttng_ust_destroy_type(type); |
| 130 | return ret; |
| 131 | } |