Linux Hardening
 help / color / mirror / Atom feed
From: Mark Harmstone <mark@harmstone.com>
To: Brahmajit Das <listout@listout.xyz>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-btrfs@vger.kernel.org
Cc: clm@fb.com, josef@toxicpanda.com, dsterba@suse.com,
	kees@kernel.org, ailiop@suse.com
Subject: Re: [PATCH v2] btrfs: replace deprecated strcpy with strscpy
Date: Thu, 19 Jun 2025 18:06:12 +0100	[thread overview]
Message-ID: <b8945d37-3eb9-4ad6-b3eb-2725dbb008ad@harmstone.com> (raw)
In-Reply-To: <20250619153904.25889-1-listout@listout.xyz>

On 19/06/2025 4.39 pm, Brahmajit Das wrote:
> strcpy is deprecated due to lack of bounds checking. This patch replaces
> strcpy 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 or a memcpy.
> 
> Update in v2: using sysfs_emit instead of scnprintf
> 
> No functional changes intended.
> 
> Link: https://github.com/KSPP/linux/issues/88
> 
> Suggested-by: Anthony Iliopoulos <ailiop@suse.com>
> Signed-off-by: Brahmajit Das <listout@listout.xyz>
> ---
>   fs/btrfs/ioctl.c   | 2 +-
>   fs/btrfs/send.c    | 2 +-
>   fs/btrfs/volumes.c | 2 +-
>   fs/btrfs/xattr.c   | 4 ++--
>   4 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index 913acef3f0a9..203f309f00b1 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -4200,7 +4200,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);

Surely this doesn't compile... strscpy takes three parameters.

> diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
> index 2891ec4056c6..66ee9e1b1e96 100644
> --- a/fs/btrfs/send.c
> +++ b/fs/btrfs/send.c
> @@ -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);
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 89835071cfea..ec5304f19ac2 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -215,7 +215,7 @@ void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
>   	u32 size_bp = size_buf;
>   
>   	if (!flags) {
> -		strcpy(bp, "NONE");
> +		memcpy(bp, "NONE", 4);
>   		return;
>   	}

Same issue here as with the other patch.

>   
> diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
> index 3e0edbcf73e1..9f652932895c 100644
> --- a/fs/btrfs/xattr.c
> +++ b/fs/btrfs/xattr.c
> @@ -12,6 +12,7 @@
>   #include <linux/posix_acl_xattr.h>
>   #include <linux/iversion.h>
>   #include <linux/sched/mm.h>
> +#include <linux/string.h>
>   #include "ctree.h"
>   #include "fs.h"
>   #include "messages.h"
> @@ -516,8 +517,7 @@ static int btrfs_initxattrs(struct inode *inode,
>   			ret = -ENOMEM;
>   			break;
>   		}
> -		strcpy(name, XATTR_SECURITY_PREFIX);
> -		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
> +		sysfs_emit(name, "%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);


  reply	other threads:[~2025-06-19 17:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-19 14:06 [PATCH] btrfs: replace deprecated strcpy with strscpy Brahmajit Das
2025-06-19 15:39 ` [PATCH v2] " Brahmajit Das
2025-06-19 17:06   ` Mark Harmstone [this message]
2025-06-19 17:59     ` Brahmajit Das
2025-06-19 18:03       ` Mark Harmstone
2025-06-20  1:43   ` [PATCH v3] " Brahmajit Das
2025-06-20  5:15     ` Mark Harmstone
2025-06-20 12:27     ` David Sterba
2025-06-19 17:03 ` [PATCH] " Mark Harmstone
2025-06-19 18:02   ` Brahmajit Das
2025-06-20 12:24     ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b8945d37-3eb9-4ad6-b3eb-2725dbb008ad@harmstone.com \
    --to=mark@harmstone.com \
    --cc=ailiop@suse.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=kees@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=listout@listout.xyz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox