Debian Codegraph @

src/libsystemd/sd-id128/id128-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <sys/socket.h>
6 #include <unistd.h>
7
8 #include "bus-container.h"
9 #include "fd-util.h"
10 #include "fs-util.h"
11 #include "hash-funcs.h"
12 #include "id128-util.h"
13 #include "io-util.h"
14 #include "namespace-util.h"
15 #include "pidref.h"
16 #include "process-util.h"
17 #include "runtime-scope.h"
18 #include "sha256.h"
19 #include "siphash24.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "sync-util.h"
23 #include "virt.h"
24
25 int id128_from_string_nonzero(const char *s, sd_id128_t *ret) {
26 sd_id128_t t;
27 int r;
28
29 assert(ret);
30
31 r = sd_id128_from_string(ASSERT_PTR(s), &t);
32 if (r < 0)
33 return r;
34
35 if (sd_id128_is_null(t))
36 return -ENXIO;
37
38 *ret = t;
39 return 0;
40 }
41
42 bool id128_is_valid(const char *s) {
43 size_t l;
44
45 assert(s);
46
47 l = strlen(s);
48
49 if (l == SD_ID128_STRING_MAX - 1)
50 /* Plain formatted 128-bit hex string */
51 return in_charset(s, HEXDIGITS);
52
53 if (l == SD_ID128_UUID_STRING_MAX - 1) {
54 /* Formatted UUID */
55 for (size_t i = 0; i < l; i++) {
56 char c = s[i];
57
58 if (IN_SET(i, 8, 13, 18, 23)) {
59 if (c != '-')
60 return false;
61 } else if (!ascii_ishex(c))
62 return false;
63 }
64 return true;
65 }
66
67 return false;
68 }
69
70 int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret) {
71 char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
72 sd_id128_t id;
73 ssize_t l;
74 int r;
75
76 assert(fd >= 0);
77
78 /* Reads an 128-bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
79 * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
80 * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
81 * accept".
82 *
83 * This returns the following:
84 * -ENOMEDIUM: an empty string,
85 * -ENOPKG: "uninitialized" or "uninitialized\n",
86 * -EUCLEAN: other invalid strings. */
87
88 l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
89 if (l < 0)
90 return (int) l;
91 if (l == 0) /* empty? */
92 return -ENOMEDIUM;
93
94 switch (l) {
95
96 case STRLEN("uninitialized"):
97 case STRLEN("uninitialized\n"):
98 return strneq(buffer, "uninitialized\n", l) ? -ENOPKG : -EINVAL;
99
100 case SD_ID128_STRING_MAX: /* plain UUID with trailing newline */
101 if (buffer[SD_ID128_STRING_MAX-1] != '\n')
102 return -EUCLEAN;
103
104 _fallthrough_;
105 case SD_ID128_STRING_MAX-1: /* plain UUID without trailing newline */
106 if (!FLAGS_SET(f, ID128_FORMAT_PLAIN))
107 return -EUCLEAN;
108
109 buffer[SD_ID128_STRING_MAX-1] = 0;
110 break;
111
112 case SD_ID128_UUID_STRING_MAX: /* RFC UUID with trailing newline */
113 if (buffer[SD_ID128_UUID_STRING_MAX-1] != '\n')
114 return -EUCLEAN;
115
116 _fallthrough_;
117 case SD_ID128_UUID_STRING_MAX-1: /* RFC UUID without trailing newline */
118 if (!FLAGS_SET(f, ID128_FORMAT_UUID))
119 return -EUCLEAN;
120
121 buffer[SD_ID128_UUID_STRING_MAX-1] = 0;
122 break;
123
124 default:
125 return -EUCLEAN;
126 }
127
128 r = sd_id128_from_string(buffer, &id);
129 if (r == -EINVAL)
130 return -EUCLEAN;
131 if (r < 0)
132 return r;
133
134 if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
135 return -ENOMEDIUM;
136
137 if (ret)
138 *ret = id;
139 return 0;
140 }
141
142 int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret) {
143 _cleanup_close_ int fd = -EBADF;
144
145 assert(wildcard_fd_is_valid(dir_fd));
146 assert(path);
147
148 fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
149 if (fd < 0)
150 return fd;
151
152 return id128_read_fd(fd, f, ret);
153 }
154
155 int id128_write_fd(int fd, Id128Flag f, sd_id128_t id) {
156 char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
157 size_t sz;
158 int r;
159
160 assert(fd >= 0);
161 assert(IN_SET((f & ID128_FORMAT_ANY), ID128_FORMAT_PLAIN, ID128_FORMAT_UUID));
162
163 if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
164 return -ENOMEDIUM;
165
166 if (FLAGS_SET(f, ID128_FORMAT_PLAIN)) {
167 assert_se(sd_id128_to_string(id, buffer));
168 sz = SD_ID128_STRING_MAX;
169 } else {
170 assert_se(sd_id128_to_uuid_string(id, buffer));
171 sz = SD_ID128_UUID_STRING_MAX;
172 }
173
174 buffer[sz - 1] = '\n';
175 r = loop_write(fd, buffer, sz);
176 if (r < 0)
177 return r;
178
179 if (FLAGS_SET(f, ID128_SYNC_ON_WRITE)) {
180 r = fsync_full(fd);
181 if (r < 0)
182 return r;
183 }
184
185 return 0;
186 }
187
188 int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id) {
189 _cleanup_close_ int fd = -EBADF;
190
191 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
192 assert(path);
193
194 fd = xopenat_full(dir_fd, path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, /* xopen_flags= */ 0, 0444);
195 if (fd < 0)
196 return fd;
197
198 return id128_write_fd(fd, f, id);
199 }
200
201 void id128_hash_func(const sd_id128_t *p, struct siphash *state) {
202 assert(p);
203
204 siphash24_compress_typesafe(*p, state);
205 }
206
207 int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) {
208 return memcmp(a, b, sizeof(sd_id128_t));
209 }
210
211 sd_id128_t id128_make_v4_uuid(sd_id128_t id) {
212 /* Stolen from generate_random_uuid() of drivers/char/random.c
213 * in the kernel sources */
214
215 /* Set UUID version to 4 --- truly random generation */
216 id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
217
218 /* Set the UUID variant to DCE */
219 id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
220
221 return id;
222 }
223
224 DEFINE_HASH_OPS(id128_hash_ops, sd_id128_t, id128_hash_func, id128_compare_func);
225 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(id128_hash_ops_free, sd_id128_t, id128_hash_func, id128_compare_func, free);
226
227 int id128_get_product(sd_id128_t *ret) {
228 sd_id128_t uuid;
229 int r;
230
231 assert(ret);
232
233 /* Reads the systems product UUID from DMI or devicetree (where it is located on POWER). This is
234 * particularly relevant in VM environments, where VM managers typically place a VM uuid there. */
235
236 r = detect_container();
237 if (r < 0)
238 return r;
239 if (r > 0) /* Refuse returning this in containers, as this is not a property of our system then, but
240 * of the host */
241 return -ENOENT;
242
243 FOREACH_STRING(i,
244 "/sys/class/dmi/id/product_uuid", /* KVM */
245 "/proc/device-tree/vm,uuid", /* Device tree */
246 "/sys/hypervisor/uuid") { /* Xen */
247
248 r = id128_read(i, ID128_FORMAT_UUID, &uuid);
249 if (r != -ENOENT)
250 break;
251 }
252 if (r < 0)
253 return r;
254
255 if (sd_id128_is_null(uuid) || sd_id128_is_allf(uuid))
256 return -EADDRNOTAVAIL; /* Recognizable error */
257
258 *ret = uuid;
259 return 0;
260 }
261
262 sd_id128_t id128_digest(const void *data, size_t size) {
263 assert(data || size == 0);
264
265 /* Hashes a UUID from some arbitrary data */
266
267 if (size == SIZE_MAX)
268 size = strlen(data);
269
270 uint8_t h[SHA256_DIGEST_SIZE];
271 sd_id128_t id;
272
273 /* Take the first half of the SHA256 result */
274 assert_cc(sizeof(h) >= sizeof(id.bytes));
275 memcpy(id.bytes, sha256_direct(data, size, h), sizeof(id.bytes));
276
277 return id128_make_v4_uuid(id);
278 }
279
280 int id128_get_boot_for_machine(const char *machine, sd_id128_t *ret) {
281 _cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, rootfd = -EBADF;
282 _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
283 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
284 pid_t pid;
285 sd_id128_t id;
286 ssize_t k;
287 int r;
288
289 assert(ret);
290
291 if (isempty(machine))
292 return sd_id128_get_boot(ret);
293
294 r = container_get_leader(RUNTIME_SCOPE_SYSTEM, machine, &pid);
295 if (r < 0)
296 return r;
297
298 r = namespace_open(pid, &pidnsfd, &mntnsfd, /* ret_netns_fd= */ NULL, /* ret_userns_fd= */ NULL, &rootfd);
299 if (r < 0)
300 return r;
301
302 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
303 return -errno;
304
305 r = namespace_fork("(sd-bootidns)", "(sd-bootid)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
306 pidnsfd, mntnsfd, -1, -1, rootfd, &child);
307 if (r < 0)
308 return r;
309 if (r == 0) {
310 pair[0] = safe_close(pair[0]);
311
312 r = id128_get_boot(&id);
313 if (r < 0)
314 _exit(EXIT_FAILURE);
315
316 k = send(pair[1], &id, sizeof(id), MSG_NOSIGNAL);
317 if (k != sizeof(id))
318 _exit(EXIT_FAILURE);
319
320 _exit(EXIT_SUCCESS);
321 }
322
323 pair[1] = safe_close(pair[1]);
324
325 r = pidref_wait_for_terminate_and_check("(sd-bootidns)", &child, 0);
326 if (r < 0)
327 return r;
328 if (r != EXIT_SUCCESS)
329 return -EIO;
330
331 k = recv(pair[0], &id, sizeof(id), 0);
332 if (k != sizeof(id))
333 return -EIO;
334
335 if (sd_id128_is_null(id))
336 return -EIO;
337
338 *ret = id;
339 return 0;
340 }