aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-09-13 10:11:14 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-09-13 10:11:14 -0400
commit0898d2aa9db8b7770c83590d0326d0e6b62cef2b (patch)
tree2383c0909c94b389ec75412748aced1678cf5967
parent5a7d8a28080caed7fd4cb1b81d092adac4445e8e (diff)
parent26052f9b9bb8de4f6a57165b0a803de9c26138bd (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
Pull crypto fixes from Herbert Xu: "This fixes a 7+ year race condition in the crypto API that causes sporadic crashes when multiple threads load the same algorithm. It also fixes the crct10dif algorithm again to prevent boot failures on systems where the initramfs tool ignores module softdeps" * git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: crypto: crct10dif - Add fallback for broken initrds crypto: api - Fix race condition in larval lookup
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/api.c7
-rw-r--r--crypto/crct10dif_common.c (renamed from crypto/crct10dif.c)100
-rw-r--r--crypto/crct10dif_generic.c127
-rw-r--r--lib/crc-t10dif.c11
5 files changed, 146 insertions, 101 deletions
diff --git a/crypto/Makefile b/crypto/Makefile
index 2d5ed08a239f..80019ba8da3a 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -83,7 +83,7 @@ obj-$(CONFIG_CRYPTO_ZLIB) += zlib.o
83obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o 83obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
84obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o 84obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
85obj-$(CONFIG_CRYPTO_CRC32) += crc32.o 85obj-$(CONFIG_CRYPTO_CRC32) += crc32.o
86obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif.o 86obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o
87obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o 87obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
88obj-$(CONFIG_CRYPTO_LZO) += lzo.o 88obj-$(CONFIG_CRYPTO_LZO) += lzo.o
89obj-$(CONFIG_CRYPTO_LZ4) += lz4.o 89obj-$(CONFIG_CRYPTO_LZ4) += lz4.o
diff --git a/crypto/api.c b/crypto/api.c
index 320ea4d8a0f5..a2b39c5f3649 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -34,6 +34,8 @@ EXPORT_SYMBOL_GPL(crypto_alg_sem);
34BLOCKING_NOTIFIER_HEAD(crypto_chain); 34BLOCKING_NOTIFIER_HEAD(crypto_chain);
35EXPORT_SYMBOL_GPL(crypto_chain); 35EXPORT_SYMBOL_GPL(crypto_chain);
36 36
37static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
38
37struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) 39struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
38{ 40{
39 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; 41 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
@@ -144,8 +146,11 @@ static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
144 } 146 }
145 up_write(&crypto_alg_sem); 147 up_write(&crypto_alg_sem);
146 148
147 if (alg != &larval->alg) 149 if (alg != &larval->alg) {
148 kfree(larval); 150 kfree(larval);
151 if (crypto_is_larval(alg))
152 alg = crypto_larval_wait(alg);
153 }
149 154
150 return alg; 155 return alg;
151} 156}
diff --git a/crypto/crct10dif.c b/crypto/crct10dif_common.c
index 92aca96d6b98..b2fab366f518 100644
--- a/crypto/crct10dif.c
+++ b/crypto/crct10dif_common.c
@@ -24,18 +24,10 @@
24 * 24 *
25 */ 25 */
26 26
27#include <linux/types.h>
28#include <linux/module.h>
29#include <linux/crc-t10dif.h> 27#include <linux/crc-t10dif.h>
30#include <crypto/internal/hash.h> 28#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/string.h>
33#include <linux/kernel.h> 29#include <linux/kernel.h>
34 30
35struct chksum_desc_ctx {
36 __u16 crc;
37};
38
39/* Table generated using the following polynomium: 31/* Table generated using the following polynomium:
40 * x^16 + x^15 + x^11 + x^9 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 32 * x^16 + x^15 + x^11 + x^9 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
41 * gt: 0x8bb7 33 * gt: 0x8bb7
@@ -86,93 +78,5 @@ __u16 crc_t10dif_generic(__u16 crc, const unsigned char *buffer, size_t len)
86} 78}
87EXPORT_SYMBOL(crc_t10dif_generic); 79EXPORT_SYMBOL(crc_t10dif_generic);
88 80
89/* 81MODULE_DESCRIPTION("T10 DIF CRC calculation common code");
90 * Steps through buffer one byte at at time, calculates reflected
91 * crc using table.
92 */
93
94static int chksum_init(struct shash_desc *desc)
95{
96 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
97
98 ctx->crc = 0;
99
100 return 0;
101}
102
103static int chksum_update(struct shash_desc *desc, const u8 *data,
104 unsigned int length)
105{
106 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
107
108 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
109 return 0;
110}
111
112static int chksum_final(struct shash_desc *desc, u8 *out)
113{
114 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
115
116 *(__u16 *)out = ctx->crc;
117 return 0;
118}
119
120static int __chksum_finup(__u16 *crcp, const u8 *data, unsigned int len,
121 u8 *out)
122{
123 *(__u16 *)out = crc_t10dif_generic(*crcp, data, len);
124 return 0;
125}
126
127static int chksum_finup(struct shash_desc *desc, const u8 *data,
128 unsigned int len, u8 *out)
129{
130 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
131
132 return __chksum_finup(&ctx->crc, data, len, out);
133}
134
135static int chksum_digest(struct shash_desc *desc, const u8 *data,
136 unsigned int length, u8 *out)
137{
138 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
139
140 return __chksum_finup(&ctx->crc, data, length, out);
141}
142
143static struct shash_alg alg = {
144 .digestsize = CRC_T10DIF_DIGEST_SIZE,
145 .init = chksum_init,
146 .update = chksum_update,
147 .final = chksum_final,
148 .finup = chksum_finup,
149 .digest = chksum_digest,
150 .descsize = sizeof(struct chksum_desc_ctx),
151 .base = {
152 .cra_name = "crct10dif",
153 .cra_driver_name = "crct10dif-generic",
154 .cra_priority = 100,
155 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
156 .cra_module = THIS_MODULE,
157 }
158};
159
160static int __init crct10dif_mod_init(void)
161{
162 int ret;
163
164 ret = crypto_register_shash(&alg);
165 return ret;
166}
167
168static void __exit crct10dif_mod_fini(void)
169{
170 crypto_unregister_shash(&alg);
171}
172
173module_init(crct10dif_mod_init);
174module_exit(crct10dif_mod_fini);
175
176MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
177MODULE_DESCRIPTION("T10 DIF CRC calculation.");
178MODULE_LICENSE("GPL"); 82MODULE_LICENSE("GPL");
diff --git a/crypto/crct10dif_generic.c b/crypto/crct10dif_generic.c
new file mode 100644
index 000000000000..877e7114ec5c
--- /dev/null
+++ b/crypto/crct10dif_generic.c
@@ -0,0 +1,127 @@
1/*
2 * Cryptographic API.
3 *
4 * T10 Data Integrity Field CRC16 Crypto Transform
5 *
6 * Copyright (c) 2007 Oracle Corporation. All rights reserved.
7 * Written by Martin K. Petersen <martin.petersen@oracle.com>
8 * Copyright (C) 2013 Intel Corporation
9 * Author: Tim Chen <tim.c.chen@linux.intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/crc-t10dif.h>
29#include <crypto/internal/hash.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32
33struct chksum_desc_ctx {
34 __u16 crc;
35};
36
37/*
38 * Steps through buffer one byte at at time, calculates reflected
39 * crc using table.
40 */
41
42static int chksum_init(struct shash_desc *desc)
43{
44 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
45
46 ctx->crc = 0;
47
48 return 0;
49}
50
51static int chksum_update(struct shash_desc *desc, const u8 *data,
52 unsigned int length)
53{
54 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
55
56 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
57 return 0;
58}
59
60static int chksum_final(struct shash_desc *desc, u8 *out)
61{
62 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
63
64 *(__u16 *)out = ctx->crc;
65 return 0;
66}
67
68static int __chksum_finup(__u16 *crcp, const u8 *data, unsigned int len,
69 u8 *out)
70{
71 *(__u16 *)out = crc_t10dif_generic(*crcp, data, len);
72 return 0;
73}
74
75static int chksum_finup(struct shash_desc *desc, const u8 *data,
76 unsigned int len, u8 *out)
77{
78 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
79
80 return __chksum_finup(&ctx->crc, data, len, out);
81}
82
83static int chksum_digest(struct shash_desc *desc, const u8 *data,
84 unsigned int length, u8 *out)
85{
86 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
87
88 return __chksum_finup(&ctx->crc, data, length, out);
89}
90
91static struct shash_alg alg = {
92 .digestsize = CRC_T10DIF_DIGEST_SIZE,
93 .init = chksum_init,
94 .update = chksum_update,
95 .final = chksum_final,
96 .finup = chksum_finup,
97 .digest = chksum_digest,
98 .descsize = sizeof(struct chksum_desc_ctx),
99 .base = {
100 .cra_name = "crct10dif",
101 .cra_driver_name = "crct10dif-generic",
102 .cra_priority = 100,
103 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
105 }
106};
107
108static int __init crct10dif_mod_init(void)
109{
110 int ret;
111
112 ret = crypto_register_shash(&alg);
113 return ret;
114}
115
116static void __exit crct10dif_mod_fini(void)
117{
118 crypto_unregister_shash(&alg);
119}
120
121module_init(crct10dif_mod_init);
122module_exit(crct10dif_mod_fini);
123
124MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
125MODULE_DESCRIPTION("T10 DIF CRC calculation.");
126MODULE_LICENSE("GPL");
127MODULE_ALIAS("crct10dif");
diff --git a/lib/crc-t10dif.c b/lib/crc-t10dif.c
index 43bc5b071f96..dfe6ec17c0a5 100644
--- a/lib/crc-t10dif.c
+++ b/lib/crc-t10dif.c
@@ -14,8 +14,10 @@
14#include <linux/err.h> 14#include <linux/err.h>
15#include <linux/init.h> 15#include <linux/init.h>
16#include <crypto/hash.h> 16#include <crypto/hash.h>
17#include <linux/static_key.h>
17 18
18static struct crypto_shash *crct10dif_tfm; 19static struct crypto_shash *crct10dif_tfm;
20static struct static_key crct10dif_fallback __read_mostly;
19 21
20__u16 crc_t10dif(const unsigned char *buffer, size_t len) 22__u16 crc_t10dif(const unsigned char *buffer, size_t len)
21{ 23{
@@ -25,6 +27,9 @@ __u16 crc_t10dif(const unsigned char *buffer, size_t len)
25 } desc; 27 } desc;
26 int err; 28 int err;
27 29
30 if (static_key_false(&crct10dif_fallback))
31 return crc_t10dif_generic(0, buffer, len);
32
28 desc.shash.tfm = crct10dif_tfm; 33 desc.shash.tfm = crct10dif_tfm;
29 desc.shash.flags = 0; 34 desc.shash.flags = 0;
30 *(__u16 *)desc.ctx = 0; 35 *(__u16 *)desc.ctx = 0;
@@ -39,7 +44,11 @@ EXPORT_SYMBOL(crc_t10dif);
39static int __init crc_t10dif_mod_init(void) 44static int __init crc_t10dif_mod_init(void)
40{ 45{
41 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); 46 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
42 return PTR_RET(crct10dif_tfm); 47 if (IS_ERR(crct10dif_tfm)) {
48 static_key_slow_inc(&crct10dif_fallback);
49 crct10dif_tfm = NULL;
50 }
51 return 0;
43} 52}
44 53
45static void __exit crc_t10dif_mod_fini(void) 54static void __exit crc_t10dif_mod_fini(void)