Linux s390 Architecture development
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: keyrings@vger.kernel.org
Cc: "Jarkko Sakkinen" <jarkko@kernel.org>,
	linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org,
	linux-s390@vger.kernel.org,
	linux-security-module@vger.kernel.org, netdev@vger.kernel.org,
	samba-technical@lists.samba.org,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Anna Schumaker" <anna@kernel.org>,
	"Bharath SM" <bharathsm@microsoft.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>,
	"David Howells" <dhowells@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Heiko Carstens" <hca@linux.ibm.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"James Bottomley" <James.Bottomley@HansenPartnership.com>,
	"James Morris" <jmorris@namei.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Marc Dionne" <marc.dionne@auristor.com>,
	"Marek Behún" <kabel@kernel.org>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Namjae Jeon" <linkinjeon@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Paulo Alcantara" <pc@manguebit.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Ronnie Sahlberg" <ronniesahlberg@gmail.com>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Shyam Prasad N" <sprasad@microsoft.com>,
	"Simon Horman" <horms@kernel.org>,
	"Sumit Garg" <sumit.garg@kernel.org>,
	"Sven Schnelle" <svens@linux.ibm.com>,
	"Tom Talpey" <tom@talpey.com>,
	"Trond Myklebust" <trondmy@kernel.org>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Willem de Bruijn" <willemb@google.com>
Subject: [PATCH v7 04/14] dns_resolver: Move the key type into a separate module
Date: Sun, 29 Nov 2026 19:05:33 +0200	[thread overview]
Message-ID: <20261129170543.2915157-5-jarkko@kernel.org> (raw)
In-Reply-To: <20261129170543.2915157-1-jarkko@kernel.org>

Once keys pin to the kernel module of their key type, having the DNS key
type in the same modules as the resolver will introduce a circular
dependency preventing module_exit() from ever running.

Introduce dns_resolver_key.ko containing key_type_dns_resolver. On module
exit, clear the resolver keyring, drop the credentials, and call
key_gc_flush() to drain unreferenced DNS resolver keys.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 Documentation/networking/dns_resolver.rst |  8 ++-
 net/Makefile                              |  2 +-
 net/dns_resolver/Kconfig                  | 15 +++++
 net/dns_resolver/Makefile                 |  4 +-
 net/dns_resolver/dns_key.c                | 65 ++++-----------------
 net/dns_resolver/dns_query.c              | 70 +++++++++++++++++++++++
 net/dns_resolver/internal.h               |  7 ---
 7 files changed, 104 insertions(+), 67 deletions(-)

diff --git a/Documentation/networking/dns_resolver.rst b/Documentation/networking/dns_resolver.rst
index 52f298834db6..898f4d7e10da 100644
--- a/Documentation/networking/dns_resolver.rst
+++ b/Documentation/networking/dns_resolver.rst
@@ -39,6 +39,7 @@ Compilation
 
 The module should be enabled by turning on the kernel configuration options::
 
+	CONFIG_DNS_KEY		- tristate "DNS Resolver key type"
 	CONFIG_DNS_RESOLVER	- tristate "DNS Resolver support"
 
 
@@ -116,8 +117,8 @@ Keys of dns_resolver type can be read from userspace using keyctl_read() or
 Mechanism
 =========
 
-The dns_resolver module registers a key type called "dns_resolver".  Keys of
-this type are used to transport and cache DNS lookup results from userspace.
+The dns_resolver_key module registers a key type called "dns_resolver".  Keys
+of this type are used to transport and cache DNS lookup results from userspace.
 
 When dns_query() is invoked, it calls request_key() to search the local
 keyrings for a cached DNS result.  If that fails to find one, it upcalls to
@@ -148,6 +149,7 @@ Debugging
 =========
 
 Debugging messages can be turned on dynamically by writing a 1 into the
-following file::
+following files::
 
 	/sys/module/dns_resolver/parameters/debug
+	/sys/module/dns_resolver_key/parameters/debug
diff --git a/net/Makefile b/net/Makefile
index 5b2dd7f07a85..69c16a7851b4 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -55,7 +55,7 @@ obj-$(CONFIG_IEEE802154)	+= ieee802154/
 obj-$(CONFIG_MAC802154)		+= mac802154/
 
 obj-$(CONFIG_SYSCTL)		+= sysctl_net.o
-obj-$(CONFIG_DNS_RESOLVER)	+= dns_resolver/
+obj-$(CONFIG_DNS_KEY)		+= dns_resolver/
 obj-$(CONFIG_CEPH_LIB)		+= ceph/
 obj-$(CONFIG_BATMAN_ADV)	+= batman-adv/
 obj-$(CONFIG_NFC)		+= nfc/
