]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - security/apparmor/crypto.c
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.git] / security / apparmor / crypto.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
f8eb8a13
JJ
2/*
3 * AppArmor security module
4 *
5 * This file contains AppArmor policy loading interface function definitions.
6 *
7 * Copyright 2013 Canonical Ltd.
8 *
f8eb8a13
JJ
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
71ac7f62 14#include <crypto/hash.h>
f8eb8a13
JJ
15
16#include "include/apparmor.h"
17#include "include/crypto.h"
18
19static unsigned int apparmor_hash_size;
20
71ac7f62 21static struct crypto_shash *apparmor_tfm;
f8eb8a13
JJ
22
23unsigned int aa_hash_size(void)
24{
25 return apparmor_hash_size;
26}
27
c7cf8aae
JJ
28void aa_snprint_hashstr(char *out, unsigned char *hash, unsigned int hsize)
29{
30 unsigned int i;
31
32 for (i = 0; i < hsize; i++)
33 sprintf(out + i*2, "%.2x", hash[i]);
34 out[hsize*2] = 0;
35}
36
37char *aa_asprint_hashstr(unsigned char *hash, unsigned int hsize, gfp_t gfp)
38{
39 char *buffer = kmalloc(hsize*2 + 1, gfp);
40 if (!buffer)
41 return NULL;
42 aa_snprint_hashstr(buffer, hash, hsize);
43
44 return buffer;
45}
46
5ac8c355
JJ
47char *aa_calc_hash(void *data, size_t len)
48{
9814448d 49 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
5ac8c355
JJ
50 char *hash = NULL;
51 int error = -ENOMEM;
52
53 if (!apparmor_tfm)
54 return NULL;
55
56 hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
57 if (!hash)
58 goto fail;
59
9814448d 60 desc->tfm = apparmor_tfm;
5ac8c355 61
9814448d 62 error = crypto_shash_init(desc);
5ac8c355
JJ
63 if (error)
64 goto fail;
9814448d 65 error = crypto_shash_update(desc, (u8 *) data, len);
5ac8c355
JJ
66 if (error)
67 goto fail;
9814448d 68 error = crypto_shash_final(desc, hash);
5ac8c355
JJ
69 if (error)
70 goto fail;
71
72 return hash;
73
74fail:
75 kfree(hash);
76
77 return ERR_PTR(error);
78}
79
f8eb8a13
JJ
80int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
81 size_t len)
82{
9814448d 83 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
f8eb8a13 84 int error = -ENOMEM;
5ac8c355 85 __le32 le32_version = cpu_to_le32(version);
f8eb8a13 86
7616ac70
AB
87 if (!aa_g_hash_policy)
88 return 0;
89
f8eb8a13
JJ
90 if (!apparmor_tfm)
91 return 0;
92
f8eb8a13
JJ
93 profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
94 if (!profile->hash)
95 goto fail;
96
9814448d 97 desc->tfm = apparmor_tfm;
71ac7f62 98
9814448d 99 error = crypto_shash_init(desc);
f8eb8a13
JJ
100 if (error)
101 goto fail;
9814448d 102 error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
f8eb8a13
JJ
103 if (error)
104 goto fail;
9814448d 105 error = crypto_shash_update(desc, (u8 *) start, len);
f8eb8a13
JJ
106 if (error)
107 goto fail;
9814448d 108 error = crypto_shash_final(desc, profile->hash);
f8eb8a13
JJ
109 if (error)
110 goto fail;
111
112 return 0;
113
114fail:
115 kfree(profile->hash);
116 profile->hash = NULL;
117
118 return error;
119}
120
121static int __init init_profile_hash(void)
122{
71ac7f62 123 struct crypto_shash *tfm;
f8eb8a13
JJ
124
125 if (!apparmor_initialized)
126 return 0;
127
3d234b33 128 tfm = crypto_alloc_shash("sha1", 0, 0);
f8eb8a13
JJ
129 if (IS_ERR(tfm)) {
130 int error = PTR_ERR(tfm);
131 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
132 return error;
133 }
134 apparmor_tfm = tfm;
71ac7f62 135 apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
f8eb8a13
JJ
136
137 aa_info_message("AppArmor sha1 policy hashing enabled");
138
139 return 0;
140}
141
142late_initcall(init_profile_hash);