Skip to contentSkip to conversation

tls: return -EFAULT if copy_to_user() fails

PATCH Applied Dan Carpenter 4 participants 8 messages Jun 23, 2017

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

PATCH 1/1 tls: return -EFAULT if copy_to_user() fails

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

Fixes 3c4d7559159b ("tls: kernel TLS support")
net/tls/tls_main.c +6 -4
index 2ebc328bda96..a03130a47b85 100644
@@ -273,7 +273,8 @@ static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
 	}
 
 	if (len = sizeof(crypto_info)) {
-		rc = copy_to_user(optval, crypto_info, sizeof(*crypto_info));
+		if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
+			rc = -EFAULT;
 		goto out;
 	}
 
@@ -293,9 +294,10 @@ static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
 		memcpy(crypto_info_aes_gcm_128->iv, ctx->iv,
 		       TLS_CIPHER_AES_GCM_128_IV_SIZE);
 		release_sock(sk);
-		rc = copy_to_user(optval,
-				  crypto_info_aes_gcm_128,
-				  sizeof(*crypto_info_aes_gcm_128));
+		if (copy_to_user(optval,
+				 crypto_info_aes_gcm_128,
+				 sizeof(*crypto_info_aes_gcm_128)))
+			rc = -EFAULT;
 		break;
 	}
 	default:
#1 lore raw
Quoted 2 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

because?

#2 lore raw
Quoted 5 lines · Joe Perches
Quoted 2 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

because?

Because it's a failure path?

regards,
dan carpenter

#3 lore raw
In reply to Joe Perches
Quoted 6 lines · Joe Perches
Quoted 2 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

because?

Rhetorical questions don't work over email. Are you honestly confused by this patch?

regards,
dan carpenter

#4 lore raw
Quoted 10 lines · Dan Carpenter
Quoted 6 lines · Joe Perches
Quoted 2 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

because?

Rhetorical questions don't work over email. Are you honestly confused by this patch?

There doesn't seem to be a fault here, just a
return of less than the expected number of bytes.

It's non-obvious why -EFAULT is the appropriate
return value.

Why is changing the return value from number of
bytes transferred, even if less than desired,
the right thing to do? Your commit message
doesn't describe any rationale.

getsockopt says:

For getsockopt(), optlen is a value-result argument, initially containing the size of the buffer pointed to by optval, and modified on return to indicate the actual size of the value returned

The generic EFAULT description in getsockopt is:

EFAULT The address pointed to by optval is not in a valid part of the
  process address space. For getsockopt(), this error may also be returned if optlen is not in a valid part of the process address space.

Is tls different?

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 5 lines · Joe Perches

getsockopt says:

For getsockopt(), optlen is a value-result argument, initially containing the size of the buffer pointed to by optval, and modified on return to indicate the actual size of the value returned

In the original code, it's not returning the "actual size of the value returned". It's returning a smaller or equal value... The man page is correct that this is how some getsockopts work, of course. But here -EFAULT is expected.

regards,
dan carpenter

Acked-by on v1 1/1
#6 lore raw
In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

Fixes 3c4d7559159b ("tls: kernel TLS support")

Yes, -EFAULT seems like the correct choice here, the return from copy_to_user isn't useful. Thanks

Quoted 29 lines
net/tls/tls_main.c +6 -4
index 2ebc328bda96..a03130a47b85 100644
@@ -273,7 +273,8 @@ static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
 	}
 
 	if (len = sizeof(crypto_info)) {
-		rc = copy_to_user(optval, crypto_info, sizeof(*crypto_info));
+		if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
+			rc = -EFAULT;
 		goto out;
 	}
 
@@ -293,9 +294,10 @@ static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
 		memcpy(crypto_info_aes_gcm_128->iv, ctx->iv,
 		       TLS_CIPHER_AES_GCM_128_IV_SIZE);
 		release_sock(sk);
-		rc = copy_to_user(optval,
-				  crypto_info_aes_gcm_128,
-				  sizeof(*crypto_info_aes_gcm_128));
+		if (copy_to_user(optval,
+				 crypto_info_aes_gcm_128,
+				 sizeof(*crypto_info_aes_gcm_128)))
+			rc = -EFAULT;
 		break;
 	}
 	default:
maintainer Applied
#7 lore raw
In reply to Dan Carpenter

From: Dan Carpenter <[email protected]>
Date: Fri, 23 Jun 2017 13:15:44 +0300

Quoted 5 lines

The copy_to_user() function returns the number of bytes remaining but we want to return -EFAULT here.

Fixes 3c4d7559159b ("tls: kernel TLS support")

Dan, I happened to realize that tls is only in net-next, but please indicate the target tree properly in your Subject lines in the future.

Applied, thanks.