diff --git a/net/dns_resolver/Kconfig b/net/dns_resolver/Kconfig
index 7c2dba273e35..2618bad2e771 100644
--- a/net/dns_resolver/Kconfig
+++ b/net/dns_resolver/Kconfig
@@ -1,10 +1,25 @@
 # SPDX-License-Identifier: GPL-2.0-only
+
+#
+# Configuration for DNS Resolver key type
+#
+config DNS_KEY
+	tristate "DNS Resolver key type"
+	depends on KEYS
+	help
+	  Support the key type used to cache DNS lookups independently of
+	  the in-kernel DNS query service.
+
+	  To compile this as a module, choose M here: the module is called
+	  dns_resolver_key.
+
 #
 # Configuration for DNS Resolver
 #
 config DNS_RESOLVER
 	tristate "DNS Resolver support"
 	depends on KEYS
+	select DNS_KEY
 	help
 	  Saying Y here will include support for the DNS Resolver key type
 	  which can be used to make upcalls to perform DNS lookups in
diff --git a/net/dns_resolver/Makefile b/net/dns_resolver/Makefile
index 877532d662d0..586e4c6514de 100644
--- a/net/dns_resolver/Makefile
+++ b/net/dns_resolver/Makefile
@@ -3,6 +3,8 @@
 # Makefile for the Linux DNS Resolver.
 #
 
+obj-$(CONFIG_DNS_KEY) += dns_resolver_key.o
 obj-$(CONFIG_DNS_RESOLVER) += dns_resolver.o
 
-dns_resolver-y :=  dns_key.o dns_query.o
+dns_resolver-y := dns_query.o
+dns_resolver_key-y := dns_key.o
diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c
index 451247864a63..7eecbefa1816 100644
--- a/net/dns_resolver/dns_key.c
+++ b/net/dns_resolver/dns_key.c
@@ -9,6 +9,7 @@
  *              Wang Lei (wang840925@gmail.com)
  *		David Howells (dhowells@redhat.com)
  */
+
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/slab.h>
@@ -22,15 +23,13 @@
 #include <keys/user-type.h>
 #include "internal.h"
 
-MODULE_DESCRIPTION("DNS Resolver");
+MODULE_DESCRIPTION("DNS Resolver key type");
 MODULE_AUTHOR("Wang Lei");
 MODULE_LICENSE("GPL");
 
-unsigned int dns_resolver_debug;
+static unsigned int dns_resolver_debug;
 module_param_named(debug, dns_resolver_debug, uint, 0644);
-MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
-
-const struct cred *dns_resolver_cache;
+MODULE_PARM_DESC(debug, "DNS Resolver key debugging mask");
 
 #define	DNS_ERRORNO_OPTION	"dnserror"
 
@@ -318,61 +317,17 @@ struct key_type key_type_dns_resolver = {
 	.describe	= dns_resolver_describe,
 	.read		= dns_resolver_read,
 };
+EXPORT_SYMBOL_GPL(key_type_dns_resolver);
 
