diff mbox

[07/17] vfs: add is_following_link() helper

Message ID 1473708559-12714-8-git-send-email-mszeredi@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Miklos Szeredi Sept. 12, 2016, 7:29 p.m. UTC
The remainging instances of ->readlink that don't use generic_readlink are
/proc/$$/fd/N, /proc/$$/map_files/A and /proc/$$/ns/X.  The reason is that
these have special get_link() implementations is that they "jump" to
locations not indicated by the symlink contents.

Since there are so few of these (the fd and map_files ones are essentially
the same), it doesn't make sense to create a separate i_op method for them.
This patch adds a helper: is_following_link() by which the two different
modes of operation can be differentiated by the special get_link()
implementations.

Also add a WARN_ON_ONCE() in generic_readlink() to prevent misuse.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/namei.c            | 9 +++++++++
 include/linux/namei.h | 1 +
 2 files changed, 10 insertions(+)
diff mbox

Patch

diff --git a/fs/namei.c b/fs/namei.c
index c06a68b82088..f72c405d1a27 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -880,6 +880,11 @@  void nd_jump_link(struct path *path)
 	nd->flags |= LOOKUP_JUMPED;
 }
 
+bool is_following_link(void)
+{
+	return current->nameidata;
+}
+
 static inline void put_link(struct nameidata *nd)
 {
 	struct saved *last = nd->stack + --nd->depth;
@@ -4670,6 +4675,10 @@  int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
 		link = inode->i_op->get_link(dentry, inode, &done);
 		if (IS_ERR(link))
 			return PTR_ERR(link);
+
+		/* "jumping" is unacceptable, warn and return error */
+		if (WARN_ON_ONCE(!link))
+			return -EIO;
 	}
 	res = readlink_copy(buffer, buflen, link);
 	do_delayed_call(&done);
diff --git a/include/linux/namei.h b/include/linux/namei.h
index f29abda31e6d..ec7b8ccfe064 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -90,6 +90,7 @@  extern struct dentry *lock_rename(struct dentry *, struct dentry *);
 extern void unlock_rename(struct dentry *, struct dentry *);
 
 extern void nd_jump_link(struct path *path);
+extern bool is_following_link(void);
 
 static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
 {