• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file is part of UBIFS.
4  *
5  * Copyright (C) 2018 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6  */
7 
8 /*
9  * This file implements various helper functions for UBIFS authentication support
10  */
11 
12 #include <linux/crypto.h>
13 #include <linux/verification.h>
14 #include <crypto/hash.h>
15 #include <crypto/sha.h>
16 #include <crypto/algapi.h>
17 #include <keys/user-type.h>
18 #include <keys/asymmetric-type.h>
19 
20 #include "ubifs.h"
21 
22 /**
23  * ubifs_node_calc_hash - calculate the hash of a UBIFS node
24  * @c: UBIFS file-system description object
25  * @node: the node to calculate a hash for
26  * @hash: the returned hash
27  *
28  * Returns 0 for success or a negative error code otherwise.
29  */
__ubifs_node_calc_hash(const struct ubifs_info * c,const void * node,u8 * hash)30 int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *node,
31 			    u8 *hash)
32 {
33 	const struct ubifs_ch *ch = node;
34 
35 	return crypto_shash_tfm_digest(c->hash_tfm, node, le32_to_cpu(ch->len),
36 				       hash);
37 }
38 
39 /**
40  * ubifs_hash_calc_hmac - calculate a HMAC from a hash
41  * @c: UBIFS file-system description object
42  * @hash: the node to calculate a HMAC for
43  * @hmac: the returned HMAC
44  *
45  * Returns 0 for success or a negative error code otherwise.
46  */
ubifs_hash_calc_hmac(const struct ubifs_info * c,const u8 * hash,u8 * hmac)47 static int ubifs_hash_calc_hmac(const struct ubifs_info *c, const u8 *hash,
48 				 u8 *hmac)
49 {
50 	return crypto_shash_tfm_digest(c->hmac_tfm, hash, c->hash_len, hmac);
51 }
52 
53 /**
54  * ubifs_prepare_auth_node - Prepare an authentication node
55  * @c: UBIFS file-system description object
56  * @node: the node to calculate a hash for
57  * @inhash: input hash of previous nodes
58  *
59  * This function prepares an authentication node for writing onto flash.
60  * It creates a HMAC from the given input hash and writes it to the node.
61  *
62  * Returns 0 for success or a negative error code otherwise.
63  */
ubifs_prepare_auth_node(struct ubifs_info * c,void * node,struct shash_desc * inhash)64 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
65 			     struct shash_desc *inhash)
66 {
67 	struct ubifs_auth_node *auth = node;
68 	u8 hash[UBIFS_HASH_ARR_SZ];
69 	int err;
70 
71 	{
72 		SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
73 
74 		hash_desc->tfm = c->hash_tfm;
75 		ubifs_shash_copy_state(c, inhash, hash_desc);
76 
77 		err = crypto_shash_final(hash_desc, hash);
78 		if (err)
79 			return err;
80 	}
81 
82 	err = ubifs_hash_calc_hmac(c, hash, auth->hmac);
83 	if (err)
84 		return err;
85 
86 	auth->ch.node_type = UBIFS_AUTH_NODE;
87 	ubifs_prepare_node(c, auth, ubifs_auth_node_sz(c), 0);
88 	return 0;
89 }
90 
ubifs_get_desc(const struct ubifs_info * c,struct crypto_shash * tfm)91 static struct shash_desc *ubifs_get_desc(const struct ubifs_info *c,
92 					 struct crypto_shash *tfm)
93 {
94 	struct shash_desc *desc;
95 	int err;
96 
97 	if (!ubifs_authenticated(c))
98 		return NULL;
99 
100 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
101 	if (!desc)
102 		return ERR_PTR(-ENOMEM);
103 
104 	desc->tfm = tfm;
105 
106 	err = crypto_shash_init(desc);
107 	if (err) {
108 		kfree(desc);
109 		return ERR_PTR(err);
110 	}
111 
112 	return desc;
113 }
114 
115 /**
116  * __ubifs_hash_get_desc - get a descriptor suitable for hashing a node
117  * @c: UBIFS file-system description object
118  *
119  * This function returns a descriptor suitable for hashing a node. Free after use
120  * with kfree.
121  */
__ubifs_hash_get_desc(const struct ubifs_info * c)122 struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c)
123 {
124 	return ubifs_get_desc(c, c->hash_tfm);
125 }
126 
127 /**
128  * ubifs_bad_hash - Report hash mismatches
129  * @c: UBIFS file-system description object
130  * @node: the node
131  * @hash: the expected hash
132  * @lnum: the LEB @node was read from
133  * @offs: offset in LEB @node was read from
134  *
135  * This function reports a hash mismatch when a node has a different hash than
136  * expected.
137  */
ubifs_bad_hash(const struct ubifs_info * c,const void * node,const u8 * hash,int lnum,int offs)138 void ubifs_bad_hash(const struct ubifs_info *c, const void *node, const u8 *hash,
139 		    int lnum, int offs)
140 {
141 	int len = min(c->hash_len, 20);
142 	int cropped = len != c->hash_len;
143 	const char *cont = cropped ? "..." : "";
144 
145 	u8 calc[UBIFS_HASH_ARR_SZ];
146 
147 	__ubifs_node_calc_hash(c, node, calc);
148 
149 	ubifs_err(c, "hash mismatch on node at LEB %d:%d", lnum, offs);
150 	ubifs_err(c, "hash expected:   %*ph%s", len, hash, cont);
151 	ubifs_err(c, "hash calculated: %*ph%s", len, calc, cont);
152 }
153 
154 /**
155  * __ubifs_node_check_hash - check the hash of a node against given hash
156  * @c: UBIFS file-system description object
157  * @node: the node
158  * @expected: the expected hash
159  *
160  * This function calculates a hash over a node and compares it to the given hash.
161  * Returns 0 if both hashes are equal or authentication is disabled, otherwise a
162  * negative error code is returned.
163  */
__ubifs_node_check_hash(const struct ubifs_info * c,const void * node,const u8 * expected)164 int __ubifs_node_check_hash(const struct ubifs_info *c, const void *node,
165 			    const u8 *expected)
166 {
167 	u8 calc[UBIFS_HASH_ARR_SZ];
168 	int err;
169 
170 	err = __ubifs_node_calc_hash(c, node, calc);
171 	if (err)
172 		return err;
173 
174 	if (ubifs_check_hash(c, expected, calc))
175 		return -EPERM;
176 
177 	return 0;
178 }
179 
180 /**
181  * ubifs_sb_verify_signature - verify the signature of a superblock
182  * @c: UBIFS file-system description object
183  * @sup: The superblock node
184  *
185  * To support offline signed images the superblock can be signed with a
186  * PKCS#7 signature. The signature is placed directly behind the superblock
187  * node in an ubifs_sig_node.
188  *
189  * Returns 0 when the signature can be successfully verified or a negative
190  * error code if not.
191  */
ubifs_sb_verify_signature(struct ubifs_info * c,const struct ubifs_sb_node * sup)192 int ubifs_sb_verify_signature(struct ubifs_info *c,
193 			      const struct ubifs_sb_node *sup)
194 {
195 	int err;
196 	struct ubifs_scan_leb *sleb;
197 	struct ubifs_scan_node *snod;
198 	const struct ubifs_sig_node *signode;
199 
200 	sleb = ubifs_scan(c, UBIFS_SB_LNUM, UBIFS_SB_NODE_SZ, c->sbuf, 0);
201 	if (IS_ERR(sleb)) {
202 		err = PTR_ERR(sleb);
203 		return err;
204 	}
205 
206 	if (sleb->nodes_cnt == 0) {
207 		ubifs_err(c, "Unable to find signature node");
208 		err = -EINVAL;
209 		goto out_destroy;
210 	}
211 
212 	snod = list_first_entry(&sleb->nodes, struct ubifs_scan_node, list);
213 
214 	if (snod->type != UBIFS_SIG_NODE) {
215 		ubifs_err(c, "Signature node is of wrong type");
216 		err = -EINVAL;
217 		goto out_destroy;
218 	}
219 
220 	signode = snod->node;
221 
222 	if (le32_to_cpu(signode->len) > snod->len + sizeof(struct ubifs_sig_node)) {
223 		ubifs_err(c, "invalid signature len %d", le32_to_cpu(signode->len));
224 		err = -EINVAL;
225 		goto out_destroy;
226 	}
227 
228 	if (le32_to_cpu(signode->type) != UBIFS_SIGNATURE_TYPE_PKCS7) {
229 		ubifs_err(c, "Signature type %d is not supported\n",
230 			  le32_to_cpu(signode->type));
231 		err = -EINVAL;
232 		goto out_destroy;
233 	}
234 
235 	err = verify_pkcs7_signature(sup, sizeof(struct ubifs_sb_node),
236 				     signode->sig, le32_to_cpu(signode->len),
237 				     NULL, VERIFYING_UNSPECIFIED_SIGNATURE,
238 				     NULL, NULL);
239 
240 	if (err)
241 		ubifs_err(c, "Failed to verify signature");
242 	else
243 		ubifs_msg(c, "Successfully verified super block signature");
244 
245 out_destroy:
246 	ubifs_scan_destroy(sleb);
247 
248 	return err;
249 }
250 
251 /**
252  * ubifs_init_authentication - initialize UBIFS authentication support
253  * @c: UBIFS file-system description object
254  *
255  * This function returns 0 for success or a negative error code otherwise.
256  */
ubifs_init_authentication(struct ubifs_info * c)257 int ubifs_init_authentication(struct ubifs_info *c)
258 {
259 	struct key *keyring_key;
260 	const struct user_key_payload *ukp;
261 	int err;
262 	char hmac_name[CRYPTO_MAX_ALG_NAME];
263 
264 	if (!c->auth_hash_name) {
265 		ubifs_err(c, "authentication hash name needed with authentication");
266 		return -EINVAL;
267 	}
268 
269 	c->auth_hash_algo = match_string(hash_algo_name, HASH_ALGO__LAST,
270 					 c->auth_hash_name);
271 	if ((int)c->auth_hash_algo < 0) {
272 		ubifs_err(c, "Unknown hash algo %s specified",
273 			  c->auth_hash_name);
274 		return -EINVAL;
275 	}
276 
277 	snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
278 		 c->auth_hash_name);
279 
280 	keyring_key = request_key(&key_type_logon, c->auth_key_name, NULL);
281 
282 	if (IS_ERR(keyring_key)) {
283 		ubifs_err(c, "Failed to request key: %ld",
284 			  PTR_ERR(keyring_key));
285 		return PTR_ERR(keyring_key);
286 	}
287 
288 	down_read(&keyring_key->sem);
289 
290 	if (keyring_key->type != &key_type_logon) {
291 		ubifs_err(c, "key type must be logon");
292 		err = -ENOKEY;
293 		goto out;
294 	}
295 
296 	ukp = user_key_payload_locked(keyring_key);
297 	if (!ukp) {
298 		/* key was revoked before we acquired its semaphore */
299 		err = -EKEYREVOKED;
300 		goto out;
301 	}
302 
303 	c->hash_tfm = crypto_alloc_shash(c->auth_hash_name, 0, 0);
304 	if (IS_ERR(c->hash_tfm)) {
305 		err = PTR_ERR(c->hash_tfm);
306 		ubifs_err(c, "Can not allocate %s: %d",
307 			  c->auth_hash_name, err);
308 		goto out;
309 	}
310 
311 	c->hash_len = crypto_shash_digestsize(c->hash_tfm);
312 	if (c->hash_len > UBIFS_HASH_ARR_SZ) {
313 		ubifs_err(c, "hash %s is bigger than maximum allowed hash size (%d > %d)",
314 			  c->auth_hash_name, c->hash_len, UBIFS_HASH_ARR_SZ);
315 		err = -EINVAL;
316 		goto out_free_hash;
317 	}
318 
319 	c->hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0);
320 	if (IS_ERR(c->hmac_tfm)) {
321 		err = PTR_ERR(c->hmac_tfm);
322 		ubifs_err(c, "Can not allocate %s: %d", hmac_name, err);
323 		goto out_free_hash;
324 	}
325 
326 	c->hmac_desc_len = crypto_shash_digestsize(c->hmac_tfm);
327 	if (c->hmac_desc_len > UBIFS_HMAC_ARR_SZ) {
328 		ubifs_err(c, "hmac %s is bigger than maximum allowed hmac size (%d > %d)",
329 			  hmac_name, c->hmac_desc_len, UBIFS_HMAC_ARR_SZ);
330 		err = -EINVAL;
331 		goto out_free_hmac;
332 	}
333 
334 	err = crypto_shash_setkey(c->hmac_tfm, ukp->data, ukp->datalen);
335 	if (err)
336 		goto out_free_hmac;
337 
338 	c->authenticated = true;
339 
340 	c->log_hash = ubifs_hash_get_desc(c);
341 	if (IS_ERR(c->log_hash)) {
342 		err = PTR_ERR(c->log_hash);
343 		goto out_free_hmac;
344 	}
345 
346 	err = 0;
347 
348 out_free_hmac:
349 	if (err)
350 		crypto_free_shash(c->hmac_tfm);
351 out_free_hash:
352 	if (err)
353 		crypto_free_shash(c->hash_tfm);
354 out:
355 	up_read(&keyring_key->sem);
356 	key_put(keyring_key);
357 
358 	return err;
359 }
360 
361 /**
362  * __ubifs_exit_authentication - release resource
363  * @c: UBIFS file-system description object
364  *
365  * This function releases the authentication related resources.
366  */
__ubifs_exit_authentication(struct ubifs_info * c)367 void __ubifs_exit_authentication(struct ubifs_info *c)
368 {
369 	if (!ubifs_authenticated(c))
370 		return;
371 
372 	crypto_free_shash(c->hmac_tfm);
373 	crypto_free_shash(c->hash_tfm);
374 	kfree(c->log_hash);
375 }
376 
377 /**
378  * ubifs_node_calc_hmac - calculate the HMAC of a UBIFS node
379  * @c: UBIFS file-system description object
380  * @node: the node to insert a HMAC into.
381  * @len: the length of the node
382  * @ofs_hmac: the offset in the node where the HMAC is inserted
383  * @hmac: returned HMAC
384  *
385  * This function calculates a HMAC of a UBIFS node. The HMAC is expected to be
386  * embedded into the node, so this area is not covered by the HMAC. Also not
387  * covered is the UBIFS_NODE_MAGIC and the CRC of the node.
388  */
ubifs_node_calc_hmac(const struct ubifs_info * c,const void * node,int len,int ofs_hmac,void * hmac)389 static int ubifs_node_calc_hmac(const struct ubifs_info *c, const void *node,
390 				int len, int ofs_hmac, void *hmac)
391 {
392 	SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
393 	int hmac_len = c->hmac_desc_len;
394 	int err;
395 
396 	ubifs_assert(c, ofs_hmac > 8);
397 	ubifs_assert(c, ofs_hmac + hmac_len < len);
398 
399 	shash->tfm = c->hmac_tfm;
400 
401 	err = crypto_shash_init(shash);
402 	if (err)
403 		return err;
404 
405 	/* behind common node header CRC up to HMAC begin */
406 	err = crypto_shash_update(shash, node + 8, ofs_hmac - 8);
407 	if (err < 0)
408 		return err;
409 
410 	/* behind HMAC, if any */
411 	if (len - ofs_hmac - hmac_len > 0) {
412 		err = crypto_shash_update(shash, node + ofs_hmac + hmac_len,
413 			    len - ofs_hmac - hmac_len);
414 		if (err < 0)
415 			return err;
416 	}
417 
418 	return crypto_shash_final(shash, hmac);
419 }
420 
421 /**
422  * __ubifs_node_insert_hmac - insert a HMAC into a UBIFS node
423  * @c: UBIFS file-system description object
424  * @node: the node to insert a HMAC into.
425  * @len: the length of the node
426  * @ofs_hmac: the offset in the node where the HMAC is inserted
427  *
428  * This function inserts a HMAC at offset @ofs_hmac into the node given in
429  * @node.
430  *
431  * This function returns 0 for success or a negative error code otherwise.
432  */
__ubifs_node_insert_hmac(const struct ubifs_info * c,void * node,int len,int ofs_hmac)433 int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *node, int len,
434 			    int ofs_hmac)
435 {
436 	return ubifs_node_calc_hmac(c, node, len, ofs_hmac, node + ofs_hmac);
437 }
438 
439 /**
440  * __ubifs_node_verify_hmac - verify the HMAC of UBIFS node
441  * @c: UBIFS file-system description object
442  * @node: the node to insert a HMAC into.
443  * @len: the length of the node
444  * @ofs_hmac: the offset in the node where the HMAC is inserted
445  *
446  * This function verifies the HMAC at offset @ofs_hmac of the node given in
447  * @node. Returns 0 if successful or a negative error code otherwise.
448  */
__ubifs_node_verify_hmac(const struct ubifs_info * c,const void * node,int len,int ofs_hmac)449 int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *node,
450 			     int len, int ofs_hmac)
451 {
452 	int hmac_len = c->hmac_desc_len;
453 	u8 *hmac;
454 	int err;
455 
456 	hmac = kmalloc(hmac_len, GFP_NOFS);
457 	if (!hmac)
458 		return -ENOMEM;
459 
460 	err = ubifs_node_calc_hmac(c, node, len, ofs_hmac, hmac);
461 	if (err) {
462 		kfree(hmac);
463 		return err;
464 	}
465 
466 	err = crypto_memneq(hmac, node + ofs_hmac, hmac_len);
467 
468 	kfree(hmac);
469 
470 	if (!err)
471 		return 0;
472 
473 	return -EPERM;
474 }
475 
__ubifs_shash_copy_state(const struct ubifs_info * c,struct shash_desc * src,struct shash_desc * target)476 int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
477 			     struct shash_desc *target)
478 {
479 	u8 *state;
480 	int err;
481 
482 	state = kmalloc(crypto_shash_descsize(src->tfm), GFP_NOFS);
483 	if (!state)
484 		return -ENOMEM;
485 
486 	err = crypto_shash_export(src, state);
487 	if (err)
488 		goto out;
489 
490 	err = crypto_shash_import(target, state);
491 
492 out:
493 	kfree(state);
494 
495 	return err;
496 }
497 
498 /**
499  * ubifs_hmac_wkm - Create a HMAC of the well known message
500  * @c: UBIFS file-system description object
501  * @hmac: The HMAC of the well known message
502  *
503  * This function creates a HMAC of a well known message. This is used
504  * to check if the provided key is suitable to authenticate a UBIFS
505  * image. This is only a convenience to the user to provide a better
506  * error message when the wrong key is provided.
507  *
508  * This function returns 0 for success or a negative error code otherwise.
509  */
ubifs_hmac_wkm(struct ubifs_info * c,u8 * hmac)510 int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac)
511 {
512 	SHASH_DESC_ON_STACK(shash, c->hmac_tfm);
513 	int err;
514 	const char well_known_message[] = "UBIFS";
515 
516 	if (!ubifs_authenticated(c))
517 		return 0;
518 
519 	shash->tfm = c->hmac_tfm;
520 
521 	err = crypto_shash_init(shash);
522 	if (err)
523 		return err;
524 
525 	err = crypto_shash_update(shash, well_known_message,
526 				  sizeof(well_known_message) - 1);
527 	if (err < 0)
528 		return err;
529 
530 	err = crypto_shash_final(shash, hmac);
531 	if (err)
532 		return err;
533 	return 0;
534 }
535 
536 /*
537  * ubifs_hmac_zero - test if a HMAC is zero
538  * @c: UBIFS file-system description object
539  * @hmac: the HMAC to test
540  *
541  * This function tests if a HMAC is zero and returns true if it is
542  * and false otherwise.
543  */
ubifs_hmac_zero(struct ubifs_info * c,const u8 * hmac)544 bool ubifs_hmac_zero(struct ubifs_info *c, const u8 *hmac)
545 {
546 	return !memchr_inv(hmac, 0, c->hmac_desc_len);
547 }
548