Skip to contentSkip to conversation

rxrpc: fix handling of an unsupported token type in rxrpc_read()

PATCH In review [email protected] 2 participants 10 messages Dec 29, 2020 – Jan 6, 2021

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

PATCH 1/1 rxrpc: fix handling of an unsupported token type in rxrpc_read()

From: Tom Rix <[email protected]>

clang static analysis reports this problem

net/rxrpc/key.c:657:11: warning: Assigned value is garbage or undefined

                toksize = toksizes[tok++];
                        ^ ~~~~~~~~~~~~~~~

rxrpc_read() contains two loops. The first loop calculates the token sizes and stores the results in toksizes[] and the second one uses the array. When there is an error in identifying the token in the first loop, the token is skipped, no change is made to the toksizes[] array. When the same error happens in the second loop, the token is not skipped. This will cause the toksizes[] array to be out of step and will overrun past the calculated sizes.

Change the error handling in the second loop to be consistent with the first. Simplify the error handling to an if check.

Fixes 9a059cd5ca7d ("rxrpc: Downgrade the BUG() for unsupported token type in rxrpc_read()")
---
 net/rxrpc/key.c | 48 ++++++++++++++++++++++--------------------------
 1 file changed, 22 insertions(+), 26 deletions(-)
net/rxrpc/key.c +22 -26
index 9631aa8543b5..eea877ee6ab3 100644
@@ -587,20 +587,19 @@ static long rxrpc_read(const struct key *key,
 	for (token = key->payload.data[0]; token; token = token->next) {
 		toksize = 4;	/* sec index */
 
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:
-			toksize += 8 * 4;	/* viceid, kvno, key*2, begin,
-						 * end, primary, tktlen */
-			if (!token->no_leak_key)
-				toksize += RND(token->kad->ticket_len);
-			break;
-
-		default: /* we have a ticket we can't encode */
+		if (token->security_index != RXRPC_SECURITY_RXKAD) {
+			/* we have a ticket we can't encode */
 			pr_err("Unsupported key token type (%u)\n",
 			       token->security_index);
 			continue;
 		}
 
+		/* viceid, kvno, key*2, begin, end, primary, tktlen */
+		toksize += 8 * 4;
+
+		if (!token->no_leak_key)
+			toksize += RND(token->kad->ticket_len);
+
 		_debug("token[%u]: toksize=%u", ntoks, toksize);
 		ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
 
@@ -654,28 +653,25 @@ static long rxrpc_read(const struct key *key,
 
 	tok = 0;
 	for (token = key->payload.data[0]; token; token = token->next) {
+		/* error reported above */
+		if (token->security_index != RXRPC_SECURITY_RXKAD)
+			continue;
+
 		toksize = toksizes[tok++];
 		ENCODE(toksize);
 		oldxdr = xdr;
 		ENCODE(token->security_index);
 
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:
-			ENCODE(token->kad->vice_id);
-			ENCODE(token->kad->kvno);
-			ENCODE_BYTES(8, token->kad->session_key);
-			ENCODE(token->kad->start);
-			ENCODE(token->kad->expiry);
-			ENCODE(token->kad->primary_flag);
-			if (token->no_leak_key)
-				ENCODE(0);
-			else
-				ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
-			break;
-
-		default:
-			break;
-		}
+		ENCODE(token->kad->vice_id);
+		ENCODE(token->kad->kvno);
+		ENCODE_BYTES(8, token->kad->session_key);
+		ENCODE(token->kad->start);
+		ENCODE(token->kad->expiry);
+		ENCODE(token->kad->primary_flag);
+		if (token->no_leak_key)
+			ENCODE(0);
+		else
+			ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
 
 		ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
 			  toksize);
Signature · 1 line
2.27.0
maintainer
#1 lore raw

net/rxrpc/key.c static long rxrpc_read(const struct key *key,

Quoted 5 lines · [email protected] wrote:
net/rxrpc/key.c +0 -4
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:
...
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:

These switches need to be kept. There's another security type on the way. See:

https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=rxrpc-rxgk

for example. I'll have a look later.

David

author
#2 lore raw
Quoted 10 lines · David Howells

net/rxrpc/key.c static long rxrpc_read(const struct key *key,

Quoted 5 lines · [email protected] wrote:
net/rxrpc/key.c +0 -4
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:
...
-		switch (token->security_index) {
-		case RXRPC_SECURITY_RXKAD:

These switches need to be kept. There's another security type on the way. See:

Quoted 3 lines

https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=rxrpc-rxgk

for example. I'll have a look later.

Yes, looks like more stuff is coming.

Thanks!

Tom

Quoted 2 lines

David

maintainer
#3 lore raw

How about this?

David
---
commit 5d370a9db65a6fae82f09a009430ae40c564b0ef

Author: David Howells <[email protected]>
Date:   Wed Jan 6 16:21:40 2021 +0000

rxrpc: Fix handling of an unsupported token type in rxrpc_read()

Clang static analysis reports the following:

net/rxrpc/key.c:657:11: warning: Assigned value is garbage or undefined
toksize = toksizes[tok++];
^ ~~~~~~~~~~~~~~~

rxrpc_read() contains two consecutive loops. The first loop calculates the token sizes and stores the results in toksizes[] and the second one uses the array. When there is an error in identifying the token in the first loop, the token is skipped, no change is made to the toksizes[] array. When the same error happens in the second loop, the token is not skipped. This will cause the toksizes[] array to be out of step and will overrun past the calculated sizes.

Fix the second loop so that it doesn't encode the size and type of an unsupported token, but rather just ignore it as does the first loop.

Fixes 9a059cd5ca7d ("rxrpc: Downgrade the BUG() for unsupported token type in rxrpc_read()")
net/rxrpc/key.c +2 -2
index 9631aa8543b5..c8e298c8d314 100644
@@ -655,12 +655,12 @@ static long rxrpc_read(const struct key *key,
 	tok = 0;
 	for (token = key->payload.data[0]; token; token = token->next) {
 		toksize = toksizes[tok++];
-		ENCODE(toksize);
 		oldxdr = xdr;
-		ENCODE(token->security_index);
 
 		switch (token->security_index) {
 		case RXRPC_SECURITY_RXKAD:
+			ENCODE(toksize);
+			ENCODE(token->security_index);
 			ENCODE(token->kad->vice_id);
 			ENCODE(token->kad->kvno);
 			ENCODE_BYTES(8, token->kad->session_key);
maintainer
#4 lore raw
Quoted 4 lines · David Howells

How about this?
...
Fix the second loop so that it doesn't encode the size and type of an unsupported token, but rather just ignore it as does the first loop.

Actually, a better way is probably just to error out in this case. This should only happen if a new token type is incompletely implemented.

David
---
commit e68ef16f59aa57564761b21e5ecb2ebbd72d1c57

Author: David Howells <[email protected]>
Date:   Wed Jan 6 16:21:40 2021 +0000

rxrpc: Fix handling of an unsupported token type in rxrpc_read()

Clang static analysis reports the following:

net/rxrpc/key.c:657:11: warning: Assigned value is garbage or undefined
toksize = toksizes[tok++];
^ ~~~~~~~~~~~~~~~

rxrpc_read() contains two consecutive loops. The first loop calculates the token sizes and stores the results in toksizes[] and the second one uses the array. When there is an error in identifying the token in the first loop, the token is skipped, no change is made to the toksizes[] array. When the same error happens in the second loop, the token is not skipped. This will cause the toksizes[] array to be out of step and will overrun past the calculated sizes.

Fix this by making both loops log a message and return an error in this case. This should only happen if a new token type is incompletely implemented, so it should normally be impossible to trigger this.

Fixes 9a059cd5ca7d ("rxrpc: Downgrade the BUG() for unsupported token type in rxrpc_read()")
net/rxrpc/key.c +4 -2
index 9631aa8543b5..8d2073e0e3da 100644
@@ -598,7 +598,7 @@ static long rxrpc_read(const struct key *key,
 		default: /* we have a ticket we can't encode */
 			pr_err("Unsupported key token type (%u)\n",
 			       token->security_index);
-			continue;
+			return -ENOPKG;
 		}
 
 		_debug("token[%u]: toksize=%u", ntoks, toksize);
@@ -674,7 +674,9 @@ static long rxrpc_read(const struct key *key,
 			break;
 
 		default:
-			break;
+			pr_err("Unsupported key token type (%u)\n",
+			       token->security_index);
+			return -ENOPKG;
 		}
 
 		ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
author
#5 lore raw
Quoted 44 lines · David Howells
Quoted 4 lines · David Howells

How about this?
...
Fix the second loop so that it doesn't encode the size and type of an unsupported token, but rather just ignore it as does the first loop.

Actually, a better way is probably just to error out in this case. This should only happen if a new token type is incompletely implemented.

David
---
commit e68ef16f59aa57564761b21e5ecb2ebbd72d1c57

Author: David Howells <[email protected]>
Date:   Wed Jan 6 16:21:40 2021 +0000

rxrpc: Fix handling of an unsupported token type in rxrpc_read()

Clang static analysis reports the following:

net/rxrpc/key.c:657:11: warning: Assigned value is garbage or undefined
toksize = toksizes[tok++];
^ ~~~~~~~~~~~~~~~

rxrpc_read() contains two consecutive loops. The first loop calculates the token sizes and stores the results in toksizes[] and the second one uses the array. When there is an error in identifying the token in the first loop, the token is skipped, no change is made to the toksizes[] array. When the same error happens in the second loop, the token is not skipped. This will cause the toksizes[] array to be out of step and will overrun past the calculated sizes.

Fix this by making both loops log a message and return an error in this case. This should only happen if a new token type is incompletely implemented, so it should normally be impossible to trigger this.

Fixes 9a059cd5ca7d ("rxrpc: Downgrade the BUG() for unsupported token type in rxrpc_read()")
net/rxrpc/key.c +0 -0
index 9631aa8543b5..8d2073e0e3da 100644

net/rxrpc/key.c static long rxrpc_read(const struct key *key, · line 602

Quoted 6 lines
net/rxrpc/key.c +1 -1
@@ -598,7 +598,7 @@ static long rxrpc_read(const struct key *key,
 		default: /* we have a ticket we can't encode */
 			pr_err("Unsupported key token type (%u)\n",
 			       token->security_index);
-			continue;
+			return -ENOPKG;
 		}

These two loops iterate over the same data, i believe returning here is all that is needed.

Tom

Quoted 14 lines
net/rxrpc/key.c +3 -1
 		_debug("token[%u]: toksize=%u", ntoks, toksize);
@@ -674,7 +674,9 @@ static long rxrpc_read(const struct key *key,
 			break;
 
 		default:
-			break;
+			pr_err("Unsupported key token type (%u)\n",
+			       token->security_index);
+			return -ENOPKG;
 		}
 
 		ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
maintainer
#6 lore raw
Quoted 2 lines · Tom Rix

These two loops iterate over the same data, i believe returning here is all that is needed.

But if the first loop is made to support a new type, but the second loop is missed, it will then likely oops. Besides, the compiler should optimise both paths together.

David

author
#7 lore raw
Quoted 7 lines · David Howells
Quoted 2 lines · Tom Rix

These two loops iterate over the same data, i believe returning here is all that is needed.

But if the first loop is made to support a new type, but the second loop is missed, it will then likely oops. Besides, the compiler should optimise both paths together.

You are right, I was only considering the existing cases.

Tom

Quoted 2 lines

David

maintainer
#8 lore raw
Quoted 10 lines · Tom Rix
Quoted 7 lines · David Howells
Quoted 2 lines · Tom Rix

These two loops iterate over the same data, i believe returning here is all that is needed.

But if the first loop is made to support a new type, but the second loop is missed, it will then likely oops. Besides, the compiler should optimise both paths together.

You are right, I was only considering the existing cases.

Thanks. Can I put that down as a Reviewed-by?

David

author Reviewed-by
#9 lore raw
Quoted 5 lines · David Howells
Quoted 3 lines · Tom Rix
Quoted 2 lines · David Howells

Tom Rix <[email protected]> wrote:

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

These two loops iterate over the same data, i believe returning here is all that is needed.

But if the first loop is made to support a new type, but the second loop is missed, it will then likely oops. Besides, the compiler should optimise both paths together.

You are right, I was only considering the existing cases.

Thanks. Can I put that down as a Reviewed-by?

Yes, please.

Quoted 3 lines

David