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