Skip to content

Commit a310808

Browse files
trbeckersmfrench
authored andcommitted
cifs: sanitize multiple delimiters in prepath
mount.cifs can pass a device with multiple delimiters in it. This will cause rename(2) to fail with ENOENT. V2: - Make sanitize_path more readable. - Fix multiple delimiters between UNC and prepath. - Avoid a memory leak if a bad user starts putting a lot of delimiters in the path on purpose. BugLink: https://bugzilla.redhat.com/show_bug.cgi?id=2031200 Fixes: 24e0a1e ("cifs: switch to new mount api") Cc: [email protected] # 5.11+ Acked-by: Ronnie Sahlberg <[email protected]> Signed-off-by: Thiago Rafael Becker <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent b774302 commit a310808

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

fs/cifs/fs_context.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,42 @@ int smb3_parse_opt(const char *options, const char *key, char **val)
434434
return rc;
435435
}
436436

437+
/*
438+
* Remove duplicate path delimiters. Windows is supposed to do that
439+
* but there are some bugs that prevent rename from working if there are
440+
* multiple delimiters.
441+
*
442+
* Returns a sanitized duplicate of @path. The caller is responsible for
443+
* cleaning up the original.
444+
*/
445+
#define IS_DELIM(c) ((c) == '/' || (c) == '\\')
446+
static char *sanitize_path(char *path)
447+
{
448+
char *cursor1 = path, *cursor2 = path;
449+
450+
/* skip all prepended delimiters */
451+
while (IS_DELIM(*cursor1))
452+
cursor1++;
453+
454+
/* copy the first letter */
455+
*cursor2 = *cursor1;
456+
457+
/* copy the remainder... */
458+
while (*(cursor1++)) {
459+
/* ... skipping all duplicated delimiters */
460+
if (IS_DELIM(*cursor1) && IS_DELIM(*cursor2))
461+
continue;
462+
*(++cursor2) = *cursor1;
463+
}
464+
465+
/* if the last character is a delimiter, skip it */
466+
if (IS_DELIM(*(cursor2 - 1)))
467+
cursor2--;
468+
469+
*(cursor2) = '\0';
470+
return kstrdup(path, GFP_KERNEL);
471+
}
472+
437473
/*
438474
* Parse a devname into substrings and populate the ctx->UNC and ctx->prepath
439475
* fields with the result. Returns 0 on success and an error otherwise
@@ -493,7 +529,7 @@ smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
493529
if (!*pos)
494530
return 0;
495531

496-
ctx->prepath = kstrdup(pos, GFP_KERNEL);
532+
ctx->prepath = sanitize_path(pos);
497533
if (!ctx->prepath)
498534
return -ENOMEM;
499535

0 commit comments

Comments
 (0)