rxrpc: fix handling of an unsupported token type in rxrpc_read()
Subsystems (3)
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.
--- net/rxrpc/key.c | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-)
net/rxrpc/key.c +22 -26
@@ -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
David Howells
maintainernet/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
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
David Howells
maintainerHow 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.
net/rxrpc/key.c +2 -2
@@ -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);
David Howells
maintainerQuoted 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.
net/rxrpc/key.c +4 -2
@@ -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, ==,
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
---
commite68ef16f59aa57564761b21e5ecb2ebbd72d1c57Author: David Howells <[email protected]> Date: Wed Jan 6 16:21:40 2021 +0000rxrpc: 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()")Reported-by Tom Rix <[email protected]>Signed-off-by David Howells <[email protected]>net/rxrpc/key.c +0
-0
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, ==,
David Howells
maintainerQuoted 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
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
David Howells
maintainerQuoted 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
Tom Rix
Reviewed-byQuoted 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