Skip to contentSkip to conversation

SUNRPC: reject oversized legacy GSS init keys

PATCH New Jiwoong Wi 1 participant 2 messages Dec 2

Subsystems (3)
lore
Share & tools
lore

Jiwoong Wi

author
#0 lore raw

PATCH 1/2 SUNRPC: reject oversized legacy GSS init keys

rsi_request() encodes the context handle and input token as two hex fields in a single PAGE_SIZE upcall buffer. A key that exceeds the buffer cannot be delivered to the userspace GSS helper, but svcauth_gss_legacy_init() first copies the token and rsi_lookup() retains it in the cache. Distinct oversized keys can thus leave unserviceable cache entries and consume memory.

Check the encoded key length before allocating the input token. Account for both "\\x" prefixes and field separators, while accepting an exact-fit upcall.

On a 4 KiB arm64 QEMU kernel, the legacy channel accepted exact-fit 4096-byte upcalls for INIT and CONTINUE_INIT. In both cases, a key one byte over the limit was rejected before rsi_alloc() and did not reach the channel. No KASAN, lockdep, Oops, or panic report occurred.

The vulnerability research and validation were conducted by members of the Tobabz team as part of the Best of the Best 15th program.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/sunrpc/auth_gss/svcauth_gss.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
net/sunrpc/auth_gss/svcauth_gss.c +14 -0
index a7e6edbbc42c..ce84a58bb22b 100644
@@ -95,6 +95,8 @@ static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
 
 #define	RSI_HASHBITS	6
 #define	RSI_HASHMAX	(1<<RSI_HASHBITS)
