Skip to content

Commit 898dbb9

Browse files
zijun-huSasha Levin
authored andcommitted
fs/filesystems: Fix potential unsigned integer underflow in fs_name()
[ Upstream commit 1363c13 ] fs_name() has @index as unsigned int, so there is underflow risk for operation '@index--'. Fix by breaking the for loop when '@index == 0' which is also more proper than '@index <= 0' for unsigned integer comparison. Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com> Link: https://lore.kernel.org/20250410-fix_fs-v1-1-7c14ccc8ebaa@quicinc.com Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 5a42579 commit 898dbb9

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

fs/filesystems.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,19 @@ static int fs_index(const char __user * __name)
156156
static int fs_name(unsigned int index, char __user * buf)
157157
{
158158
struct file_system_type * tmp;
159-
int len, res;
159+
int len, res = -EINVAL;
160160

161161
read_lock(&file_systems_lock);
162-
for (tmp = file_systems; tmp; tmp = tmp->next, index--)
163-
if (index <= 0 && try_module_get(tmp->owner))
162+
for (tmp = file_systems; tmp; tmp = tmp->next, index--) {
163+
if (index == 0) {
164+
if (try_module_get(tmp->owner))
165+
res = 0;
164166
break;
167+
}
168+
}
165169
read_unlock(&file_systems_lock);
166-
if (!tmp)
167-
return -EINVAL;
170+
if (res)
171+
return res;
168172

169173
/* OK, we got the reference, so we can safely block */
170174
len = strlen(tmp->name) + 1;

0 commit comments

Comments
 (0)