2019-06-01 10:08:55 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-08-14 11:27:36 -07:00
|
|
|
/*
|
|
|
|
|
* AppArmor security module
|
|
|
|
|
*
|
|
|
|
|
* This file contains AppArmor policy loading interface function definitions.
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2013 Canonical Ltd.
|
|
|
|
|
*
|
|
|
|
|
* Fns to provide a checksum of policy that has been loaded this can be
|
|
|
|
|
* compared to userspace policy compiles to check loaded policy is what
|
|
|
|
|
* it should be.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-30 10:48:05 -07:00
|
|
|
#include <crypto/sha2.h>
|
2013-08-14 11:27:36 -07:00
|
|
|
|
|
|
|
|
#include "include/apparmor.h"
|
|
|
|
|
#include "include/crypto.h"
|
|
|
|
|
|
|
|
|
|
unsigned int aa_hash_size(void)
|
|
|
|
|
{
|
2025-06-30 10:48:05 -07:00
|
|
|
return SHA256_DIGEST_SIZE;
|
2013-08-14 11:27:36 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-16 00:42:55 -08:00
|
|
|
char *aa_calc_hash(void *data, size_t len)
|
|
|
|
|
{
|
2023-03-29 11:50:44 +02:00
|
|
|
char *hash;
|
2017-01-16 00:42:55 -08:00
|
|
|
|
2025-06-30 10:48:05 -07:00
|
|
|
hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
|
2017-01-16 00:42:55 -08:00
|
|
|
if (!hash)
|
2023-03-29 11:50:44 +02:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2017-01-16 00:42:55 -08:00
|
|
|
|
2025-06-30 10:48:05 -07:00
|
|
|
sha256(data, len, hash);
|
2017-01-16 00:42:55 -08:00
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 11:27:36 -07:00
|
|
|
int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
|
|
|
|
|
size_t len)
|
|
|
|
|
{
|
2025-06-30 10:48:05 -07:00
|
|
|
struct sha256_ctx sctx;
|
2017-01-16 00:42:55 -08:00
|
|
|
__le32 le32_version = cpu_to_le32(version);
|
2013-08-14 11:27:36 -07:00
|
|
|
|
2016-07-25 10:59:07 -07:00
|
|
|
if (!aa_g_hash_policy)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2025-06-30 10:48:05 -07:00
|
|
|
profile->hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
|
2013-08-14 11:27:36 -07:00
|
|
|
if (!profile->hash)
|
2023-03-29 11:50:44 +02:00
|
|
|
return -ENOMEM;
|
2013-08-14 11:27:36 -07:00
|
|
|
|
2025-06-30 10:48:05 -07:00
|
|
|
sha256_init(&sctx);
|
|
|
|
|
sha256_update(&sctx, (u8 *)&le32_version, 4);
|
|
|
|
|
sha256_update(&sctx, (u8 *)start, len);
|
|
|
|
|
sha256_final(&sctx, profile->hash);
|
2013-08-14 11:27:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:23:40 -05:00
|
|
|
int __init init_profile_hash(void)
|
2013-08-14 11:27:36 -07:00
|
|
|
{
|
2025-06-30 10:48:05 -07:00
|
|
|
if (apparmor_initialized)
|
|
|
|
|
aa_info_message("AppArmor sha256 policy hashing enabled");
|
2013-08-14 11:27:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|