chiark / gitweb /
c66e9faeea67a1f610a766dc5b037dada335f417
[elogind.git] / src / basic / khash.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2016 Lennart Poettering
6 ***/
7
8 #include <linux/if_alg.h>
9 #include <stdbool.h>
10 #include <sys/socket.h>
11
12 #include "alloc-util.h"
13 #include "fd-util.h"
14 #include "hexdecoct.h"
15 #include "khash.h"
16 #include "macro.h"
17 #include "missing.h"
18 #include "string-util.h"
19 #include "util.h"
20
21 /* On current kernels the maximum digest (according to "grep digestsize /proc/crypto | sort -u") is actually 32, but
22  * let's add some extra room, the few wasted bytes don't really matter... */
23 #define LONGEST_DIGEST 128
24
25 struct khash {
26         int fd;
27         char *algorithm;
28         uint8_t digest[LONGEST_DIGEST+1];
29         size_t digest_size;
30         bool digest_valid;
31 };
32
33 int khash_supported(void) {
34         static const union {
35                 struct sockaddr sa;
36                 struct sockaddr_alg alg;
37         } sa = {
38                 .alg.salg_family = AF_ALG,
39                 .alg.salg_type = "hash",
40                 .alg.salg_name = "sha256", /* a very common algorithm */
41         };
42
43         static int cached = -1;
44
45         if (cached < 0) {
46                 _cleanup_close_ int fd1 = -1, fd2 = -1;
47                 uint8_t buf[LONGEST_DIGEST+1];
48
49                 fd1 = socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
50                 if (fd1 < 0) {
51                         /* The kernel returns EAFNOSUPPORT if AF_ALG is not supported at all */
52                         if (IN_SET(errno, EAFNOSUPPORT, EOPNOTSUPP))
53                                 return (cached = false);
54
55                         return -errno;
56                 }
57
58                 if (bind(fd1, &sa.sa, sizeof(sa)) < 0) {
59                         /* The kernel returns ENOENT if the selected algorithm is not supported at all. We use a check
60                          * for SHA256 as a proxy for whether the whole API is supported at all. After all it's one of
61                          * the most common hash functions, and if it isn't supported, that's ample indication that
62                          * something is really off. */
63
64                         if (IN_SET(errno, ENOENT, EOPNOTSUPP))
65                                 return (cached = false);
66
67                         return -errno;
68                 }
69
70                 fd2 = accept4(fd1, NULL, 0, SOCK_CLOEXEC);
71                 if (fd2 < 0) {
72                         if (errno == EOPNOTSUPP)
73                                 return (cached = false);
74
75                         return -errno;
76                 }
77
78                 if (recv(fd2, buf, sizeof(buf), 0) < 0) {
79                         /* On some kernels we get ENOKEY for non-keyed hash functions (such as sha256), let's refuse
80                          * using the API in those cases, since the kernel is
81                          * broken. https://github.com/systemd/systemd/issues/8278 */
82
83                         if (IN_SET(errno, ENOKEY, EOPNOTSUPP))
84                                 return (cached = false);
85                 }
86
87                 cached = true;
88         }
89
90         return cached;
91 }
92
93 int khash_new_with_key(khash **ret, const char *algorithm, const void *key, size_t key_size) {
94         union {
95                 struct sockaddr sa;
96                 struct sockaddr_alg alg;
97         } sa = {
98                 .alg.salg_family = AF_ALG,
99                 .alg.salg_type = "hash",
100         };
101
102         _cleanup_(khash_unrefp) khash *h = NULL;
103         _cleanup_close_ int fd = -1;
104         int supported;
105         ssize_t n;
106
107         assert(ret);
108         assert(key || key_size == 0);
109
110         /* Filter out an empty algorithm early, as we do not support an algorithm by that name. */
111         if (isempty(algorithm))
112                 return -EINVAL;
113
114         /* Overly long hash algorithm names we definitely do not support */
115         if (strlen(algorithm) >= sizeof(sa.alg.salg_name))
116                 return -EOPNOTSUPP;
117
118         supported = khash_supported();
119         if (supported < 0)
120                 return supported;
121         if (supported == 0)
122                 return -EOPNOTSUPP;
123
124         fd = socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
125         if (fd < 0)
126                 return -errno;
127
128         strcpy((char*) sa.alg.salg_name, algorithm);
129         if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
130                 if (errno == ENOENT)
131                         return -EOPNOTSUPP;
132                 return -errno;
133         }
134
135         if (key) {
136                 if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, key, key_size) < 0)
137                         return -errno;
138         }
139
140         h = new0(khash, 1);
141         if (!h)
142                 return -ENOMEM;
143
144         h->fd = accept4(fd, NULL, 0, SOCK_CLOEXEC);
145         if (h->fd < 0)
146                 return -errno;
147
148         h->algorithm = strdup(algorithm);
149         if (!h->algorithm)
150                 return -ENOMEM;
151
152         /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */
153         (void) send(h->fd, NULL, 0, 0);
154
155         /* Figure out the digest size */
156         n = recv(h->fd, h->digest, sizeof(h->digest), 0);
157         if (n < 0)
158                 return -errno;
159         if (n >= LONGEST_DIGEST) /* longer than what we expected? If so, we don't support this */
160                 return -EOPNOTSUPP;
161
162         h->digest_size = (size_t) n;
163         h->digest_valid = true;
164
165         /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */
166         (void) send(h->fd, NULL, 0, 0);
167
168         *ret = h;
169         h = NULL;
170
171         return 0;
172 }
173
174 int khash_new(khash **ret, const char *algorithm) {
175         return khash_new_with_key(ret, algorithm, NULL, 0);
176 }
177
178 khash* khash_unref(khash *h) {
179         if (!h)
180                 return NULL;
181
182         safe_close(h->fd);
183         free(h->algorithm);
184         return mfree(h);
185 }
186
187 int khash_dup(khash *h, khash **ret) {
188         _cleanup_(khash_unrefp) khash *copy = NULL;
189
190         assert(h);
191         assert(ret);
192
193         copy = newdup(khash, h, 1);
194         if (!copy)
195                 return -ENOMEM;
196
197         copy->fd = -1;
198         copy->algorithm = strdup(h->algorithm);
199         if (!copy->algorithm)
200                 return -ENOMEM;
201
202         copy->fd = accept4(h->fd, NULL, 0, SOCK_CLOEXEC);
203         if (copy->fd < 0)
204                 return -errno;
205
206         *ret = TAKE_PTR(copy);
207
208         return 0;
209 }
210
211 const char *khash_get_algorithm(khash *h) {
212         assert(h);
213
214         return h->algorithm;
215 }
216
217 size_t khash_get_size(khash *h) {
218         assert(h);
219
220         return h->digest_size;
221 }
222
223 int khash_reset(khash *h) {
224         ssize_t n;
225
226         assert(h);
227
228         n = send(h->fd, NULL, 0, 0);
229         if (n < 0)
230                 return -errno;
231
232         h->digest_valid = false;
233
234         return 0;
235 }
236
237 int khash_put(khash *h, const void *buffer, size_t size) {
238         ssize_t n;
239
240         assert(h);
241         assert(buffer || size == 0);
242
243         if (size <= 0)
244                 return 0;
245
246         n = send(h->fd, buffer, size, MSG_MORE);
247         if (n < 0)
248                 return -errno;
249
250         h->digest_valid = false;
251
252         return 0;
253 }
254
255 int khash_put_iovec(khash *h, const struct iovec *iovec, size_t n) {
256         struct msghdr mh = {
257                 mh.msg_iov = (struct iovec*) iovec,
258                 mh.msg_iovlen = n,
259         };
260         ssize_t k;
261
262         assert(h);
263         assert(iovec || n == 0);
264
265         if (n <= 0)
266                 return 0;
267
268         k = sendmsg(h->fd, &mh, MSG_MORE);
269         if (k < 0)
270                 return -errno;
271
272         h->digest_valid = false;
273
274         return 0;
275 }
276
277 static int retrieve_digest(khash *h) {
278         ssize_t n;
279
280         assert(h);
281
282         if (h->digest_valid)
283                 return 0;
284
285         n = recv(h->fd, h->digest, h->digest_size, 0);
286         if (n < 0)
287                 return n;
288         if ((size_t) n != h->digest_size) /* digest size changed? */
289                 return -EIO;
290
291         h->digest_valid = true;
292
293         return 0;
294 }
295
296 int khash_digest_data(khash *h, const void **ret) {
297         int r;
298
299         assert(h);
300         assert(ret);
301
302         r = retrieve_digest(h);
303         if (r < 0)
304                 return r;
305
306         *ret = h->digest;
307         return 0;
308 }
309
310 int khash_digest_string(khash *h, char **ret) {
311         int r;
312         char *p;
313
314         assert(h);
315         assert(ret);
316
317         r = retrieve_digest(h);
318         if (r < 0)
319                 return r;
320
321         p = hexmem(h->digest, h->digest_size);
322         if (!p)
323                 return -ENOMEM;
324
325         *ret = p;
326         return 0;
327 }