blob: af03d98c75520f4906e07fae3200d948d115289f [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
John Johansen5ac8c352017-01-16 00:42:55 -080032char *aa_calc_hash(void *data, size_t len)
33{
Nicolas Iooss98144482017-04-06 06:55:21 -070034 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
John Johansen5ac8c352017-01-16 00:42:55 -080035 char *hash = NULL;
36 int error = -ENOMEM;
37
38 if (!apparmor_tfm)
39 return NULL;
40
41 hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
42 if (!hash)
43 goto fail;
44
Nicolas Iooss98144482017-04-06 06:55:21 -070045 desc->tfm = apparmor_tfm;
46 desc->flags = 0;
John Johansen5ac8c352017-01-16 00:42:55 -080047
Nicolas Iooss98144482017-04-06 06:55:21 -070048 error = crypto_shash_init(desc);
John Johansen5ac8c352017-01-16 00:42:55 -080049 if (error)
50 goto fail;
Nicolas Iooss98144482017-04-06 06:55:21 -070051 error = crypto_shash_update(desc, (u8 *) data, len);
John Johansen5ac8c352017-01-16 00:42:55 -080052 if (error)
53 goto fail;
Nicolas Iooss98144482017-04-06 06:55:21 -070054 error = crypto_shash_final(desc, hash);
John Johansen5ac8c352017-01-16 00:42:55 -080055 if (error)
56 goto fail;
57
58 return hash;
59
60fail:
61 kfree(hash);
62
63 return ERR_PTR(error);
64}
65
John Johansenf8eb8a12013-08-14 11:27:36 -070066int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
67 size_t len)
68{
Nicolas Iooss98144482017-04-06 06:55:21 -070069 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
John Johansenf8eb8a12013-08-14 11:27:36 -070070 int error = -ENOMEM;
John Johansen5ac8c352017-01-16 00:42:55 -080071 __le32 le32_version = cpu_to_le32(version);
John Johansenf8eb8a12013-08-14 11:27:36 -070072
Arnd Bergmann7616ac72016-07-25 10:59:07 -070073 if (!aa_g_hash_policy)
74 return 0;
75
John Johansenf8eb8a12013-08-14 11:27:36 -070076 if (!apparmor_tfm)
77 return 0;
78
John Johansenf8eb8a12013-08-14 11:27:36 -070079 profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
80 if (!profile->hash)
81 goto fail;
82
Nicolas Iooss98144482017-04-06 06:55:21 -070083 desc->tfm = apparmor_tfm;
84 desc->flags = 0;
Tyler Hicks71ac7f62013-09-29 08:39:21 -070085
Nicolas Iooss98144482017-04-06 06:55:21 -070086 error = crypto_shash_init(desc);
John Johansenf8eb8a12013-08-14 11:27:36 -070087 if (error)
88 goto fail;
Nicolas Iooss98144482017-04-06 06:55:21 -070089 error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
John Johansenf8eb8a12013-08-14 11:27:36 -070090 if (error)
91 goto fail;
Nicolas Iooss98144482017-04-06 06:55:21 -070092 error = crypto_shash_update(desc, (u8 *) start, len);
John Johansenf8eb8a12013-08-14 11:27:36 -070093 if (error)
94 goto fail;
Nicolas Iooss98144482017-04-06 06:55:21 -070095 error = crypto_shash_final(desc, profile->hash);
John Johansenf8eb8a12013-08-14 11:27:36 -070096 if (error)
97 goto fail;
98
99 return 0;
100
101fail:
102 kfree(profile->hash);
103 profile->hash = NULL;
104
105 return error;
106}
107
108static int __init init_profile_hash(void)
109{
Tyler Hicks71ac7f62013-09-29 08:39:21 -0700110 struct crypto_shash *tfm;
John Johansenf8eb8a12013-08-14 11:27:36 -0700111
112 if (!apparmor_initialized)
113 return 0;
114
Eric Biggers3d234b32018-11-14 12:21:11 -0800115 tfm = crypto_alloc_shash("sha1", 0, 0);
John Johansenf8eb8a12013-08-14 11:27:36 -0700116 if (IS_ERR(tfm)) {
117 int error = PTR_ERR(tfm);
118 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
119 return error;
120 }
121 apparmor_tfm = tfm;
Tyler Hicks71ac7f62013-09-29 08:39:21 -0700122 apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
John Johansenf8eb8a12013-08-14 11:27:36 -0700123
124 aa_info_message("AppArmor sha1 policy hashing enabled");
125
126 return 0;
127}
128
129late_initcall(init_profile_hash);