Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-context-vsuid.c
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 saved set-user 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 uid_t cached_vsuid = INVALID_UID;
37
38 static
39 uid_t get_vsuid(void)
40 {
41 uid_t vsuid;
42
43 vsuid = CMM_LOAD_SHARED(cached_vsuid);
44
45 if (caa_unlikely(vsuid == INVALID_UID)) {
46 uid_t uid, euid, suid;
47
48 if (getresuid(&uid, &euid, &suid) == 0) {
49 vsuid = suid;
50 CMM_STORE_SHARED(cached_vsuid, vsuid);
51 }
52 }
53
54 return vsuid;
55 }
56
57 /*
58 * The vsuid can change on setuid, setreuid and setresuid.
59 */
60 void lttng_context_vsuid_reset(void)
61 {
62 CMM_STORE_SHARED(cached_vsuid, INVALID_UID);
63 }
64
65 static
66 size_t vsuid_get_size(struct lttng_ctx_field *field, size_t offset)
67 {
68 size_t size = 0;
69
70 size += lib_ring_buffer_align(offset, lttng_alignof(uid_t));
71 size += sizeof(uid_t);
72 return size;
73 }
74
75 static
76 void vsuid_record(struct lttng_ctx_field *field,
77 struct lttng_ust_lib_ring_buffer_ctx *ctx,
78 struct lttng_channel *chan)
79 {
80 uid_t vsuid;
81
82 vsuid = get_vsuid();
83 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vsuid));
84 chan->ops->event_write(ctx, &vsuid, sizeof(vsuid));
85 }
86
87 static
88 void vsuid_get_value(struct lttng_ctx_field *field,
89 struct lttng_ctx_value *value)
90 {
91 value->u.s64 = get_vsuid();
92 }
93
94 int lttng_add_vsuid_to_ctx(struct lttng_ctx **ctx)
95 {
96 struct lttng_ctx_field *field;
97
98 field = lttng_append_context(ctx);
99 if (!field)
100 return -ENOMEM;
101 if (lttng_find_context(*ctx, "vsuid")) {
102 lttng_remove_context_field(ctx, field);
103 return -EEXIST;
104 }
105 field->event_field.name = "vsuid";
106 field->event_field.type.atype = atype_integer;
107 field->event_field.type.u.integer.size = sizeof(uid_t) * CHAR_BIT;
108 field->event_field.type.u.integer.alignment = lttng_alignof(uid_t) * CHAR_BIT;
109 field->event_field.type.u.integer.signedness = lttng_is_signed_type(uid_t);
110 field->event_field.type.u.integer.reverse_byte_order = 0;
111 field->event_field.type.u.integer.base = 10;
112 field->event_field.type.u.integer.encoding = lttng_encode_none;
113 field->get_size = vsuid_get_size;
114 field->record = vsuid_record;
115 field->get_value = vsuid_get_value;
116 lttng_context_update(*ctx);
117 return 0;
118 }
This page took 0.030476 seconds and 4 git commands to generate.