btrfs: replace strcpy() with strscpy()
authorBrahmajit Das <listout@listout.xyz>
Fri, 20 Jun 2025 16:49:57 +0000 (22:19 +0530)
committerDavid Sterba <dsterba@suse.com>
Mon, 21 Jul 2025 22:05:00 +0000 (00:05 +0200)
strcpy() is discouraged from use due to lack of bounds checking.
Replaces it with strscpy(), the recommended alternative for null
terminated strings, to follow best practices.

There are instances where strscpy() cannot be used such as where both
the source and destination are character pointers. In that instance we
can use sysfs_emit().

Link: https://github.com/KSPP/linux/issues/88
Suggested-by: Anthony Iliopoulos <ailiop@suse.com>
Signed-off-by: Brahmajit Das <bdas@suse.de>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c
fs/btrfs/relocation.c
fs/btrfs/send.c
fs/btrfs/volumes.c
fs/btrfs/xattr.c

index 3621ed2..c49df25 100644 (file)
@@ -4199,7 +4199,7 @@ static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
        }
 
        spin_lock(&fs_info->super_lock);
-       strcpy(super_block->label, label);
+       strscpy(super_block->label, label);
        spin_unlock(&fs_info->super_lock);
        ret = btrfs_commit_transaction(trans);
 
index 82080fe..175fc3a 100644 (file)
@@ -3888,7 +3888,7 @@ static void free_reloc_control(struct reloc_control *rc)
  */
 static void describe_relocation(struct btrfs_block_group *block_group)
 {
-       char buf[128] = {'\0'};
+       char buf[128] = "NONE";
 
        btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
 
index a045c1b..01aab5b 100644 (file)
@@ -758,7 +758,7 @@ static int send_header(struct send_ctx *sctx)
 {
        struct btrfs_stream_header hdr;
 
-       strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
+       strscpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
        hdr.version = cpu_to_le32(sctx->proto);
        return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
                                        &sctx->send_off);
index c99aec9..714ebbd 100644 (file)
@@ -214,10 +214,8 @@ void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
        u64 flags = bg_flags;
        u32 size_bp = size_buf;
 
-       if (!flags) {
-               strcpy(bp, "NONE");
+       if (!flags)
                return;
-       }
 
 #define DESCRIBE_FLAG(flag, desc)                                              \
        do {                                                            \
index 3e0edbc..79fb161 100644 (file)
@@ -510,14 +510,15 @@ static int btrfs_initxattrs(struct inode *inode,
         */
        nofs_flag = memalloc_nofs_save();
        for (xattr = xattr_array; xattr->name != NULL; xattr++) {
-               name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
-                              strlen(xattr->name) + 1, GFP_KERNEL);
+               const size_t name_len = XATTR_SECURITY_PREFIX_LEN +
+                                       strlen(xattr->name) + 1;
+
+               name = kmalloc(name_len, GFP_KERNEL);
                if (!name) {
                        ret = -ENOMEM;
                        break;
                }
-               strcpy(name, XATTR_SECURITY_PREFIX);
-               strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
+               scnprintf(name, name_len, "%s%s", XATTR_SECURITY_PREFIX, xattr->name);
 
                if (strcmp(name, XATTR_NAME_CAPS) == 0)
                        clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);