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 effective 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> | |
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_vegid = INVALID_GID; | |
40 | ||
41 | static | |
42 | gid_t get_vegid(void) | |
43 | { | |
44 | gid_t vegid; | |
45 | ||
46 | vegid = CMM_LOAD_SHARED(cached_vegid); | |
47 | ||
48 | if (caa_unlikely(vegid == INVALID_GID)) { | |
49 | vegid = getegid(); | |
50 | CMM_STORE_SHARED(cached_vegid, vegid); | |
51 | } | |
52 | ||
53 | return vegid; | |
54 | } | |
55 | ||
56 | /* | |
57 | * The vegid can change on setuid, setreuid, setresuid and seteuid. | |
58 | */ | |
59 | void lttng_context_vegid_reset(void) | |
60 | { | |
61 | CMM_STORE_SHARED(cached_vegid, INVALID_GID); | |
62 | } | |
63 | ||
64 | static | |
4e48b5d2 | 65 | size_t vegid_get_size(void *priv __attribute__((unused)), |
2208d8b5 | 66 | size_t offset) |
fca2f191 MJ |
67 | { |
68 | size_t size = 0; | |
69 | ||
dc325c1d | 70 | size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(gid_t)); |
fca2f191 MJ |
71 | size += sizeof(gid_t); |
72 | return size; | |
73 | } | |
74 | ||
75 | static | |
4e48b5d2 | 76 | void vegid_record(void *priv __attribute__((unused)), |
fca2f191 | 77 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 78 | struct lttng_ust_channel_buffer *chan) |
fca2f191 MJ |
79 | { |
80 | gid_t vegid; | |
81 | ||
82 | vegid = get_vegid(); | |
8936b6c0 | 83 | chan->ops->event_write(ctx, &vegid, sizeof(vegid), lttng_ust_rb_alignof(vegid)); |
fca2f191 MJ |
84 | } |
85 | ||
86 | static | |
4e48b5d2 | 87 | void vegid_get_value(void *priv __attribute__((unused)), |
daacdbfc | 88 | struct lttng_ust_ctx_value *value) |
fca2f191 MJ |
89 | { |
90 | value->u.s64 = get_vegid(); | |
91 | } | |
92 | ||
4e48b5d2 MD |
93 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
94 | lttng_ust_static_event_field("vegid", | |
95 | lttng_ust_static_type_integer(sizeof(gid_t) * CHAR_BIT, | |
96 | lttng_ust_rb_alignof(gid_t) * CHAR_BIT, | |
97 | lttng_ust_is_signed_type(gid_t), | |
98 | BYTE_ORDER, 10), | |
99 | false, false), | |
100 | vegid_get_size, | |
101 | vegid_record, | |
102 | vegid_get_value, | |
103 | NULL, NULL); | |
104 | ||
daacdbfc | 105 | int lttng_add_vegid_to_ctx(struct lttng_ust_ctx **ctx) |
fca2f191 | 106 | { |
a084756d MD |
107 | int ret; |
108 | ||
4e48b5d2 | 109 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
a084756d MD |
110 | ret = -EEXIST; |
111 | goto error_find_context; | |
112 | } | |
4e48b5d2 MD |
113 | ret = lttng_ust_context_append(ctx, ctx_field); |
114 | if (ret) | |
115 | return ret; | |
fca2f191 | 116 | return 0; |
a084756d | 117 | |
a084756d | 118 | error_find_context: |
a084756d | 119 | return ret; |
fca2f191 | 120 | } |