Skip to contentSkip to conversation

mount.cifs: fix buffer overrun in set_password

PATCH In review David Disseldorp 4 participants 8 messages Mar 31 – Apr 2

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

PATCH 1/1 mount.cifs: fix buffer overrun in set_password

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
 mount.cifs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +4 -4
index 1923913..d41ca6a 100644
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
 	unsigned int i = 0, j = 0;
 
 	while (src[i]) {
-		if (src[i] == ',')
-			dst[j++] = ',';
-		dst[j++] = src[i++];
-		if (j > pass_length) {
+		if (j + 2 >= pass_length) {
 			fprintf(stderr, "Converted password too long!\n");
 			return EX_USAGE;
 		}
+		if (src[i] == ',')
+			dst[j++] = ',';
+		dst[j++] = src[i++];
 	}
 	dst[j] = '\0';
 	if (is_pass2)
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 39 lines · David Disseldorp

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
 mount.cifs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +4 -4
index 1923913..d41ca6a 100644
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
 	unsigned int i = 0, j = 0;
 
 	while (src[i]) {
-		if (src[i] == ',')
-			dst[j++] = ',';
-		dst[j++] = src[i++];
-		if (j > pass_length) {
+		if (j + 2 >= pass_length) {
 			fprintf(stderr, "Converted password too long!\n");
 			return EX_USAGE;
 		}
+		if (src[i] == ',')
+			dst[j++] = ',';
+		dst[j++] = src[i++];
 	}
 	dst[j] = '\0';
 	if (is_pass2)
Signature · 1 line
2.51.0
#2 lore raw
In reply to David Disseldorp

added RB from Henrique and updated
git.samba.org/data/git/cifs-utils.git and
https://github.com/smfrench/smb3-utils for-next

Quoted 39 lines · David Disseldorp

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
 mount.cifs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +4 -5
index 1923913..d41ca6a 100644
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
        unsigned int i = 0, j = 0;
 
        while (src[i]) {
-               if (src[i] == ',')
-                       dst[j++] = ',';
-               dst[j++] = src[i++];
-               if (j > pass_length) {
+               if (j + 2 >= pass_length) {
                        fprintf(stderr, "Converted password too long!\n");
                        return EX_USAGE;
                }
+               if (src[i] == ',')
+                       dst[j++] = ',';
+               dst[j++] = src[i++];
        }
        dst[j] = '\0';
        if (is_pass2)
--

2.51.0

Signature · 3 lines
Thanks,

Steve
author Reviewed-by Henrique Carvalho Reviewed-by Henrique Carvalho
#3 lore raw
Quoted 3 lines · Steve French

added RB from Henrique and updated
git.samba.org/data/git/cifs-utils.git and
https://github.com/smfrench/smb3-utils for-next

Thanks Steve. FWIW, the reviewed-by tag has an incorrect email address:

Should be

I'll close the corresponding PR at
https://github.com/smfrench/smb3-utils/pull/16

Cheers, David

Quoted 42 lines · David Disseldorp
Quoted 39 lines

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
 mount.cifs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +4 -5
index 1923913..d41ca6a 100644
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
        unsigned int i = 0, j = 0;
 
        while (src[i]) {
-               if (src[i] == ',')
-                       dst[j++] = ',';
-               dst[j++] = src[i++];
-               if (j > pass_length) {
+               if (j + 2 >= pass_length) {
                        fprintf(stderr, "Converted password too long!\n");
                        return EX_USAGE;
                }
+               if (src[i] == ',')
+                       dst[j++] = ',';
+               dst[j++] = src[i++];
        }
        dst[j] = '\0';
        if (is_pass2)
--

2.51.0

#4 lore raw

Good catch. Fixed the RB tag

Quoted 60 lines · David Disseldorp
Quoted 3 lines · Steve French

added RB from Henrique and updated
git.samba.org/data/git/cifs-utils.git and
https://github.com/smfrench/smb3-utils for-next

Thanks Steve. FWIW, the reviewed-by tag has an incorrect email address:

Should be

I'll close the corresponding PR at
https://github.com/smfrench/smb3-utils/pull/16

Cheers, David

Quoted 42 lines · David Disseldorp
Quoted 39 lines

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
 mount.cifs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +4 -5
index 1923913..d41ca6a 100644
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
        unsigned int i = 0, j = 0;
 
        while (src[i]) {
-               if (src[i] == ',')
-                       dst[j++] = ',';
-               dst[j++] = src[i++];
-               if (j > pass_length) {
+               if (j + 2 >= pass_length) {
                        fprintf(stderr, "Converted password too long!\n");
                        return EX_USAGE;
                }
+               if (src[i] == ',')
+                       dst[j++] = ',';
+               dst[j++] = src[i++];
        }
        dst[j] = '\0';
        if (is_pass2)
--

2.51.0

Signature · 3 lines
Thanks,

Steve
#5 lore raw
In reply to David Disseldorp

Hi Dave,

Quoted 18 lines · David Disseldorp

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
mount.cifs.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +0 -0
index 1923913..d41ca6a 100644

mount.cifs.c set_password(struct parsed_mount_info *parsed_info, const char *src, · line 353

Quoted 8 lines
mount.cifs.c +1 -4
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
 	unsigned int i = 0, j = 0;
 
 	while (src[i]) {
-		if (src[i] == ',')
-			dst[j++] = ',';
-		dst[j++] = src[i++];
-		if (j > pass_length) {
+		if (j + 2 >= pass_length) {

There is the overrun bug, yes, but unconditionally accounting for comma expansion here will crop the password if it has at least 1 comma and (unparsed) length == MOUNT_PASSWD_SIZE, e.g.:

(password here is <511 * 'a'> + ',')

# echo -e "username=administrator\npassword=$(printf 'a%.0s' {1..511})," > /tmp/creds
# ./mount.cifs -o credentials=/tmp/creds //w25.vm.test/test /mnt/test
Converted password too long!
error 1 (Operation not permitted) opening credential file /tmp/creds

Maybe password/password2 could be malloc'd with size "strlen(src) + <number of commas in src>" and bounds checked against that? Just a thought.

Cheers,

Enzo

#6 lore raw
Quoted 42 lines · Enzo Matsumiya

Hi Dave,

Quoted 18 lines · David Disseldorp

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
mount.cifs.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +0 -0
index 1923913..d41ca6a 100644

mount.cifs.c set_password(struct parsed_mount_info *parsed_info, const char *src, · line 353

Quoted 8 lines
mount.cifs.c +1 -4
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
 	unsigned int i = 0, j = 0;
 
 	while (src[i]) {
-		if (src[i] == ',')
-			dst[j++] = ',';
-		dst[j++] = src[i++];
-		if (j > pass_length) {
+		if (j + 2 >= pass_length) {

There is the overrun bug, yes, but unconditionally accounting for comma expansion here will crop the password if it has at least 1 comma and (unparsed) length == MOUNT_PASSWD_SIZE, e.g.:

(password here is <511 * 'a'> + ',')

# echo -e "username=administrator\npassword=$(printf 'a%.0s' {1..511})," > /tmp/creds
# ./mount.cifs -o credentials=/tmp/creds //w25.vm.test/test /mnt/test
Converted password too long!
error 1 (Operation not permitted) opening credential file /tmp/creds
Quoted 4 lines

Maybe password/password2 could be malloc'd with size "strlen(src) + <number of commas in src>" and bounds checked against that? Just a thought.

Maybe we could have buffers double the size, but that would represent a behavior change. Passwords containing commas were never guaranteed to fit in current MOUNT_PASSWD_SIZE sized buffers.

I believe this would be a separate chage.

Quoted 5 lines

Cheers,

Enzo

#7 lore raw

Thanks for the feedback, Enzo and Henrique...

Quoted 35 lines · Henrique Carvalho
Quoted 34 lines · Enzo Matsumiya

Hi Dave,

Quoted 18 lines · David Disseldorp

The existing (j > pass_length) check is insufficient to avoid dst buffer overrun into the start of the adjacent struct parsed_mount_info field. Check for overrun before writing to dst, and account for comma-expansion and null-termination.

---
mount.cifs.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
mount.cifs.c +0 -0
index 1923913..d41ca6a 100644

mount.cifs.c set_password(struct parsed_mount_info *parsed_info, const char *src, · line 353

Quoted 8 lines
mount.cifs.c +1 -4
@@ -350,13 +350,13 @@ set_password(struct parsed_mount_info *parsed_info, const char *src,
 	unsigned int i = 0, j = 0;
 
 	while (src[i]) {
-		if (src[i] == ',')
-			dst[j++] = ',';
-		dst[j++] = src[i++];
-		if (j > pass_length) {
+		if (j + 2 >= pass_length) {  

There is the overrun bug, yes, but unconditionally accounting for comma expansion here will crop the password if it has at least 1 comma and (unparsed) length == MOUNT_PASSWD_SIZE, e.g.:

Quoted 6 lines
Quoted 6 lines

(password here is <511 * 'a'> + ',')

# echo -e "username=administrator\npassword=$(printf 'a%.0s' {1..511})," > /tmp/creds
# ./mount.cifs -o credentials=/tmp/creds //w25.vm.test/test /mnt/test
Converted password too long!
error 1 (Operation not permitted) opening credential file /tmp/creds

Yes, it's a valid point. I did consider it, but decided against it as:
- it makes the code slightly more complex, and adds more password-content based logic
- length restrictions are already opaque and confusing
+ "it broke when I changed password from 300x ':' to 300x ',' - why?"
- we're cropping the field any way (oversize pws were previously still passed to the kernel)

Quoted 8 lines
Quoted 4 lines

Maybe password/password2 could be malloc'd with size "strlen(src) + <number of commas in src>" and bounds checked against that? Just a thought.

Maybe we could have buffers double the size, but that would represent a behavior change. Passwords containing commas were never guaranteed to fit in current MOUNT_PASSWD_SIZE sized buffers.

These options would still leave us with password-content specific behaviour, which seems fragile (open to side-channels attacks, etc). Shouldn't the new mount API allow for the password to be passed though to the kernel individually, and in-turn completely avoid comma escaping?

Quoted 1 line

I believe this would be a separate chage.

Agreed.