Skip to content

Commit 978a271

Browse files
scsi: target: Replace deprecated strncpy() with strscpy()
JIRA: https://issues.redhat.com/browse/RHEL-111938 strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead. The destination buffer db_root is only used with "%s" format strings and must therefore be NUL-terminated, but not NUL-padded. Use scnprintf() because snprintf() could return a value >= DB_ROOT_LEN and lead to an out-of-bounds access. This doesn't happen because count is explicitly checked against DB_ROOT_LEN before. However, scnprintf() always returns the number of characters actually written to the string buffer, which is always within the bounds of db_root_stage, and should be preferred over snprintf(). The size parameter of strscpy() is optional and since DB_ROOT_LEN is the size of the destination buffer, it can be removed. Remove it to simplify the code. Compile-tested only. Link: KSPP/linux#90 Link: KSPP/linux#105 Cc: linux-hardening@vger.kernel.org Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Link: https://lore.kernel.org/r/20250302225641.245127-2-thorsten.blum@linux.dev Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> (cherry picked from commit dfb7df1) Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
1 parent e80c982 commit 978a271

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/target/target_core_configfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static ssize_t target_core_item_dbroot_store(struct config_item *item,
123123
goto unlock;
124124
}
125125

126-
read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
126+
read_bytes = scnprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
127127
if (!read_bytes)
128128
goto unlock;
129129

@@ -143,7 +143,7 @@ static ssize_t target_core_item_dbroot_store(struct config_item *item,
143143
}
144144
filp_close(fp, NULL);
145145

146-
strncpy(db_root, db_root_stage, read_bytes);
146+
strscpy(db_root, db_root_stage);
147147
pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
148148

149149
r = read_bytes;
@@ -3664,7 +3664,7 @@ static void target_init_dbroot(void)
36643664
}
36653665
filp_close(fp, NULL);
36663666

3667-
strncpy(db_root, db_root_stage, DB_ROOT_LEN);
3667+
strscpy(db_root, db_root_stage);
36683668
pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
36693669
}
36703670

0 commit comments

Comments
 (0)