Commit 2c6b7bcd authored by Linus Torvalds's avatar Linus Torvalds

readdir: be more conservative with directory entry names

Commit 8a23eb80 ("Make filldir[64]() verify the directory entry
filename is valid") added some minimal validity checks on the directory
entries passed to filldir[64]().  But they really were pretty minimal.

This fleshes out at least the name length check: we used to disallow
zero-length names, but really, negative lengths or oevr-long names
aren't ok either.  Both could happen if there is some filesystem
corruption going on.

Now, most filesystems tend to use just an "unsigned char" or similar for
the length of a directory entry name, so even with a corrupt filesystem
you should never see anything odd like that.  But since we then use the
name length to create the directory entry record length, let's make sure
it actually is half-way sensible.

Note how POSIX states that the size of a path component is limited by
NAME_MAX, but we actually use PATH_MAX for the check here.  That's
because while NAME_MAX is generally the correct maximum name length
(it's 255, for the same old "name length is usually just a byte on
disk"), there's nothing in the VFS layer that really cares.

So the real limitation at a VFS layer is the total pathname length you
can pass as a filename: PATH_MAX.
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 131701c6
...@@ -102,10 +102,14 @@ EXPORT_SYMBOL(iterate_dir); ...@@ -102,10 +102,14 @@ EXPORT_SYMBOL(iterate_dir);
* filename length, and the above "soft error" worry means * filename length, and the above "soft error" worry means
* that it's probably better left alone until we have that * that it's probably better left alone until we have that
* issue clarified. * issue clarified.
*
* Note the PATH_MAX check - it's arbitrary but the real
* kernel limit on a possible path component, not NAME_MAX,
* which is the technical standard limit.
*/ */
static int verify_dirent_name(const char *name, int len) static int verify_dirent_name(const char *name, int len)
{ {
if (!len) if (len <= 0 || len >= PATH_MAX)
return -EIO; return -EIO;
if (memchr(name, '/', len)) if (memchr(name, '/', len))
return -EIO; return -EIO;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment