blob: 532471d0b3a0facd6c7cb70a930e4028ca6d0761 [file] [log] [blame]
John Johansenf8eb8a12013-08-14 11:27:36 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor policy loading interface function definitions.
5 *
6 * Copyright 2013 Canonical Ltd.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
11 * License.
12 *
13 * Fns to provide a checksum of policy that has been loaded this can be
14 * compared to userspace policy compiles to check loaded policy is what
15 * it should be.
16 */
17
Tyler Hicks71ac7f62013-09-29 08:39:21 -070018#include <crypto/hash.h>
John Johansenf8eb8a12013-08-14 11:27:36 -070019
20#include "include/apparmor.h"
21#include "include/crypto.h"
22
23static unsigned int apparmor_hash_size;
24
Tyler Hicks71ac7f62013-09-29 08:39:21 -070025static struct crypto_shash *apparmor_tfm;
John Johansenf8eb8a12013-08-14 11:27:36 -070026
27unsigned int aa_hash_size(void)
28{
29 return apparmor_hash_size;
30}
31
32int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
33 size_t len)
34{
Tyler Hicks71ac7f62013-09-29 08:39:21 -070035 struct {
36 struct shash_desc shash;
37 char ctx[crypto_shash_descsize(apparmor_tfm)];
38 } desc;
John Johansenf8eb8a12013-08-14 11:27:36 -070039 int error = -ENOMEM;
40 u32 le32_version = cpu_to_le32(version);
41
42 if (!apparmor_tfm)
43 return 0;
44
John Johansenf8eb8a12013-08-14 11:27:36 -070045 profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
46 if (!profile->hash)
47 goto fail;
48
Tyler Hicks71ac7f62013-09-29 08:39:21 -070049 desc.shash.tfm = apparmor_tfm;
50 desc.shash.flags = 0;
51
52 error = crypto_shash_init(&desc.shash);
John Johansenf8eb8a12013-08-14 11:27:36 -070053 if (error)
54 goto fail;
Tyler Hicks71ac7f62013-09-29 08:39:21 -070055 error = crypto_shash_update(&desc.shash, (u8 *) &le32_version, 4);
John Johansenf8eb8a12013-08-14 11:27:36 -070056 if (error)
57 goto fail;
Tyler Hicks71ac7f62013-09-29 08:39:21 -070058 error = crypto_shash_update(&desc.shash, (u8 *) start, len);
John Johansenf8eb8a12013-08-14 11:27:36 -070059 if (error)
60 goto fail;
Tyler Hicks71ac7f62013-09-29 08:39:21 -070061 error = crypto_shash_final(&desc.shash, profile->hash);
John Johansenf8eb8a12013-08-14 11:27:36 -070062 if (error)
63 goto fail;
64
65 return 0;
66
67fail:
68 kfree(profile->hash);
69 profile->hash = NULL;
70
71 return error;
72}
73
74static int __init init_profile_hash(void)
75{
Tyler Hicks71ac7f62013-09-29 08:39:21 -070076 struct crypto_shash *tfm;
John Johansenf8eb8a12013-08-14 11:27:36 -070077
78 if (!apparmor_initialized)
79 return 0;
80
Tyler Hicks71ac7f62013-09-29 08:39:21 -070081 tfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC);
John Johansenf8eb8a12013-08-14 11:27:36 -070082 if (IS_ERR(tfm)) {
83 int error = PTR_ERR(tfm);
84 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
85 return error;
86 }
87 apparmor_tfm = tfm;
Tyler Hicks71ac7f62013-09-29 08:39:21 -070088 apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
John Johansenf8eb8a12013-08-14 11:27:36 -070089
90 aa_info_message("AppArmor sha1 policy hashing enabled");
91
92 return 0;
93}
94
95late_initcall(init_profile_hash);