hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/security/integrity/evm/evm_crypto.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2005-2010 IBM Corporation
34 *
....@@ -5,17 +6,11 @@
56 * Mimi Zohar <zohar@us.ibm.com>
67 * Kylene Hall <kjhall@us.ibm.com>
78 *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation, version 2 of the License.
11
- *
129 * File: evm_crypto.c
1310 * Using root's kernel master key (kmk), calculate the HMAC
1411 */
1512
16
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
-
18
-#include <linux/module.h>
13
+#include <linux/export.h>
1914 #include <linux/crypto.h>
2015 #include <linux/xattr.h>
2116 #include <linux/evm.h>
....@@ -27,7 +22,7 @@
2722 #define EVMKEY "evm-key"
2823 #define MAX_KEY_SIZE 128
2924 static unsigned char evmkey[MAX_KEY_SIZE];
30
-static int evmkey_len = MAX_KEY_SIZE;
25
+static const int evmkey_len = MAX_KEY_SIZE;
3126
3227 struct crypto_shash *hmac_tfm;
3328 static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
....@@ -38,7 +33,7 @@
3833
3934 static unsigned long evm_set_key_flags;
4035
41
-static char * const evm_hmac = "hmac(sha1)";
36
+static const char evm_hmac[] = "hmac(sha1)";
4237
4338 /**
4439 * evm_set_key() - set EVM HMAC key from the kernel
....@@ -78,7 +73,7 @@
7873 {
7974 long rc;
8075 const char *algo;
81
- struct crypto_shash **tfm;
76
+ struct crypto_shash **tfm, *tmp_tfm;
8277 struct shash_desc *desc;
8378
8479 if (type == EVM_XATTR_HMAC) {
....@@ -96,39 +91,37 @@
9691 algo = hash_algo_name[hash_algo];
9792 }
9893
99
- if (IS_ERR_OR_NULL(*tfm)) {
100
- mutex_lock(&mutex);
101
- if (*tfm)
102
- goto out;
103
- *tfm = crypto_alloc_shash(algo, 0,
104
- CRYPTO_ALG_ASYNC | CRYPTO_NOLOAD);
105
- if (IS_ERR(*tfm)) {
106
- rc = PTR_ERR(*tfm);
107
- pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
108
- *tfm = NULL;
94
+ if (*tfm)
95
+ goto alloc;
96
+ mutex_lock(&mutex);
97
+ if (*tfm)
98
+ goto unlock;
99
+
100
+ tmp_tfm = crypto_alloc_shash(algo, 0, CRYPTO_NOLOAD);
101
+ if (IS_ERR(tmp_tfm)) {
102
+ pr_err("Can not allocate %s (reason: %ld)\n", algo,
103
+ PTR_ERR(tmp_tfm));
104
+ mutex_unlock(&mutex);
105
+ return ERR_CAST(tmp_tfm);
106
+ }
107
+ if (type == EVM_XATTR_HMAC) {
108
+ rc = crypto_shash_setkey(tmp_tfm, evmkey, evmkey_len);
109
+ if (rc) {
110
+ crypto_free_shash(tmp_tfm);
109111 mutex_unlock(&mutex);
110112 return ERR_PTR(rc);
111113 }
112
- if (type == EVM_XATTR_HMAC) {
113
- rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
114
- if (rc) {
115
- crypto_free_shash(*tfm);
116
- *tfm = NULL;
117
- mutex_unlock(&mutex);
118
- return ERR_PTR(rc);
119
- }
120
- }
121
-out:
122
- mutex_unlock(&mutex);
123114 }
124
-
115
+ *tfm = tmp_tfm;
116
+unlock:
117
+ mutex_unlock(&mutex);
118
+alloc:
125119 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
126120 GFP_KERNEL);
127121 if (!desc)
128122 return ERR_PTR(-ENOMEM);
129123
130124 desc->tfm = *tfm;
131
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
132125
133126 rc = crypto_shash_init(desc);
134127 if (rc) {
....@@ -177,8 +170,7 @@
177170 crypto_shash_update(desc, (const u8 *)&hmac_misc, sizeof(hmac_misc));
178171 if ((evm_hmac_attrs & EVM_ATTR_FSUUID) &&
179172 type != EVM_XATTR_PORTABLE_DIGSIG)
180
- crypto_shash_update(desc, &inode->i_sb->s_uuid.b[0],
181
- sizeof(inode->i_sb->s_uuid));
173
+ crypto_shash_update(desc, (u8 *)&inode->i_sb->s_uuid, UUID_SIZE);
182174 crypto_shash_final(desc, digest);
183175 }
184176