Skip to content

Commit 37a3741

Browse files
committed
Revert "apparmor: use SHA-256 library API instead of crypto_shash API"
This reverts commit e9ed1eb. Eric has requested that this patch be taken through the libcrypto-next tree, instead. Signed-off-by: John Johansen <[email protected]>
1 parent aff426f commit 37a3741

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

security/apparmor/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ config SECURITY_APPARMOR_INTROSPECT_POLICY
5959
config SECURITY_APPARMOR_HASH
6060
bool "Enable introspection of sha256 hashes for loaded profiles"
6161
depends on SECURITY_APPARMOR_INTROSPECT_POLICY
62-
select CRYPTO_LIB_SHA256
62+
select CRYPTO
63+
select CRYPTO_SHA256
6364
default y
6465
help
6566
This option selects whether introspection of loaded policy

security/apparmor/crypto.c

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,113 @@
1111
* it should be.
1212
*/
1313

14-
#include <crypto/sha2.h>
14+
#include <crypto/hash.h>
1515

1616
#include "include/apparmor.h"
1717
#include "include/crypto.h"
1818

19+
static unsigned int apparmor_hash_size;
20+
21+
static struct crypto_shash *apparmor_tfm;
22+
1923
unsigned int aa_hash_size(void)
2024
{
21-
return SHA256_DIGEST_SIZE;
25+
return apparmor_hash_size;
2226
}
2327

2428
char *aa_calc_hash(void *data, size_t len)
2529
{
30+
SHASH_DESC_ON_STACK(desc, apparmor_tfm);
2631
char *hash;
32+
int error;
33+
34+
if (!apparmor_tfm)
35+
return NULL;
2736

28-
hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
37+
hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
2938
if (!hash)
3039
return ERR_PTR(-ENOMEM);
3140

32-
sha256(data, len, hash);
41+
desc->tfm = apparmor_tfm;
42+
43+
error = crypto_shash_init(desc);
44+
if (error)
45+
goto fail;
46+
error = crypto_shash_update(desc, (u8 *) data, len);
47+
if (error)
48+
goto fail;
49+
error = crypto_shash_final(desc, hash);
50+
if (error)
51+
goto fail;
52+
3353
return hash;
54+
55+
fail:
56+
kfree(hash);
57+
58+
return ERR_PTR(error);
3459
}
3560

3661
int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
3762
size_t len)
3863
{
39-
struct sha256_state state;
64+
SHASH_DESC_ON_STACK(desc, apparmor_tfm);
65+
int error;
4066
__le32 le32_version = cpu_to_le32(version);
4167

4268
if (!aa_g_hash_policy)
4369
return 0;
4470

45-
profile->hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
71+
if (!apparmor_tfm)
72+
return 0;
73+
74+
profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
4675
if (!profile->hash)
4776
return -ENOMEM;
4877

49-
sha256_init(&state);
50-
sha256_update(&state, (u8 *)&le32_version, 4);
51-
sha256_update(&state, (u8 *)start, len);
52-
sha256_final(&state, profile->hash);
78+
desc->tfm = apparmor_tfm;
79+
80+
error = crypto_shash_init(desc);
81+
if (error)
82+
goto fail;
83+
error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
84+
if (error)
85+
goto fail;
86+
error = crypto_shash_update(desc, (u8 *) start, len);
87+
if (error)
88+
goto fail;
89+
error = crypto_shash_final(desc, profile->hash);
90+
if (error)
91+
goto fail;
92+
5393
return 0;
94+
95+
fail:
96+
kfree(profile->hash);
97+
profile->hash = NULL;
98+
99+
return error;
54100
}
55101

56102
static int __init init_profile_hash(void)
57103
{
58-
if (apparmor_initialized)
59-
aa_info_message("AppArmor sha256 policy hashing enabled");
104+
struct crypto_shash *tfm;
105+
106+
if (!apparmor_initialized)
107+
return 0;
108+
109+
tfm = crypto_alloc_shash("sha256", 0, 0);
110+
if (IS_ERR(tfm)) {
111+
int error = PTR_ERR(tfm);
112+
AA_ERROR("failed to setup profile sha256 hashing: %d\n", error);
113+
return error;
114+
}
115+
apparmor_tfm = tfm;
116+
apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
117+
118+
aa_info_message("AppArmor sha256 policy hashing enabled");
119+
60120
return 0;
61121
}
122+
62123
late_initcall(init_profile_hash);

0 commit comments

Comments
 (0)