ovl: reject lowerdir ending with unescaped backslash
PATCH 1/1 ovl: reject lowerdir ending with unescaped backslash
In ovl_parse_param_split_lowerdirs() the escape handling consumes the backslash and the character after it. When the backslash is the last character of the lowerdir value, the loop increment moves the cursor past the NUL terminator, so the loop scans heap memory beyond the kstrdup'd string until it happens to hit a zero byte; a colon found there is written over as well.
A trailing unescaped backslash escapes nothing, so reject it with EINVAL.
---
Found by code inspection. Behavior check in a QEMU guest:fsconfig(lowerdir="...\") on an unpatched kernel returns -ENOENT after the loop scanned past the allocation; with this patch it returns -EINVAL.
fs/overlayfs/params.c | 3 +++ 1 file changed, 3 insertions(+)
fs/overlayfs/params.c +3 -0
@@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str)if (*s =='\\' ) {/* keep esc chars in split lowerdir */ *d++ = *s++; +/* A trailing backslash escapes nothing. */ +if (!*s) +return -EINVAL ; }else if (*s ==':' ) {bool next_colon = (*(s +1 ) ==':' );
Signature · 1 line
2.43.0
Amir Goldstein
maintainerQuoted 36 lines · Guanglei Zhu
In
ovl_parse_param_split_lowerdirs()the escape handling consumes the backslash and the character after it. When the backslash is the last character of the lowerdir value, the loop increment moves the cursor past the NUL terminator, so the loop scans heap memory beyond the kstrdup'd string until it happens to hit a zero byte; a colon found there is written over as well.A trailing unescaped backslash escapes nothing, so reject it with EINVAL.
Signed-off-by Guanglei Zhu <[email protected]>---
Found by code inspection. Behavior check in a QEMU guest:fsconfig(lowerdir="...\")on an unpatched kernel returns-ENOENTafter the loop scanned past the allocation; with this patch it returns-EINVAL.fs/overlayfs/params.c | 3 +++ 1 file changed, 3 insertions(+)fs/overlayfs/params.c +3
-1@@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str)if (*s =='\\' ) {/* keep esc chars in split lowerdir */ *d++ = *s++; +/* A trailing backslash escapes nothing. */ +if (!*s) +return -EINVAL ; }else if (*s ==':' ) {bool next_colon = (*(s +1 ) ==':' ); --
Quoted 2 lines
2.43.0
Hi Guanglei Zhu,
The fix looks correct, but the commit message is inaccurately making wrong claims about heap memory and scanning past the allocation, which makes this sound more severe than it actually is.
See Sashiko review comment:
https://sashiko.dev/#/patchset/20261012060847.836027-1-zhugl3%40xiaopeng.com
Thanks,
Amir.
PATCH v2 1/1 ovl: reject lowerdir ending with unescaped backslash
In reply to Guanglei ZhuIn ovl_parse_param_split_lowerdirs() a backslash consumes the character that follows it. When the backslash is the last character of the lowerdir value, it consumes the NUL terminator: the loop exits normally and the backslash is kept in the output, so the lowerdir is passed downstream with a trailing backslash and the mount fails later with a confusing ENOENT from path lookup.
A trailing unescaped backslash escapes nothing, so reject it with EINVAL.
---
v2: fix the commit message. v1 wrongly claimed that the loop scans heap memory past the allocation; there is no out-of-bounds access, the backslash merely consumes the NUL terminator. Thanks to Amir Goldstein and Sashiko for catching this.
Verified with an ASAN build of a user-space replica of the loop:
no out-of-bounds access, the output retains the trailing backslash. With this patch fsconfig(lowerdir="...\") returns -EINVAL instead of -ENOENT.
fs/overlayfs/params.c | 3 +++ 1 file changed, 3 insertions(+)
fs/overlayfs/params.c +3 -0
@@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str)if (*s =='\\' ) {/* keep esc chars in split lowerdir */ *d++ = *s++; +/* A trailing backslash escapes nothing. */ +if (!*s) +return -EINVAL ; }else if (*s ==':' ) {bool next_colon = (*(s +1 ) ==':' );
Signature · 1 line
2.43.0
Quoted 41 lines · Guanglei Zhu
In
ovl_parse_param_split_lowerdirs()a backslash consumes the character that follows it. When the backslash is the last character of the lowerdir value, it consumes the NUL terminator: the loop exits normally and the backslash is kept in the output, so the lowerdir is passed downstream with a trailing backslash and the mount fails later with a confusing ENOENT from path lookup.A trailing unescaped backslash escapes nothing, so reject it with EINVAL.
Signed-off-by Guanglei Zhu <[email protected]>---
v2: fix the commit message. v1 wrongly claimed that the loop scans heap memory past the allocation; there is no out-of-bounds access, the backslash merely consumes the NUL terminator. Thanks to Amir Goldstein and Sashiko for catching this.Verified with an ASAN build of a user-space replica of the loop:
no out-of-bounds access, the output retains the trailing backslash. With this patchfsconfig(lowerdir="...\")returns-EINVALinstead of-ENOENT.fs/overlayfs/params.c | 3 +++ 1 file changed, 3 insertions(+)fs/overlayfs/params.c +3
-1@@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str)if (*s =='\\' ) {/* keep esc chars in split lowerdir */ *d++ = *s++; +/* A trailing backslash escapes nothing. */ +if (!*s) +return -EINVAL ; }else if (*s ==':' ) {bool next_colon = (*(s +1 ) ==':' ); --
Quoted 2 lines
2.43.0
Christian,
Could you pick this up via vfs.fixes?
Thanks,
Amir.
Quoted 3 lines
Christian,
Could you pick this up via vfs.fixes?
In vfs.fixes now. Thanks!
Christian Brauner
AppliedQuoted 8 lines · Guanglei Zhu
In
ovl_parse_param_split_lowerdirs()a backslash consumes the character that follows it. When the backslash is the last character of the lowerdir value, it consumes the NUL terminator: the loop exits normally and the backslash is kept in the output, so the lowerdir is passed downstream with a trailing backslash and the mount fails later with a confusing ENOENT from path lookup.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/1] ovl: reject lowerdir ending with unescaped backslash
https://git.kernel.org/vfs/vfs/c/81f4be14beee