Commit | Line | Data |
---|---|---|
fca2f191 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
fca2f191 MJ |
3 | * |
4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
c0c0989a | 5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
fca2f191 | 6 | * |
c0c0989a | 7 | * LTTng UST namespaced saved set-group ID context. |
fca2f191 MJ |
8 | */ |
9 | ||
fca2f191 | 10 | #define _LGPL_SOURCE |
9af5d97a | 11 | #include <limits.h> |
b4051ad8 | 12 | #include <stddef.h> |
fca2f191 MJ |
13 | #include <sys/types.h> |
14 | #include <sys/stat.h> | |
15 | #include <unistd.h> | |
16 | #include <lttng/ust-events.h> | |
17 | #include <lttng/ust-tracer.h> | |
0466ac28 | 18 | #include <lttng/ringbuffer-context.h> |
fc80554e MJ |
19 | |
20 | #include "context-internal.h" | |
fca2f191 MJ |
21 | #include "creds.h" |
22 | ||
23 | ||
24 | /* | |
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. | |
33 | */ | |
34 | ||
35 | /* | |
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. | |
38 | */ | |
39 | static gid_t cached_vsgid = INVALID_GID; | |
40 | ||
41 | static | |
42 | gid_t get_vsgid(void) | |
43 | { | |
44 | gid_t vsgid; | |
45 | ||
46 | vsgid = CMM_LOAD_SHARED(cached_vsgid); | |
47 | ||
48 | if (caa_unlikely(vsgid == INVALID_GID)) { | |
49 | gid_t gid, egid, sgid; | |
50 | ||
51 | if (getresgid(&gid, &egid, &sgid) == 0) { | |
52 | vsgid = sgid; | |
53 | CMM_STORE_SHARED(cached_vsgid, vsgid); | |
54 | } | |
55 | } | |
56 | ||
57 | return vsgid; | |
58 | } | |
59 | ||
60 | /* | |
61 | * The vsgid can change on setuid, setreuid and setresuid. | |
62 | */ | |
63 | void lttng_context_vsgid_reset(void) | |
64 | { | |
65 | CMM_STORE_SHARED(cached_vsgid, INVALID_GID); | |
66 | } | |
67 | ||
68 | static | |
2208d8b5 MJ |
69 | size_t vsgid_get_size(struct lttng_ust_ctx_field *field __attribute__((unused)), |
70 | size_t offset) | |
fca2f191 MJ |
71 | { |
72 | size_t size = 0; | |
73 | ||
dc325c1d | 74 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(gid_t)); |
fca2f191 MJ |
75 | size += sizeof(gid_t); |
76 | return size; | |
77 | } | |
78 | ||
79 | static | |
2208d8b5 | 80 | void vsgid_record(struct lttng_ust_ctx_field *field __attribute__((unused)), |
fca2f191 | 81 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 82 | struct lttng_ust_channel_buffer *chan) |
fca2f191 MJ |
83 | { |
84 | gid_t vsgid; | |
85 | ||
86 | vsgid = get_vsgid(); | |
8936b6c0 | 87 | chan->ops->event_write(ctx, &vsgid, sizeof(vsgid), lttng_ust_rb_alignof(vsgid)); |
fca2f191 MJ |
88 | } |
89 | ||
90 | static | |
2208d8b5 | 91 | void vsgid_get_value(struct lttng_ust_ctx_field *field __attribute__((unused)), |
daacdbfc | 92 | struct lttng_ust_ctx_value *value) |
fca2f191 MJ |
93 | { |
94 | value->u.s64 = get_vsgid(); | |
95 | } | |
96 | ||
daacdbfc | 97 | int lttng_add_vsgid_to_ctx(struct lttng_ust_ctx **ctx) |
fca2f191 | 98 | { |
daacdbfc | 99 | struct lttng_ust_ctx_field *field; |
a084756d MD |
100 | struct lttng_ust_type_common *type; |
101 | int ret; | |
102 | ||
103 | type = lttng_ust_create_type_integer(sizeof(gid_t) * CHAR_BIT, | |
dc325c1d | 104 | lttng_ust_rb_alignof(gid_t) * CHAR_BIT, |
eae3c729 | 105 | lttng_ust_is_signed_type(gid_t), |
a084756d MD |
106 | BYTE_ORDER, 10); |
107 | if (!type) | |
fca2f191 | 108 | return -ENOMEM; |
a084756d MD |
109 | field = lttng_append_context(ctx); |
110 | if (!field) { | |
111 | ret = -ENOMEM; | |
112 | goto error_context; | |
113 | } | |
fca2f191 | 114 | if (lttng_find_context(*ctx, "vsgid")) { |
a084756d MD |
115 | ret = -EEXIST; |
116 | goto error_find_context; | |
117 | } | |
118 | field->event_field->name = strdup("vsgid"); | |
119 | if (!field->event_field->name) { | |
120 | ret = -ENOMEM; | |
121 | goto error_name; | |
fca2f191 | 122 | } |
a084756d | 123 | field->event_field->type = type; |
fca2f191 MJ |
124 | field->get_size = vsgid_get_size; |
125 | field->record = vsgid_record; | |
126 | field->get_value = vsgid_get_value; | |
127 | lttng_context_update(*ctx); | |
128 | return 0; | |
a084756d MD |
129 | |
130 | error_name: | |
131 | error_find_context: | |
132 | lttng_remove_context_field(ctx, field); | |
133 | error_context: | |
134 | lttng_ust_destroy_type(type); | |
135 | return ret; | |
fca2f191 | 136 | } |