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 real group ID context. |
fca2f191 MJ |
8 | */ |
9 | ||
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> | |
0b4b8811 | 18 | #include <lttng/ust-ringbuffer-context.h> |
fc80554e MJ |
19 | |
20 | #include "context-internal.h" | |
6abdf6dc | 21 | #include "common/creds.h" |
fca2f191 MJ |
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_vgid = INVALID_GID; | |
40 | ||
41 | static | |
42 | gid_t get_vgid(void) | |
43 | { | |
44 | gid_t vgid; | |
45 | ||
46 | vgid = CMM_LOAD_SHARED(cached_vgid); | |
47 | ||
48 | if (caa_unlikely(cached_vgid == (gid_t) -1)) { | |
49 | vgid = getgid(); | |
50 | CMM_STORE_SHARED(cached_vgid, vgid); | |
51 | } | |
52 | ||
53 | return vgid; | |
54 | } | |
55 | ||
56 | /* | |
57 | * The vgid can change on setuid, setreuid and setresuid. | |
58 | */ | |
59 | void lttng_context_vgid_reset(void) | |
60 | { | |
61 | CMM_STORE_SHARED(cached_vgid, INVALID_GID); | |
62 | } | |
63 | ||
64 | static | |
4e48b5d2 | 65 | size_t vgid_get_size(void *priv __attribute__((unused)), |
b2e37d27 | 66 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
2208d8b5 | 67 | size_t offset) |
fca2f191 MJ |
68 | { |
69 | size_t size = 0; | |
70 | ||
b5457df5 | 71 | size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(gid_t)); |
fca2f191 MJ |
72 | size += sizeof(gid_t); |
73 | return size; | |
74 | } | |
75 | ||
76 | static | |
4e48b5d2 | 77 | void vgid_record(void *priv __attribute__((unused)), |
b2e37d27 MD |
78 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
79 | struct lttng_ust_ring_buffer_ctx *ctx, | |
80 | struct lttng_ust_channel_buffer *chan) | |
fca2f191 MJ |
81 | { |
82 | gid_t vgid; | |
83 | ||
84 | vgid = get_vgid(); | |
8936b6c0 | 85 | chan->ops->event_write(ctx, &vgid, sizeof(vgid), lttng_ust_rb_alignof(vgid)); |
fca2f191 MJ |
86 | } |
87 | ||
88 | static | |
4e48b5d2 | 89 | void vgid_get_value(void *priv __attribute__((unused)), |
b2e37d27 | 90 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
daacdbfc | 91 | struct lttng_ust_ctx_value *value) |
fca2f191 | 92 | { |
b2e37d27 | 93 | value->u.u64 = get_vgid(); |
fca2f191 MJ |
94 | } |
95 | ||
4e48b5d2 MD |
96 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
97 | lttng_ust_static_event_field("vgid", | |
98 | lttng_ust_static_type_integer(sizeof(gid_t) * CHAR_BIT, | |
99 | lttng_ust_rb_alignof(gid_t) * CHAR_BIT, | |
100 | lttng_ust_is_signed_type(gid_t), | |
baa8acf3 | 101 | LTTNG_UST_BYTE_ORDER, 10), |
4e48b5d2 MD |
102 | false, false), |
103 | vgid_get_size, | |
104 | vgid_record, | |
105 | vgid_get_value, | |
106 | NULL, NULL); | |
107 | ||
daacdbfc | 108 | int lttng_add_vgid_to_ctx(struct lttng_ust_ctx **ctx) |
fca2f191 | 109 | { |
a084756d MD |
110 | int ret; |
111 | ||
4e48b5d2 | 112 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
a084756d MD |
113 | ret = -EEXIST; |
114 | goto error_find_context; | |
115 | } | |
4e48b5d2 MD |
116 | ret = lttng_ust_context_append(ctx, ctx_field); |
117 | if (ret) | |
118 | return ret; | |
fca2f191 | 119 | return 0; |
a084756d | 120 | |
a084756d | 121 | error_find_context: |
a084756d | 122 | return ret; |
fca2f191 | 123 | } |