Commit | Line | Data |
---|---|---|
fca2f191 MJ |
1 | /* |
2 | * lttng-context-vsgid.c | |
3 | * | |
4 | * LTTng UST namespaced saved set-group ID context. | |
5 | * | |
6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * 2019 Michael Jeanson <mjeanson@efficios.com> | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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 | |
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE | |
25 | #define _LGPL_SOURCE | |
b4051ad8 | 26 | #include <stddef.h> |
fca2f191 MJ |
27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | |
29 | #include <unistd.h> | |
30 | #include <lttng/ust-events.h> | |
31 | #include <lttng/ust-tracer.h> | |
32 | #include <lttng/ringbuffer-config.h> | |
33 | #include "creds.h" | |
34 | ||
35 | ||
36 | /* | |
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. | |
45 | */ | |
46 | ||
47 | /* | |
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. | |
50 | */ | |
51 | static gid_t cached_vsgid = INVALID_GID; | |
52 | ||
53 | static | |
54 | gid_t get_vsgid(void) | |
55 | { | |
56 | gid_t vsgid; | |
57 | ||
58 | vsgid = CMM_LOAD_SHARED(cached_vsgid); | |
59 | ||
60 | if (caa_unlikely(vsgid == INVALID_GID)) { | |
61 | gid_t gid, egid, sgid; | |
62 | ||
63 | if (getresgid(&gid, &egid, &sgid) == 0) { | |
64 | vsgid = sgid; | |
65 | CMM_STORE_SHARED(cached_vsgid, vsgid); | |
66 | } | |
67 | } | |
68 | ||
69 | return vsgid; | |
70 | } | |
71 | ||
72 | /* | |
73 | * The vsgid can change on setuid, setreuid and setresuid. | |
74 | */ | |
75 | void lttng_context_vsgid_reset(void) | |
76 | { | |
77 | CMM_STORE_SHARED(cached_vsgid, INVALID_GID); | |
78 | } | |
79 | ||
80 | static | |
81 | size_t vsgid_get_size(struct lttng_ctx_field *field, size_t offset) | |
82 | { | |
83 | size_t size = 0; | |
84 | ||
85 | size += lib_ring_buffer_align(offset, lttng_alignof(gid_t)); | |
86 | size += sizeof(gid_t); | |
87 | return size; | |
88 | } | |
89 | ||
90 | static | |
91 | void vsgid_record(struct lttng_ctx_field *field, | |
92 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
93 | struct lttng_channel *chan) | |
94 | { | |
95 | gid_t vsgid; | |
96 | ||
97 | vsgid = get_vsgid(); | |
98 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vsgid)); | |
99 | chan->ops->event_write(ctx, &vsgid, sizeof(vsgid)); | |
100 | } | |
101 | ||
102 | static | |
103 | void vsgid_get_value(struct lttng_ctx_field *field, | |
104 | struct lttng_ctx_value *value) | |
105 | { | |
106 | value->u.s64 = get_vsgid(); | |
107 | } | |
108 | ||
109 | int lttng_add_vsgid_to_ctx(struct lttng_ctx **ctx) | |
110 | { | |
111 | struct lttng_ctx_field *field; | |
112 | ||
113 | field = lttng_append_context(ctx); | |
114 | if (!field) | |
115 | return -ENOMEM; | |
116 | if (lttng_find_context(*ctx, "vsgid")) { | |
117 | lttng_remove_context_field(ctx, field); | |
118 | return -EEXIST; | |
119 | } | |
120 | field->event_field.name = "vsgid"; | |
121 | field->event_field.type.atype = atype_integer; | |
218deb69 MD |
122 | field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT; |
123 | field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT; | |
124 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t); | |
125 | field->event_field.type.u.integer.reverse_byte_order = 0; | |
126 | field->event_field.type.u.integer.base = 10; | |
127 | field->event_field.type.u.integer.encoding = lttng_encode_none; | |
fca2f191 MJ |
128 | field->get_size = vsgid_get_size; |
129 | field->record = vsgid_record; | |
130 | field->get_value = vsgid_get_value; | |
131 | lttng_context_update(*ctx); | |
132 | return 0; | |
133 | } |