505a1b636d34b54e8f27d777188871a3e26d0bf5
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-veuid.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 effective user ID context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <limits.h>
12 #include <stddef.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ust-tracer.h>
18 #include <lttng/ust-ringbuffer-context.h>
19
20 #include "context-internal.h"
21 #include "common/creds.h"
22
23
24 /*
25 * At the kernel level, user IDs and group IDs are a per-thread attribute.
26 * However, POSIX requires that all threads in a process share the same
27 * credentials. The NPTL threading implementation handles the POSIX
28 * requirements by providing wrapper functions for the various system calls
29 * that change process UIDs and GIDs. These wrapper functions (including those
30 * for setreuid() and setregid()) employ a signal-based technique to ensure
31 * that when one thread changes credentials, all of the other threads in the
32 * process also change their credentials.
33 */
34
35 /*
36 * We cache the result to ensure we don't trigger a system call for
37 * each event. User / group IDs are global to the process.
38 */
39 static uid_t cached_veuid = INVALID_UID;
40
41 static
42 uid_t get_veuid(void)
43 {
44 uid_t veuid;
45
46 veuid = CMM_LOAD_SHARED(cached_veuid);
47
48 if (caa_unlikely(veuid == INVALID_UID)) {
49 veuid = geteuid();
50 CMM_STORE_SHARED(cached_veuid, veuid);
51 }
52
53 return veuid;
54 }
55
56 /*
57 * The veuid can change on setuid, setreuid, setresuid and seteuid.
58 */
59 void lttng_context_veuid_reset(void)
60 {
61 CMM_STORE_SHARED(cached_veuid, INVALID_UID);
62 }
63
64 static
65 size_t veuid_get_size(void *priv __attribute__((unused)),
66 size_t offset)
67 {
68 size_t size = 0;
69
70 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uid_t));
71 size += sizeof(uid_t);
72 return size;
73 }
74
75 static
76 void veuid_record(void *priv __attribute__((unused)),
77 struct lttng_ust_ring_buffer_ctx *ctx,
78 struct lttng_ust_channel_buffer *chan)
79 {
80 uid_t veuid;
81
82 veuid = get_veuid();
83 chan->ops->event_write(ctx, &veuid, sizeof(veuid), lttng_ust_rb_alignof(veuid));
84 }
85
86 static
87 void veuid_get_value(void *priv __attribute__((unused)),
88 struct lttng_ust_ctx_value *value)
89 {
90 value->u.s64 = get_veuid();
91 }
92
93 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
94 lttng_ust_static_event_field("veuid",
95 lttng_ust_static_type_integer(sizeof(uid_t) * CHAR_BIT,
96 lttng_ust_rb_alignof(uid_t) * CHAR_BIT,
97 lttng_ust_is_signed_type(uid_t),
98 LTTNG_UST_BYTE_ORDER, 10),
99 false, false),
100 veuid_get_size,
101 veuid_record,
102 veuid_get_value,
103 NULL, NULL);
104
105 int lttng_add_veuid_to_ctx(struct lttng_ust_ctx **ctx)
106 {
107 int ret;
108
109 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
110 ret = -EEXIST;
111 goto error_find_context;
112 }
113 ret = lttng_ust_context_append(ctx, ctx_field);
114 if (ret)
115 return ret;
116 return 0;
117
118 error_find_context:
119 return ret;
120 }
This page took 0.033679 seconds and 3 git commands to generate.