Skip to content

Commit a46b587

Browse files
committed
Fix #1516, Patch virtual mount point checking
Sponsored by 21Software Reject any paths which might be part of a path traversal attack by matching directory entries such as `.` and `..` at the end of the given path. This hardens a vulnerability where an attacker might be able to write files in unauthorized locations outside of a virtual mount point directory.
1 parent 3bcb137 commit a46b587

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/os/shared/src/osapi-filesys.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,40 @@ bool OS_FileSys_FindVirtMountPoint(void *ref, const OS_object_token_t *token, co
117117
* For instance consider a virtual_mountpt of /mnt/abc and searching
118118
* for target=/mnt/abcd - this should return false in that case.
119119
*/
120-
return (target[mplen] == '/' || target[mplen] == 0);
120+
if (!(target[mplen] == '/' || target[mplen] == 0))
121+
{
122+
return false;
123+
}
124+
125+
/*
126+
* SECURITY HARDENING:
127+
* Reject any attempts to traverse outside the mount point by using
128+
* path components of "." or ".." within the remainder of the path.
129+
* Although higher-level APIs also check for "..", this adds defense
130+
* in depth so a direct caller of search logic cannot bypass checks.
131+
*/
132+
if (target[mplen] == '/')
133+
{
134+
const char *remainder = target + mplen; /* starts with '/' or is end */
135+
for (const char *p = remainder; *p != 0; ++p)
136+
{
137+
if (p[0] == '/' && p[1] == '.')
138+
{
139+
/* Match /./ or /. at end */
140+
if (p[2] == '/' || p[2] == 0)
141+
{
142+
return false; /* reject single-dot component */
143+
}
144+
/* Match /../ or /.. at end */
145+
if (p[2] == '.' && (p[3] == '/' || p[3] == 0))
146+
{
147+
return false; /* reject parent-dir traversal */
148+
}
149+
}
150+
}
151+
}
152+
153+
return true;
121154
}
122155

123156
/*----------------------------------------------------------------

0 commit comments

Comments
 (0)