Skip to contentSkip to conversation

crypto: af_alg - Add af_alg_restrict sysctl, defaulting to 1

PATCH In review Eric Biggers 10 participants 26 messages Jun 22 – Jul 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: af_alg - Add af_alg_restrict sysctl, defaulting to 1

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

---
 Documentation/admin-guide/sysctl/crypto.rst | 36 +++++++++++
 Documentation/crypto/userspace-if.rst       | 13 +++-
 crypto/af_alg.c                             | 72 +++++++++++++++++++--
 crypto/algif_aead.c                         | 11 ++++
 crypto/algif_hash.c                         | 24 +++++++
 crypto/algif_rng.c                          |  9 +++
 crypto/algif_skcipher.c                     | 20 ++++++
 include/crypto/if_alg.h                     |  8 +++
 8 files changed, 184 insertions(+), 9 deletions(-)
Documentation/admin-guide/sysctl/crypto.rst +36 -0
index b707bd314a64..9a1bd53287f4 100644
@@ -5,10 +5,46 @@
 These files show up in ``/proc/sys/crypto/``, depending on the
 kernel configuration:
 
 .. contents:: :local:
 
+.. _af_alg_restrict:
+
+af_alg_restrict
+===============
+
+Controls the level of restriction of AF_ALG.
+
+AF_ALG is a deprecated and rarely-used userspace interface that is a
+frequent source of vulnerabilities. It also unnecessarily exposes a
+large number of kernel implementation details. For more information
+about AF_ALG, see :ref:`Documentation/crypto/userspace-if.rst
+<crypto_userspace_interface>`.
+
+Starting in Linux v7.3, AF_ALG supports only a limited set of
+algorithms by default. This sysctl allows the system administrator to
+remove this restriction when needed for compatibility reasons, or to
+go further and disable AF_ALG entirely. The default value is 1.
+
+===  ==================================================================
+0    AF_ALG is unrestricted.
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
+
+     Specifically, there is an allowlist for unprivileged processes
+     and a somewhat longer allowlist for processes that hold
+     CAP_SYS_ADMIN or CAP_NET_ADMIN in the initial user namespace.
+
+     Attempts to bind() an AF_ALG socket with a disallowed algorithm
+     fail with ENOENT.
+
+2    AF_ALG is completely disabled. Attempts to create an AF_ALG
+     socket fail with EAFNOSUPPORT.
+===  ==================================================================
+
 fips_enabled
 ============
 
 Read-only flag that indicates whether FIPS mode is enabled.
 
Documentation/crypto/userspace-if.rst +10 -3
index ab93300c8e04..d6194346e366 100644
@@ -1,5 +1,7 @@
+.. _crypto_userspace_interface:
+
 User Space Interface
 ====================
 
 Introduction
 ------------
@@ -10,13 +12,18 @@ code.
 
 AF_ALG is insecure and is deprecated. Originally added to the kernel in 2010,
 most kernel developers now consider it to be a mistake. Support for hardware
 accelerators, which was the original purpose of AF_ALG, has been removed.
 
-AF_ALG continues to be supported only for backwards compatibility. On systems
-where no programs using AF_ALG remain, the support for it should be disabled by
-disabling ``CONFIG_CRYPTO_USER_API_*``.
+AF_ALG continues to be supported only for backwards compatibility.
+
+Starting in Linux v7.3, the set of algorithms supported by AF_ALG is limited by
+default. See :ref:`/proc/sys/crypto/af_alg_restrict <af_alg_restrict>`.
+
+On systems where no programs using AF_ALG remain, the support for it should be
+disabled entirely by setting ``/proc/sys/crypto/af_alg_restrict`` to 2 or by
+disabling ``CONFIG_CRYPTO_USER_API_*`` in the kernel configuration.
 
 Deprecation
 -----------
 
 AF_ALG was originally intended to provide userspace programs access to crypto
crypto/af_alg.c +66 -6

141 lines not on this page · raw message