-static int __init init_dns_resolver(void)
+static int __init init_dns_key(void)
 {
-	struct cred *cred;
-	struct key *keyring;
-	int ret;
-
-	/* create an override credential set with a special thread keyring in
-	 * which DNS requests are cached
-	 *
-	 * this is used to prevent malicious redirections from being installed
-	 * with add_key().
-	 */
-	cred = prepare_kernel_cred(&init_task);
-	if (!cred)
-		return -ENOMEM;
-
-	keyring = keyring_alloc(".dns_resolver",
-				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
-				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
-				KEY_USR_VIEW | KEY_USR_READ,
-				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
-	if (IS_ERR(keyring)) {
-		ret = PTR_ERR(keyring);
-		goto failed_put_cred;
-	}
-
-	ret = register_key_type(&key_type_dns_resolver);
-	if (ret < 0)
-		goto failed_put_key;
-
-	/* instruct request_key() to use this special keyring as a cache for
-	 * the results it looks up */
-	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
-	cred->thread_keyring = keyring;
-	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
-	dns_resolver_cache = cred;
-
-	kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
-	return 0;
-
-failed_put_key:
-	key_put(keyring);
-failed_put_cred:
-	put_cred(cred);
-	return ret;
+	return register_key_type(&key_type_dns_resolver);
 }
 
-static void __exit exit_dns_resolver(void)
+static void __exit exit_dns_key(void)
 {
-	key_revoke(dns_resolver_cache->thread_keyring);
 	unregister_key_type(&key_type_dns_resolver);
-	put_cred(dns_resolver_cache);
 }
 
-module_init(init_dns_resolver)
-module_exit(exit_dns_resolver)
-MODULE_LICENSE("GPL");
+module_init(init_dns_key);
+module_exit(exit_dns_key);
diff --git a/net/dns_resolver/dns_query.c b/net/dns_resolver/dns_query.c
index b8fb9a1245bf..9fd3ac16b19d 100644
--- a/net/dns_resolver/dns_query.c
+++ b/net/dns_resolver/dns_query.c
@@ -24,8 +24,10 @@
  */
 
 #include <linux/module.h>
+#include <linux/moduleparam.h>
 #include <linux/slab.h>
 #include <linux/cred.h>
+#include <linux/keyctl.h>
 #include <linux/dns_resolver.h>
 #include <linux/err.h>
 #include <net/net_namespace.h>
@@ -35,6 +37,9 @@
 
 #include "internal.h"
 
+static unsigned int dns_resolver_debug;
+static const struct cred *dns_resolver_cache;
+
 /**
  * dns_query - Query the DNS
  * @net: The network namespace to operate in.
@@ -137,3 +142,68 @@ int dns_query(struct net *net,
 	return ret;
 }
 EXPORT_SYMBOL(dns_query);
+
+static int __init init_dns_resolver(void)
+{
+	struct cred *cred;
+	struct key *keyring;
+	int ret;
+
+	/*
+	 * Create an override credential set with a special thread keyring in
+	 * which DNS requests are cached.  This prevents malicious redirections
+	 * from being installed with add_key().
+	 */
+	cred = prepare_kernel_cred(&init_task);
+	if (!cred)
+		return -ENOMEM;
+
+	keyring = keyring_alloc(".dns_resolver",
+				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
+				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
+				KEY_USR_VIEW | KEY_USR_READ,
+				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
+	if (IS_ERR(keyring)) {
+		ret = PTR_ERR(keyring);
+		goto failed_put_cred;
+	}
+
+	/*
+	 * Instruct request_key() to use this special keyring as a cache for
+	 * the results it looks up.
+	 */
+	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
+	cred->thread_keyring = keyring;
+	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
+	dns_resolver_cache = cred;
+
+	kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
+	return 0;
+
+failed_put_cred:
+	put_cred(cred);
+	return ret;
+}
+
+static void __exit exit_dns_resolver(void)
+{
+	int ret;
+
+	ret = keyring_clear(dns_resolver_cache->thread_keyring);
+	key_revoke(dns_resolver_cache->thread_keyring);
+	put_cred(dns_resolver_cache);
+	/* Drain deferred credential and keyring destruction. */
+	if (ret)
+		key_gc_flush(&key_type_keyring);
+	key_gc_flush(&key_type_dns_resolver);
+}
+
+module_init(init_dns_resolver);
+module_exit(exit_dns_resolver);
+
+MODULE_DESCRIPTION("DNS Resolver");
+MODULE_AUTHOR("Wang Lei");
+MODULE_LICENSE("GPL");
+
+module_param_named(debug, dns_resolver_debug, uint, 0644);
+MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
diff --git a/net/dns_resolver/internal.h b/net/dns_resolver/internal.h
index d0d8edcea092..855e0bbf2dd5 100644
--- a/net/dns_resolver/internal.h
+++ b/net/dns_resolver/internal.h
@@ -18,16 +18,9 @@ enum {
 	dns_key_error,
 };
 
-/*
- * dns_key.c
- */
-extern const struct cred *dns_resolver_cache;
-
 /*
  * debug tracing
  */
-extern unsigned int dns_resolver_debug;
-
 #define	kdebug(FMT, ...)				\
 do {							\
 	if (unlikely(dns_resolver_debug))		\
-- 
2.47.3


  parent reply	other threads:[~2026-11-29 17:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 01/14] KEYS: Move key garbage collection to a dedicated workqueue Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 02/14] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 03/14] KEYS: Add key_gc_flush() for draining RCU callbacks Jarkko Sakkinen
2026-11-29 17:05 ` Jarkko Sakkinen [this message]
2026-11-29 17:05 ` [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 06/14] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 08/14] rxrpc: Release conn->key when init_connection_security() fails Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 09/14] rxrpc: Move key types to their own module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 11/14] platform/cznic: turris-signing-key: Flush key GC on key release Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 12/14] KEYS: trusted: Unregister key type on module exit in TEE backend Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 14/14] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-29 17:11 ` [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129170543.2915157-5-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=agordeev@linux.ibm.com \
    --cc=anna@kernel.org \
    --cc=bharathsm@microsoft.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=edumazet@kernel.org \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=horms@kernel.org \
    --cc=jmorris@namei.org \
    --cc=kabel@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=kuba@kernel.org \
    --cc=linkinjeon@kernel.org \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=marc.dionne@auristor.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=pc@manguebit.org \
    --cc=rdunlap@infradead.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=samba-technical@lists.samba.org \
    --cc=serge@hallyn.com \
    --cc=skhan@linuxfoundation.org \
    --cc=sprasad@microsoft.com \
    --cc=sumit.garg@kernel.org \
    --cc=svens@linux.ibm.com \
    --cc=tom@talpey.com \
    --cc=trondmy@kernel.org \
    --cc=willemb@google.com \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox