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.
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"
21 #include "common/creds.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 gid_t cached_vsgid
= INVALID_GID
;
46 vsgid
= CMM_LOAD_SHARED(cached_vsgid
);
48 if (caa_unlikely(vsgid
== INVALID_GID
)) {
49 gid_t gid
, egid
, sgid
;
51 if (getresgid(&gid
, &egid
, &sgid
) == 0) {
53 CMM_STORE_SHARED(cached_vsgid
, vsgid
);
61 * The vsgid can change on setuid, setreuid and setresuid.
63 void lttng_context_vsgid_reset(void)
65 CMM_STORE_SHARED(cached_vsgid
, INVALID_GID
);
69 size_t vsgid_get_size(void *priv
__attribute__((unused
)),
74 size
+= lttng_ust_ring_buffer_align(offset
, lttng_ust_rb_alignof(gid_t
));
75 size
+= sizeof(gid_t
);
80 void vsgid_record(void *priv
__attribute__((unused
)),
81 struct lttng_ust_ring_buffer_ctx
*ctx
,
82 struct lttng_ust_channel_buffer
*chan
)
87 chan
->ops
->event_write(ctx
, &vsgid
, sizeof(vsgid
), lttng_ust_rb_alignof(vsgid
));
91 void vsgid_get_value(void *priv
__attribute__((unused
)),
92 struct lttng_ust_ctx_value
*value
)
94 value
->u
.s64
= get_vsgid();
97 static const struct lttng_ust_ctx_field
*ctx_field
= lttng_ust_static_ctx_field(
98 lttng_ust_static_event_field("vsgid",
99 lttng_ust_static_type_integer(sizeof(gid_t
) * CHAR_BIT
,
100 lttng_ust_rb_alignof(gid_t
) * CHAR_BIT
,
101 lttng_ust_is_signed_type(gid_t
),
109 int lttng_add_vsgid_to_ctx(struct lttng_ust_ctx
**ctx
)
113 if (lttng_find_context(*ctx
, ctx_field
->event_field
->name
)) {
115 goto error_find_context
;
117 ret
= lttng_ust_context_append(ctx
, ctx_field
);