Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
0006  */
0007 
0008 /*
0009  * This file implements various helper functions for UBIFS authentication support
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  * ubifs_node_calc_hash - calculate the hash of a UBIFS node
0023  * @c: UBIFS file-system description object
0024  * @node: the node to calculate a hash for
0025  * @hash: the returned hash
0026  *
0027  * Returns 0 for success or a negative error code otherwise.
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  * ubifs_hash_calc_hmac - calculate a HMAC from a hash
0040  * @c: UBIFS file-system description object
0041  * @hash: the node to calculate a HMAC for
0042  * @hmac: the returned HMAC
0043  *
0044  * Returns 0 for success or a negative error code otherwise.
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  * ubifs_prepare_auth_node - Prepare an authentication node
0054  * @c: UBIFS file-system description object
0055  * @node: the node to calculate a hash for
0056  * @inhash: input hash of previous nodes
0057  *
0058  * This function prepares an authentication node for writing onto flash.
0059  * It creates a HMAC from the given input hash and writes it to the node.
0060  *
0061  * Returns 0 for success or a negative error code otherwise.
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  * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
0116  * @c: UBIFS file-system description object
0117  *
0118  * This function returns a descriptor suitable for hashing a node. Free after use
0119  * with kfree.
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  * ubifs_bad_hash - Report hash mismatches
0128  * @c: UBIFS file-system description object
0129  * @node: the node
0130  * @hash: the expected hash
0131  * @lnum: the LEB @node was read from
0132  * @offs: offset in LEB @node was read from
0133  *
0134  * This function reports a hash mismatch when a node has a different hash than
0135  * expected.
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  * __ubifs_node_check_hash - check the hash of a node against given hash
0155  * @c: UBIFS file-system description object
0156  * @node: the node
0157  * @expected: the expected hash
0158  *
0159  * This function calculates a hash over a node and compares it to the given hash.
0160  * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
0161  * negative error code is returned.
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  * ubifs_sb_verify_signature - verify the signature of a superblock
0181  * @c: UBIFS file-system description object
0182  * @sup: The superblock node
0183  *
0184  * To support offline signed images the superblock can be signed with a
0185  * PKCS#7 signature. The signature is placed directly behind the superblock
0186  * node in an ubifs_sig_node.
0187  *
0188  * Returns 0 when the signature can be successfully verified or a negative
0189  * error code if not.
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  * ubifs_init_authentication - initialize UBIFS authentication support
0252  * @c: UBIFS file-system description object
0253  *
0254  * This function returns 0 for success or a negative error code otherwise.
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         /* key was revoked before we acquired its semaphore */
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  * __ubifs_exit_authentication - release resource
0362  * @c: UBIFS file-system description object
0363  *
0364  * This function releases the authentication related resources.
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  * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
0378  * @c: UBIFS file-system description object
0379  * @node: the node to insert a HMAC into.
0380  * @len: the length of the node
0381  * @ofs_hmac: the offset in the node where the HMAC is inserted
0382  * @hmac: returned HMAC
0383  *
0384  * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
0385  * embedded into the node, so this area is not covered by the HMAC. Also not
0386  * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
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     /* behind common node header CRC up to HMAC begin */
0405     err = crypto_shash_update(shash, node + 8, ofs_hmac - 8);
0406     if (err < 0)
0407         return err;
0408 
0409     /* behind HMAC, if any */
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  * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
0422  * @c: UBIFS file-system description object
0423  * @node: the node to insert a HMAC into.
0424  * @len: the length of the node
0425  * @ofs_hmac: the offset in the node where the HMAC is inserted
0426  *
0427  * This function inserts a HMAC at offset @ofs_hmac into the node given in
0428  * @node.
0429  *
0430  * This function returns 0 for success or a negative error code otherwise.
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  * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
0440  * @c: UBIFS file-system description object
0441  * @node: the node to insert a HMAC into.
0442  * @len: the length of the node
0443  * @ofs_hmac: the offset in the node where the HMAC is inserted
0444  *
0445  * This function verifies the HMAC at offset @ofs_hmac of the node given in
0446  * @node. Returns 0 if successful or a negative error code otherwise.
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  * ubifs_hmac_wkm - Create a HMAC of the well known message
0499  * @c: UBIFS file-system description object
0500  * @hmac: The HMAC of the well known message
0501  *
0502  * This function creates a HMAC of a well known message. This is used
0503  * to check if the provided key is suitable to authenticate a UBIFS
0504  * image. This is only a convenience to the user to provide a better
0505  * error message when the wrong key is provided.
0506  *
0507  * This function returns 0 for success or a negative error code otherwise.
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  * ubifs_hmac_zero - test if a HMAC is zero
0537  * @c: UBIFS file-system description object
0538  * @hmac: the HMAC to test
0539  *
0540  * This function tests if a HMAC is zero and returns true if it is
0541  * and false otherwise.
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 }