Debian Codegraph @

src/util/misc.c

raw
1 /*
2 * Miscellaneous Helpers
3 *
4 * These are helpers that have no other obvious home.
5 */
6
7 #include <c-stdaux.h>
8 #include <fcntl.h>
9 #include <grp.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include "util/error.h"
14 #include "util/misc.h"
15 #include "util/syscall.h"
16
17 #ifdef F_LINUX_SPECIFIC_BASE
18 # define MISC_F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
19 # define MISC_F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
20 #else
21 # define MISC_F_ADD_SEALS (1024 + 9)
22 # define MISC_F_GET_SEALS (1024 + 10)
23 #endif
24
25 /**
26 * misc_memfd() - create non-executable memfd
27 * @name: name for memfd inode
28 * @uflags: memfd flags
29 * @useals: initial seals
30 *
31 * This is a convenience wrapper around `memfd_create(2)`. It creates a memfd
32 * and returns it. However, a set of additional rules apply:
33 *
34 * * The caller must specify either `MFD_EXEC` or `MFD_NOEXEC_SEAL`. This
35 * is enforced. If the kernel does not support the flags, this will strip
36 * them and manually edit the executable bit on the inode. Unfortunately,
37 * `F_SEAL_EXEC` will then not be available. If required, you must set it
38 * explicitly in @seals to ensure the call fails on older kernels. You
39 * likely then also want `F_SEAL_SEAL` and `MFD_ALLOW_SEALING`, though.
40 *
41 * IOW, if you require `F_SEAL_EXEC`, you likely need:
42 *
43 * misc_memfd(
44 * "...",
45 * MISC_MFD_ALLOW_SEALING | MISC_MFD_NOEXEC_SEAL,
46 * MISC_F_SEAL_EXEC
47 * );
48 *
49 * Throw in `MFD_CLOEXEC` or `F_SEAL_SEAL` as required.
50 *
51 * * If `MFD_NOEXEC_SEAL` is used without `MFD_ALLOW_SEALING`, sealing will
52 * be disabled (even though some kernel versions implicitly enable it).
53 *
54 * * An initial set of seals is applied to the memfd, if specified in
55 * @seals. Note that this is not allowed if sealing was not enabled.
56 *
57 * Return: New memfd file-descriptor on success, -1 on failure.
58 */
59 int misc_memfd(const char *name, unsigned int uflags, unsigned int useals) {
60 _c_cleanup_(c_closep) int fd = -1;
61 unsigned int flags = uflags;
62 unsigned int seals = useals;
63 unsigned int kseals;
64 struct stat st;
65 int r;
66
67 /*
68 * You cannot set any seals if you disable sealing (apart from
69 * F_SEAL_SEAL, which is what the kernel sets to disable sealing).
70 */
71 if (!(flags & MISC_MFD_ALLOW_SEALING)) {
72 if (seals & ~MISC_F_SEAL_SEAL)
73 return error_origin(-ENOTRECOVERABLE);
74
75 /* Already set by the kernel if sealing is disabled. */
76 seals &= ~MISC_F_SEAL_SEAL;
77 }
78
79 /*
80 * Newer kernels require you to either specify `MFD_EXEC` or
81 * `MFD_NOEXEC_SEAL` or it will warn loudly. Hence we enforce this in
82 * our codebase. For older kernels, we strip these flags automatically.
83 */
84 if (!(flags & (MISC_MFD_EXEC | MISC_MFD_NOEXEC_SEAL)))
85 return error_origin(-ENOTRECOVERABLE);
86
87 /*
88 * Create the memfd. If the flags are not supported, strip the EXEC
89 * flags and try again. We can emulate them, and older kernels refuse
90 * them.
91 */
92 fd = syscall_memfd_create(name, flags);
93 if (fd < 0 && errno == EINVAL) {
94 flags &= ~(MISC_MFD_EXEC | MISC_MFD_NOEXEC_SEAL);
95 fd = syscall_memfd_create(name, flags);
96 }
97 if (fd < 0)
98 return error_origin(-errno);
99
100 /*
101 * If the kernel did not support `MFD_NOEXEC_SEAL`, but we want it, we
102 * strip the executable bits ourselves. They are set by default.
103 */
104 if ((uflags & MISC_MFD_NOEXEC_SEAL) && !(flags & MISC_MFD_NOEXEC_SEAL)) {
105 r = fstat(fd, &st);
106 if (r < 0)
107 return error_origin(-errno);
108
109 r = fchmod(fd, st.st_mode & 07666);
110 if (r < 0)
111 return error_origin(-errno);
112 }
113
114 /*
115 * If we ended up passing `MFG_NOEXEC_SEAL` to the kernel, some kernel
116 * versions will implicitly enable sealing. This is very unfortunate,
117 * so we revert this if the caller did not explicitly allow it. To
118 * disable sealing, simply set `F_SEAL_SEAL`, which is also what the
119 * kernel does.
120 */
121 if ((flags & MISC_MFD_NOEXEC_SEAL) && !(flags & MISC_MFD_ALLOW_SEALING))
122 seals |= MISC_F_SEAL_SEAL;
123
124 /*
125 * If the user wants initial seals, lets set them. Sadly,
126 * memfd_create(2) does not support initial seals, so we have to call
127 * into the kernel again.
128 */
129 if (seals) {
130 r = misc_memfd_get_seals(fd, &kseals);
131 if (r)
132 return error_fold(r);
133
134 if (seals & ~kseals) {
135 r = misc_memfd_add_seals(fd, seals);
136 if (r)
137 return error_fold(r);
138 }
139 }
140
141 r = fd;
142 fd = -1;
143 return r;
144 }
145
146 /**
147 * misc_memfd_add_seals() - add seals to a memfd
148 * @fd: memfd to operate on
149 * @seals: seals to set
150 *
151 * Add the specified seals to the set of seals on the memfd. If the FD does not
152 * refer to a memfd (or other file that supports sealing), an error will be
153 * returned. If the memfd does not support sealing, or if `F_SEAL_SEAL` was
154 * already set, `-EPERM` is returned.
155 *
156 * Write access is needed to add seals to a memfd, or `-EPERM` will be
157 * returned.
158 *
159 * Adding seals that are already set is a no-op (unless `F_SEAL_SEAL` is set,
160 * in which case this operation always fails).
161 *
162 * Return: 0 on success, negative error code on failure.
163 */
164 int misc_memfd_add_seals(int fd, unsigned int seals) {
165 int r;
166
167 r = fcntl(fd, MISC_F_ADD_SEALS, seals);
168 if (r < 0)
169 return error_origin(-errno);
170
171 return 0;
172 }
173
174 /**
175 * misc_memfd_get_seals() - query seals of a memfd
176 * @fd: memfd to operate on
177 * @sealsp: output argument to store retrieved seals
178 *
179 * Query the seals of the memfd. If the FD does not refer to a memfd (or other
180 * file that supports sealing), an error will be returned.
181 *
182 * On success, the seals are written to @sealsp.
183 *
184 * Return: 0 on success, negative error code on failure.
185 */
186 int misc_memfd_get_seals(int fd, unsigned int *sealsp) {
187 int seals;
188
189 seals = fcntl(fd, MISC_F_GET_SEALS);
190 if (seals < 0)
191 return error_origin(-errno);
192
193 *sealsp = seals;
194 return 0;
195 }
196
197 /**
198 * util_umul64_saturating() - saturating multiplication
199 * @a: first operand
200 * @b: second operand
201 *
202 * This calculates @a multiplied by @b and returns the result. In case of an
203 * integer overflow, it will return `UINT64_MAX`.
204 *
205 * Return: The saturated result is returned.
206 */
207 uint64_t util_umul64_saturating(uint64_t a, uint64_t b) {
208 uint64_t res;
209
210 if (__builtin_mul_overflow(a, b, &res))
211 res = UINT64_MAX;
212
213 return res;
214 }
215
216 /**
217 * util_z2u_saturating() - saturating cast of size_t to unsigned int
218 * @v: value to cast
219 *
220 * This will cast a value of `size_t` to `unsigned int`, saturating the
221 * value at `UINT_MAX` in case of overflow.
222 *
223 * Return: The casted, saturated value is returned.
224 */
225 unsigned int util_z2u_saturating(size_t v) {
226 unsigned int cast;
227
228 cast = (unsigned int)v;
229 if ((size_t)cast != v)
230 return UINT_MAX;
231 else
232 return cast;
233 }
234
235 /**
236 * util_t2u_saturating() - saturating cast of uint64_t to unsigned int
237 * @v: value to cast
238 *
239 * This will cast a value of `uint64_t` to `unsigned int`, saturating the
240 * value at `UINT_MAX` in case of overflow.
241 *
242 * Return: The casted, saturated value is returned.
243 */
244 unsigned int util_t2u_saturating(uint64_t v) {
245 unsigned int cast;
246
247 cast = (unsigned int)v;
248 if ((uint64_t)cast != v)
249 return UINT_MAX;
250 else
251 return cast;
252 }
253
254 int util_drop_permissions(uint32_t uid, uint32_t gid) {
255 int r;
256
257 /* for compatibility to dbus-daemon, this must be non-fatal */
258 setgroups(0, NULL);
259
260 r = setgid(gid);
261 if (r < 0)
262 return error_origin(-errno);
263
264 r = setuid(uid);
265 if (r < 0)
266 return error_origin(-errno);
267
268 return 0;
269 }
270
271 void util_peak_update(size_t *peak, size_t update) {
272 if (update > *peak)
273 *peak = update;
274 }