1
0
Fork 0
mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2025-01-23 16:53:58 -05:00

net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()

ctx->crypto_send.info is not protected by lock_sock in
do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
and error paths of do_tls_setsockopt_conf() may lead to a use-after-free
or null-deref.

More discussion:  https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/

Fixes: 3c4d755915 ("tls: kernel TLS support")
Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
Link: https://lore.kernel.org/r/20230228023344.9623-1-hbh25y@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Hangyu Hua 2023-02-28 10:33:44 +08:00 committed by Jakub Kicinski
parent 81563d8548
commit 49c47cc21b

View file

@ -405,13 +405,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(crypto_info_aes_gcm_128->iv, memcpy(crypto_info_aes_gcm_128->iv,
cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
TLS_CIPHER_AES_GCM_128_IV_SIZE); TLS_CIPHER_AES_GCM_128_IV_SIZE);
memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq, memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq,
TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, if (copy_to_user(optval,
crypto_info_aes_gcm_128, crypto_info_aes_gcm_128,
sizeof(*crypto_info_aes_gcm_128))) sizeof(*crypto_info_aes_gcm_128)))
@ -429,13 +427,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(crypto_info_aes_gcm_256->iv, memcpy(crypto_info_aes_gcm_256->iv,
cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE, cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
TLS_CIPHER_AES_GCM_256_IV_SIZE); TLS_CIPHER_AES_GCM_256_IV_SIZE);
memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq, memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq,
TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, if (copy_to_user(optval,
crypto_info_aes_gcm_256, crypto_info_aes_gcm_256,
sizeof(*crypto_info_aes_gcm_256))) sizeof(*crypto_info_aes_gcm_256)))
@ -451,13 +447,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(aes_ccm_128->iv, memcpy(aes_ccm_128->iv,
cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE, cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE,
TLS_CIPHER_AES_CCM_128_IV_SIZE); TLS_CIPHER_AES_CCM_128_IV_SIZE);
memcpy(aes_ccm_128->rec_seq, cctx->rec_seq, memcpy(aes_ccm_128->rec_seq, cctx->rec_seq,
TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128))) if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128)))
rc = -EFAULT; rc = -EFAULT;
break; break;
@ -472,13 +466,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(chacha20_poly1305->iv, memcpy(chacha20_poly1305->iv,
cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE, cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE,
TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE); TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq, memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq,
TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE); TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, chacha20_poly1305, if (copy_to_user(optval, chacha20_poly1305,
sizeof(*chacha20_poly1305))) sizeof(*chacha20_poly1305)))
rc = -EFAULT; rc = -EFAULT;
@ -493,13 +485,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(sm4_gcm_info->iv, memcpy(sm4_gcm_info->iv,
cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE, cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE,
TLS_CIPHER_SM4_GCM_IV_SIZE); TLS_CIPHER_SM4_GCM_IV_SIZE);
memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq, memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq,
TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE); TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info))) if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info)))
rc = -EFAULT; rc = -EFAULT;
break; break;
@ -513,13 +503,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(sm4_ccm_info->iv, memcpy(sm4_ccm_info->iv,
cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE, cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE,
TLS_CIPHER_SM4_CCM_IV_SIZE); TLS_CIPHER_SM4_CCM_IV_SIZE);
memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq, memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq,
TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE); TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info))) if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info)))
rc = -EFAULT; rc = -EFAULT;
break; break;
@ -535,13 +523,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(crypto_info_aria_gcm_128->iv, memcpy(crypto_info_aria_gcm_128->iv,
cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE, cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE,
TLS_CIPHER_ARIA_GCM_128_IV_SIZE); TLS_CIPHER_ARIA_GCM_128_IV_SIZE);
memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq, memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq,
TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE); TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, if (copy_to_user(optval,
crypto_info_aria_gcm_128, crypto_info_aria_gcm_128,
sizeof(*crypto_info_aria_gcm_128))) sizeof(*crypto_info_aria_gcm_128)))
@ -559,13 +545,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
lock_sock(sk);
memcpy(crypto_info_aria_gcm_256->iv, memcpy(crypto_info_aria_gcm_256->iv,
cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE, cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE,
TLS_CIPHER_ARIA_GCM_256_IV_SIZE); TLS_CIPHER_ARIA_GCM_256_IV_SIZE);
memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq, memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq,
TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE); TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE);
release_sock(sk);
if (copy_to_user(optval, if (copy_to_user(optval,
crypto_info_aria_gcm_256, crypto_info_aria_gcm_256,
sizeof(*crypto_info_aria_gcm_256))) sizeof(*crypto_info_aria_gcm_256)))
@ -614,11 +598,9 @@ static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
if (len < sizeof(value)) if (len < sizeof(value))
return -EINVAL; return -EINVAL;
lock_sock(sk);
value = -EINVAL; value = -EINVAL;
if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
value = ctx->rx_no_pad; value = ctx->rx_no_pad;
release_sock(sk);
if (value < 0) if (value < 0)
return value; return value;
@ -635,6 +617,8 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
{ {
int rc = 0; int rc = 0;
lock_sock(sk);
switch (optname) { switch (optname) {
case TLS_TX: case TLS_TX:
case TLS_RX: case TLS_RX:
@ -651,6 +635,9 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
rc = -ENOPROTOOPT; rc = -ENOPROTOOPT;
break; break;
} }
release_sock(sk);
return rc; return rc;
} }