Skip to contentSkip to conversation

btrfs-progs: utils: make sure set_label_mounted uses correct length buffers

PATCH Applied Petros Angelatos 2 participants 2 messages Mar 22, 2016 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: utils: make sure set_label_mounted uses correct length buffers

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

---
 utils.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
utils.c +4 -1
index c0c564e..ec6c613 100644
@@ -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));
Signature · 1 line
2.7.2
Applied
#1 lore raw
Quoted 11 lines · Petros Angelatos

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

Good catch, I was not aware of the hidden requirement.

Quoted 6 lines

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

Applied, thanks.