Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-context-vsgid.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-group ID context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <stddef.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <lttng/ust-events.h>
16 #include <lttng/ust-tracer.h>
17 #include <lttng/ringbuffer-config.h>
18 #include "creds.h"
19
20
21 /*
22 * At the kernel level, user IDs and group IDs are a per-thread attribute.
23 * However, POSIX requires that all threads in a process share the same
24 * credentials. The NPTL threading implementation handles the POSIX
25 * requirements by providing wrapper functions for the various system calls
26 * that change process UIDs and GIDs. These wrapper functions (including those
27 * for setreuid() and setregid()) employ a signal-based technique to ensure
28 * that when one thread changes credentials, all of the other threads in the
29 * process also change their credentials.
30 */
31
32 /*
33 * We cache the result to ensure we don't trigger a system call for
34 * each event. User / group IDs are global to the process.
35 */
36 static gid_t cached_vsgid = INVALID_GID;
37
38 static
39 gid_t get_vsgid(void)
40 {
41 gid_t vsgid;
42
43 vsgid = CMM_LOAD_SHARED(cached_vsgid);
44
45 if (caa_unlikely(vsgid == INVALID_GID)) {
46 gid_t gid, egid, sgid;
47
48 if (getresgid(&gid, &egid, &sgid) == 0) {
49 vsgid = sgid;
50 CMM_STORE_SHARED(cached_vsgid, vsgid);
51 }
52 }
53
54 return vsgid;
55 }
56
57 /*
58 * The vsgid can change on setuid, setreuid and setresuid.
59 */
60 void lttng_context_vsgid_reset(void)
61 {
62 CMM_STORE_SHARED(cached_vsgid, INVALID_GID);
63 }
64
65 static
66 size_t vsgid_get_size(struct lttng_ctx_field *field, size_t offset)
67 {
68 size_t size = 0;
69
70 size += lib_ring_buffer_align(offset, lttng_alignof(gid_t));
71 size += sizeof(gid_t);
72 return size;
73 }
74
75 static
76 void vsgid_record(struct lttng_ctx_field *field,
77 struct lttng_ust_lib_ring_buffer_ctx *ctx,
78 struct lttng_channel *chan)
79 {
80 gid_t vsgid;
81
82 vsgid = get_vsgid();
83 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vsgid));
84 chan->ops->event_write(ctx, &vsgid, sizeof(vsgid));
85 }
86
87 static
88 void vsgid_get_value(struct lttng_ctx_field *field,
89 struct lttng_ctx_value *value)
90 {
91 value->u.s64 = get_vsgid();
92 }
93
94 int lttng_add_vsgid_to_ctx(struct lttng_ctx **ctx)
95 {
96 struct lttng_ctx_field *field;
97
98 field = lttng_append_context(ctx);
99 if (!field)
100 return -ENOMEM;
101 if (lttng_find_context(*ctx, "vsgid")) {
102 lttng_remove_context_field(ctx, field);
103 return -EEXIST;
104 }
105 field->event_field.name = "vsgid";
106 field->event_field.type.atype = atype_integer;
107 field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT;
108 field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT;
109 field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t);
110 field->event_field.type.u.integer.reverse_byte_order = 0;
111 field->event_field.type.u.integer.base = 10;
112 field->event_field.type.u.integer.encoding = lttng_encode_none;
113 field->get_size = vsgid_get_size;
114 field->record = vsgid_record;
115 field->get_value = vsgid_get_value;
116 lttng_context_update(*ctx);
117 return 0;
118 }
This page took 0.030706 seconds and 4 git commands to generate.