Skip to content

Commit 71ac7f6

Browse files
tyhicksJames Morris
authored andcommitted
apparmor: Use shash crypto API interface for profile hashes
Use the shash interface, rather than the hash interface, when hashing AppArmor profiles. The shash interface does not use scatterlists and it is a better fit for what AppArmor needs. This fixes a kernel paging BUG when aa_calc_profile_hash() is passed a buffer from vmalloc(). The hash interface requires callers to handle vmalloc() buffers differently than what AppArmor was doing. Due to vmalloc() memory not being physically contiguous, each individual page behind the buffer must be assigned to a scatterlist with sg_set_page() and then the scatterlist passed to crypto_hash_update(). The shash interface does not have that limitation and allows vmalloc() and kmalloc() buffers to be handled in the same manner. BugLink: https://launchpad.net/bugs/1216294/ BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=62261 Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Acked-by: Seth Arnold <seth.arnold@canonical.com> Signed-off-by: John Johansen <john.johansen@canonical.com> Signed-off-by: James Morris <james.l.morris@oracle.com>
1 parent 15c03dd commit 71ac7f6

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

security/apparmor/crypto.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
* it should be.
1616
*/
1717

18-
#include <linux/crypto.h>
18+
#include <crypto/hash.h>
1919

2020
#include "include/apparmor.h"
2121
#include "include/crypto.h"
2222

2323
static unsigned int apparmor_hash_size;
2424

25-
static struct crypto_hash *apparmor_tfm;
25+
static struct crypto_shash *apparmor_tfm;
2626

2727
unsigned int aa_hash_size(void)
2828
{
@@ -32,35 +32,33 @@ unsigned int aa_hash_size(void)
3232
int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
3333
size_t len)
3434
{
35-
struct scatterlist sg[2];
36-
struct hash_desc desc = {
37-
.tfm = apparmor_tfm,
38-
.flags = 0
39-
};
35+
struct {
36+
struct shash_desc shash;
37+
char ctx[crypto_shash_descsize(apparmor_tfm)];
38+
} desc;
4039
int error = -ENOMEM;
4140
u32 le32_version = cpu_to_le32(version);
4241

4342
if (!apparmor_tfm)
4443
return 0;
4544

46-
sg_init_table(sg, 2);
47-
sg_set_buf(&sg[0], &le32_version, 4);
48-
sg_set_buf(&sg[1], (u8 *) start, len);
49-
5045
profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
5146
if (!profile->hash)
5247
goto fail;
5348

54-
error = crypto_hash_init(&desc);
49+
desc.shash.tfm = apparmor_tfm;
50+
desc.shash.flags = 0;
51+
52+
error = crypto_shash_init(&desc.shash);
5553
if (error)
5654
goto fail;
57-
error = crypto_hash_update(&desc, &sg[0], 4);
55+
error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
5856
if (error)
5957
goto fail;
60-
error = crypto_hash_update(&desc, &sg[1], len);
58+
error = crypto_shash_update(&desc.shash, (u8 *) start, len);
6159
if (error)
6260
goto fail;
63-
error = crypto_hash_final(&desc, profile->hash);
61+
error = crypto_shash_final(&desc.shash, profile->hash);
6462
if (error)
6563
goto fail;
6664

@@ -75,19 +73,19 @@ int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
7573

7674
static int __init init_profile_hash(void)
7775
{
78-
struct crypto_hash *tfm;
76+
struct crypto_shash *tfm;
7977

8078
if (!apparmor_initialized)
8179
return 0;
8280

83-
tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
81+
tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);
8482
if (IS_ERR(tfm)) {
8583
int error = PTR_ERR(tfm);
8684
AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
8785
return error;
8886
}
8987
apparmor_tfm = tfm;
90-
apparmor_hash_size = crypto_hash_digestsize(apparmor_tfm);
88+
apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
9189

9290
aa_info_message("AppArmor sha1 policy hashing enabled");
9391

0 commit comments

Comments
 (0)