Skip to contentSkip to conversation

btrfs-progs: common: make sure that qgroup id is in range

PATCH Superseded Sidong Yang 3 participants 5 messages Mar 15, 2021 – Mar 16, 2021 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: common: make sure that qgroup id is in range

When user assign qgroup with qgroup id that is too big to exceeds range and invade level value, and it works without any error. but this action would be make undefined error. this code make sure that qgroup id doesn't exceed range(0 ~ 2^48-1).

---
 common/utils.c | 5 +++++
 1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..a2f72550 100644
@@ -727,6 +727,8 @@ u64 parse_qgroupid(const char *p)
 		id = strtoull(p, &ptr_parse_end, 10);
 		if (ptr_parse_end != ptr_src_end)
 			goto path;
+		if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+			goto err;
 		return id;
 	}
 	level = strtoull(p, &ptr_parse_end, 10);
@@ -734,6 +736,9 @@ u64 parse_qgroupid(const char *p)
 		goto path;
 
 	id = strtoull(s + 1, &ptr_parse_end, 10);
+	if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+		goto err;
+
 	if (ptr_parse_end != ptr_src_end)
 		goto  path;
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 15 lines · Sidong Yang

When user assign qgroup with qgroup id that is too big to exceeds range and invade level value, and it works without any error. but this action would be make undefined error. this code make sure that qgroup id doesn't exceed range(0 ~ 2^48-1).

---
 common/utils.c | 5 +++++
 1 file changed, 5 insertions(+)
common/utils.c +0 -0
index 57e41432..a2f72550 100644

common/utils.c u64 parse_qgroupid(const char *p) · line 731

Quoted 5 lines
common/utils.c +2 -0
@@ -727,6 +727,8 @@ u64 parse_qgroupid(const char *p)
 		id = strtoull(p, &ptr_parse_end, 10);
 		if (ptr_parse_end != ptr_src_end)
 			goto path;
+		if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+			goto err;

Please add a helper that validates the id or use btrfs_qgroup_level and check that level is 0 if that makes sense in the context of the call.

#2 lore raw
In reply to Sidong Yang
Quoted 6 lines · Sidong Yang

When user assign qgroup with qgroup id that is too big to exceeds range and invade level value, and it works without any error. but this action would be make undefined error. this code make sure that qgroup id doesn't exceed range(0 ~ 2^48-1).

Shouldn't the check also happen inside the ioctl?

Thanks,
Qu

Quoted 28 lines
---
  common/utils.c | 5 +++++
  1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..a2f72550 100644
@@ -727,6 +727,8 @@ u64 parse_qgroupid(const char *p)
  		id = strtoull(p, &ptr_parse_end, 10);
  		if (ptr_parse_end != ptr_src_end)
  			goto path;
+		if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+			goto err;
  		return id;
  	}
  	level = strtoull(p, &ptr_parse_end, 10);
@@ -734,6 +736,9 @@ u64 parse_qgroupid(const char *p)
  		goto path;
 
  	id = strtoull(s + 1, &ptr_parse_end, 10);
+	if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+		goto err;
+
  	if (ptr_parse_end != ptr_src_end)
  		goto  path;
 
#3 lore raw
Quoted 11 lines · Qu Wenruo
Quoted 6 lines · Sidong Yang

When user assign qgroup with qgroup id that is too big to exceeds range and invade level value, and it works without any error. but this action would be make undefined error. this code make sure that qgroup id doesn't exceed range(0 ~ 2^48-1).

Shouldn't the check also happen inside the ioctl?

Yes, I checked the ioctl code in kernel. but there is only the code that check if it is zero like !sa->qgroupid. and it just assign to key.offset. Also it should be checked in ioctl?

Quoted 31 lines

Thanks,
Qu

Quoted 28 lines
---
  common/utils.c | 5 +++++
  1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..a2f72550 100644
@@ -727,6 +727,8 @@ u64 parse_qgroupid(const char *p)
  		id = strtoull(p, &ptr_parse_end, 10);
  		if (ptr_parse_end != ptr_src_end)
  			goto path;
+		if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+			goto err;
  		return id;
  	}
  	level = strtoull(p, &ptr_parse_end, 10);
@@ -734,6 +736,9 @@ u64 parse_qgroupid(const char *p)
  		goto path;
 
  	id = strtoull(s + 1, &ptr_parse_end, 10);
+	if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+		goto err;
+
  	if (ptr_parse_end != ptr_src_end)
  		goto  path;
 
#4 lore raw
Quoted 9 lines · Sidong Yang
Quoted 8 lines · Qu Wenruo
Quoted 5 lines · Sidong Yang

When user assign qgroup with qgroup id that is too big to exceeds range and invade level value, and it works without any error. but this action would be make undefined error. this code make sure that qgroup id doesn't exceed range(0 ~ 2^48-1).

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Shouldn't the check also happen inside the ioctl?

Yes, I checked the ioctl code in kernel. but there is only the code that check if it is zero like !sa->qgroupid. and it just assign to key.offset. Also it should be checked in ioctl?

After more check, the ioctl interface doesn't need that check, or user can't parse any qgroup with higher qgroup level.

Thus the check should only exist in user space to avoid case like 1/(U48_MAX + 1).

Thanks,
Qu

Quoted 32 lines
Quoted 32 lines

Thanks,
Qu

Quoted 28 lines
---
   common/utils.c | 5 +++++
   1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..a2f72550 100644
@@ -727,6 +727,8 @@ u64 parse_qgroupid(const char *p)
   		id = strtoull(p, &ptr_parse_end, 10);
   		if (ptr_parse_end != ptr_src_end)
   			goto path;
+		if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+			goto err;
   		return id;
   	}
   	level = strtoull(p, &ptr_parse_end, 10);
@@ -734,6 +736,9 @@ u64 parse_qgroupid(const char *p)
   		goto path;
 
   	id = strtoull(s + 1, &ptr_parse_end, 10);
+	if (id >> BTRFS_QGROUP_LEVEL_SHIFT)
+		goto err;
+
   	if (ptr_parse_end != ptr_src_end)
   		goto  path;