2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
7 * LTTng UST namespaced saved set-user ID context.
13 #include <sys/types.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ust-tracer.h>
18 #include <lttng/ringbuffer-context.h>
20 #include "context-internal.h"
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.
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.
39 static uid_t cached_vsuid
= INVALID_UID
;
46 vsuid
= CMM_LOAD_SHARED(cached_vsuid
);
48 if (caa_unlikely(vsuid
== INVALID_UID
)) {
49 uid_t uid
, euid
, suid
;
51 if (getresuid(&uid
, &euid
, &suid
) == 0) {
53 CMM_STORE_SHARED(cached_vsuid
, vsuid
);
61 * The vsuid can change on setuid, setreuid and setresuid.
63 void lttng_context_vsuid_reset(void)
65 CMM_STORE_SHARED(cached_vsuid
, INVALID_UID
);
69 size_t vsuid_get_size(struct lttng_ust_ctx_field
*field
, size_t offset
)
73 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(uid_t
));
74 size
+= sizeof(uid_t
);
79 void vsuid_record(struct lttng_ust_ctx_field
*field
,
80 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
81 struct lttng_ust_channel_buffer
*chan
)
86 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vsuid
));
87 chan
->ops
->event_write(ctx
, &vsuid
, sizeof(vsuid
));
91 void vsuid_get_value(struct lttng_ust_ctx_field
*field
,
92 struct lttng_ust_ctx_value
*value
)
94 value
->u
.s64
= get_vsuid();
97 int lttng_add_vsuid_to_ctx(struct lttng_ust_ctx
**ctx
)
99 struct lttng_ust_ctx_field
*field
;
100 struct lttng_ust_type_common
*type
;
103 type
= lttng_ust_create_type_integer(sizeof(uid_t
) * CHAR_BIT
,
104 lttng_alignof(uid_t
) * CHAR_BIT
,
105 lttng_ust_is_signed_type(uid_t
),
109 field
= lttng_append_context(ctx
);
114 if (lttng_find_context(*ctx
, "vsuid")) {
116 goto error_find_context
;
118 field
->event_field
->name
= strdup("vsuid");
119 if (!field
->event_field
->name
) {
123 field
->event_field
->type
= type
;
124 field
->get_size
= vsuid_get_size
;
125 field
->record
= vsuid_record
;
126 field
->get_value
= vsuid_get_value
;
127 lttng_context_update(*ctx
);
132 lttng_remove_context_field(ctx
, field
);
134 lttng_ust_destroy_type(type
);