Skip to contentSkip to conversation

scsi: target: minor CHAP fixes

PATCH Applied David Disseldorp 4 participants 7 messages Jun 5 – Jun 9

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

cover scsi: target: minor CHAP fixes

These patches resolve a couple of other issues pointed out by the sashiko bot when reviewing a separate CHAP change:
https://sashiko.dev/#/patchset/20260521151121.808477-1-hossu.alexandru%40gmail.com

Hexadecimal CHAP_I handling appears to have been broken for over a decade, so another option might be to pull out support for such encoded values.

----------------------------------------------------------------
David Disseldorp (2):
scsi: target: fix hexadecimal CHAP_I handling
scsi: target: use constant-time crypto_memneq for CHAP digests

 drivers/target/iscsi/iscsi_target_auth.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
#1 lore raw

PATCH 1/2 scsi: target: fix hexadecimal CHAP_I handling

A mutual CHAP handshake requires target processing of an initiator-sent CHAP_I identifier. The RFC 3720 specification states:

11.1.4. Challenge Handshake Authentication Protocol (CHAP)
...
CHAP_A=<A> CHAP_I=<I> CHAP_C=<C>
...
Where N, (A,A1,A2), I, C, and R are (correspondingly) the Name, Algorithm, Identifier, Challenge, and Response as defined in [RFC1994], N is a text string, A,A1,A2, and I are numbers

CHAP_I parsing currently calls extract_param(), which returns the @identifier string (stripped of any 0b/0B or 0x/0X prefix) and a @type which indicates DECIMAL, HEX or BASE64 encoding (based on any stripped prefix).

Any HEX encoded CHAP_I string is further processed via:
ret = kstrtoul(&identifier[2], 0, &id);
This is incorrect for two reasons:
* The @identifier string has already been stripped of the 0x/0X prefix, so skipping the first two bytes omits part of the number.
* The kstrtoul() call specifies a base of 0, which will see &identifier[2] parsed as a decimal, unless a '0x' or (octal) '0' is erroneously present at that offset.

Fix this by passing the (zero-offset) identifier string to kstrtoul() along with a base=16 parameter. Also add an explicit error handler for BASE64 encoding.

Hex-encoded CHAP_I handling can be testing using the libiscsi EncodedI test linked below.

Reported-by Sashiko (gemini/gemini-3.1-pro-preview)
Fixes c3fb804c12bad ("scsi: target: fix hexadecimal CHAP_I handling")
---
 drivers/target/iscsi/iscsi_target_auth.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/target/iscsi/iscsi_target_auth.c +4 -2
index a3ad2d244dbee..5858cc3089796 100644
@@ -438,9 +438,11 @@ static int chap_server_compute_hash(
 	}
 
 	if (type == HEX)
-		ret = kstrtoul(&identifier[2], 0, &id);
+		ret = kstrtoul(identifier, 16, &id);
+	else if (type == DECIMAL)
+		ret = kstrtoul(identifier, 10, &id);
 	else
-		ret = kstrtoul(identifier, 0, &id);
+		ret = -EINVAL;
 
 	if (ret < 0) {
 		pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
Signature · 1 line
2.51.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] scsi: target: fix hexadecimal CHAP_I handling

Quoted 61 lines · David Disseldorp

A mutual CHAP handshake requires target processing of an initiator-sent CHAP_I identifier. The RFC 3720 specification states:

11.1.4. Challenge Handshake Authentication Protocol (CHAP)
...
CHAP_A=<A> CHAP_I=<I> CHAP_C=<C>
...
Where N, (A,A1,A2), I, C, and R are (correspondingly) the Name, Algorithm, Identifier, Challenge, and Response as defined in [RFC1994], N is a text string, A,A1,A2, and I are numbers

CHAP_I parsing currently calls extract_param(), which returns the @identifier string (stripped of any 0b/0B or 0x/0X prefix) and a @type which indicates DECIMAL, HEX or BASE64 encoding (based on any stripped prefix).

Any HEX encoded CHAP_I string is further processed via:
ret = kstrtoul(&identifier[2], 0, &id);
This is incorrect for two reasons:
* The @identifier string has already been stripped of the 0x/0X prefix, so skipping the first two bytes omits part of the number.
* The kstrtoul() call specifies a base of 0, which will see &identifier[2] parsed as a decimal, unless a '0x' or (octal) '0' is erroneously present at that offset.

Fix this by passing the (zero-offset) identifier string to kstrtoul() along with a base=16 parameter. Also add an explicit error handler for BASE64 encoding.

Hex-encoded CHAP_I handling can be testing using the libiscsi EncodedI test linked below.

Reported-by Sashiko (gemini/gemini-3.1-pro-preview)
Fixes c3fb804c12bad ("scsi: target: fix hexadecimal CHAP_I handling")
---
 drivers/target/iscsi/iscsi_target_auth.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/target/iscsi/iscsi_target_auth.c +4 -3
