]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/checksum.c
Update bcachefs sources to da8056a215 bcachefs: Store logical location of journal...
[bcachefs-tools-debian] / libbcachefs / checksum.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "checksum.h"
4 #include "super.h"
5 #include "super-io.h"
6
7 #include <linux/crc32c.h>
8 #include <linux/crypto.h>
9 #include <linux/xxhash.h>
10 #include <linux/key.h>
11 #include <linux/random.h>
12 #include <linux/scatterlist.h>
13 #include <crypto/algapi.h>
14 #include <crypto/chacha.h>
15 #include <crypto/hash.h>
16 #include <crypto/poly1305.h>
17 #include <crypto/skcipher.h>
18 #include <keys/user-type.h>
19
20 /*
21  * bch2_checksum state is an abstraction of the checksum state calculated over different pages.
22  * it features page merging without having the checksum algorithm lose its state.
23  * for native checksum aglorithms (like crc), a default seed value will do.
24  * for hash-like algorithms, a state needs to be stored
25  */
26
27 struct bch2_checksum_state {
28         union {
29                 u64 seed;
30                 struct xxh64_state h64state;
31         };
32         unsigned int type;
33 };
34
35 static void bch2_checksum_init(struct bch2_checksum_state *state)
36 {
37         switch (state->type) {
38         case BCH_CSUM_none:
39         case BCH_CSUM_crc32c:
40         case BCH_CSUM_crc64:
41                 state->seed = 0;
42                 break;
43         case BCH_CSUM_crc32c_nonzero:
44                 state->seed = U32_MAX;
45                 break;
46         case BCH_CSUM_crc64_nonzero:
47                 state->seed = U64_MAX;
48                 break;
49         case BCH_CSUM_xxhash:
50                 xxh64_reset(&state->h64state, 0);
51                 break;
52         default:
53                 BUG();
54         }
55 }
56
57 static u64 bch2_checksum_final(const struct bch2_checksum_state *state)
58 {
59         switch (state->type) {
60         case BCH_CSUM_none:
61         case BCH_CSUM_crc32c:
62         case BCH_CSUM_crc64:
63                 return state->seed;
64         case BCH_CSUM_crc32c_nonzero:
65                 return state->seed ^ U32_MAX;
66         case BCH_CSUM_crc64_nonzero:
67                 return state->seed ^ U64_MAX;
68         case BCH_CSUM_xxhash:
69                 return xxh64_digest(&state->h64state);
70         default:
71                 BUG();
72         }
73 }
74
75 static void bch2_checksum_update(struct bch2_checksum_state *state, const void *data, size_t len)
76 {
77         switch (state->type) {
78         case BCH_CSUM_none:
79                 return;
80         case BCH_CSUM_crc32c_nonzero:
81         case BCH_CSUM_crc32c:
82                 state->seed = crc32c(state->seed, data, len);
83                 break;
84         case BCH_CSUM_crc64_nonzero:
85         case BCH_CSUM_crc64:
86                 state->seed = crc64_be(state->seed, data, len);
87                 break;
88         case BCH_CSUM_xxhash:
89                 xxh64_update(&state->h64state, data, len);
90                 break;
91         default:
92                 BUG();
93         }
94 }
95
96 static inline int do_encrypt_sg(struct crypto_sync_skcipher *tfm,
97                                 struct nonce nonce,
98                                 struct scatterlist *sg, size_t len)
99 {
100         SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
101         int ret;
102
103         skcipher_request_set_sync_tfm(req, tfm);
104         skcipher_request_set_crypt(req, sg, sg, len, nonce.d);
105
106         ret = crypto_skcipher_encrypt(req);
107         if (ret)
108                 pr_err("got error %i from crypto_skcipher_encrypt()", ret);
109
110         return ret;
111 }
112
113 static inline int do_encrypt(struct crypto_sync_skcipher *tfm,
114                               struct nonce nonce,
115                               void *buf, size_t len)
116 {
117         struct scatterlist sg;
118
119         sg_init_one(&sg, buf, len);
120         return do_encrypt_sg(tfm, nonce, &sg, len);
121 }
122
123 int bch2_chacha_encrypt_key(struct bch_key *key, struct nonce nonce,
124                             void *buf, size_t len)
125 {
126         struct crypto_sync_skcipher *chacha20 =
127                 crypto_alloc_sync_skcipher("chacha20", 0, 0);
128         int ret;
129
130         if (!chacha20) {
131                 pr_err("error requesting chacha20 module: %li", PTR_ERR(chacha20));
132                 return PTR_ERR(chacha20);
133         }
134
135         ret = crypto_skcipher_setkey(&chacha20->base,
136                                      (void *) key, sizeof(*key));
137         if (ret) {
138                 pr_err("crypto_skcipher_setkey() error: %i", ret);
139                 goto err;
140         }
141
142         ret = do_encrypt(chacha20, nonce, buf, len);
143 err:
144         crypto_free_sync_skcipher(chacha20);
145         return ret;
146 }
147
148 static int gen_poly_key(struct bch_fs *c, struct shash_desc *desc,
149                         struct nonce nonce)
150 {
151         u8 key[POLY1305_KEY_SIZE];
152         int ret;
153
154         nonce.d[3] ^= BCH_NONCE_POLY;
155
156         memset(key, 0, sizeof(key));
157         ret = do_encrypt(c->chacha20, nonce, key, sizeof(key));
158         if (ret)
159                 return ret;
160
161         desc->tfm = c->poly1305;
162         crypto_shash_init(desc);
163         crypto_shash_update(desc, key, sizeof(key));
164         return 0;
165 }
166
167 struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
168                               struct nonce nonce, const void *data, size_t len)
169 {
170         switch (type) {
171         case BCH_CSUM_none:
172         case BCH_CSUM_crc32c_nonzero:
173         case BCH_CSUM_crc64_nonzero:
174         case BCH_CSUM_crc32c:
175         case BCH_CSUM_xxhash:
176         case BCH_CSUM_crc64: {
177                 struct bch2_checksum_state state;
178
179                 state.type = type;
180
181                 bch2_checksum_init(&state);
182                 bch2_checksum_update(&state, data, len);
183
184                 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
185         }
186
187         case BCH_CSUM_chacha20_poly1305_80:
188         case BCH_CSUM_chacha20_poly1305_128: {
189                 SHASH_DESC_ON_STACK(desc, c->poly1305);
190                 u8 digest[POLY1305_DIGEST_SIZE];
191                 struct bch_csum ret = { 0 };
192
193                 gen_poly_key(c, desc, nonce);
194
195                 crypto_shash_update(desc, data, len);
196                 crypto_shash_final(desc, digest);
197
198                 memcpy(&ret, digest, bch_crc_bytes[type]);
199                 return ret;
200         }
201         default:
202                 BUG();
203         }
204 }
205
206 int bch2_encrypt(struct bch_fs *c, unsigned type,
207                   struct nonce nonce, void *data, size_t len)
208 {
209         if (!bch2_csum_type_is_encryption(type))
210                 return 0;
211
212         return do_encrypt(c->chacha20, nonce, data, len);
213 }
214
215 static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
216                                            struct nonce nonce, struct bio *bio,
217                                            struct bvec_iter *iter)
218 {
219         struct bio_vec bv;
220
221         switch (type) {
222         case BCH_CSUM_none:
223                 return (struct bch_csum) { 0 };
224         case BCH_CSUM_crc32c_nonzero:
225         case BCH_CSUM_crc64_nonzero:
226         case BCH_CSUM_crc32c:
227         case BCH_CSUM_xxhash:
228         case BCH_CSUM_crc64: {
229                 struct bch2_checksum_state state;
230
231                 state.type = type;
232                 bch2_checksum_init(&state);
233
234 #ifdef CONFIG_HIGHMEM
235                 __bio_for_each_segment(bv, bio, *iter, *iter) {
236                         void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
237                         bch2_checksum_update(&state, p, bv.bv_len);
238                         kunmap_atomic(p);
239                 }
240 #else
241                 __bio_for_each_bvec(bv, bio, *iter, *iter)
242                         bch2_checksum_update(&state, page_address(bv.bv_page) + bv.bv_offset,
243                                 bv.bv_len);
244 #endif
245                 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
246         }
247
248         case BCH_CSUM_chacha20_poly1305_80:
249         case BCH_CSUM_chacha20_poly1305_128: {
250                 SHASH_DESC_ON_STACK(desc, c->poly1305);
251                 u8 digest[POLY1305_DIGEST_SIZE];
252                 struct bch_csum ret = { 0 };
253
254                 gen_poly_key(c, desc, nonce);
255
256 #ifdef CONFIG_HIGHMEM
257                 __bio_for_each_segment(bv, bio, *iter, *iter) {
258                         void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
259
260                         crypto_shash_update(desc, p, bv.bv_len);
261                         kunmap_atomic(p);
262                 }
263 #else
264                 __bio_for_each_bvec(bv, bio, *iter, *iter)
265                         crypto_shash_update(desc,
266                                 page_address(bv.bv_page) + bv.bv_offset,
267                                 bv.bv_len);
268 #endif
269                 crypto_shash_final(desc, digest);
270
271                 memcpy(&ret, digest, bch_crc_bytes[type]);
272                 return ret;
273         }
274         default:
275                 BUG();
276         }
277 }
278
279 struct bch_csum bch2_checksum_bio(struct bch_fs *c, unsigned type,
280                                   struct nonce nonce, struct bio *bio)
281 {
282         struct bvec_iter iter = bio->bi_iter;
283
284         return __bch2_checksum_bio(c, type, nonce, bio, &iter);
285 }
286
287 int bch2_encrypt_bio(struct bch_fs *c, unsigned type,
288                      struct nonce nonce, struct bio *bio)
289 {
290         struct bio_vec bv;
291         struct bvec_iter iter;
292         struct scatterlist sgl[16], *sg = sgl;
293         size_t bytes = 0;
294         int ret = 0;
295
296         if (!bch2_csum_type_is_encryption(type))
297                 return 0;
298
299         sg_init_table(sgl, ARRAY_SIZE(sgl));
300
301         bio_for_each_segment(bv, bio, iter) {
302                 if (sg == sgl + ARRAY_SIZE(sgl)) {
303                         sg_mark_end(sg - 1);
304
305                         ret = do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
306                         if (ret)
307                                 return ret;
308
309                         nonce = nonce_add(nonce, bytes);
310                         bytes = 0;
311
312                         sg_init_table(sgl, ARRAY_SIZE(sgl));
313                         sg = sgl;
314                 }
315
316                 sg_set_page(sg++, bv.bv_page, bv.bv_len, bv.bv_offset);
317                 bytes += bv.bv_len;
318         }
319
320         sg_mark_end(sg - 1);
321         return do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
322 }
323
324 struct bch_csum bch2_checksum_merge(unsigned type, struct bch_csum a,
325                                     struct bch_csum b, size_t b_len)
326 {
327         struct bch2_checksum_state state;
328
329         state.type = type;
330         bch2_checksum_init(&state);
331         state.seed = a.lo;
332
333         BUG_ON(!bch2_checksum_mergeable(type));
334
335         while (b_len) {
336                 unsigned b = min_t(unsigned, b_len, PAGE_SIZE);
337
338                 bch2_checksum_update(&state,
339                                 page_address(ZERO_PAGE(0)), b);
340                 b_len -= b;
341         }
342         a.lo = bch2_checksum_final(&state);
343         a.lo ^= b.lo;
344         a.hi ^= b.hi;
345         return a;
346 }
347
348 int bch2_rechecksum_bio(struct bch_fs *c, struct bio *bio,
349                         struct bversion version,
350                         struct bch_extent_crc_unpacked crc_old,
351                         struct bch_extent_crc_unpacked *crc_a,
352                         struct bch_extent_crc_unpacked *crc_b,
353                         unsigned len_a, unsigned len_b,
354                         unsigned new_csum_type)
355 {
356         struct bvec_iter iter = bio->bi_iter;
357         struct nonce nonce = extent_nonce(version, crc_old);
358         struct bch_csum merged = { 0 };
359         struct crc_split {
360                 struct bch_extent_crc_unpacked  *crc;
361                 unsigned                        len;
362                 unsigned                        csum_type;
363                 struct bch_csum                 csum;
364         } splits[3] = {
365                 { crc_a, len_a, new_csum_type },
366                 { crc_b, len_b, new_csum_type },
367                 { NULL,  bio_sectors(bio) - len_a - len_b, new_csum_type },
368         }, *i;
369         bool mergeable = crc_old.csum_type == new_csum_type &&
370                 bch2_checksum_mergeable(new_csum_type);
371         unsigned crc_nonce = crc_old.nonce;
372
373         BUG_ON(len_a + len_b > bio_sectors(bio));
374         BUG_ON(crc_old.uncompressed_size != bio_sectors(bio));
375         BUG_ON(crc_is_compressed(crc_old));
376         BUG_ON(bch2_csum_type_is_encryption(crc_old.csum_type) !=
377                bch2_csum_type_is_encryption(new_csum_type));
378
379         for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
380                 iter.bi_size = i->len << 9;
381                 if (mergeable || i->crc)
382                         i->csum = __bch2_checksum_bio(c, i->csum_type,
383                                                       nonce, bio, &iter);
384                 else
385                         bio_advance_iter(bio, &iter, i->len << 9);
386                 nonce = nonce_add(nonce, i->len << 9);
387         }
388
389         if (mergeable)
390                 for (i = splits; i < splits + ARRAY_SIZE(splits); i++)
391                         merged = bch2_checksum_merge(new_csum_type, merged,
392                                                      i->csum, i->len << 9);
393         else
394                 merged = bch2_checksum_bio(c, crc_old.csum_type,
395                                 extent_nonce(version, crc_old), bio);
396
397         if (bch2_crc_cmp(merged, crc_old.csum))
398                 return -EIO;
399
400         for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
401                 if (i->crc)
402                         *i->crc = (struct bch_extent_crc_unpacked) {
403                                 .csum_type              = i->csum_type,
404                                 .compression_type       = crc_old.compression_type,
405                                 .compressed_size        = i->len,
406                                 .uncompressed_size      = i->len,
407                                 .offset                 = 0,
408                                 .live_size              = i->len,
409                                 .nonce                  = crc_nonce,
410                                 .csum                   = i->csum,
411                         };
412
413                 if (bch2_csum_type_is_encryption(new_csum_type))
414                         crc_nonce += i->len;
415         }
416
417         return 0;
418 }
419
420 #ifdef __KERNEL__
421 static int __bch2_request_key(char *key_description, struct bch_key *key)
422 {
423         struct key *keyring_key;
424         const struct user_key_payload *ukp;
425         int ret;
426
427         keyring_key = request_key(&key_type_logon, key_description, NULL);
428         if (IS_ERR(keyring_key))
429                 return PTR_ERR(keyring_key);
430
431         down_read(&keyring_key->sem);
432         ukp = dereference_key_locked(keyring_key);
433         if (ukp->datalen == sizeof(*key)) {
434                 memcpy(key, ukp->data, ukp->datalen);
435                 ret = 0;
436         } else {
437                 ret = -EINVAL;
438         }
439         up_read(&keyring_key->sem);
440         key_put(keyring_key);
441
442         return ret;
443 }
444 #else
445 #include <keyutils.h>
446
447 static int __bch2_request_key(char *key_description, struct bch_key *key)
448 {
449         key_serial_t key_id;
450
451         key_id = request_key("user", key_description, NULL,
452                              KEY_SPEC_USER_KEYRING);
453         if (key_id < 0)
454                 return -errno;
455
456         if (keyctl_read(key_id, (void *) key, sizeof(*key)) != sizeof(*key))
457                 return -1;
458
459         return 0;
460 }
461 #endif
462
463 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
464 {
465         char key_description[60];
466         char uuid[40];
467
468         uuid_unparse_lower(sb->user_uuid.b, uuid);
469         sprintf(key_description, "bcachefs:%s", uuid);
470
471         return __bch2_request_key(key_description, key);
472 }
473
474 int bch2_decrypt_sb_key(struct bch_fs *c,
475                         struct bch_sb_field_crypt *crypt,
476                         struct bch_key *key)
477 {
478         struct bch_encrypted_key sb_key = crypt->key;
479         struct bch_key user_key;
480         int ret = 0;
481
482         /* is key encrypted? */
483         if (!bch2_key_is_encrypted(&sb_key))
484                 goto out;
485
486         ret = bch2_request_key(c->disk_sb.sb, &user_key);
487         if (ret) {
488                 bch_err(c, "error requesting encryption key: %i", ret);
489                 goto err;
490         }
491
492         /* decrypt real key: */
493         ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
494                              &sb_key, sizeof(sb_key));
495         if (ret)
496                 goto err;
497
498         if (bch2_key_is_encrypted(&sb_key)) {
499                 bch_err(c, "incorrect encryption key");
500                 ret = -EINVAL;
501                 goto err;
502         }
503 out:
504         *key = sb_key.key;
505 err:
506         memzero_explicit(&sb_key, sizeof(sb_key));
507         memzero_explicit(&user_key, sizeof(user_key));
508         return ret;
509 }
510
511 static int bch2_alloc_ciphers(struct bch_fs *c)
512 {
513         if (!c->chacha20)
514                 c->chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
515         if (IS_ERR(c->chacha20)) {
516                 bch_err(c, "error requesting chacha20 module: %li",
517                         PTR_ERR(c->chacha20));
518                 return PTR_ERR(c->chacha20);
519         }
520
521         if (!c->poly1305)
522                 c->poly1305 = crypto_alloc_shash("poly1305", 0, 0);
523         if (IS_ERR(c->poly1305)) {
524                 bch_err(c, "error requesting poly1305 module: %li",
525                         PTR_ERR(c->poly1305));
526                 return PTR_ERR(c->poly1305);
527         }
528
529         return 0;
530 }
531
532 int bch2_disable_encryption(struct bch_fs *c)
533 {
534         struct bch_sb_field_crypt *crypt;
535         struct bch_key key;
536         int ret = -EINVAL;
537
538         mutex_lock(&c->sb_lock);
539
540         crypt = bch2_sb_get_crypt(c->disk_sb.sb);
541         if (!crypt)
542                 goto out;
543
544         /* is key encrypted? */
545         ret = 0;
546         if (bch2_key_is_encrypted(&crypt->key))
547                 goto out;
548
549         ret = bch2_decrypt_sb_key(c, crypt, &key);
550         if (ret)
551                 goto out;
552
553         crypt->key.magic        = BCH_KEY_MAGIC;
554         crypt->key.key          = key;
555
556         SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 0);
557         bch2_write_super(c);
558 out:
559         mutex_unlock(&c->sb_lock);
560
561         return ret;
562 }
563
564 int bch2_enable_encryption(struct bch_fs *c, bool keyed)
565 {
566         struct bch_encrypted_key key;
567         struct bch_key user_key;
568         struct bch_sb_field_crypt *crypt;
569         int ret = -EINVAL;
570
571         mutex_lock(&c->sb_lock);
572
573         /* Do we already have an encryption key? */
574         if (bch2_sb_get_crypt(c->disk_sb.sb))
575                 goto err;
576
577         ret = bch2_alloc_ciphers(c);
578         if (ret)
579                 goto err;
580
581         key.magic = BCH_KEY_MAGIC;
582         get_random_bytes(&key.key, sizeof(key.key));
583
584         if (keyed) {
585                 ret = bch2_request_key(c->disk_sb.sb, &user_key);
586                 if (ret) {
587                         bch_err(c, "error requesting encryption key: %i", ret);
588                         goto err;
589                 }
590
591                 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
592                                               &key, sizeof(key));
593                 if (ret)
594                         goto err;
595         }
596
597         ret = crypto_skcipher_setkey(&c->chacha20->base,
598                         (void *) &key.key, sizeof(key.key));
599         if (ret)
600                 goto err;
601
602         crypt = bch2_sb_resize_crypt(&c->disk_sb, sizeof(*crypt) / sizeof(u64));
603         if (!crypt) {
604                 ret = -ENOMEM; /* XXX this technically could be -ENOSPC */
605                 goto err;
606         }
607
608         crypt->key = key;
609
610         /* write superblock */
611         SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 1);
612         bch2_write_super(c);
613 err:
614         mutex_unlock(&c->sb_lock);
615         memzero_explicit(&user_key, sizeof(user_key));
616         memzero_explicit(&key, sizeof(key));
617         return ret;
618 }
619
620 void bch2_fs_encryption_exit(struct bch_fs *c)
621 {
622         if (!IS_ERR_OR_NULL(c->poly1305))
623                 crypto_free_shash(c->poly1305);
624         if (!IS_ERR_OR_NULL(c->chacha20))
625                 crypto_free_sync_skcipher(c->chacha20);
626         if (!IS_ERR_OR_NULL(c->sha256))
627                 crypto_free_shash(c->sha256);
628 }
629
630 int bch2_fs_encryption_init(struct bch_fs *c)
631 {
632         struct bch_sb_field_crypt *crypt;
633         struct bch_key key;
634         int ret = 0;
635
636         pr_verbose_init(c->opts, "");
637
638         c->sha256 = crypto_alloc_shash("sha256", 0, 0);
639         if (IS_ERR(c->sha256)) {
640                 bch_err(c, "error requesting sha256 module");
641                 ret = PTR_ERR(c->sha256);
642                 goto out;
643         }
644
645         crypt = bch2_sb_get_crypt(c->disk_sb.sb);
646         if (!crypt)
647                 goto out;
648
649         ret = bch2_alloc_ciphers(c);
650         if (ret)
651                 goto out;
652
653         ret = bch2_decrypt_sb_key(c, crypt, &key);
654         if (ret)
655                 goto out;
656
657         ret = crypto_skcipher_setkey(&c->chacha20->base,
658                         (void *) &key.key, sizeof(key.key));
659         if (ret)
660                 goto out;
661 out:
662         memzero_explicit(&key, sizeof(key));
663         pr_verbose_init(c->opts, "ret %i", ret);
664         return ret;
665 }