Skip to content

Commit c82f77a

Browse files
tobluxtyhicks
authored andcommitted
ecryptfs: Replace strcpy with strscpy in ecryptfs_cipher_code_to_string
strcpy() has been deprecated [1] because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Since the parameter 'char *str' is just a pointer with no size information, extend the function with a 'size' parameter to pass the destination buffer's size as an additional argument. Adjust the call sites accordingly. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1] Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Signed-off-by: Tyler Hicks <code@tyhicks.com>
1 parent 3bdc6ca commit c82f77a

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

fs/ecryptfs/crypto.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,19 +862,21 @@ u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
862862
/**
863863
* ecryptfs_cipher_code_to_string
864864
* @str: Destination to write out the cipher name
865+
* @size: Destination buffer size
865866
* @cipher_code: The code to convert to cipher name string
866867
*
867868
* Returns zero on success
868869
*/
869-
int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
870+
int ecryptfs_cipher_code_to_string(char *str, size_t size, u8 cipher_code)
870871
{
871872
int rc = 0;
872873
int i;
873874

874875
str[0] = '\0';
875876
for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
876877
if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
877-
strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
878+
strscpy(str, ecryptfs_cipher_code_str_map[i].cipher_str,
879+
size);
878880
if (str[0] == '\0') {
879881
ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
880882
"[%d]\n", cipher_code);

fs/ecryptfs/ecryptfs_kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ int ecryptfs_read_and_validate_header_region(struct inode *inode);
571571
int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
572572
struct inode *inode);
573573
u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes);
574-
int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code);
574+
int ecryptfs_cipher_code_to_string(char *str, size_t size, u8 cipher_code);
575575
void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat);
576576
int ecryptfs_generate_key_packet_set(char *dest_base,
577577
struct ecryptfs_crypt_stat *crypt_stat,

fs/ecryptfs/keystore.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,9 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
911911
s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
912912
(*packet_size) += ECRYPTFS_SIG_SIZE;
913913
s->cipher_code = data[(*packet_size)++];
914-
rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
914+
rc = ecryptfs_cipher_code_to_string(s->cipher_string,
915+
sizeof(s->cipher_string),
916+
s->cipher_code);
915917
if (rc) {
916918
printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
917919
__func__, s->cipher_code);
@@ -1129,7 +1131,9 @@ decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
11291131
memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
11301132
auth_tok->session_key.decrypted_key_size);
11311133
crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
1132-
rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
1134+
rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1135+
sizeof(crypt_stat->cipher),
1136+
cipher_code);
11331137
if (rc) {
11341138
ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
11351139
cipher_code);
@@ -1395,6 +1399,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
13951399
goto out_free;
13961400
}
13971401
rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
1402+
sizeof(crypt_stat->cipher),
13981403
(u16)data[(*packet_size)]);
13991404
if (rc)
14001405
goto out_free;

0 commit comments

Comments
 (0)