index a3ad2d244dbee..5858cc3089796 100644
@@ -438,9 +438,11 @@ static int chap_server_compute_hash(
        }
 
        if (type == HEX)
-               ret = kstrtoul(&identifier[2], 0, &id);
+               ret = kstrtoul(identifier, 16, &id);
+       else if (type == DECIMAL)
+               ret = kstrtoul(identifier, 10, &id);
        else
-               ret = kstrtoul(identifier, 0, &id);
+               ret = -EINVAL;
 
        if (ret < 0) {
                pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
--
Quoted 3 lines

2.51.0

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] scsi: target: fix hexadecimal CHAP_I handling

In reply to David Disseldorp
Quoted 9 lines
---
  drivers/target/iscsi/iscsi_target_auth.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/target/iscsi/iscsi_target_auth.c +0 -0
index a3ad2d244dbee..5858cc3089796 100644

drivers/target/iscsi/iscsi_target_auth.c static int chap_server_compute_hash( · line 445

Quoted 10 lines
drivers/target/iscsi/iscsi_target_auth.c +4 -2
@@ -438,9 +438,11 @@ static int chap_server_compute_hash(
  	}
 
  	if (type == HEX)
-		ret = kstrtoul(&identifier[2], 0, &id);
+		ret = kstrtoul(identifier, 16, &id);
+	else if (type == DECIMAL)
+		ret = kstrtoul(identifier, 10, &id);
  	else
-		ret = kstrtoul(identifier, 0, &id);
+		ret = -EINVAL;

Only hex for decimal are supported, so I am not sure if much point in this check.

Regardless of that:

Quoted 3 lines
drivers/target/iscsi/iscsi_target_auth.c +0 -0
  	if (ret < 0) {
  		pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
#4 lore raw

PATCH 2/2 scsi: target: use constant-time crypto_memneq for CHAP digests

In reply to David Disseldorp

A constant-time memory comparison is more suitable than plain memcmp() for authentication digest comparison.
CHAP digests use an authenticator-provided random challenge, so any timing side-channel shouldn't be easily exploitable.

---
 drivers/target/iscsi/iscsi_target_auth.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/target/iscsi/iscsi_target_auth.c +2 -1
index 5858cc3089796..f3c0cdd318300 100644
@@ -9,6 +9,7 @@
  ******************************************************************************/
 
 #include <crypto/hash.h>
+#include <crypto/utils.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/err.h>
@@ -408,7 +409,7 @@ static int chap_server_compute_hash(
 	pr_debug("[server] %s Server Digest: %s\n",
 		chap->digest_name, response);
 
-	if (memcmp(server_digest, client_digest, chap->digest_size) != 0) {
+	if (crypto_memneq(server_digest, client_digest, chap->digest_size)) {
 		pr_debug("[server] %s Digests do not match!\n\n",
 			chap->digest_name);
 		goto out;
Signature · 1 line
2.51.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] scsi: target: use constant-time crypto_memneq for CHAP digests

Quoted 35 lines · David Disseldorp

A constant-time memory comparison is more suitable than plain memcmp() for authentication digest comparison.
CHAP digests use an authenticator-provided random challenge, so any timing side-channel shouldn't be easily exploitable.

---
 drivers/target/iscsi/iscsi_target_auth.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/target/iscsi/iscsi_target_auth.c +2 -2
index 5858cc3089796..f3c0cdd318300 100644
@@ -9,6 +9,7 @@
  ******************************************************************************/
 
 #include <crypto/hash.h>
+#include <crypto/utils.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/err.h>
@@ -408,7 +409,7 @@ static int chap_server_compute_hash(
        pr_debug("[server] %s Server Digest: %s\n",
                chap->digest_name, response);
 
-       if (memcmp(server_digest, client_digest, chap->digest_size) != 0) {
+       if (crypto_memneq(server_digest, client_digest, chap->digest_size)) {
                pr_debug("[server] %s Digests do not match!\n\n",
                        chap->digest_name);
                goto out;
--
Quoted 3 lines

2.51.0

#6 lore raw
In reply to David Disseldorp
Quoted 9 lines · David Disseldorp

These patches resolve a couple of other issues pointed out by the sashiko bot when reviewing a separate CHAP change:
https://sashiko.dev/#/patchset/20260521151121.808477-1-hossu.alexandru%40gmail.com

Hexadecimal CHAP_I handling appears to have been broken for over a decade, so another option might be to pull out support for such encoded values.

[...]

Applied to 7.1/scsi-fixes, thanks!

[1/2] scsi: target: fix hexadecimal CHAP_I handling

      https://git.kernel.org/mkp/scsi/c/7e161211f1dd
[2/2] scsi: target: use constant-time crypto_memneq for CHAP digests
      https://git.kernel.org/mkp/scsi/c/cf14fc2be868
Signature · 1 line
Martin K. Petersen