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-group ID context.
12 #include <sys/types.h>
15 #include <lttng/ust-events.h>
16 #include <lttng/ust-tracer.h>
17 #include <lttng/ringbuffer-context.h>
19 #include "context-internal.h"
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.
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.
38 static gid_t cached_vsgid
= INVALID_GID
;
45 vsgid
= CMM_LOAD_SHARED(cached_vsgid
);
47 if (caa_unlikely(vsgid
== INVALID_GID
)) {
48 gid_t gid
, egid
, sgid
;
50 if (getresgid(&gid
, &egid
, &sgid
) == 0) {
52 CMM_STORE_SHARED(cached_vsgid
, vsgid
);
60 * The vsgid can change on setuid, setreuid and setresuid.
62 void lttng_context_vsgid_reset(void)
64 CMM_STORE_SHARED(cached_vsgid
, INVALID_GID
);
68 size_t vsgid_get_size(struct lttng_ust_ctx_field
*field
, size_t offset
)
72 size
+= lib_ring_buffer_align(offset
, lttng_alignof(gid_t
));
73 size
+= sizeof(gid_t
);
78 void vsgid_record(struct lttng_ust_ctx_field
*field
,
79 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
80 struct lttng_ust_channel_buffer
*chan
)
85 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vsgid
));
86 chan
->ops
->event_write(ctx
, &vsgid
, sizeof(vsgid
));
90 void vsgid_get_value(struct lttng_ust_ctx_field
*field
,
91 struct lttng_ust_ctx_value
*value
)
93 value
->u
.s64
= get_vsgid();
96 int lttng_add_vsgid_to_ctx(struct lttng_ust_ctx
**ctx
)
98 struct lttng_ust_ctx_field
*field
;
99 struct lttng_ust_type_common
*type
;
102 type
= lttng_ust_create_type_integer(sizeof(gid_t
) * CHAR_BIT
,
103 lttng_alignof(gid_t
) * CHAR_BIT
,
104 lttng_is_signed_type(gid_t
),
108 field
= lttng_append_context(ctx
);
113 if (lttng_find_context(*ctx
, "vsgid")) {
115 goto error_find_context
;
117 field
->event_field
->name
= strdup("vsgid");
118 if (!field
->event_field
->name
) {
122 field
->event_field
->type
= type
;
123 field
->get_size
= vsgid_get_size
;
124 field
->record
= vsgid_record
;
125 field
->get_value
= vsgid_get_value
;
126 lttng_context_update(*ctx
);
131 lttng_remove_context_field(ctx
, field
);
133 lttng_ust_destroy_type(type
);