2e442c94433dcbf031921452bab8419fb03cdb8a
[lttng-ust.git] / src / lib / lttng-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 <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_vsuid = INVALID_UID;
40
41 static
42 uid_t get_vsuid(void)
43 {
44 uid_t vsuid;
45
46 vsuid = CMM_LOAD_SHARED(cached_vsuid);
47
48 if (caa_unlikely(vsuid == INVALID_UID)) {
49 uid_t uid, euid, suid;
50
51 if (getresuid(&uid, &euid, &suid) == 0) {
52 vsuid = suid;
53 CMM_STORE_SHARED(cached_vsuid, vsuid);
54 }
55 }
56
57 return vsuid;
58 }
59
60 /*
61 * The vsuid can change on setuid, setreuid and setresuid.
62 */
63 void lttng_context_vsuid_reset(void)
64 {
65 CMM_STORE_SHARED(cached_vsuid, INVALID_UID);
66 }
67
68 static
69 size_t vsuid_get_size(void *priv __attribute__((unused)),
70 size_t offset)
71 {
72 size_t size = 0;
73
74 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uid_t));
75 size += sizeof(uid_t);
76 return size;
77 }
78
79 static
80 void vsuid_record(void *priv __attribute__((unused)),
81 struct lttng_ust_ring_buffer_ctx *ctx,
82 struct lttng_ust_channel_buffer *chan)
83 {
84 uid_t vsuid;
85
86 vsuid = get_vsuid();
87 chan->ops->event_write(ctx, &vsuid, sizeof(vsuid), lttng_ust_rb_alignof(vsuid));
88 }
89
90 static
91 void vsuid_get_value(void *priv __attribute__((unused)),
92 struct lttng_ust_ctx_value *value)
93 {
94 value->u.s64 = get_vsuid();
95 }
96
97 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
98 lttng_ust_static_event_field("vsuid",
99 lttng_ust_static_type_integer(sizeof(uid_t) * CHAR_BIT,
100 lttng_ust_rb_alignof(uid_t) * CHAR_BIT,
101 lttng_ust_is_signed_type(uid_t),
102 LTTNG_UST_BYTE_ORDER, 10),
103 false, false),
104 vsuid_get_size,
105 vsuid_record,
106 vsuid_get_value,
107 NULL, NULL);
108
109 int lttng_add_vsuid_to_ctx(struct lttng_ust_ctx **ctx)
110 {
111 int ret;
112
113 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
114 ret = -EEXIST;
115 goto error_find_context;
116 }
117 ret = lttng_ust_context_append(ctx, ctx_field);
118 if (ret)
119 return ret;
120 return 0;
121
122 error_find_context:
123 return ret;
124 }
This page took 0.033388 seconds and 3 git commands to generate.