summaryrefslogtreecommitdiff
path: root/src/basic/khash.c
blob: b3e1dc444840d05691022f7cb00b3d5fabe851eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* SPDX-License-Identifier: LGPL-2.1+ */
/***
  This file is part of systemd.

  Copyright 2016 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <linux/if_alg.h>
#include <stdbool.h>
#include <sys/socket.h>

#include "alloc-util.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "khash.h"
#include "macro.h"
#include "missing.h"
#include "string-util.h"
#include "util.h"

/* On current kernels the maximum digest (according to "grep digestsize /proc/crypto | sort -u") is actually 32, but
 * let's add some extra room, the few wasted bytes don't really matter... */
#define LONGEST_DIGEST 128

struct khash {
        int fd;
        char *algorithm;
        uint8_t digest[LONGEST_DIGEST+1];
        size_t digest_size;
        bool digest_valid;
};

int khash_supported(void) {
        static const union {
                struct sockaddr sa;
                struct sockaddr_alg alg;
        } sa = {
                .alg.salg_family = AF_ALG,
                .alg.salg_type = "hash",
                .alg.salg_name = "sha256", /* a very common algorithm */
        };

        static int cached = -1;

        if (cached < 0) {
                _cleanup_close_ int fd1 = -1, fd2 = -1;
                uint8_t buf[LONGEST_DIGEST+1];

                fd1 = socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
                if (fd1 < 0) {
                        /* The kernel returns EAFNOSUPPORT if AF_ALG is not supported at all */
                        if (IN_SET(errno, EAFNOSUPPORT, EOPNOTSUPP))
                                return (cached = false);

                        return -errno;
                }

                if (bind(fd1, &sa.sa, sizeof(sa)) < 0) {
                        /* The kernel returns ENOENT if the selected algorithm is not supported at all. We use a check
                         * for SHA256 as a proxy for whether the whole API is supported at all. After all it's one of
                         * the most common hash functions, and if it isn't supported, that's ample indication that
                         * something is really off. */

                        if (IN_SET(errno, ENOENT, EOPNOTSUPP))
                                return (cached = false);

                        return -errno;
                }

                fd2 = accept4(fd1, NULL, 0, SOCK_CLOEXEC);
                if (fd2 < 0) {
                        if (errno == EOPNOTSUPP)
                                return (cached = false);

                        return -errno;
                }

                if (recv(fd2, buf, sizeof(buf), 0) < 0) {
                        /* On some kernels we get ENOKEY for non-keyed hash functions (such as sha256), let's refuse
                         * using the API in those cases, since the kernel is
                         * broken. https://github.com/systemd/systemd/issues/8278 */

                        if (IN_SET(errno, ENOKEY, EOPNOTSUPP))
                                return (cached = false);
                }

                cached = true;
        }

        return cached;
}

int khash_new_with_key(khash **ret, const char *algorithm, const void *key, size_t key_size) {
        union {
                struct sockaddr sa;
                struct sockaddr_alg alg;
        } sa = {
                .alg.salg_family = AF_ALG,
                .alg.salg_type = "hash",
        };

        _cleanup_(khash_unrefp) khash *h = NULL;
        _cleanup_close_ int fd = -1;
        int supported;
        ssize_t n;

        assert(ret);
        assert(key || key_size == 0);

        /* Filter out an empty algorithm early, as we do not support an algorithm by that name. */
        if (isempty(algorithm))
                return -EINVAL;

        /* Overly long hash algorithm names we definitely do not support */
        if (strlen(algorithm) >= sizeof(sa.alg.salg_name))
                return -EOPNOTSUPP;

        supported = khash_supported();
        if (supported < 0)
                return supported;
        if (supported == 0)
                return -EOPNOTSUPP;

        fd = socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
        if (fd < 0)
                return -errno;

        strcpy((char*) sa.alg.salg_name, algorithm);
        if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
                if (errno == ENOENT)
                        return -EOPNOTSUPP;
                return -errno;
        }

        if (key) {
                if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, key, key_size) < 0)
                        return -errno;
        }

        h = new0(khash, 1);
        if (!h)
                return -ENOMEM;

        h->fd = accept4(fd, NULL, 0, SOCK_CLOEXEC);
        if (h->fd < 0)
                return -errno;

        h->algorithm = strdup(algorithm);
        if (!h->algorithm)
                return -ENOMEM;

        /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */
        (void) send(h->fd, NULL, 0, 0);

        /* Figure out the digest size */
        n = recv(h->fd, h->digest, sizeof(h->digest), 0);
        if (n < 0)
                return -errno;
        if (n >= LONGEST_DIGEST) /* longer than what we expected? If so, we don't support this */
                return -EOPNOTSUPP;

        h->digest_size = (size_t) n;
        h->digest_valid = true;

        /* Temporary fix for rc kernel bug: https://bugzilla.redhat.com/show_bug.cgi?id=1395896 */
        (void) send(h->fd, NULL, 0, 0);

        *ret = h;
        h = NULL;

        return 0;
}

