Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * AppArmor security module |
| 4 | * |
| 5 | * This file contains AppArmor policy loading interface function definitions. |
| 6 | * |
| 7 | * Copyright 2013 Canonical Ltd. |
| 8 | * |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 9 | * Fns to provide a checksum of policy that has been loaded this can be |
| 10 | * compared to userspace policy compiles to check loaded policy is what |
| 11 | * it should be. |
| 12 | */ |
| 13 | |
Tyler Hicks | 71ac7f6 | 2013-09-29 08:39:21 -0700 | [diff] [blame] | 14 | #include <crypto/hash.h> |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "include/apparmor.h" |
| 17 | #include "include/crypto.h" |
| 18 | |
| 19 | static unsigned int apparmor_hash_size; |
| 20 | |
Tyler Hicks | 71ac7f6 | 2013-09-29 08:39:21 -0700 | [diff] [blame] | 21 | static struct crypto_shash *apparmor_tfm; |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 22 | |
| 23 | unsigned int aa_hash_size(void) |
| 24 | { |
| 25 | return apparmor_hash_size; |
| 26 | } |
| 27 | |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 28 | char *aa_calc_hash(void *data, size_t len) |
| 29 | { |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 30 | SHASH_DESC_ON_STACK(desc, apparmor_tfm); |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 31 | char *hash = NULL; |
| 32 | int error = -ENOMEM; |
| 33 | |
| 34 | if (!apparmor_tfm) |
| 35 | return NULL; |
| 36 | |
| 37 | hash = kzalloc(apparmor_hash_size, GFP_KERNEL); |
| 38 | if (!hash) |
| 39 | goto fail; |
| 40 | |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 41 | desc->tfm = apparmor_tfm; |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 42 | |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 43 | error = crypto_shash_init(desc); |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 44 | if (error) |
| 45 | goto fail; |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 46 | error = crypto_shash_update(desc, (u8 *) data, len); |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 47 | if (error) |
| 48 | goto fail; |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 49 | error = crypto_shash_final(desc, hash); |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 50 | if (error) |
| 51 | goto fail; |
| 52 | |
| 53 | return hash; |
| 54 | |
| 55 | fail: |
| 56 | kfree(hash); |
| 57 | |
| 58 | return ERR_PTR(error); |
| 59 | } |
| 60 | |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 61 | int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start, |
| 62 | size_t len) |
| 63 | { |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 64 | SHASH_DESC_ON_STACK(desc, apparmor_tfm); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 65 | int error = -ENOMEM; |
John Johansen | 5ac8c35 | 2017-01-16 00:42:55 -0800 | [diff] [blame] | 66 | __le32 le32_version = cpu_to_le32(version); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 67 | |
Arnd Bergmann | 7616ac7 | 2016-07-25 10:59:07 -0700 | [diff] [blame] | 68 | if (!aa_g_hash_policy) |
| 69 | return 0; |
| 70 | |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 71 | if (!apparmor_tfm) |
| 72 | return 0; |
| 73 | |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 74 | profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL); |
| 75 | if (!profile->hash) |
| 76 | goto fail; |
| 77 | |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 78 | desc->tfm = apparmor_tfm; |
Tyler Hicks | 71ac7f6 | 2013-09-29 08:39:21 -0700 | [diff] [blame] | 79 | |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 80 | error = crypto_shash_init(desc); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 81 | if (error) |
| 82 | goto fail; |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 83 | error = crypto_shash_update(desc, (u8 *) &le32_version, 4); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 84 | if (error) |
| 85 | goto fail; |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 86 | error = crypto_shash_update(desc, (u8 *) start, len); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 87 | if (error) |
| 88 | goto fail; |
Nicolas Iooss | 9814448 | 2017-04-06 06:55:21 -0700 | [diff] [blame] | 89 | error = crypto_shash_final(desc, profile->hash); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 90 | if (error) |
| 91 | goto fail; |
| 92 | |
| 93 | return 0; |
| 94 | |
| 95 | fail: |
| 96 | kfree(profile->hash); |
| 97 | profile->hash = NULL; |
| 98 | |
| 99 | return error; |
| 100 | } |
| 101 | |
| 102 | static int __init init_profile_hash(void) |
| 103 | { |
Tyler Hicks | 71ac7f6 | 2013-09-29 08:39:21 -0700 | [diff] [blame] | 104 | struct crypto_shash *tfm; |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 105 | |
| 106 | if (!apparmor_initialized) |
| 107 | return 0; |
| 108 | |
Eric Biggers | 3d234b3 | 2018-11-14 12:21:11 -0800 | [diff] [blame] | 109 | tfm = crypto_alloc_shash("sha1", 0, 0); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 110 | if (IS_ERR(tfm)) { |
| 111 | int error = PTR_ERR(tfm); |
| 112 | AA_ERROR("failed to setup profile sha1 hashing: %d\n", error); |
| 113 | return error; |
| 114 | } |
| 115 | apparmor_tfm = tfm; |
Tyler Hicks | 71ac7f6 | 2013-09-29 08:39:21 -0700 | [diff] [blame] | 116 | apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm); |
John Johansen | f8eb8a1 | 2013-08-14 11:27:36 -0700 | [diff] [blame] | 117 | |
| 118 | aa_info_message("AppArmor sha1 policy hashing enabled"); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | late_initcall(init_profile_hash); |