Skip to content

Commit 1417c82

Browse files
DemiMarieopsiff
authored andcommitted
crypto: af_alg - Drop support for off-CPU cryptography
AF_ALG is deprecated and exposed to unprivileged userspace. Only use the least buggy algorithm implementations: the pure software ones. This removes one of the main advantages of AF_ALG, which is the ability to use it with off-CPU accelerators. However, using off-CPU accelerators has huge overheads, both in performance and attack surface. I have yet to see real-world, performance-critical workloads where using an accelerator via AF_ALG is actually a win over doing cryptography in userspace. If using an off-CPU accelerator really does turn out to be a win, a new API should be developed that is actually a good fit for it. Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> [WangYuli: Rewirte userspace-if.rst because of conflicts] Link: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git/commit/?id=7524070f26d8d347c26787dc297fb844baa26abf Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
1 parent 83c8511 commit 1417c82

7 files changed

Lines changed: 31 additions & 10 deletions

File tree

Documentation/crypto/userspace-if.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ performed by the consumer:
8080
system calls to send data to the kernel or obtain data from the
8181
kernel, the file descriptor returned by accept must be used.
8282

83+
.. caution::
84+
85+
Support for hardware cryptographic accelerators has been removed from
86+
AF_ALG. Only algorithms implemented in software are now accessible
87+
through this interface. Hardware accelerator drivers are frequently
88+
buggy, and removing their exposure via AF_ALG reduces the kernel's
89+
attack surface. This means AF_ALG no longer fulfills its original
90+
purpose of providing access to off-CPU cryptography.
91+
8392
In-place Cipher operation
8493
-------------------------
8594

crypto/af_alg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
181181
if (IS_ERR(type))
182182
return PTR_ERR(type);
183183

184-
private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
184+
private = type->bind(sa->salg_name);
185185
if (IS_ERR(private)) {
186186
module_put(type->owner);
187187
return PTR_ERR(private);

crypto/algif_aead.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ static struct proto_ops algif_aead_ops_nokey = {
365365
.poll = af_alg_poll,
366366
};
367367

368-
static void *aead_bind(const char *name, u32 type, u32 mask)
368+
static void *aead_bind(const char *name)
369369
{
370-
return crypto_alloc_aead(name, type, mask);
370+
return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
371371
}
372372

373373
static void aead_release(void *private)

crypto/algif_hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ static struct proto_ops algif_hash_ops_nokey = {
380380
.accept = hash_accept_nokey,
381381
};
382382

383-
static void *hash_bind(const char *name, u32 type, u32 mask)
383+
static void *hash_bind(const char *name)
384384
{
385-
return crypto_alloc_ahash(name, type, mask);
385+
return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK);
386386
}
387387

388388
static void hash_release(void *private)

crypto/algif_rng.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static struct proto_ops __maybe_unused algif_rng_test_ops = {
197197
.sendmsg = rng_test_sendmsg,
198198
};
199199

200-
static void *rng_bind(const char *name, u32 type, u32 mask)
200+
static void *rng_bind(const char *name)
201201
{
202202
struct rng_parent_ctx *pctx;
203203
struct crypto_rng *rng;
@@ -206,7 +206,7 @@ static void *rng_bind(const char *name, u32 type, u32 mask)
206206
if (!pctx)
207207
return ERR_PTR(-ENOMEM);
208208

209-
rng = crypto_alloc_rng(name, type, mask);
209+
rng = crypto_alloc_rng(name, 0, AF_ALG_CRYPTOAPI_MASK);
210210
if (IS_ERR(rng)) {
211211
kfree(pctx);
212212
return ERR_CAST(rng);

crypto/algif_skcipher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ static struct proto_ops algif_skcipher_ops_nokey = {
345345
.poll = af_alg_poll,
346346
};
347347

348-
static void *skcipher_bind(const char *name, u32 type, u32 mask)
348+
static void *skcipher_bind(const char *name)
349349
{
350-
return crypto_alloc_skcipher(name, type, mask);
350+
return crypto_alloc_skcipher(name, 0, AF_ALG_CRYPTOAPI_MASK);
351351
}
352352

353353
static void skcipher_release(void *private)

include/crypto/if_alg.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct af_alg_control {
4141
};
4242

4343
struct af_alg_type {
44-
void *(*bind)(const char *name, u32 type, u32 mask);
44+
void *(*bind)(const char *name);
4545
void (*release)(void *private);
4646
int (*setkey)(void *private, const u8 *key, unsigned int keylen);
4747
int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
@@ -246,4 +246,16 @@ int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
246246
struct af_alg_async_req *areq, size_t maxsize,
247247
size_t *outlen);
248248

249+
/*
250+
* Mask used to disable unsupported algorithm implementations.
251+
*
252+
* This is the same as FSCRYPT_CRYPTOAPI_MASK in fs/crypto/fscrypt_private.h.
253+
* In additions to the motivations there, this API is exposed to userspace
254+
* that might not be fully trusted.
255+
*/
256+
#define AF_ALG_CRYPTOAPI_MASK \
257+
(CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | \
258+
CRYPTO_ALG_KERN_DRIVER_ONLY)
259+
260+
249261
#endif /* _CRYPTO_IF_ALG_H */

0 commit comments

Comments
 (0)