2 * lttng-context-vsuid.c
4 * LTTng UST namespaced saved set-user ID context.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * 2019 Michael Jeanson <mjeanson@efficios.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
30 #include <lttng/ust-events.h>
31 #include <lttng/ust-tracer.h>
32 #include <lttng/ringbuffer-config.h>
37 * At the kernel level, user IDs and group IDs are a per-thread attribute.
38 * However, POSIX requires that all threads in a process share the same
39 * credentials. The NPTL threading implementation handles the POSIX
40 * requirements by providing wrapper functions for the various system calls
41 * that change process UIDs and GIDs. These wrapper functions (including those
42 * for setreuid() and setregid()) employ a signal-based technique to ensure
43 * that when one thread changes credentials, all of the other threads in the
44 * process also change their credentials.
48 * We cache the result to ensure we don't trigger a system call for
49 * each event. User / group IDs are global to the process.
51 static uid_t cached_vsuid
= INVALID_UID
;
58 vsuid
= CMM_LOAD_SHARED(cached_vsuid
);
60 if (caa_unlikely(vsuid
== INVALID_UID
)) {
61 uid_t uid
, euid
, suid
;
63 if (getresuid(&uid
, &euid
, &suid
) == 0) {
65 CMM_STORE_SHARED(cached_vsuid
, vsuid
);
73 * The vsuid can change on setuid, setreuid and setresuid.
75 void lttng_context_vsuid_reset(void)
77 CMM_STORE_SHARED(cached_vsuid
, INVALID_UID
);
81 size_t vsuid_get_size(struct lttng_ctx_field
*field
, size_t offset
)
85 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uid_t
));
86 size
+= sizeof(uid_t
);
91 void vsuid_record(struct lttng_ctx_field
*field
,
92 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
93 struct lttng_channel
*chan
)
98 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vsuid
));
99 chan
->ops
->event_write(ctx
, &vsuid
, sizeof(vsuid
));
103 void vsuid_get_value(struct lttng_ctx_field
*field
,
104 struct lttng_ctx_value
*value
)
106 value
->u
.s64
= get_vsuid();
109 int lttng_add_vsuid_to_ctx(struct lttng_ctx
**ctx
)
111 struct lttng_ctx_field
*field
;
113 field
= lttng_append_context(ctx
);
116 if (lttng_find_context(*ctx
, "vsuid")) {
117 lttng_remove_context_field(ctx
, field
);
120 field
->event_field
.name
= "vsuid";
121 field
->event_field
.type
.atype
= atype_integer
;
122 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(uid_t
) * CHAR_BIT
;
123 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(uid_t
) * CHAR_BIT
;
124 field
->event_field
.type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(uid_t
);
125 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
126 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
127 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
128 field
->get_size
= vsuid_get_size
;
129 field
->record
= vsuid_record
;
130 field
->get_value
= vsuid_get_value
;
131 lttng_context_update(*ctx
);