0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/crypto.h>
0013 #include <linux/verification.h>
0014 #include <crypto/hash.h>
0015 #include <crypto/algapi.h>
0016 #include <keys/user-type.h>
0017 #include <keys/asymmetric-type.h>
0018
0019 #include "ubifs.h"
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node,
0030 u8 *hash)
0031 {
0032 const struct ubifs_ch *ch = node;
0033
0034 return crypto_shash_tfm_digest(c->hash_tfm, node, le32_to_cpu(ch->len),
0035 hash);
0036 }
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
0047 u8 *hmac)
0048 {
0049 return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac);
0050 }
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
0064 struct shash_desc *inhash)
0065 {
0066 struct ubifs_auth_node *auth = node;
0067 u8 hash[UBIFS_HASH_ARR_SZ];
0068 int err;
0069
0070 {
0071 SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
0072
0073 hash_desc->tfm = c->hash_tfm;
0074 ubifs_shash_copy_state(c, inhash, hash_desc);
0075
0076 err = crypto_shash_final(hash_desc, hash);
0077 if (err)
0078 return err;
0079 }
0080
0081 err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
0082 if (err)
0083 return err;
0084
0085 auth->ch.node_type = UBIFS_AUTH_NODE;
0086 ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0);
0087 return 0;
0088 }
0089
0090 static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c,
0091 struct crypto_shash *tfm)
0092 {
0093 struct shash_desc *desc;
0094 int err;
0095
0096 if (!ubifs_authenticated(c))
0097 return NULL;
0098
0099 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
0100 if (!desc)
0101 return ERR_PTR(-ENOMEM);
0102
0103 desc->tfm = tfm;
0104
0105 err = crypto_shash_init(desc);
0106 if (err) {
0107 kfree(desc);
0108 return ERR_PTR(err);
0109 }
0110
0111 return desc;
0112 }
0113
0114
0115
0116
0117
0118
0119
0120
0121 struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c)
0122 {
0123 return ubifs_get_desc(c, c->hash_tfm);
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash,
0138 int lnum, int offs)
0139 {
0140 int len = min(c->hash_len, 20);
0141 int cropped = len != c->hash_len;
0142 const char *cont = cropped ? "..." : "";
0143
0144 u8 calc[UBIFS_HASH_ARR_SZ];
0145
0146 __ubifs_node_calc_hash(c, node, calc);
0147
0148 ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs);
0149 ubifs_err(c, "hash expected: %*ph%s", len, hash, cont);
0150 ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont);
0151 }
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node,
0164 const u8 *expected)
0165 {
0166 u8 calc[UBIFS_HASH_ARR_SZ];
0167 int err;
0168
0169 err = __ubifs_node_calc_hash(c, node, calc);
0170 if (err)
0171 return err;
0172
0173 if (ubifs_check_hash(c, expected, calc))
0174 return -EPERM;
0175
0176 return 0;
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 int ubifs_sb_verify_signature(struct ubifs_info *c,
0192 const struct ubifs_sb_node *sup)
0193 {
0194 int err;
0195 struct ubifs_scan_leb *sleb;
0196 struct ubifs_scan_node *snod;
0197 const struct ubifs_sig_node *signode;
0198
0199 sleb = ubifs_scan(c, UBIFS_SB_LNUM, UBIFS_SB_NODE_SZ, c->sbuf, 0);
0200 if (IS_ERR(sleb)) {
0201 err = PTR_ERR(sleb);
0202 return err;
0203 }
0204
0205 if (sleb->nodes_cnt == 0) {
0206 ubifs_err(c, "Unable to find signature node");
0207 err = -EINVAL;
0208 goto out_destroy;
0209 }
0210
0211 snod = list_first_entry(&sleb->nodes, struct ubifs_scan_node, list);
0212
0213 if (snod->type != UBIFS_SIG_NODE) {
0214 ubifs_err(c, "Signature node is of wrong type");
0215 err = -EINVAL;
0216 goto out_destroy;
0217 }
0218
0219 signode = snod->node;
0220
0221 if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) {
0222 ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len));
0223 err = -EINVAL;
0224 goto out_destroy;
0225 }
0226
0227 if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) {
0228 ubifs_err(c, "Signature type %d is not supported\n",
0229 le32_to_cpu(signode->type));
0230 err = -EINVAL;
0231 goto out_destroy;
0232 }
0233
0234 err = verify_pkcs7_signature(sup, sizeof(struct ubifs_sb_node),
0235 signode->sig, le32_to_cpu(signode->len),
0236 NULL, VERIFYING_UNSPECIFIED_SIGNATURE,
0237 NULL, NULL);
0238
0239 if (err)
0240 ubifs_err(c, "Failed to verify signature");
0241 else
0242 ubifs_msg(c, "Successfully verified super block signature");
0243
0244 out_destroy:
0245 ubifs_scan_destroy(sleb);
0246
0247 return err;
0248 }
0249
0250
0251
0252
0253
0254
0255
0256 int ubifs_init_authentication(struct ubifs_info *c)
0257 {
0258 struct key *keyring_key;
0259 const struct user_key_payload *ukp;
0260 int err;
0261 char hmac_name[CRYPTO_MAX_ALG_NAME];
0262
0263 if (!c->auth_hash_name) {
0264 ubifs_err(c, "authentication hash name needed with authentication");
0265 return -EINVAL;
0266 }
0267
0268 c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST,
0269 c->auth_hash_name);
0270 if ((int)c->auth_hash_algo < 0) {
0271 ubifs_err(c, "Unknown hash algo %s specified",
0272 c->auth_hash_name);
0273 return -EINVAL;
0274 }
0275
0276 snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
0277 c->auth_hash_name);
0278
0279 keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL);
0280
0281 if (IS_ERR(keyring_key)) {
0282 ubifs_err(c, "Failed to request key: %ld",
0283 PTR_ERR(keyring_key));
0284 return PTR_ERR(keyring_key);
0285 }
0286
0287 down_read(&keyring_key->sem);
0288
0289 if (keyring_key->type != &key_type_logon) {
0290 ubifs_err(c, "key type must be logon");
0291 err = -ENOKEY;
0292 goto out;
0293 }
0294
0295 ukp = user_key_payload_locked(keyring_key);
0296 if (!ukp) {
0297
0298 err = -EKEYREVOKED;
0299 goto out;
0300 }
0301
0302 c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0);
0303 if (IS_ERR(c->hash_tfm)) {
0304 err = PTR_ERR(c->hash_tfm);
0305 ubifs_err(c, "Can not allocate %s: %d",
0306 c->auth_hash_name, err);
0307 goto out;
0308 }
0309
0310 c->hash_len = crypto_shash_digestsize(c->hash_tfm);
0311 if (c->hash_len > UBIFS_HASH_ARR_SZ) {
0312 ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)",
0313 c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ);
0314 err = -EINVAL;
0315 goto out_free_hash;
0316 }
0317
0318 c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0);
0319 if (IS_ERR(c->hmac_tfm)) {
0320 err = PTR_ERR(c->hmac_tfm);
0321 ubifs_err(c, "Can not allocate %s: %d", hmac_name, err);
0322 goto out_free_hash;
0323 }
0324
0325 c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm);
0326 if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) {
0327 ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
0328 hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ);
0329 err = -EINVAL;
0330 goto out_free_hmac;
0331 }
0332
0333 err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen);
0334 if (err)
0335 goto out_free_hmac;
0336
0337 c->authenticated = true;
0338
0339 c->log_hash = ubifs_hash_get_desc(c);
0340 if (IS_ERR(c->log_hash)) {
0341 err = PTR_ERR(c->log_hash);
0342 goto out_free_hmac;
0343 }
0344
0345 err = 0;
0346
0347 out_free_hmac:
0348 if (err)
0349 crypto_free_shash(c->hmac_tfm);
0350 out_free_hash:
0351 if (err)
0352 crypto_free_shash(c->hash_tfm);
0353 out:
0354 up_read(&keyring_key->sem);
0355 key_put(keyring_key);
0356
0357 return err;
0358 }
0359
0360
0361
0362
0363
0364
0365
0366 void __ubifs_exit_authentication(struct ubifs_info *c)
0367 {
0368 if (!ubifs_authenticated(c))
0369 return;
0370
0371 crypto_free_shash(c->hmac_tfm);
0372 crypto_free_shash(c->hash_tfm);
0373 kfree(c->log_hash);
0374 }
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388 static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node,
0389 int len, int ofs_hmac, void *hmac)
0390 {
0391 SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
0392 int hmac_len = c->hmac_desc_len;
0393 int err;
0394
0395 ubifs_assert(c, ofs_hmac > 8);
0396 ubifs_assert(c, ofs_hmac + hmac_len < len);
0397
0398 shash->tfm = c->hmac_tfm;
0399
0400 err = crypto_shash_init(shash);
0401 if (err)
0402 return err;
0403
0404
0405 err = crypto_shash_update(shash, node + 8, ofs_hmac - 8);
0406 if (err < 0)
0407 return err;
0408
0409
0410 if (len - ofs_hmac - hmac_len > 0) {
0411 err = crypto_shash_update(shash, node + ofs_hmac + hmac_len,
0412 len - ofs_hmac - hmac_len);
0413 if (err < 0)
0414 return err;
0415 }
0416
0417 return crypto_shash_final(shash, hmac);
0418 }
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len,
0433 int ofs_hmac)
0434 {
0435 return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac);
0436 }
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448 int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node,
0449 int len, int ofs_hmac)
0450 {
0451 int hmac_len = c->hmac_desc_len;
0452 u8 *hmac;
0453 int err;
0454
0455 hmac = kmalloc(hmac_len, GFP_NOFS);
0456 if (!hmac)
0457 return -ENOMEM;
0458
0459 err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac);
0460 if (err) {
0461 kfree(hmac);
0462 return err;
0463 }
0464
0465 err = crypto_memneq(hmac, node + ofs_hmac, hmac_len);
0466
0467 kfree(hmac);
0468
0469 if (!err)
0470 return 0;
0471
0472 return -EPERM;
0473 }
0474
0475 int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
0476 struct shash_desc *target)
0477 {
0478 u8 *state;
0479 int err;
0480
0481 state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS);
0482 if (!state)
0483 return -ENOMEM;
0484
0485 err = crypto_shash_export(src, state);
0486 if (err)
0487 goto out;
0488
0489 err = crypto_shash_import(target, state);
0490
0491 out:
0492 kfree(state);
0493
0494 return err;
0495 }
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509 int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac)
0510 {
0511 SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
0512 int err;
0513 const char well_known_message[] = "UBIFS";
0514
0515 if (!ubifs_authenticated(c))
0516 return 0;
0517
0518 shash->tfm = c->hmac_tfm;
0519
0520 err = crypto_shash_init(shash);
0521 if (err)
0522 return err;
0523
0524 err = crypto_shash_update(shash, well_known_message,
0525 sizeof(well_known_message) - 1);
0526 if (err < 0)
0527 return err;
0528
0529 err = crypto_shash_final(shash, hmac);
0530 if (err)
0531 return err;
0532 return 0;
0533 }
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543 bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac)
0544 {
0545 return !memchr_inv(hmac, 0, c->hmac_desc_len);
0546 }