Skip to contentSkip to conversation

harden against corrupt symlinks

PATCH Mike Waychison 4 participants 5 messages May 31, 2005 – Aug 18, 2006 VFS

lore
Share & tools
lore
#0 lore raw

Hi Al,

We've hit the situation a few times where a corrupt symlink could easily oops the kernel. The problem was tracked down to an older e2fsutils that didn't do much sanity checking on symlinks during a fsck. This patch uses strnlen when reading in the symlink and ensures that it doesn't exceed PATH_MAX.

Would you accept this kind of 'hardening'?

fs/namei.c +11 -1
@@ -1936,7 +1936,12 @@ int vfs_readlink(struct dentry *dentry, 
 	if (IS_ERR(link))
 		goto out;
 
-	len = strlen(link);
+	len = strnlen(link, PATH_MAX);
+	if (len == PATH_MAX) {
+		len = -ENAMETOOLONG;
+		goto out;
+	}
+
 	if (len > (unsigned) buflen)
 		len = buflen;
 	if (copy_to_user(buffer, link, len))
@@ -1953,6 +1958,11 @@ __vfs_follow_link(struct nameidata *nd, 
 	if (IS_ERR(link))
 		goto fail;
 
+	if (strnlen(link, PATH_MAX) == PATH_MAX) {
+		link = ERR_PTR(-ENAMETOOLONG);
+		goto fail;
+	}
+
 	if (*link == '/') {
 		path_release(nd);
 		if (!walk_init_root(link, nd))
#1 lore raw
Quoted 9 lines · Mike Waychison

We've hit the situation a few times where a corrupt symlink could easily oops the kernel. The problem was tracked down to an older e2fsutils that didn't do much sanity checking on symlinks during a fsck. This patch uses strnlen when reading in the symlink and ensures that it doesn't exceed PATH_MAX.

Would you accept this kind of 'hardening'?

Andrew Morton suggested a very similar fix 3 years ago to this list:

Subject: Re: ext3 -> crash -> fsck -> readlink -> oops

I thought it made it into the kernel then, but I guess not.

Quoted 18 lines
fs/namei.c +6 -1
@@ -1936,7 +1936,12 @@ int vfs_readlink(struct dentry *dentry, 
 	if (IS_ERR(link))
 		goto out;
 
-	len = strlen(link);
+	len = strnlen(link, PATH_MAX);
+	if (len == PATH_MAX) {
+		len = -ENAMETOOLONG;
+		goto out;
+	}
+
 	if (len > (unsigned) buflen)
 		len = buflen;
 	if (copy_to_user(buffer, link, len))
@@ -1953,6 +1958,11 @@ __vfs_follow_link(struct nameidata *nd, 
 	if (IS_ERR(link))

fs/namei.c __vfs_follow_link(struct nameidata *nd, · line 1968

Quoted 10 lines
fs/namei.c +5 -0
 		goto fail;
 
+	if (strnlen(link, PATH_MAX) == PATH_MAX) {
+		link = ERR_PTR(-ENAMETOOLONG);
+		goto fail;
+	}
+
 	if (*link == '/') {
 		path_release(nd);
 		if (!walk_init_root(link, nd))

Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.

#2 lore raw
Quoted 9 lines · On Wed, 1 Jun 2005 02:15:48 -0600 Andreas Dilger <[email protected]> wrote:
Quoted 8 lines · Mike Waychison

We've hit the situation a few times where a corrupt symlink could easily oops the kernel. The problem was tracked down to an older e2fsutils that didn't do much sanity checking on symlinks during a fsck. This patch uses strnlen when reading in the symlink and ensures that it doesn't exceed PATH_MAX.

Would you accept this kind of 'hardening'?

Quoted 7 lines
Quoted 1 line

Andrew Morton suggested a very similar fix 3 years ago to this list:

Subject: Re: ext3 -> crash -> fsck -> readlink -> oops

I thought it made it into the kernel then, but I guess not.

This fix has drifted across my google desk - I thought we'd fixed it, but still not. Third time lucky, perhaps.

Quoted 20 lines
Quoted 20 lines
fs/namei.c +6 -1
@@ -1936,7 +1936,12 @@ int vfs_readlink(struct dentry *dentry, 
 	if (IS_ERR(link))
 		goto out;
 
-	len = strlen(link);
+	len = strnlen(link, PATH_MAX);
+	if (len == PATH_MAX) {
+		len = -ENAMETOOLONG;
+		goto out;
+	}
+
 	if (len > (unsigned) buflen)
 		len = buflen;
 	if (copy_to_user(buffer, link, len))
@@ -1953,6 +1958,11 @@ __vfs_follow_link(struct nameidata *nd, 
 	if (IS_ERR(link))
 		goto fail;
 
Quoted 9 lines

fs/namei.c __vfs_follow_link(struct nameidata *nd, · line 1968

Quoted 8 lines
fs/namei.c +5 -0
+	if (strnlen(link, PATH_MAX) == PATH_MAX) {
+		link = ERR_PTR(-ENAMETOOLONG);
+		goto fail;
+	}
+
 	if (*link == '/') {
 		path_release(nd);
 		if (!walk_init_root(link, nd))

A few things...

Should we instead be treating this as a filesystem driver bug, and fix up the filesystem(s) to not return overly-long pathname components?

Running strnlen() against each component in __vfs_follow_link() looks expensive. Can it be avoided?

Given that this is the VFS correcting for filesystem misbehaviour, it would seem to make sense to perform this correction at a low level, immediately after the filesytem driver has returned us the pathname component. An apparently-obvious way of doing this is to wrap ->follow_link. Can anyone think of a smarter way?

#3 lore raw
Quoted 33 lines · Andrew Morton
Quoted 20 lines
Quoted 20 lines
fs/namei.c +6 -1
@@ -1936,7 +1936,12 @@ int vfs_readlink(struct dentry *dentry, 
 	if (IS_ERR(link))
 		goto out;
 
-	len = strlen(link);
+	len = strnlen(link, PATH_MAX);
+	if (len == PATH_MAX) {
+		len = -ENAMETOOLONG;
+		goto out;
+	}
+
 	if (len > (unsigned) buflen)
 		len = buflen;
 	if (copy_to_user(buffer, link, len))
@@ -1953,6 +1958,11 @@ __vfs_follow_link(struct nameidata *nd, 
 	if (IS_ERR(link))
 		goto fail;
 
Quoted 9 lines

fs/namei.c __vfs_follow_link(struct nameidata *nd, · line 1968

Quoted 8 lines
fs/namei.c +5 -0
+	if (strnlen(link, PATH_MAX) == PATH_MAX) {
+		link = ERR_PTR(-ENAMETOOLONG);
+		goto fail;
+	}
+
 	if (*link == '/') {
 		path_release(nd);
 		if (!walk_init_root(link, nd))

Should we instead be treating this as a filesystem driver bug, and fix up the filesystem(s) to not return overly-long pathname components?

Quoted 8 lines

Running strnlen() against each component in __vfs_follow_link() looks expensive. Can it be avoided?

Given that this is the VFS correcting for filesystem misbehaviour, it would seem to make sense to perform this correction at a low level, immediately after the filesytem driver has returned us the pathname component. An apparently-obvious way of doing this is to wrap ->follow_link. Can anyone think of a smarter way?

#define PATH_MAX 4096 /* # chars in a path name including nul */

One possibility is for the case of long symlinks to always set the last byte of the page to NUL to ensure that the link is terminated. This appears easily doable by having page_getlink() do the NUL termination after kmap() but before returning, something like:

static char *page_getlink(struct dentry * dentry, struct page **ppage)

 {
	char *link;

page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
NULL);
if (IS_ERR(page))
goto sync_fail;
wait_on_page_locked(page);
if (!PageUptodate(page))
goto async_fail;
*ppage = page;
- return kmap(page);
+ link = kmap(page);
+ /* PATH_MAX is strictly <= PAGE_SIZE */
+ link[PATH_MAX - 1] = '\0';
+ return link;

Many of the other filesystems that don't use page_follow_link_light() already do NUL termination themselves.

nfs_follow_link() is very similar, but not identical and needs the same fix.

ocfs2_page_getlink() is an exact duplicate of page_getlink() and the bug duplication could be avoided if page_getlink() was exported. Otherwise it needs the same fix.

I _think_ PATH_MAX is the right thing here (instead of PAGE_SIZE), since the caller expects at most PATH_MAX in the returned link, and PAGE_SIZE may be considerably larger. I don't think PATH_MAX will ever be larger than PAGE_SIZE. We could also use min(PAGE_SIZE, PATH_MAX) and it would be resolved at compile time, but it seems wishy-washy to me.

The other option is to actually get the link length out of the filesystem itself, and avoid strlen(link) entirely, but that is a more complex change.

Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.

#4 lore raw
Quoted 21 lines · Andreas Dilger
Quoted 6 lines · Andrew Morton

Given that this is the VFS correcting for filesystem misbehaviour, it would seem to make sense to perform this correction at a low level, immediately after the filesytem driver has returned us the pathname component. An apparently-obvious way of doing this is to wrap ->follow_link. Can anyone think of a smarter way?

#define PATH_MAX 4096 /* # chars in a path name including nul */

One possibility is for the case of long symlinks to always set the last byte of the page to NUL to ensure that the link is terminated. This appears easily doable by having page_getlink() do the NUL termination after kmap() but before returning, something like:

static char *page_getlink(struct dentry * dentry, struct page **ppage)

 {
	char *link;
(quoted diff) +0 -0
 	page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
 			       NULL);
 	if (IS_ERR(page))

Review

Quoted 10 lines
(quoted diff) +4 -1
 		goto sync_fail;
 	wait_on_page_locked(page);
 	if (!PageUptodate(page))
 		goto async_fail;
 	*ppage = page;
-	return kmap(page);
+	link = kmap(page);
+	/* PATH_MAX is strictly <= PAGE_SIZE */
+	link[PATH_MAX - 1] = '\0';
+	return link;

This seems reasonable.

Quoted 6 lines

Many of the other filesystems that don't use page_follow_link_light() already do NUL termination themselves.

nfs_follow_link() is very similar, but not identical and needs the same fix.

jfs_follow_link() too. :-)

Quoted 5 lines

ocfs2_page_getlink() is an exact duplicate of page_getlink() and the bug duplication could be avoided if page_getlink() was exported. Otherwise it needs the same fix.

Quoted 8 lines

I _think_ PATH_MAX is the right thing here (instead of PAGE_SIZE), since the caller expects at most PATH_MAX in the returned link, and PAGE_SIZE may be considerably larger. I don't think PATH_MAX will ever be larger than PAGE_SIZE. We could also use min(PAGE_SIZE, PATH_MAX) and it would be resolved at compile time, but it seems wishy-washy to me.

The other option is to actually get the link length out of the filesystem itself, and avoid strlen(link) entirely, but that is a more complex change.

Is it? Is there any file system where the link length is not i_size?

Signature · 2 lines
David Kleikamp
IBM Linux Technology Center