int khash_new(khash **ret, const char *algorithm) {
        return khash_new_with_key(ret, algorithm, NULL, 0);
}

khash* khash_unref(khash *h) {
        if (!h)
                return NULL;

        safe_close(h->fd);
        free(h->algorithm);
        return mfree(h);
}

int khash_dup(khash *h, khash **ret) {
        _cleanup_(khash_unrefp) khash *copy = NULL;

        assert(h);
        assert(ret);

        copy = newdup(khash, h, 1);
        if (!copy)
                return -ENOMEM;

        copy->fd = -1;
        copy->algorithm = strdup(h->algorithm);
        if (!copy->algorithm)
                return -ENOMEM;

        copy->fd = accept4(h->fd, NULL, 0, SOCK_CLOEXEC);
        if (copy->fd < 0)
                return -errno;

        *ret = TAKE_PTR(copy);

        return 0;
}

const char *khash_get_algorithm(khash *h) {
        assert(h);

        return h->algorithm;
}

size_t khash_get_size(khash *h) {
        assert(h);

        return h->digest_size;
}

int khash_reset(khash *h) {
        ssize_t n;

        assert(h);

        n = send(h->fd, NULL, 0, 0);
        if (n < 0)
                return -errno;

        h->digest_valid = false;

        return 0;
}

int khash_put(khash *h, const void *buffer, size_t size) {
        ssize_t n;

        assert(h);
        assert(buffer || size == 0);

        if (size <= 0)
                return 0;

        n = send(h->fd, buffer, size, MSG_MORE);
        if (n < 0)
                return -errno;

        h->digest_valid = false;

        return 0;
}

int khash_put_iovec(khash *h, const struct iovec *iovec, size_t n) {
        struct msghdr mh = {
                mh.msg_iov = (struct iovec*) iovec,
                mh.msg_iovlen = n,
        };
        ssize_t k;

        assert(h);
        assert(iovec || n == 0);

        if (n <= 0)
                return 0;

        k = sendmsg(h->fd, &mh, MSG_MORE);
        if (k < 0)
                return -errno;

        h->digest_valid = false;

        return 0;
}

static int retrieve_digest(khash *h) {
        ssize_t n;

        assert(h);

        if (h->digest_valid)
                return 0;

        n = recv(h->fd, h->digest, h->digest_size, 0);
        if (n < 0)
                return n;
        if ((size_t) n != h->digest_size) /* digest size changed? */
                return -EIO;

        h->digest_valid = true;

        return 0;
}

int khash_digest_data(khash *h, const void **ret) {
        int r;

        assert(h);
        assert(ret);

        r = retrieve_digest(h);
        if (r < 0)
                return r;

        *ret = h->digest;
        return 0;
}

int khash_digest_string(khash *h, char **ret) {
        int r;
        char *p;

        assert(h);
        assert(ret);

        r = retrieve_digest(h);
        if (r < 0)
                return r;

        p = hexmem(h->digest, h->digest_size);
        if (!p)
                return -ENOMEM;

        *ret = p;
        return 0;
}