crypto/algif_aead.c +11 -0
index 787aac8aeb24..b9217f9086aa 100644
@@ -32,10 +32,15 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry aead_allowlist[] = {
+	{ "ccm(aes)", true }, /* bluez */
+	{},
+};
+
 static inline bool aead_sufficient_data(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
@@ -342,10 +347,16 @@ static struct proto_ops algif_aead_ops_nokey = {
 	.poll		=	af_alg_poll,
 };
 
 static void *aead_bind(const char *name)
 {
+	int err;
+
+	err = af_alg_check_restriction(name, aead_allowlist);
+	if (err)
+		return ERR_PTR(err);
+
 	return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void aead_release(void *private)
 {
crypto/algif_hash.c +24 -0
index 5452ad6c1506..a8d958d51ece 100644
@@ -14,10 +14,28 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+	{ "cmac(aes)", true }, /* iwd, bluez */
+	{ "hmac(md5)", true }, /* iwd */
+	{ "hmac(sha1)", true }, /* iwd */
+	{ "hmac(sha224)", true }, /* iwd */
+	{ "hmac(sha256)", true }, /* iwd */
+	{ "hmac(sha384)", true }, /* iwd */
+	{ "hmac(sha512)", true }, /* iwd, sha512hmac */
+	{ "md4", true }, /* iwd */
+	{ "md5", true }, /* iwd */
+	{ "sha1", false }, /* iwd, iproute2 < 7.0 */
+	{ "sha224", true }, /* iwd */
+	{ "sha256", true }, /* iwd */
+	{ "sha384", true }, /* iwd */
+	{ "sha512", true }, /* iwd */
+	{},
+};
+
 struct hash_ctx {
 	struct af_alg_sgl sgl;
 
 	u8 *result;
 
@@ -380,10 +398,16 @@ static struct proto_ops algif_hash_ops_nokey = {
 	.accept		=	hash_accept_nokey,
 };
 
 static void *hash_bind(const char *name)
 {
+	int err;
+
+	err = af_alg_check_restriction(name, hash_allowlist);
+	if (err)
+		return ERR_PTR(err);
+
 	return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void hash_release(void *private)
 {
crypto/algif_rng.c +9 -0
index 4dfe7899f8fa..bd522915d56d 100644
@@ -48,10 +48,14 @@
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>");
 MODULE_DESCRIPTION("User-space interface for random number generators");
 
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+	{},
+};
+
 struct rng_ctx {
 #define MAXSIZE 128
 	unsigned int len;
 	struct crypto_rng *drng;
 	u8 *addtl;
@@ -199,10 +203,15 @@ static struct proto_ops __maybe_unused algif_rng_test_ops = {
 
 static void *rng_bind(const char *name)
 {
 	struct rng_parent_ctx *pctx;
 	struct crypto_rng *rng;
+	int err;
+
+	err = af_alg_check_restriction(name, rng_allowlist);
+	if (err)
+		return ERR_PTR(err);
 
 	pctx = kzalloc_obj(*pctx);
 	if (!pctx)
 		return ERR_PTR(-ENOMEM);
 
crypto/algif_skcipher.c +20 -0
index df20bdfe1f1f..2b8069667974 100644
@@ -32,10 +32,24 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+	{ "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+	{ "adiantum(xchacha20,aes)", false }, /* cryptsetup */
+	{ "cbc(aes)", true }, /* iwd */
+	{ "cbc(des)", true }, /* iwd */
+	{ "cbc(des3_ede)", true }, /* iwd */
+	{ "ctr(aes)", true }, /* iwd */
+	{ "ecb(aes)", true }, /* iwd, bluez */
+	{ "ecb(des)", true }, /* iwd */
+	{ "hctr2(aes)", false }, /* cryptsetup */
+	{ "xts(aes)", false }, /* cryptsetup benchmark */
+	{},
+};
+
 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 			    size_t size)
 {
 	struct sock *sk = sock->sk;
 	struct alg_sock *ask = alg_sk(sk);
@@ -307,10 +321,16 @@ static struct proto_ops algif_skcipher_ops_nokey = {
 	.poll		=	af_alg_poll,
 };
 
 static void *skcipher_bind(const char *name)
 {
+	int err;
+
+	err = af_alg_check_restriction(name, skcipher_allowlist);
+	if (err)
+		return ERR_PTR(err);
+
 	return crypto_alloc_skcipher(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void skcipher_release(void *private)
 {
include/crypto/if_alg.h +8 -0
index 7643ba954125..4e9ed8e73403 100644
@@ -159,13 +159,21 @@ struct af_alg_ctx {
 	unsigned int len;
 
 	unsigned int inflight;
 };
 
+struct af_alg_allowlist_entry {
+	const char *name;
+	bool privileged;
+};
+
 int af_alg_register_type(const struct af_alg_type *type);
 int af_alg_unregister_type(const struct af_alg_type *type);
 
+int af_alg_check_restriction(const char *name,
+			     const struct af_alg_allowlist_entry allowlist[]);
+
 int af_alg_release(struct socket *sock);
 void af_alg_release_parent(struct sock *sk);
 int af_alg_accept(struct sock *sk, struct socket *newsock,
 		  struct proto_accept_arg *arg);
 

base-commit: 1dc18801be29bc54709aa355b8acd80e183b03cd

Signature · 1 line
2.54.0
#1 lore raw

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: Documentation/crypto/userspace-if.rst:10
error: Documentation/crypto/userspace-if.rst: patch does not apply
error: patch failed: crypto/algif_aead.c:342
error: crypto/algif_aead.c: patch does not apply
error: patch failed: crypto/algif_hash.c:380
error: crypto/algif_hash.c: patch does not apply
error: patch failed: crypto/algif_rng.c:199
error: crypto/algif_rng.c: patch does not apply
error: patch failed: crypto/algif_skcipher.c:307
error: crypto/algif_skcipher.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Eric Biggers

Hello Eric,

Quoted 6 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare.  It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent
exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

You should also CC: [email protected] for AF_ALG related changes, as ell uses AF_ALG extensively for crypto and checksumming.

Cheers

Quoted 503 lines

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist.  And when it is used, only some functionality is known
to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list (
https://lore.kernel.org/linux-crypto/[email protected]
/).

Thus, let's restrict the set of allowed algorithms by default, depending
on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

    0: unrestricted
    1: limited functionality
    2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future.  However, the common use cases such as iwd and bluez are taken into account already.  I've tested that iwd still works with the default value of 1.

---
 Documentation/admin-guide/sysctl/crypto.rst | 36 +++++++++++ Documentation/crypto/userspace-if.rst       | 13 +++- crypto/af_alg.c                             | 72
+++++++++++++++++++--
 crypto/algif_aead.c                         | 11 ++++ crypto/algif_hash.c                         | 24 +++++++ crypto/algif_rng.c                          |  9 +++ crypto/algif_skcipher.c                     | 20 ++++++ include/crypto/if_alg.h                     |  8 +++

 8 files changed, 184 insertions(+), 9 deletions(-)
Documentation/admin-guide/sysctl/crypto.rst +0 -0
index b707bd314a64..9a1bd53287f4 100644
@@ -5,10 +5,46 @@

 These files show up in ``/proc/sys/crypto/``, depending on the kernel configuration:

 .. contents:: :local:

+.. _af_alg_restrict:

Documentation/admin-guide/sysctl/crypto.rst +14 -0
+
+af_alg_restrict
+===============
+
+Controls the level of restriction of AF_ALG.
+
+AF_ALG is a deprecated and rarely-used userspace interface that is a
+frequent source of vulnerabilities. It also unnecessarily exposes a
+large number of kernel implementation details. For more information
+about AF_ALG, see :ref:`Documentation/crypto/userspace-if.rst
+<crypto_userspace_interface>`.
+
+Starting in Linux v7.3, AF_ALG supports only a limited set of
+algorithms by default. This sysctl allows the system administrator

to

Documentation/admin-guide/sysctl/crypto.rst +4 -0
+remove this restriction when needed for compatibility reasons, or to
+go further and disable AF_ALG entirely. The default value is 1.
+
+=== 

==================================================================
+0    AF_ALG is unrestricted.

Documentation/admin-guide/sysctl/crypto.rst +15 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
+
+     Specifically, there is an allowlist for unprivileged processes
+     and a somewhat longer allowlist for processes that hold
+     CAP_SYS_ADMIN or CAP_NET_ADMIN in the initial user namespace.
+
+     Attempts to bind() an AF_ALG socket with a disallowed algorithm
+     fail with ENOENT.
+
+2    AF_ALG is completely disabled. Attempts to create an AF_ALG
+     socket fail with EAFNOSUPPORT.
+=== 

==================================================================
+
 fips_enabled
 ============

 Read-only flag that indicates whether FIPS mode is enabled.

Documentation/crypto/userspace-if.rst +2 -0
index ab93300c8e04..d6194346e366 100644
@@ -1,5 +1,7 @@
+.. _crypto_userspace_interface:
+

 User Space Interface
 ====================

 Introduction
 ------------

Documentation/crypto/userspace-if.rst +0 -0
@@ -10,13 +12,18 @@ code.


 AF_ALG is insecure and is deprecated. Originally added to the kernel in 2010,
 most kernel developers now consider it to be a mistake. Support for hardware
 accelerators, which was the original purpose of AF_ALG, has been removed.

-AF_ALG continues to be supported only for backwards compatibility. On systems
-where no programs using AF_ALG remain, the support for it should be disabled by

Documentation/crypto/userspace-if.rst +3 -1
-disabling ``CONFIG_CRYPTO_USER_API_*``.
+AF_ALG continues to be supported only for backwards compatibility.
+
+Starting in Linux v7.3, the set of algorithms supported by AF_ALG is

limited by
+default. See :ref:/proc/sys/crypto/af_alg_restrict <af_alg_restrict>.

Documentation/crypto/userspace-if.rst +2 -0
+
+On systems where no programs using AF_ALG remain, the support for it

should be
+disabled entirely by setting ``/proc/sys/crypto/af_alg_restrict`` to 2 or by
+disabling ``CONFIG_CRYPTO_USER_API_*`` in the kernel configuration.
 Deprecation
 -----------

 AF_ALG was originally intended to provide userspace programs access to crypto

crypto/af_alg.c +0 -0
index cce000e8590e..34b801568fba 100644
@@ -6,10 +6,11 @@
   *
   * Copyright (c) 2010 Herbert Xu <[email protected]>
   */


 #include <linux/atomic.h>
+#include <linux/capability.h>
 #include <crypto/if_alg.h>
 #include <linux/crypto.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/key.h>

crypto/af_alg.c +2 -0
@@ -20,14 +21,32 @@
  #include <linux/rwsem.h>
  #include <linux/sched.h>
  #include <linux/sched/signal.h>
  #include <linux/security.h>
  #include <linux/string.h>
+#include <linux/sysctl.h>
+#include <linux/user_namespace.h>
  #include <keys/user-type.h>
  #include <keys/trusted-type.h>
  #include <keys/encrypted-type.h>

crypto/af_alg.c +16 -0
+static int af_alg_restrict = 1;
+
+static const struct ctl_table af_alg_table[] = {
+	{
+		.procname       = "af_alg_restrict",
+		.data           = &af_alg_restrict,
+		.maxlen         = sizeof(int),
+		.mode           = 0644,
+		.proc_handler   = proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_TWO,
+	},
+};
+
+static struct ctl_table_header *af_alg_header;
+

 struct alg_type_list {
  const struct af_alg_type *type;
  struct list_head list;
 };

crypto/af_alg.c +0 -0
@@ -108,10 +127,43 @@ int af_alg_unregister_type(const struct
 af_alg_type *type)


  return err;
 }
 EXPORT_SYMBOL_GPL(af_alg_unregister_type);

crypto/af_alg.c +8 -0
+static bool af_alg_capable(void)
+{
+	return ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN) ||
+	       capable(CAP_SYS_ADMIN);
+}
+
+int af_alg_check_restriction(const char *name,
+			     const struct af_alg_allowlist_entry

allowlist[])

crypto/af_alg.c +7 -0
+{
+	int level = READ_ONCE(af_alg_restrict);
+
+	if (level == 0)
+		return 0;
+	if (level == 1) {
+		for (const struct af_alg_allowlist_entry *ent =

allowlist;

crypto/af_alg.c +8 -0
+		     ent->name; ent++) {
+			if (strcmp(name, ent->name) == 0 &&
+			    (!ent->privileged || af_alg_capable()))
+				return 0;
+		}
+	}
+	/*
+	 * Use -ENOENT (the error code for "algorithm not found")

instead of

crypto/af_alg.c +1 -0
+	 * -EACCES or -EPERM, for the highest chance of correctly

triggering

crypto/af_alg.c +3 -0
+	 * fallback code paths in userspace programs.
+	 *
+	 * Don't log a warning, since it would be noisy.  iwd tries

to bind a

crypto/af_alg.c +6 -0
+	 * bunch of algorithms that it never uses.
+	 */
+	return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(af_alg_check_restriction);
+

 static void alg_do_release(const struct af_alg_type *type, void *private)
 {
  if (!type)
  return;

crypto/af_alg.c +0 -0
@@ -504,10 +556,13 @@ static int alg_create(struct net *net, struct
 socket *sock, int protocol,
  		      int kern)
  {
  	struct sock *sk;
  	int err;

crypto/af_alg.c +3 -0
+	if (READ_ONCE(af_alg_restrict) == 2)
+		return -EAFNOSUPPORT;
+

  if (sock->type != SOCK_SEQPACKET)
  return -ESOCKTNOSUPPORT;
  if (protocol != 0)
  return -EPROTONOSUPPORT;

crypto/af_alg.c +0 -0
@@ -1220,31 +1275,36 @@ int af_alg_get_rsgl(struct sock *sk, struct
 msghdr *msg, int flags,
  }
  EXPORT_SYMBOL_GPL(af_alg_get_rsgl);


 static int __init af_alg_init(void)
 {

crypto/af_alg.c +3 -1
-	int err = proto_register(&alg_proto, 0);
+	int err;
+
+	af_alg_header = register_sysctl("crypto", af_alg_table);

crypto/af_alg.c +1 -0
+	err = proto_register(&alg_proto, 0);

  if (err)

crypto/af_alg.c +1 -1
-		goto out;
+		goto out_unregister_sysctl;


  err = sock_register(&alg_family);

crypto/af_alg.c +1 -1
-	if (err != 0)
+	if (err)

  goto out_unregister_proto;

crypto/af_alg.c +1 -2
-out:
-	return err;
+	return 0;


 out_unregister_proto:
  proto_unregister(&alg_proto);

crypto/af_alg.c +3 -1
-	goto out;
+out_unregister_sysctl:
+	unregister_sysctl_table(af_alg_header);
+	return err;

 }

 static void __exit af_alg_exit(void)
 {
  sock_unregister(PF_ALG);
  proto_unregister(&alg_proto);

crypto/af_alg.c +1 -0
+	unregister_sysctl_table(af_alg_header);

 }

 module_init(af_alg_init); module_exit(af_alg_exit); MODULE_DESCRIPTION("Crypto userspace interface");

crypto/algif_aead.c +0 -0
index 787aac8aeb24..b9217f9086aa 100644
@@ -32,10 +32,15 @@
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <linux/net.h>
  #include <net/sock.h>

crypto/algif_aead.c +5 -0
+static const struct af_alg_allowlist_entry aead_allowlist[] = {
+	{ "ccm(aes)", true }, /* bluez */
+	{},
+};
+

 static inline bool aead_sufficient_data(struct sock *sk) {
  struct alg_sock *ask = alg_sk(sk); struct sock *psk = ask->parent;
  struct alg_sock *pask = alg_sk(psk);

crypto/algif_aead.c +0 -0
@@ -342,10 +347,16 @@ static struct proto_ops algif_aead_ops_nokey =
 {
  	.poll		=	af_alg_poll,
  };


 static void *aead_bind(const char *name)
 {

crypto/algif_aead.c +6 -0
+	int err;
+
+	err = af_alg_check_restriction(name, aead_allowlist);
+	if (err)
+		return ERR_PTR(err);
+

  return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK); }

 static void aead_release(void *private)
 {

crypto/algif_hash.c +0 -0
index 5452ad6c1506..a8d958d51ece 100644
@@ -14,10 +14,28 @@
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <linux/net.h>
  #include <net/sock.h>

crypto/algif_hash.c +18 -0
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+	{ "cmac(aes)", true }, /* iwd, bluez */
+	{ "hmac(md5)", true }, /* iwd */
+	{ "hmac(sha1)", true }, /* iwd */
+	{ "hmac(sha224)", true }, /* iwd */
+	{ "hmac(sha256)", true }, /* iwd */
+	{ "hmac(sha384)", true }, /* iwd */
+	{ "hmac(sha512)", true }, /* iwd, sha512hmac */
+	{ "md4", true }, /* iwd */
+	{ "md5", true }, /* iwd */
+	{ "sha1", false }, /* iwd, iproute2 < 7.0 */
+	{ "sha224", true }, /* iwd */
+	{ "sha256", true }, /* iwd */
+	{ "sha384", true }, /* iwd */
+	{ "sha512", true }, /* iwd */
+	{},
+};
+

 struct hash_ctx {
  struct af_alg_sgl sgl;

  u8 *result;

crypto/algif_hash.c +0 -0
@@ -380,10 +398,16 @@ static struct proto_ops algif_hash_ops_nokey =
 {
  	.accept		=	hash_accept_nokey,
  };


 static void *hash_bind(const char *name)
 {

crypto/algif_hash.c +6 -0
+	int err;
+
+	err = af_alg_check_restriction(name, hash_allowlist);
+	if (err)
+		return ERR_PTR(err);
+

  return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK); }

 static void hash_release(void *private)
 {

crypto/algif_rng.c +0 -0
index 4dfe7899f8fa..bd522915d56d 100644
@@ -48,10 +48,14 @@


 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>"); MODULE_DESCRIPTION("User-space interface for random number generators");

crypto/algif_rng.c +4 -0
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+	{},
+};
+

 struct rng_ctx {
 #define MAXSIZE 128
  unsigned int len;
  struct crypto_rng *drng;
  u8 *addtl;

crypto/algif_rng.c +0 -0
@@ -199,10 +203,15 @@ static struct proto_ops __maybe_unused
 algif_rng_test_ops = {


 static void *rng_bind(const char *name)
 {
  struct rng_parent_ctx *pctx;
  struct crypto_rng *rng;

crypto/algif_rng.c +5 -0
+	int err;
+
+	err = af_alg_check_restriction(name, rng_allowlist);
+	if (err)
+		return ERR_PTR(err);


  pctx = kzalloc_obj(*pctx);
  if (!pctx)
  return ERR_PTR(-ENOMEM);

crypto/algif_skcipher.c +0 -0
index df20bdfe1f1f..2b8069667974 100644
@@ -32,10 +32,24 @@
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <linux/net.h>
  #include <net/sock.h>

crypto/algif_skcipher.c +14 -0
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+	{ "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+	{ "adiantum(xchacha20,aes)", false }, /* cryptsetup */
+	{ "cbc(aes)", true }, /* iwd */
+	{ "cbc(des)", true }, /* iwd */
+	{ "cbc(des3_ede)", true }, /* iwd */
+	{ "ctr(aes)", true }, /* iwd */
+	{ "ecb(aes)", true }, /* iwd, bluez */
+	{ "ecb(des)", true }, /* iwd */
+	{ "hctr2(aes)", false }, /* cryptsetup */
+	{ "xts(aes)", false }, /* cryptsetup benchmark */
+	{},
+};
+

 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 {
  struct sock *sk = sock->sk;
  struct alg_sock *ask = alg_sk(sk);

crypto/algif_skcipher.c +0 -0
@@ -307,10 +321,16 @@ static struct proto_ops
 algif_skcipher_ops_nokey = {
  	.poll		=	af_alg_poll,
  };


 static void *skcipher_bind(const char *name)
 {

crypto/algif_skcipher.c +6 -0
+	int err;
+
+	err = af_alg_check_restriction(name, skcipher_allowlist);
+	if (err)
+		return ERR_PTR(err);
+

  return crypto_alloc_skcipher(name, 0,
AF_ALG_CRYPTOAPI_MASK);
 }

 static void skcipher_release(void *private)
 {

include/crypto/if_alg.h +0 -0
index 7643ba954125..4e9ed8e73403 100644
@@ -159,13 +159,21 @@ struct af_alg_ctx {
  	unsigned int len;


  unsigned int inflight;
 };

include/crypto/if_alg.h +5 -0
+struct af_alg_allowlist_entry {
+	const char *name;
+	bool privileged;
+};
+

 int af_alg_register_type(const struct af_alg_type *type); int af_alg_unregister_type(const struct af_alg_type *type);

include/crypto/if_alg.h +2 -0
+int af_alg_check_restriction(const char *name,
+			     const struct af_alg_allowlist_entry

allowlist[]);
+
 int af_alg_release(struct socket *sock);
 void af_alg_release_parent(struct sock *sk);
 int af_alg_accept(struct sock *sk, struct socket *newsock, struct proto_accept_arg *arg);

base-commit: 1dc18801be29bc54709aa355b8acd80e183b03cd

#4 lore raw
In reply to Eric Biggers

Hi Eric,

Quoted 66 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

---
 Documentation/admin-guide/sysctl/crypto.rst | 36 +++++++++++
 Documentation/crypto/userspace-if.rst       | 13 +++-
 crypto/af_alg.c                             | 72 +++++++++++++++++++--
 crypto/algif_aead.c                         | 11 ++++
 crypto/algif_hash.c                         | 24 +++++++
 crypto/algif_rng.c                          |  9 +++
 crypto/algif_skcipher.c                     | 20 ++++++
 include/crypto/if_alg.h                     |  8 +++
 8 files changed, 184 insertions(+), 9 deletions(-)
Documentation/admin-guide/sysctl/crypto.rst +14 -0
index b707bd314a64..9a1bd53287f4 100644
@@ -5,10 +5,46 @@
 These files show up in ``/proc/sys/crypto/``, depending on the
 kernel configuration:
 
 .. contents:: :local:
 
+.. _af_alg_restrict:
+
+af_alg_restrict
+===============
+
+Controls the level of restriction of AF_ALG.
+
+AF_ALG is a deprecated and rarely-used userspace interface that is a
+frequent source of vulnerabilities. It also unnecessarily exposes a
+large number of kernel implementation details. For more information
+about AF_ALG, see :ref:`Documentation/crypto/userspace-if.rst
+<crypto_userspace_interface>`.
+
+Starting in Linux v7.3, AF_ALG supports only a limited set of

Documentation/admin-guide/sysctl/crypto.rst line 33

Quoted 10 lines
Documentation/admin-guide/sysctl/crypto.rst +10 -0
+algorithms by default. This sysctl allows the system administrator to
+remove this restriction when needed for compatibility reasons, or to
+go further and disable AF_ALG entirely. The default value is 1.
+
+===  ==================================================================
+0    AF_ALG is unrestricted.
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.

Is the expectation that we go shopping for userspace crypto here? This would mean the Bluetooth stack would use 2 different crypto stacks:
the kernel for module parts and a userspace library for components like bluetoothd, resulting in 2 different sources to audit. So, while I agree we should restrict it to just the algorithms in use and with capabilities, I don't think we should assume the current userspace users are broken and need fixing.

Quoted 400 lines
Documentation/admin-guide/sysctl/crypto.rst +11 -0
+     Specifically, there is an allowlist for unprivileged processes
+     and a somewhat longer allowlist for processes that hold
+     CAP_SYS_ADMIN or CAP_NET_ADMIN in the initial user namespace.
+
+     Attempts to bind() an AF_ALG socket with a disallowed algorithm
+     fail with ENOENT.
+
+2    AF_ALG is completely disabled. Attempts to create an AF_ALG
+     socket fail with EAFNOSUPPORT.
+===  ==================================================================
+
 fips_enabled
 ============
 
 Read-only flag that indicates whether FIPS mode is enabled.
 
Documentation/crypto/userspace-if.rst +10 -3
index ab93300c8e04..d6194346e366 100644
@@ -1,5 +1,7 @@
+.. _crypto_userspace_interface:
+
 User Space Interface
 ====================
 
 Introduction
 ------------
@@ -10,13 +12,18 @@ code.
 
 AF_ALG is insecure and is deprecated. Originally added to the kernel in 2010,
 most kernel developers now consider it to be a mistake. Support for hardware
 accelerators, which was the original purpose of AF_ALG, has been removed.
 
-AF_ALG continues to be supported only for backwards compatibility. On systems
-where no programs using AF_ALG remain, the support for it should be disabled by
-disabling ``CONFIG_CRYPTO_USER_API_*``.
+AF_ALG continues to be supported only for backwards compatibility.
+
+Starting in Linux v7.3, the set of algorithms supported by AF_ALG is limited by
+default. See :ref:`/proc/sys/crypto/af_alg_restrict <af_alg_restrict>`.
+
+On systems where no programs using AF_ALG remain, the support for it should be
+disabled entirely by setting ``/proc/sys/crypto/af_alg_restrict`` to 2 or by
+disabling ``CONFIG_CRYPTO_USER_API_*`` in the kernel configuration.
 
 Deprecation
 -----------
 
 AF_ALG was originally intended to provide userspace programs access to crypto
crypto/af_alg.c +66 -6
index cce000e8590e..34b801568fba 100644
@@ -6,10 +6,11 @@
  *
  * Copyright (c) 2010 Herbert Xu <[email protected]>
  */
 
 #include <linux/atomic.h>
+#include <linux/capability.h>
 #include <crypto/if_alg.h>
 #include <linux/crypto.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/key.h>
@@ -20,14 +21,32 @@
 #include <linux/rwsem.h>
 #include <linux/sched.h>
 #include <linux/sched/signal.h>
 #include <linux/security.h>
 #include <linux/string.h>
+#include <linux/sysctl.h>
+#include <linux/user_namespace.h>
 #include <keys/user-type.h>
 #include <keys/trusted-type.h>
 #include <keys/encrypted-type.h>
 
+static int af_alg_restrict = 1;
+
+static const struct ctl_table af_alg_table[] = {
+       {
+               .procname       = "af_alg_restrict",
+               .data           = &af_alg_restrict,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec_minmax,
+               .extra1         = SYSCTL_ZERO,
+               .extra2         = SYSCTL_TWO,
+       },
+};
+
+static struct ctl_table_header *af_alg_header;
+
 struct alg_type_list {
        const struct af_alg_type *type;
        struct list_head list;
 };
 
@@ -108,10 +127,43 @@ int af_alg_unregister_type(const struct af_alg_type *type)
 
        return err;
 }
 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
 
+static bool af_alg_capable(void)
+{
+       return ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN) ||
+              capable(CAP_SYS_ADMIN);
+}
+
+int af_alg_check_restriction(const char *name,
+                            const struct af_alg_allowlist_entry allowlist[])
+{
+       int level = READ_ONCE(af_alg_restrict);
+
+       if (level == 0)
+               return 0;
+       if (level == 1) {
+               for (const struct af_alg_allowlist_entry *ent = allowlist;
+                    ent->name; ent++) {
+                       if (strcmp(name, ent->name) == 0 &&
+                           (!ent->privileged || af_alg_capable()))
+                               return 0;
+               }
+       }
+       /*
+        * Use -ENOENT (the error code for "algorithm not found") instead of
+        * -EACCES or -EPERM, for the highest chance of correctly triggering
+        * fallback code paths in userspace programs.
+        *
+        * Don't log a warning, since it would be noisy.  iwd tries to bind a
+        * bunch of algorithms that it never uses.
+        */
+       return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(af_alg_check_restriction);
+
 static void alg_do_release(const struct af_alg_type *type, void *private)
 {
        if (!type)
                return;
 
@@ -504,10 +556,13 @@ static int alg_create(struct net *net, struct socket *sock, int protocol,
                      int kern)
 {
        struct sock *sk;
        int err;
 
+       if (READ_ONCE(af_alg_restrict) == 2)
+               return -EAFNOSUPPORT;
+
        if (sock->type != SOCK_SEQPACKET)
                return -ESOCKTNOSUPPORT;
        if (protocol != 0)
                return -EPROTONOSUPPORT;
 
@@ -1220,31 +1275,36 @@ int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
 }
 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
 
 static int __init af_alg_init(void)
 {
-       int err = proto_register(&alg_proto, 0);
+       int err;
+
+       af_alg_header = register_sysctl("crypto", af_alg_table);
 
+       err = proto_register(&alg_proto, 0);
        if (err)
-               goto out;
+               goto out_unregister_sysctl;
 
        err = sock_register(&alg_family);
-       if (err != 0)
+       if (err)
                goto out_unregister_proto;
 
-out:
-       return err;
+       return 0;
 
 out_unregister_proto:
        proto_unregister(&alg_proto);
-       goto out;
+out_unregister_sysctl:
+       unregister_sysctl_table(af_alg_header);
+       return err;
 }
 
 static void __exit af_alg_exit(void)
 {
        sock_unregister(PF_ALG);
        proto_unregister(&alg_proto);
+       unregister_sysctl_table(af_alg_header);
 }
 
 module_init(af_alg_init);
 module_exit(af_alg_exit);
 MODULE_DESCRIPTION("Crypto userspace interface");
crypto/algif_aead.c +11 -0
index 787aac8aeb24..b9217f9086aa 100644
@@ -32,10 +32,15 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry aead_allowlist[] = {
+       { "ccm(aes)", true }, /* bluez */
+       {},
+};
+
 static inline bool aead_sufficient_data(struct sock *sk)
 {
        struct alg_sock *ask = alg_sk(sk);
        struct sock *psk = ask->parent;
        struct alg_sock *pask = alg_sk(psk);
@@ -342,10 +347,16 @@ static struct proto_ops algif_aead_ops_nokey = {
        .poll           =       af_alg_poll,
 };
 
 static void *aead_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, aead_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void aead_release(void *private)
 {
crypto/algif_hash.c +24 -0
index 5452ad6c1506..a8d958d51ece 100644
@@ -14,10 +14,28 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+       { "cmac(aes)", true }, /* iwd, bluez */
+       { "hmac(md5)", true }, /* iwd */
+       { "hmac(sha1)", true }, /* iwd */
+       { "hmac(sha224)", true }, /* iwd */
+       { "hmac(sha256)", true }, /* iwd */
+       { "hmac(sha384)", true }, /* iwd */
+       { "hmac(sha512)", true }, /* iwd, sha512hmac */
+       { "md4", true }, /* iwd */
+       { "md5", true }, /* iwd */
+       { "sha1", false }, /* iwd, iproute2 < 7.0 */
+       { "sha224", true }, /* iwd */
+       { "sha256", true }, /* iwd */
+       { "sha384", true }, /* iwd */
+       { "sha512", true }, /* iwd */
+       {},
+};
+
 struct hash_ctx {
        struct af_alg_sgl sgl;
 
        u8 *result;
 
@@ -380,10 +398,16 @@ static struct proto_ops algif_hash_ops_nokey = {
        .accept         =       hash_accept_nokey,
 };
 
 static void *hash_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, hash_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void hash_release(void *private)
 {
crypto/algif_rng.c +9 -0
index 4dfe7899f8fa..bd522915d56d 100644
@@ -48,10 +48,14 @@
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>");
 MODULE_DESCRIPTION("User-space interface for random number generators");
 
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+       {},
+};
+
 struct rng_ctx {
 #define MAXSIZE 128
        unsigned int len;
        struct crypto_rng *drng;
        u8 *addtl;
@@ -199,10 +203,15 @@ static struct proto_ops __maybe_unused algif_rng_test_ops = {
 
 static void *rng_bind(const char *name)
 {
        struct rng_parent_ctx *pctx;
        struct crypto_rng *rng;
+       int err;
+
+       err = af_alg_check_restriction(name, rng_allowlist);
+       if (err)
+               return ERR_PTR(err);
 
        pctx = kzalloc_obj(*pctx);
        if (!pctx)
                return ERR_PTR(-ENOMEM);
 
crypto/algif_skcipher.c +20 -0
index df20bdfe1f1f..2b8069667974 100644
@@ -32,10 +32,24 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+       { "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+       { "adiantum(xchacha20,aes)", false }, /* cryptsetup */
+       { "cbc(aes)", true }, /* iwd */
+       { "cbc(des)", true }, /* iwd */
+       { "cbc(des3_ede)", true }, /* iwd */
+       { "ctr(aes)", true }, /* iwd */
+       { "ecb(aes)", true }, /* iwd, bluez */
+       { "ecb(des)", true }, /* iwd */
+       { "hctr2(aes)", false }, /* cryptsetup */
+       { "xts(aes)", false }, /* cryptsetup benchmark */
+       {},
+};
+
 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
                            size_t size)
 {
        struct sock *sk = sock->sk;
        struct alg_sock *ask = alg_sk(sk);
@@ -307,10 +321,16 @@ static struct proto_ops algif_skcipher_ops_nokey = {
        .poll           =       af_alg_poll,
 };
 
 static void *skcipher_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, skcipher_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_skcipher(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void skcipher_release(void *private)
 {
include/crypto/if_alg.h +8 -0
index 7643ba954125..4e9ed8e73403 100644
@@ -159,13 +159,21 @@ struct af_alg_ctx {
        unsigned int len;
 
        unsigned int inflight;
 };
 
+struct af_alg_allowlist_entry {
+       const char *name;
+       bool privileged;
+};
+
 int af_alg_register_type(const struct af_alg_type *type);
 int af_alg_unregister_type(const struct af_alg_type *type);
 
+int af_alg_check_restriction(const char *name,
+                            const struct af_alg_allowlist_entry allowlist[]);
+
 int af_alg_release(struct socket *sock);
 void af_alg_release_parent(struct sock *sk);
 int af_alg_accept(struct sock *sk, struct socket *newsock,
                  struct proto_accept_arg *arg);
 

base-commit: 1dc18801be29bc54709aa355b8acd80e183b03cd --
2.54.0

Signature · 1 line
Luiz Augusto von Dentz
#5 lore raw
Quoted 8 lines · Luiz Augusto von Dentz

Documentation/admin-guide/sysctl/crypto.rst line 33

Quoted 6 lines
+===  ==================================================================
+0    AF_ALG is unrestricted.
Documentation/admin-guide/sysctl/crypto.rst +4 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.

Is the expectation that we go shopping for userspace crypto here?

Yes, same as what 99% of userspace already does. Probably you'll just want to link to OpenSSL, but it could be something else if you want.

- Eric

#3 lore raw
In reply to Bastien Nocera
Quoted 10 lines · Bastien Nocera

Hello Eric,

Quoted 6 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare.  It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent
exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Quoted 4 lines

You should also CC: [email protected] for AF_ALG related changes, as ell uses AF_ALG extensively for crypto and checksumming.

Cheers

The known users of libell (iwd and bluez) are already taken into account.

- Eric

#10 lore raw
In reply to Eric Biggers
Quoted 21 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

0: unrestricted
1: limited functionality
2: completely disabled

Quoted 7 lines

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

I think there is room for something in-between the allowlist provided here and "no restrictions". For instance, I think it makes sense to have a mode that allows modern¸ widely-used algorithms (AES-GCM, ChaCha20-Poly1305, SHA-3, HMAC, etc) to all users.

This makes it less likely someone turns off all restrictions.

XFRM allows providing an arbitrary algorithm name, and it appears to be accessible in unprivileged user namespaces. That also needs an allowlist.

Quoted 45 lines
crypto/algif_aead.c +11 -0
index 787aac8aeb24..b9217f9086aa 100644
@@ -32,10 +32,15 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry aead_allowlist[] = {
+	{ "ccm(aes)", true }, /* bluez */
+	{},
+};
+
 static inline bool aead_sufficient_data(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
@@ -342,10 +347,16 @@ static struct proto_ops algif_aead_ops_nokey = {
 	.poll		=	af_alg_poll,
 };
 
 static void *aead_bind(const char *name)
 {
+	int err;
+
+	err = af_alg_check_restriction(name, aead_allowlist);
+	if (err)
+		return ERR_PTR(err);
+
 	return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void aead_release(void *private)
 {
crypto/algif_hash.c +0 -0
index 5452ad6c1506..a8d958d51ece 100644
@@ -14,10 +14,28 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>

crypto/algif_hash.c line 26

Quoted 10 lines
crypto/algif_hash.c +8 -0
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+	{ "cmac(aes)", true }, /* iwd, bluez */
+	{ "hmac(md5)", true }, /* iwd */
+	{ "hmac(sha1)", true }, /* iwd */
+	{ "hmac(sha224)", true }, /* iwd */
+	{ "hmac(sha256)", true }, /* iwd */
+	{ "hmac(sha384)", true }, /* iwd */
+	{ "hmac(sha512)", true }, /* iwd, sha512hmac */

Should this entry have privileged = false? sha512hmac doesn't need privileges.

Quoted 37 lines
crypto/algif_hash.c +16 -0
+	{ "md4", true }, /* iwd */
+	{ "md5", true }, /* iwd */
+	{ "sha1", false }, /* iwd, iproute2 < 7.0 */
+	{ "sha224", true }, /* iwd */
+	{ "sha256", true }, /* iwd */
+	{ "sha384", true }, /* iwd */
+	{ "sha512", true }, /* iwd */
+	{},
+};
+
 struct hash_ctx {
 	struct af_alg_sgl sgl;
 
 	u8 *result;
 
@@ -380,10 +398,16 @@ static struct proto_ops algif_hash_ops_nokey = {
 	.accept		=	hash_accept_nokey,
 };
 
 static void *hash_bind(const char *name)
 {
+	int err;
+
+	err = af_alg_check_restriction(name, hash_allowlist);
+	if (err)
+		return ERR_PTR(err);
+
 	return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void hash_release(void *private)
 {
crypto/algif_rng.c +0 -0
index 4dfe7899f8fa..bd522915d56d 100644

crypto/algif_rng.c line 55

Quoted 8 lines
crypto/algif_rng.c +3 -0
@@ -48,10 +48,14 @@
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>");
 MODULE_DESCRIPTION("User-space interface for random number generators");
 
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+	{},
+};

Can this whole file be deleted? You wrote that it isn't actually used.

(snip)

Quoted 13 lines
crypto/algif_skcipher.c +3 -0
index df20bdfe1f1f..2b8069667974 100644
@@ -32,10 +32,24 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+	{ "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+	{ "adiantum(xchacha20,aes)", false }, /* cryptsetup */

crypto/algif_skcipher.c line 49

Quoted 10 lines
crypto/algif_skcipher.c +10 -0
+	{ "cbc(aes)", true }, /* iwd */
+	{ "cbc(des)", true }, /* iwd */
+	{ "cbc(des3_ede)", true }, /* iwd */
+	{ "ctr(aes)", true }, /* iwd */
+	{ "ecb(aes)", true }, /* iwd, bluez */
+	{ "ecb(des)", true }, /* iwd */
+	{ "hctr2(aes)", false }, /* cryptsetup */
+	{ "xts(aes)", false }, /* cryptsetup benchmark */
+	{},
+};

Do the cryptsetup ones really need to be accessible to unprivileged users?

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
#6 lore raw
In reply to Eric Biggers
Quoted 8 lines · Eric Biggers
Quoted 7 lines · Luiz Augusto von Dentz
Quoted 6 lines
+===  ==================================================================
+0    AF_ALG is unrestricted.
Documentation/admin-guide/sysctl/crypto.rst +4 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
Quoted 4 lines
Quoted 1 line

Is the expectation that we go shopping for userspace crypto here?

Yes, same as what 99% of userspace already does. Probably you'll just want to link to OpenSSL, but it could be something else if you want.

Hard disagree on OpenSSL. It's not a good library.

See <https://cryptography.io/en/latest/statements/state-of-openssl/>.

Distributions should ship AWS-LC and either rebuild reverse dependencies when needed, or work with upstream to catch ABI breaks.

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
#7 lore raw
Quoted 14 lines · Demi Marie Obenour
Quoted 8 lines · Eric Biggers
Quoted 7 lines · Luiz Augusto von Dentz
Quoted 6 lines
+===  ==================================================================
+0    AF_ALG is unrestricted.
Documentation/admin-guide/sysctl/crypto.rst +4 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
Quoted 4 lines
Quoted 1 line

Is the expectation that we go shopping for userspace crypto here?

Yes, same as what 99% of userspace already does. Probably you'll just want to link to OpenSSL, but it could be something else if you want.

Quoted 6 lines

Hard disagree on OpenSSL. It's not a good library.

See <https://cryptography.io/en/latest/statements/state-of-openssl/>.

Distributions should ship AWS-LC and either rebuild reverse dependencies when needed, or work with upstream to catch ABI breaks.

I don't like OpenSSL either, but it's the de facto standard on most distros. While perhaps distros should make that switch, there's no need to wait for that to move away from AF_ALG.

- Eric

#11 lore raw
In reply to Demi Marie Obenour
Quoted 6 lines · Demi Marie Obenour

I think there is room for something in-between the allowlist provided here and "no restrictions". For instance, I think it makes sense to have a mode that allows modern¸ widely-used algorithms (AES-GCM, ChaCha20-Poly1305, SHA-3, HMAC, etc) to all users.

This makes it less likely someone turns off all restrictions.

Would it be worth the complexity when AF_ALG as a whole is deprecated?

Quoted 3 lines

XFRM allows providing an arbitrary algorithm name, and it appears to be accessible in unprivileged user namespaces. That also needs an allowlist.

Doesn't XFRM have a fixed list already? The algorithm comes in as a string, but it has to be in the list.

Quoted 4 lines

crypto/algif_hash.c line 26

Quoted 1 line
crypto/algif_hash.c +1 -0
+	{ "hmac(sha512)", true }, /* iwd, sha512hmac */

Should this entry have privileged = false? sha512hmac doesn't need privileges.

It's for the FIPS 140-3 integrity check use case, which runs as root.

Quoted 10 lines
Quoted 10 lines
crypto/algif_rng.c +0 -0
index 4dfe7899f8fa..bd522915d56d 100644
@@ -48,10 +48,14 @@
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>");
 MODULE_DESCRIPTION("User-space interface for random number generators");
 
Quoted 5 lines

crypto/algif_rng.c line 55

Quoted 3 lines
crypto/algif_rng.c +3 -0
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+	{},
+};

Can this whole file be deleted? You wrote that it isn't actually used.

That's not quite what I said. There's still one known use case: CAVP testing for FIPS 140-3 certifications. But that only accesses the drbg and jitterentropy RNGs and can run as root.

We could consider deleting it anyway and require that the FIPS people use custom kernel modules. But that is out of the scope of this patch.

Quoted 7 lines
Quoted 7 lines
crypto/algif_rng.c +7 -0
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+	{ "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+	{ "adiantum(xchacha20,aes)", false }, /* cryptsetup */
+	{ "cbc(aes)", true }, /* iwd */
+	{ "cbc(des)", true }, /* iwd */
+	{ "cbc(des3_ede)", true }, /* iwd */
+	{ "ctr(aes)", true }, /* iwd */
Quoted 8 lines

crypto/algif_rng.c

Quoted 6 lines
crypto/algif_rng.c +6 -0
+	{ "ecb(aes)", true }, /* iwd, bluez */
+	{ "ecb(des)", true }, /* iwd */
+	{ "hctr2(aes)", false }, /* cryptsetup */
+	{ "xts(aes)", false }, /* cryptsetup benchmark */
+	{},
+};

Do the cryptsetup ones really need to be accessible to unprivileged users?

If we don't allow them, people running 'cryptsetup benchmark' will notice the change. Formatting could also be affected.

Requiring sudo in these cases might be feasible, but we need to strike the right balance. Maybe Milan has an opinion.

- Eric

#8 lore raw
In reply to Eric Biggers
Quoted 8 lines · Eric Biggers
Quoted 7 lines · Luiz Augusto von Dentz
Quoted 6 lines
+===  ==================================================================
+0    AF_ALG is unrestricted.
Documentation/admin-guide/sysctl/crypto.rst +4 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
Quoted 6 lines
Quoted 1 line

Is the expectation that we go shopping for userspace crypto here?

Yes, same as what 99% of userspace already does. Probably you'll just want to link to OpenSSL, but it could be something else if you want.

- Eric

By the way you do know that bluez already has a local implementation of ECDH, right? See src/shared/ecc.c.

- Eric

#9 lore raw

Hi Eric,

Quoted 12 lines · Eric Biggers
Quoted 10 lines · Eric Biggers
Quoted 8 lines · Luiz Augusto von Dentz

Documentation/admin-guide/sysctl/crypto.rst line 33

Quoted 6 lines
+===  ==================================================================
+0    AF_ALG is unrestricted.
Documentation/admin-guide/sysctl/crypto.rst +4 -0
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.

Is the expectation that we go shopping for userspace crypto here?

Quoted 7 lines
Quoted 4 lines

Yes, same as what 99% of userspace already does. Probably you'll just want to link to OpenSSL, but it could be something else if you want.

- Eric

By the way you do know that bluez already has a local implementation of ECDH, right? See src/shared/ecc.c.

It's never been audited; it's only used for hardware emulation, I didn't even remember we had that thingy. What we really use is src/shared/crypto.c, and I'm not looking forward to having it changed. With something like Zephyr, changing crypto libraries every so often just because one didn't fit on a platform wasn't a great experience, and that is a much bigger project. In the end it seems they are using a forked mbedtls:

https://github.com/zephyrproject-rtos/mbedtls

I'm quite sure whatever choice we make will be the wrong choice for someone. Then someone will have the brilliant idea to add some sort of backend support to let everyone plug in their preferred crypto library, possibly adding even more code to audit.

Quoted 1 line

- Eric

Signature · 1 line
Luiz Augusto von Dentz
#12 lore raw
In reply to Eric Biggers
Quoted 18 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

Quoted 7 lines

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

In our brave new world of containers, this is a bit awkward. The admin is sort of asking two separate questions:

1. Is the actual running distro and its privileged components capable of working without AF_ALG or with only the parts marked as being unprivileged?

2. Is the system running contains that need the unprivileged parts? (Which is maybe just sha1 for ip? I really don't know.)

Should there maybe be two separate options so that all options are available? Or maybe something between 2 and 3 that means "limited functionality and privileged modes are completely disabled"?

#20 lore raw
In reply to Eric Biggers
Quoted 5 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

I absolutely want to see this attack surface reduction.

Quoted 5 lines

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:
[...]
Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

I wince at this bit, though. This is a "security policy in the kernel" which we try to avoid, and it's could be done already in userspace with modprobe blacklist.

But, as you say, AF_ALG is deprecated. I understand that to mean that the alg list is only ever going to *shrink* in the future.

Using a sysctl means monolithic kernels are protected, but wouldn't those systems just compile AF_ALG out?

So, I guess, I would want a more clear rationale for why we do it this way instead of via modprobe blacklist. I see a few reasons, but they don't really convince me that we should ignore the "no security policy in the kernel" rule to do it this way.

-Kees

Signature · 1 line
Kees Cook
#13 lore raw
In reply to Andy Lutomirski
Quoted 34 lines · Andy Lutomirski
Quoted 18 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

Quoted 7 lines

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

In our brave new world of containers, this is a bit awkward. The admin is sort of asking two separate questions:

1. Is the actual running distro and its privileged components capable of working without AF_ALG or with only the parts marked as being unprivileged?

Quoted 6 lines

2. Is the system running contains that need the unprivileged parts? (Which is maybe just sha1 for ip? I really don't know.)

Should there maybe be two separate options so that all options are available? Or maybe something between 2 and 3 that means "limited functionality and privileged modes are completely disabled"?

If we want to offer more settings we could. I could see this getting quite complex pretty quickly once everyone weighs in, though. There's quite a bit of value in keeping things simple, even if the offered settings won't be optimal for every case.

- Eric

#21 lore raw
In reply to Kees Cook
Quoted 22 lines · Kees Cook
Quoted 5 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

I absolutely want to see this attack surface reduction.

Quoted 5 lines

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:
[...]
Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

I wince at this bit, though. This is a "security policy in the kernel" which we try to avoid, and it's could be done already in userspace with modprobe blacklist.

But, as you say, AF_ALG is deprecated. I understand that to mean that the alg list is only ever going to *shrink* in the future.

Quoted 7 lines

Using a sysctl means monolithic kernels are protected, but wouldn't those systems just compile AF_ALG out?

So, I guess, I would want a more clear rationale for why we do it this way instead of via modprobe blacklist. I see a few reasons, but they don't really convince me that we should ignore the "no security policy in the kernel" rule to do it this way.

As we saw when distros tried to mitigate copy.fail, a lot of distros have CONFIG_CRYPTO_USER_API_* set to 'y', so algif_aead.ko couldn't be blacklisted. (Ironically because of FIPS 140, which is yet another example of how FIPS 140 harms real-world security.)

But even when 'm', the module blacklist is just a binary choice for each algorithm type: aead, skcipher, hash, and rng. Loading algif_aead.ko allows not just "ccm(aes)" that bluez needs, but also bizarre things like "authencesn(hmac(sha256),cbc(aes))" that are used only in exploits.

And sure, userspace could theoretically gather the complete list of algorithm modules (e.g. authencesn.ko) and blacklist them individually. But no one does that, and many are built-in anyway -- and this time not just because of FIPS.

So we need an allowlist at the algorithm level, not just the algorithm type level. Putting the allowlist in the kernel, taking into account the real use cases like iwd and bluez, and having a simple tristate sysctl similar to some of the existing ones, is the simplest and most practical way to achieve this by default across Linux distros.

If we did something like delegate the algorithm allowlist to LSMs, I think that in practice it's just going to be almost never used.

- Eric

#22 lore raw
In reply to Eric Biggers
Quoted 330 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

---
 Documentation/admin-guide/sysctl/crypto.rst | 36 +++++++++++
 Documentation/crypto/userspace-if.rst       | 13 +++-
 crypto/af_alg.c                             | 72 +++++++++++++++++++--
 crypto/algif_aead.c                         | 11 ++++
 crypto/algif_hash.c                         | 24 +++++++
 crypto/algif_rng.c                          |  9 +++
 crypto/algif_skcipher.c                     | 20 ++++++
 include/crypto/if_alg.h                     |  8 +++
 8 files changed, 184 insertions(+), 9 deletions(-)
Documentation/admin-guide/sysctl/crypto.rst +36 -0
index b707bd314a64..9a1bd53287f4 100644
@@ -5,10 +5,46 @@
 These files show up in ``/proc/sys/crypto/``, depending on the
 kernel configuration:
 
 .. contents:: :local:
 
+.. _af_alg_restrict:
+
+af_alg_restrict
+===============
+
+Controls the level of restriction of AF_ALG.
+
+AF_ALG is a deprecated and rarely-used userspace interface that is a
+frequent source of vulnerabilities. It also unnecessarily exposes a
+large number of kernel implementation details. For more information
+about AF_ALG, see :ref:`Documentation/crypto/userspace-if.rst
+<crypto_userspace_interface>`.
+
+Starting in Linux v7.3, AF_ALG supports only a limited set of
+algorithms by default. This sysctl allows the system administrator to
+remove this restriction when needed for compatibility reasons, or to
+go further and disable AF_ALG entirely. The default value is 1.
+
+===  ==================================================================
+0    AF_ALG is unrestricted.
+
+1    AF_ALG is supported with a limited list of algorithms. The list
+     is designed for compatibility with known users such as iwd and
+     bluez that haven't yet been fixed to use userspace crypto code.
+
+     Specifically, there is an allowlist for unprivileged processes
+     and a somewhat longer allowlist for processes that hold
+     CAP_SYS_ADMIN or CAP_NET_ADMIN in the initial user namespace.
+
+     Attempts to bind() an AF_ALG socket with a disallowed algorithm
+     fail with ENOENT.
+
+2    AF_ALG is completely disabled. Attempts to create an AF_ALG
+     socket fail with EAFNOSUPPORT.
+===  ==================================================================
+
 fips_enabled
 ============
 
 Read-only flag that indicates whether FIPS mode is enabled.
 
Documentation/crypto/userspace-if.rst +10 -3
index ab93300c8e04..d6194346e366 100644
@@ -1,5 +1,7 @@
+.. _crypto_userspace_interface:
+
 User Space Interface
 ====================
 
 Introduction
 ------------
@@ -10,13 +12,18 @@ code.
 
 AF_ALG is insecure and is deprecated. Originally added to the kernel in 2010,
 most kernel developers now consider it to be a mistake. Support for hardware
 accelerators, which was the original purpose of AF_ALG, has been removed.
 
-AF_ALG continues to be supported only for backwards compatibility. On systems
-where no programs using AF_ALG remain, the support for it should be disabled by
-disabling ``CONFIG_CRYPTO_USER_API_*``.
+AF_ALG continues to be supported only for backwards compatibility.
+
+Starting in Linux v7.3, the set of algorithms supported by AF_ALG is limited by
+default. See :ref:`/proc/sys/crypto/af_alg_restrict <af_alg_restrict>`.
+
+On systems where no programs using AF_ALG remain, the support for it should be
+disabled entirely by setting ``/proc/sys/crypto/af_alg_restrict`` to 2 or by
+disabling ``CONFIG_CRYPTO_USER_API_*`` in the kernel configuration.
 
 Deprecation
 -----------
 
 AF_ALG was originally intended to provide userspace programs access to crypto
crypto/af_alg.c +66 -6
index cce000e8590e..34b801568fba 100644
@@ -6,10 +6,11 @@
  *
  * Copyright (c) 2010 Herbert Xu <[email protected]>
  */
 
 #include <linux/atomic.h>
+#include <linux/capability.h>
 #include <crypto/if_alg.h>
 #include <linux/crypto.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/key.h>
@@ -20,14 +21,32 @@
 #include <linux/rwsem.h>
 #include <linux/sched.h>
 #include <linux/sched/signal.h>
 #include <linux/security.h>
 #include <linux/string.h>
+#include <linux/sysctl.h>
+#include <linux/user_namespace.h>
 #include <keys/user-type.h>
 #include <keys/trusted-type.h>
 #include <keys/encrypted-type.h>
 
+static int af_alg_restrict = 1;
+
+static const struct ctl_table af_alg_table[] = {
+       {
+               .procname       = "af_alg_restrict",
+               .data           = &af_alg_restrict,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec_minmax,
+               .extra1         = SYSCTL_ZERO,
+               .extra2         = SYSCTL_TWO,
+       },
+};
+
+static struct ctl_table_header *af_alg_header;
+
 struct alg_type_list {
        const struct af_alg_type *type;
        struct list_head list;
 };
 
@@ -108,10 +127,43 @@ int af_alg_unregister_type(const struct af_alg_type *type)
 
        return err;
 }
 EXPORT_SYMBOL_GPL(af_alg_unregister_type);
 
+static bool af_alg_capable(void)
+{
+       return ns_capable_noaudit(&init_user_ns, CAP_NET_ADMIN) ||
+              capable(CAP_SYS_ADMIN);
+}
+
+int af_alg_check_restriction(const char *name,
+                            const struct af_alg_allowlist_entry allowlist[])
+{
+       int level = READ_ONCE(af_alg_restrict);
+
+       if (level == 0)
+               return 0;
+       if (level == 1) {
+               for (const struct af_alg_allowlist_entry *ent = allowlist;
+                    ent->name; ent++) {
+                       if (strcmp(name, ent->name) == 0 &&
+                           (!ent->privileged || af_alg_capable()))
+                               return 0;
+               }
+       }
+       /*
+        * Use -ENOENT (the error code for "algorithm not found") instead of
+        * -EACCES or -EPERM, for the highest chance of correctly triggering
+        * fallback code paths in userspace programs.
+        *
+        * Don't log a warning, since it would be noisy.  iwd tries to bind a
+        * bunch of algorithms that it never uses.
+        */
+       return -ENOENT;
+}
+EXPORT_SYMBOL_GPL(af_alg_check_restriction);
+
 static void alg_do_release(const struct af_alg_type *type, void *private)
 {
        if (!type)
                return;
 
@@ -504,10 +556,13 @@ static int alg_create(struct net *net, struct socket *sock, int protocol,
                      int kern)
 {
        struct sock *sk;
        int err;
 
+       if (READ_ONCE(af_alg_restrict) == 2)
+               return -EAFNOSUPPORT;
+
        if (sock->type != SOCK_SEQPACKET)
                return -ESOCKTNOSUPPORT;
        if (protocol != 0)
                return -EPROTONOSUPPORT;
 
@@ -1220,31 +1275,36 @@ int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
 }
 EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
 
 static int __init af_alg_init(void)
 {
-       int err = proto_register(&alg_proto, 0);
+       int err;
+
+       af_alg_header = register_sysctl("crypto", af_alg_table);
 
+       err = proto_register(&alg_proto, 0);
        if (err)
-               goto out;
+               goto out_unregister_sysctl;
 
        err = sock_register(&alg_family);
-       if (err != 0)
+       if (err)
                goto out_unregister_proto;
 
-out:
-       return err;
+       return 0;
 
 out_unregister_proto:
        proto_unregister(&alg_proto);
-       goto out;
+out_unregister_sysctl:
+       unregister_sysctl_table(af_alg_header);
+       return err;
 }
 
 static void __exit af_alg_exit(void)
 {
        sock_unregister(PF_ALG);
        proto_unregister(&alg_proto);
+       unregister_sysctl_table(af_alg_header);
 }
 
 module_init(af_alg_init);
 module_exit(af_alg_exit);
 MODULE_DESCRIPTION("Crypto userspace interface");
crypto/algif_aead.c +11 -0
index 787aac8aeb24..b9217f9086aa 100644
@@ -32,10 +32,15 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry aead_allowlist[] = {
+       { "ccm(aes)", true }, /* bluez */
+       {},
+};
+
 static inline bool aead_sufficient_data(struct sock *sk)
 {
        struct alg_sock *ask = alg_sk(sk);
        struct sock *psk = ask->parent;
        struct alg_sock *pask = alg_sk(psk);
@@ -342,10 +347,16 @@ static struct proto_ops algif_aead_ops_nokey = {
        .poll           =       af_alg_poll,
 };
 
 static void *aead_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, aead_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_aead(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void aead_release(void *private)
 {
crypto/algif_hash.c +6 -0
index 5452ad6c1506..a8d958d51ece 100644
@@ -14,10 +14,28 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+       { "cmac(aes)", true }, /* iwd, bluez */
+       { "hmac(md5)", true }, /* iwd */
+       { "hmac(sha1)", true }, /* iwd */
+       { "hmac(sha224)", true }, /* iwd */
+       { "hmac(sha256)", true }, /* iwd */

crypto/algif_hash.c line 34

Quoted 10 lines
crypto/algif_hash.c +10 -0
+       { "hmac(sha384)", true }, /* iwd */
+       { "hmac(sha512)", true }, /* iwd, sha512hmac */
+       { "md4", true }, /* iwd */
+       { "md5", true }, /* iwd */
+       { "sha1", false }, /* iwd, iproute2 < 7.0 */
+       { "sha224", true }, /* iwd */
+       { "sha256", true }, /* iwd */
+       { "sha384", true }, /* iwd */
+       { "sha512", true }, /* iwd */
+       {},

In OpenWrt, https://gitlab.com/linux-afs/kafs-client and strongswan seem to be the other users of the user API. I haven't looked into what they need.

Quoted 137 lines
crypto/algif_hash.c +8 -0
+};
+
 struct hash_ctx {
        struct af_alg_sgl sgl;
 
        u8 *result;
 
@@ -380,10 +398,16 @@ static struct proto_ops algif_hash_ops_nokey = {
        .accept         =       hash_accept_nokey,
 };
 
 static void *hash_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, hash_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_ahash(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void hash_release(void *private)
 {
crypto/algif_rng.c +9 -0
index 4dfe7899f8fa..bd522915d56d 100644
@@ -48,10 +48,14 @@
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Stephan Mueller <[email protected]>");
 MODULE_DESCRIPTION("User-space interface for random number generators");
 
+static const struct af_alg_allowlist_entry rng_allowlist[] = {
+       {},
+};
+
 struct rng_ctx {
 #define MAXSIZE 128
        unsigned int len;
        struct crypto_rng *drng;
        u8 *addtl;
@@ -199,10 +203,15 @@ static struct proto_ops __maybe_unused algif_rng_test_ops = {
 
 static void *rng_bind(const char *name)
 {
        struct rng_parent_ctx *pctx;
        struct crypto_rng *rng;
+       int err;
+
+       err = af_alg_check_restriction(name, rng_allowlist);
+       if (err)
+               return ERR_PTR(err);
 
        pctx = kzalloc_obj(*pctx);
        if (!pctx)
                return ERR_PTR(-ENOMEM);
 
crypto/algif_skcipher.c +20 -0
index df20bdfe1f1f..2b8069667974 100644
@@ -32,10 +32,24 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
 #include <net/sock.h>
 
+static const struct af_alg_allowlist_entry skcipher_allowlist[] = {
+       { "adiantum(xchacha12,aes)", false }, /* cryptsetup */
+       { "adiantum(xchacha20,aes)", false }, /* cryptsetup */
+       { "cbc(aes)", true }, /* iwd */
+       { "cbc(des)", true }, /* iwd */
+       { "cbc(des3_ede)", true }, /* iwd */
+       { "ctr(aes)", true }, /* iwd */
+       { "ecb(aes)", true }, /* iwd, bluez */
+       { "ecb(des)", true }, /* iwd */
+       { "hctr2(aes)", false }, /* cryptsetup */
+       { "xts(aes)", false }, /* cryptsetup benchmark */
+       {},
+};
+
 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
                            size_t size)
 {
        struct sock *sk = sock->sk;
        struct alg_sock *ask = alg_sk(sk);
@@ -307,10 +321,16 @@ static struct proto_ops algif_skcipher_ops_nokey = {
        .poll           =       af_alg_poll,
 };
 
 static void *skcipher_bind(const char *name)
 {
+       int err;
+
+       err = af_alg_check_restriction(name, skcipher_allowlist);
+       if (err)
+               return ERR_PTR(err);
+
        return crypto_alloc_skcipher(name, 0, AF_ALG_CRYPTOAPI_MASK);
 }
 
 static void skcipher_release(void *private)
 {
include/crypto/if_alg.h +8 -0
index 7643ba954125..4e9ed8e73403 100644
@@ -159,13 +159,21 @@ struct af_alg_ctx {
        unsigned int len;
 
        unsigned int inflight;
 };
 
+struct af_alg_allowlist_entry {
+       const char *name;
+       bool privileged;
+};
+
 int af_alg_register_type(const struct af_alg_type *type);
 int af_alg_unregister_type(const struct af_alg_type *type);
 
+int af_alg_check_restriction(const char *name,
+                            const struct af_alg_allowlist_entry allowlist[]);
+
 int af_alg_release(struct socket *sock);
 void af_alg_release_parent(struct sock *sk);
 int af_alg_accept(struct sock *sk, struct socket *newsock,
                  struct proto_accept_arg *arg);
 

base-commit: 1dc18801be29bc54709aa355b8acd80e183b03cd --
2.54.0

#23 lore raw
Quoted 16 lines · Rosen Penev
Quoted 6 lines
crypto/algif_hash.c +6 -0
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+       { "cmac(aes)", true }, /* iwd, bluez */
+       { "hmac(md5)", true }, /* iwd */
+       { "hmac(sha1)", true }, /* iwd */
+       { "hmac(sha224)", true }, /* iwd */
+       { "hmac(sha256)", true }, /* iwd */

crypto/algif_hash.c line 34

Quoted 10 lines
crypto/algif_hash.c +10 -0
+       { "hmac(sha384)", true }, /* iwd */
+       { "hmac(sha512)", true }, /* iwd, sha512hmac */
+       { "md4", true }, /* iwd */
+       { "md5", true }, /* iwd */
+       { "sha1", false }, /* iwd, iproute2 < 7.0 */
+       { "sha224", true }, /* iwd */
+       { "sha256", true }, /* iwd */
+       { "sha384", true }, /* iwd */
+       { "sha512", true }, /* iwd */
+       {},
Quoted 3 lines

In OpenWrt, https://gitlab.com/linux-afs/kafs-client and strongswan seem to be the other users of the user API. I haven't looked into what they need.

[Please trim your replies, thanks!]

https://gitlab.com/linux-afs/kafs-client uses AF_ALG only for "hmac(md5)", which I already put on the privileged allowlist due to iwd also using it. So it would still work by default with the current patch, unless it needs to use it unprivileged.

(FWIW, a use of a single obsolete algorithm like this is also a good candidate for just replacing with local code...)

https://github.com/strongswan/strongswan already supports userspace crypto libraries.

- Eric

#24 lore raw
Quoted 18 lines · Eric Biggers
Quoted 16 lines · Rosen Penev
Quoted 16 lines
crypto/algif_hash.c +16 -0
+static const struct af_alg_allowlist_entry hash_allowlist[] = {
+       { "cmac(aes)", true }, /* iwd, bluez */
+       { "hmac(md5)", true }, /* iwd */
+       { "hmac(sha1)", true }, /* iwd */
+       { "hmac(sha224)", true }, /* iwd */
+       { "hmac(sha256)", true }, /* iwd */
+       { "hmac(sha384)", true }, /* iwd */
+       { "hmac(sha512)", true }, /* iwd, sha512hmac */
+       { "md4", true }, /* iwd */
+       { "md5", true }, /* iwd */
+       { "sha1", false }, /* iwd, iproute2 < 7.0 */
+       { "sha224", true }, /* iwd */
+       { "sha256", true }, /* iwd */
+       { "sha384", true }, /* iwd */
+       { "sha512", true }, /* iwd */
+       {},
Quoted 5 lines
Quoted 3 lines

In OpenWrt, https://gitlab.com/linux-afs/kafs-client and strongswan seem to be the other users of the user API. I haven't looked into what they need.

[Please trim your replies, thanks!]

Not sure what you mean.

Quoted 11 lines

https://gitlab.com/linux-afs/kafs-client uses AF_ALG only for "hmac(md5)", which I already put on the privileged allowlist due to iwd also using it. So it would still work by default with the current patch, unless it needs to use it unprivileged.

(FWIW, a use of a single obsolete algorithm like this is also a good candidate for just replacing with local code...)

https://github.com/strongswan/strongswan already supports userspace crypto libraries.

Oh lovely. Looks like this needs fixing in OpenWrt.

Quoted 2 lines

- Eric

#14 lore raw
In reply to Eric Biggers
Quoted 42 lines · Eric Biggers
Quoted 34 lines · Andy Lutomirski
Quoted 18 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

Quoted 7 lines

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

In our brave new world of containers, this is a bit awkward. The admin is sort of asking two separate questions:

1. Is the actual running distro and its privileged components capable of working without AF_ALG or with only the parts marked as being unprivileged?

Quoted 6 lines

2. Is the system running contains that need the unprivileged parts? (Which is maybe just sha1 for ip? I really don't know.)

Should there maybe be two separate options so that all options are available? Or maybe something between 2 and 3 that means "limited functionality and privileged modes are completely disabled"?

Quoted 6 lines

If we want to offer more settings we could. I could see this getting quite complex pretty quickly once everyone weighs in, though. There's quite a bit of value in keeping things simple, even if the offered settings won't be optimal for every case.

- Eric

What about exposing both allowlists to userspace and making them configurable?

I'm mostly concerned about systems running code (possibly closed-source) that uses algorithms that nobody here knows about. It would be better to allow a single algorithm than to turn off all restrictions.

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
#15 lore raw
Quoted 50 lines · Demi Marie Obenour
Quoted 42 lines · Eric Biggers
Quoted 34 lines · Andy Lutomirski
Quoted 18 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

Quoted 7 lines

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

In our brave new world of containers, this is a bit awkward. The admin is sort of asking two separate questions:

1. Is the actual running distro and its privileged components capable of working without AF_ALG or with only the parts marked as being unprivileged?

Quoted 6 lines

2. Is the system running contains that need the unprivileged parts? (Which is maybe just sha1 for ip? I really don't know.)

Should there maybe be two separate options so that all options are available? Or maybe something between 2 and 3 that means "limited functionality and privileged modes are completely disabled"?

Quoted 6 lines

If we want to offer more settings we could. I could see this getting quite complex pretty quickly once everyone weighs in, though. There's quite a bit of value in keeping things simple, even if the offered settings won't be optimal for every case.

- Eric

Quoted 10 lines

What about exposing both allowlists to userspace and making them configurable?

I'm mostly concerned about systems running code (possibly closed-source) that uses algorithms that nobody here knows about. It would be better to allow a single algorithm than to turn off all restrictions.

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)

I think the following is what you're asking for:

    sysctl crypto.algif_aead_priv_allowlist='ccm(aes)'
    sysctl crypto.algif_aead_unpriv_allowlist=''
    sysctl crypto.algif_hash_priv_allowlist='cmac(aes),hmac(md5),hmac(sha1),hmac(sha256),hmac(sha384),hmac(sha512),md4,md5,sha224,sha256,sha384,sha512'
    sysctl crypto.algif_hash_unpriv_allowlist='sha1'
    sysctl crypto.algif_skcipher_priv_allowlist='cbc(aes),cbc(des),cbc(des3_ede),ctr(aes),ecb(aes),ecb(des)'
    sysctl crypto.algif_skcipher_unpriv_allowlist='adiantum(xchacha12,aes),adiantum(xchacha20,aes),hctr2(aes),xts(aes)'
    sysctl crypto.algif_rng_priv_allowlist=''
    sysctl crypto.algif_rng_unpriv_allowlist=''

We could do that if it's what people want. Just keep in mind that it would be much more complex than the single tristate sysctl. And in practice the number of people who are knowledgeable enough to create these lists is quite small; we've seen similar things with other "Crypto API" configuration knobs that seem to never be touched in practice.

Any thoughts?

- Eric

#16 lore raw

Hi,

Quoted 5 lines · Eric Biggers

We could do that if it's what people want. Just keep in mind that it would be much more complex than the single tristate sysctl. And in practice the number of people who are knowledgeable enough to create these lists is quite small; we've seen similar things with other "Crypto API" configuration knobs that seem to never be touched in practice.

I don't think finer grained control is necessary.

The tristate is the best possible interface for the people running precompiled distribution kernels. Ideally, deactivating the restriction should also be disallowed in lockdown mode -- and this becomes a lot easier to subvert if the list of algorithms is runtime configurable.

I think it is safe to assume that the people using AF_ALG with hardware crypto engines are building embedded systems with a custom kernel configuration, so the .config is probably the best place for the selection of algorithms that should remain available in restricted mode.

Simon

#17 lore raw
Quoted 17 lines · Simon Richter

Hi,

Quoted 5 lines · Eric Biggers

We could do that if it's what people want. Just keep in mind that it would be much more complex than the single tristate sysctl. And in practice the number of people who are knowledgeable enough to create these lists is quite small; we've seen similar things with other "Crypto API" configuration knobs that seem to never be touched in practice.

I don't think finer grained control is necessary.

The tristate is the best possible interface for the people running precompiled distribution kernels. Ideally, deactivating the restriction should also be disallowed in lockdown mode -- and this becomes a lot easier to subvert if the list of algorithms is runtime configurable.

Quoted 6 lines

I think it is safe to assume that the people using AF_ALG with hardware crypto engines are building embedded systems with a custom kernel configuration, so the .config is probably the best place for the selection of algorithms that should remain available in restricted mode.

Simon

I agree.

That said, if the crypto_rng support is to remain, should it have a non-empty allowlist for privileged processes? Otherwise, it's dead code with the default sysctl value.

Signature · 2 lines
Sincerely,
Demi Marie Obenour (she/her/hers)
#18 lore raw

Hi,

Quoted 3 lines · Demi Marie Obenour

That said, if the crypto_rng support is to remain, should it have a non-empty allowlist for privileged processes? Otherwise, it's dead code with the default sysctl value.

For desktop users, it is indeed dead, but only if we can guarantee that they will never change the sysctl value to allow all algorithms.

So I'd probably do an enabled-by-default configuration option that takes away the option to enable all algorithms, and the RNG code in AF_ALG becomes conditional on that option.

I think that embedded systems will still use this code until a better alternative exists, so it's not entirely dead, and the best way to get rid of it is to build something better.

I'd love to see a vDSO based interface for all crypto operations that allows reusing the code in the kernel from userspace, avoids the system call overhead in the likely case that the CPU based implementation is also the most efficient, and allows for more flexibility with hardware interfaces (quite a few allow submission directly from unprivileged processes).

Simon

#19 lore raw
Quoted 7 lines · Simon Richter

Hi,

Quoted 3 lines · Demi Marie Obenour

That said, if the crypto_rng support is to remain, should it have a non-empty allowlist for privileged processes? Otherwise, it's dead code with the default sysctl value.

[...]

Quoted 3 lines

I think that embedded systems will still use this code until a better alternative exists, so it's not entirely dead, and the best way to get rid of it is to build something better.

/dev/urandom, /dev/random, and /dev/hwrng. I.e. what userspace *actually* uses already. algif_rng is a redundant, much harder to use interface that got added off to the side for some reason.

- Eric

maintainer
#25 lore raw
In reply to Eric Biggers
Quoted 30 lines · Eric Biggers

AF_ALG is a frequent source of vulnerabilities and a maintenance nightmare. It exposes far more functionality to userspace than ever should have been exposed, especially to unprivileged processes. Recent exploits have targeted kernel internal implementation details like "authencesn" that have zero use case for userspace access.

Fortunately, AF_ALG is rarely used in practice, as userspace crypto libraries exist. And when it is used, only some functionality is known to be used, and many users are known to hold capabilities already. iwd for example requires CAP_NET_ADMIN and has a known algorithm list
(https://lore.kernel.org/linux-crypto/[email protected]/).

Thus, let's restrict the set of allowed algorithms by default, depending on the capabilities held.

Add a sysctl /proc/sys/crypto/af_alg_restrict with meaning:

0: unrestricted
1: limited functionality
2: completely disabled

Set the default value to 1, which enables an algorithm allowlist for unprivileged processes and a slightly longer allowlist for privileged processes.

Note that the list may be tweaked in the future. However, the common use cases such as iwd and bluez are taken into account already. I've tested that iwd still works with the default value of 1.

Quoted 10 lines
---
 Documentation/admin-guide/sysctl/crypto.rst | 36 +++++++++++
 Documentation/crypto/userspace-if.rst       | 13 +++-
 crypto/af_alg.c                             | 72 +++++++++++++++++++--
 crypto/algif_aead.c                         | 11 ++++
 crypto/algif_hash.c                         | 24 +++++++
 crypto/algif_rng.c                          |  9 +++
 crypto/algif_skcipher.c                     | 20 ++++++
 include/crypto/if_alg.h                     |  8 +++
 8 files changed, 184 insertions(+), 9 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt