Skip to contentSkip to conversation

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

PATCH Superseded Sidong Yang 3 participants 7 messages Mar 16, 2021 – Mar 19, 2021 Btrfs file system

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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).

---
v2:
Use btrfs_qgroup_level() for checking

---
 common/utils.c | 5 +++++
 1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..ba0bcb24 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 (btrfs_qgroup_level(id))
+			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 (btrfs_qgroup_level(id))
+		goto err;
+
 	if (ptr_parse_end != ptr_src_end)
 		goto  path;
 
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
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).

Thanks,
Qu

Quoted 31 lines

---
v2:
Use btrfs_qgroup_level() for checking

---
  common/utils.c | 5 +++++
  1 file changed, 5 insertions(+)
common/utils.c +5 -0
index 57e41432..ba0bcb24 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 (btrfs_qgroup_level(id))
+			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 (btrfs_qgroup_level(id))
+		goto err;
+
  	if (ptr_parse_end != ptr_src_end)
  		goto  path;
 
#2 lore raw
In reply to Sidong Yang
Quoted 4 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).

Should the level be also validate? The function parse_qgroupid does not do full validation, so eg 0//0 would be parsed as a path and not as a typo, level larger than 64K will be silently clamped.

#3 lore raw
Quoted 9 lines · David Sterba
Quoted 4 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).

Should the level be also validate? The function parse_qgroupid does not do full validation, so eg 0//0 would be parsed as a path and not as a typo, level larger than 64K will be silently clamped.

I agree. 0//0 would be parsed as path but it failed in btrfs_util_is_subvolume() and goes to err. I understand that upper 16 bits of qgroupid is for level. so, The valid llevel range is [0~2^16-1]. But I can't get it that level larger than 64K will be clampled.

one more question about that, I see that the ioctl calls just store the qgroupid without any opeartion with level. is the level meaningless in kernel?

#4 lore raw
Quoted 11 lines · Sidong Yang
Quoted 9 lines · David Sterba
Quoted 4 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).

Should the level be also validate? The function parse_qgroupid does not do full validation, so eg 0//0 would be parsed as a path and not as a typo, level larger than 64K will be silently clamped.

Quoted 9 lines

I agree. 0//0 would be parsed as path but it failed in btrfs_util_is_subvolume() and goes to err. I understand that upper 16 bits of qgroupid is for level. so, The valid llevel range is [0~2^16-1]. But I can't get it that level larger than 64K will be clampled.

one more question about that, I see that the ioctl calls just store the qgroupid without any opeartion with level. is the level meaningless in kernel?

No, kernel uses the qgroup level, but it's deep in qgroup code.

In fact, kernel treats qgroupid just as an u64, and sometimes checks the qgroup level for relationship, but under most cases, it's just a u64, level/id doesn't really matter that much.

Thanks,
Qu

#5 lore raw
In reply to Sidong Yang
Quoted 7 lines · Sidong Yang
Quoted 6 lines · David Sterba
Quoted 4 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 8 lines
Quoted 3 lines

Should the level be also validate? The function parse_qgroupid does not do full validation, so eg 0//0 would be parsed as a path and not as a typo, level larger than 64K will be silently clamped.

I agree. 0//0 would be parsed as path but it failed in btrfs_util_is_subvolume() and goes to err. I understand that upper 16 bits of qgroupid is for level. so, The valid llevel range is [0~2^16-1]. But I can't get it that level larger than 64K will be clampled.

The way the level gets stored into the final qgroup id is level << 48. For example invalid values 70000/281474976710779 would be stored as subvol id 123 and level 4465, where the u64 is 0x117100000000007b

Quoted 3 lines

one more question about that, I see that the ioctl calls just store the qgroupid without any opeartion with level. is the level meaningless in kernel?

The quota groups are hierarchical and the level denotes the level, where 0/subvolid is the lowest always attached to a subvolume and the higher levels are artificial and may contain any qgroups and do the whole accounting on the subtree. The original design doc .pdf can be found in
https://git.kernel.org/pub/scm/linux/kernel/git/arne/qgroups-doc.git/tree/

#6 lore raw
Quoted 25 lines · David Sterba
Quoted 7 lines · Sidong Yang
Quoted 6 lines · David Sterba
Quoted 4 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 8 lines
Quoted 3 lines

Should the level be also validate? The function parse_qgroupid does not do full validation, so eg 0//0 would be parsed as a path and not as a typo, level larger than 64K will be silently clamped.

I agree. 0//0 would be parsed as path but it failed in btrfs_util_is_subvolume() and goes to err. I understand that upper 16 bits of qgroupid is for level. so, The valid llevel range is [0~2^16-1]. But I can't get it that level larger than 64K will be clampled.

The way the level gets stored into the final qgroup id is level << 48. For example invalid values 70000/281474976710779 would be stored as subvol id 123 and level 4465, where the u64 is 0x117100000000007b

Quoted 3 lines

one more question about that, I see that the ioctl calls just store the qgroupid without any opeartion with level. is the level meaningless in kernel?

Quoted 5 lines

The quota groups are hierarchical and the level denotes the level, where 0/subvolid is the lowest always attached to a subvolume and the higher levels are artificial and may contain any qgroups and do the whole accounting on the subtree. The original design doc .pdf can be found in
https://git.kernel.org/pub/scm/linux/kernel/git/arne/qgroups-doc.git/tree/

Thanks for detailed description.
I'll write the new patch for checking level also.

Thanks,
Sidong