From: Petros Angelatos <petrosagg@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Petros Angelatos <petrosagg@gmail.com>
Subject: [PATCH] btrfs-progs: utils: make sure set_label_mounted uses correct length buffers
Date: Tue, 22 Mar 2016 03:40:29 -0700 [thread overview]
Message-ID: <1458643229-25209-1-git-send-email-petrosagg@gmail.com> (raw)
When `btrfs filesystem label /foo bar` command is invoked, it will pass
the buffer allocated in the argv array directly to set_label_mounted()
and then to the BTRFS_IOC_SET_FSLABEL ioctl.
However, the kernel code handling the ioctl will always try to copy
BTRFS_LABEL_SIZE bytes[1] from the userland pointer. Under certain
conditions and when the label is small enough, the command will fail
with:
[root@localhost /]# btrfs filesystem label /mnt f
ERROR: unable to set label Bad address
Fix this by making sure we pass a BTRFS_LABEL_SIZE sized buffer to the
ioctl containing the desired label.
[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/btrfs/ioctl.c?id=refs/tags/v4.5#n5231
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
---
utils.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/utils.c b/utils.c
index c0c564e..ec6c613 100644
--- a/utils.c
+++ b/utils.c
@@ -1755,9 +1755,10 @@ static int set_label_unmounted(const char *dev, const char *label)
return 0;
}
-static int set_label_mounted(const char *mount_path, const char *label)
+static int set_label_mounted(const char *mount_path, const char *labelp)
{
int fd;
+ char label[BTRFS_LABEL_SIZE];
fd = open(mount_path, O_RDONLY | O_NOATIME);
if (fd < 0) {
@@ -1765,6 +1766,8 @@ static int set_label_mounted(const char *mount_path, const char *label)
return -1;
}
+ memset(label, '\0', sizeof(label));
+ strncpy(label, labelp, sizeof(label));
if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
fprintf(stderr, "ERROR: unable to set label %s\n",
strerror(errno));
--
2.7.2
next reply other threads:[~2016-03-22 10:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-22 10:40 Petros Angelatos [this message]
2016-03-22 15:37 ` [PATCH] btrfs-progs: utils: make sure set_label_mounted uses correct length buffers 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=1458643229-25209-1-git-send-email-petrosagg@gmail.com \
--to=petrosagg@gmail.com \
--cc=linux-btrfs@vger.kernel.org \
/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