+/* Two "\\x" prefixes and two trailing separators. */
+#define	RSI_UPCALL_KEY_OVERHEAD	6
 
 struct rsi {
 	struct cache_head	h;
@@ -107,6 +109,14 @@ struct rsi {
 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
 static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
 
+static bool rsi_key_too_large(unsigned int handle_len,
+			      unsigned int token_len)
+{
+	unsigned int max_len = (PAGE_SIZE - RSI_UPCALL_KEY_OVERHEAD) / 2;
+
+	return handle_len > max_len || token_len > max_len - handle_len;
+}
+
 static void rsi_free(struct rsi *rsii)
 {
 	kfree(rsii->in_handle.data);
@@ -1177,6 +1187,10 @@ svcauth_gss_legacy_init(struct svc_rqst *rqstp,
 		kfree(rsikey.in_handle.data);
 		return SVC_DENIED;
 	}
+	if (rsi_key_too_large(rsikey.in_handle.len, len)) {
+		kfree(rsikey.in_handle.data);
+		return SVC_DENIED;
+	}
 	p = xdr_inline_decode(xdr, len);
 	if (!p) {
 		kfree(rsikey.in_handle.data);

base-commit: 86df59b69d8fbacb9ba5aae22291d472f02b15a0

Signature · 1 line
2.50.1 (Apple Git-155)
author
#1 lore raw

PATCH 2/2 SUNRPC: avoid GSS init cache entries without a writer

The legacy RPCSEC_GSS init cache retains each new context-handle and input-token key before checking whether userspace can process its upcall. An unauthenticated client can send distinct keys and exhaust memory when no GSS helper is running. New keys can also accumulate during the 30-second daemon-restart grace period.

Require an open channel writer before inserting a new GSS init cache entry. Look up existing keys first, and serialize writer open and close with the admission check under hash_lock. Apply the admission rule only to the GSS init cache.

When a new key is refused during the restart grace period, drop the RPC request without closing its connection so the client can retransmit after the helper returns. Preserve the existing close behavior if the channel was never opened, the grace period expired, or allocation failed. Report the no-writer rejection from the admission path to distinguish it from allocation failure.

Validated the full series on nfsd-testing with a 4 KiB arm64 kernel built by GCC 13.3 with CONFIG_WERROR=y, Generic KASAN, and lockdep. The changed objects passed W=1. In QEMU, an NFSv4.2 sec=krb5 mount remained pending for 25 seconds with zero rsi_alloc() calls while rpc.svcgssd was closed. After the helper reopened, the same mount and read/write I/O succeeded. A fresh request after the restart grace period closed without allocating a cache entry. A cache API module passed 41 checks. No KASAN, lockdep, Oops, or panic report occurred.

The vulnerability research and validation were conducted by members of the Tobabz team as part of the Best of the Best 15th program.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/linux/sunrpc/cache.h      |  5 ++
 net/sunrpc/auth_gss/svcauth_gss.c | 17 ++++---
 net/sunrpc/cache.c                | 77 ++++++++++++++++++++++++-------
 3 files changed, 77 insertions(+), 22 deletions(-)
include/linux/sunrpc/cache.h +5 -0
index 2735c332ddb7..32b58613f24c 100644
@@ -71,6 +71,7 @@ enum {
 struct cache_detail {
 	struct module *		owner;
 	int			hash_size;
+	bool			require_writer; /* for new cache entries */
 	struct hlist_head *	hash_table;
 	spinlock_t		hash_lock;
 
@@ -187,6 +188,10 @@ extern const struct file_operations cache_flush_operations_pipefs;
 extern struct cache_head *
 sunrpc_cache_lookup_rcu(struct cache_detail *detail,
 			struct cache_head *key, int hash);
+struct cache_head *
+sunrpc_cache_lookup_rcu_with_grace(struct cache_detail *detail,
+				   struct cache_head *key, int hash,
+				   bool *restart_grace);
 extern struct cache_head *
 sunrpc_cache_update(struct cache_detail *detail,
 		    struct cache_head *new, struct cache_head *old, int hash);
net/sunrpc/auth_gss/svcauth_gss.c +11 -6
index ce84a58bb22b..4ec48ce0f060 100644
@@ -107,7 +107,8 @@ struct rsi {
 };
 
 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
-static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
+static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item,
+			      bool *restart_grace);
 
 static bool rsi_key_too_large(unsigned int handle_len,
 			      unsigned int token_len)
@@ -262,7 +263,7 @@ static int rsi_parse(struct cache_detail *cd,
 	if (dup_to_netobj(&rsii.in_token, buf, len))
 		goto out;
 
-	rsip = rsi_lookup(cd, &rsii);
+	rsip = rsi_lookup(cd, &rsii, NULL);
 	if (!rsip)
 		goto out;
 
@@ -318,6 +319,7 @@ static int rsi_parse(struct cache_detail *cd,
 static const struct cache_detail rsi_cache_template = {
 	.owner		= THIS_MODULE,
 	.hash_size	= RSI_HASHMAX,
+	.require_writer = true,
 	.name           = "auth.rpcsec.init",
 	.cache_put      = rsi_put,
 	.cache_upcall	= rsi_upcall,
@@ -329,12 +331,14 @@ static const struct cache_detail rsi_cache_template = {
 	.alloc		= rsi_alloc,
 };
 
-static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
+static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item,
+			      bool *restart_grace)
 {
 	struct cache_head *ch;
 	int hash = rsi_hash(item);
 
-	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
+	ch = sunrpc_cache_lookup_rcu_with_grace(cd, &item->h, hash,
+						restart_grace);
 	if (ch)
 		return container_of(ch, struct rsi, h);
 	else
@@ -1170,6 +1174,7 @@ svcauth_gss_legacy_init(struct svc_rqst *rqstp,
 	__be32 *p;
 	u32 len;
 	int ret;
+	bool restart_grace;
 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
 
 	memset(&rsikey, 0, sizeof(rsikey));
@@ -1205,10 +1210,10 @@ svcauth_gss_legacy_init(struct svc_rqst *rqstp,
 	rsikey.in_token.len = len;
 
 	/* Perform upcall, or find upcall result: */
-	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
+	rsip = rsi_lookup(sn->rsi_cache, &rsikey, &restart_grace);
 	rsi_free(&rsikey);
 	if (!rsip)
-		return SVC_CLOSE;
+		return restart_grace ? SVC_DROP : SVC_CLOSE;
 	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
 		/* No upcall result: */
 		return SVC_CLOSE;
net/sunrpc/cache.c +61 -16

125 lines not on this page · raw message

Signature · 1 line
2.50.1 (Apple Git-155)