[PATCH 24/35] VFS: Split inode_permission() and create path_permission()

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Valerie Aurora
Date: Thursday, April 15, 2010 - 4:04 pm

Split inode_permission() into inode and file-system-dependent parts.
Create path_permission() to check permission based on the path to the
inode.  This is for union mounts, in which an inode can be located on
a read-only lower layer file system but is still writable, since we
will copy it up to the writable top layer file system.  So in that
case, we want to ignore MS_RDONLY on the lower layer.  To make this
decision, we must know the path (vfsmount, dentry) of both the target
and its parent.
---
 fs/namei.c         |   92 ++++++++++++++++++++++++++++++++++++++++++++--------
 include/linux/fs.h |    1 +
 2 files changed, 79 insertions(+), 14 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 900df0f..24e0cb2 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -241,29 +241,20 @@ int generic_permission(struct inode *inode, int mask,
 }
 
 /**
- * inode_permission  -  check for access rights to a given inode
+ * __inode_permission  -  check for access rights to a given inode
  * @inode:	inode to check permission on
  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  *
  * Used to check for read/write/execute permissions on an inode.
- * We use "fsuid" for this, letting us set arbitrary permissions
- * for filesystem access without changing the "normal" uids which
- * are used for other things.
+ *
+ * This does not check for a read-only file system.  You probably want
+ * inode_permission().
  */
-int inode_permission(struct inode *inode, int mask)
+static int __inode_permission(struct inode *inode, int mask)
 {
 	int retval;
 
 	if (mask & MAY_WRITE) {
-		umode_t mode = inode->i_mode;
-
-		/*
-		 * Nobody gets write access to a read-only fs.
-		 */
-		if (IS_RDONLY(inode) &&
-		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
-			return -EROFS;
-
 		/*
 		 * Nobody gets write access to an immutable file.
 		 */
@@ -288,6 +279,79 @@ int inode_permission(struct inode *inode, int mask)
 }
 
 /**
+ * sb_permission  -  check superblock-level permissions
+ * @sb: superblock of inode to check permission on
+ * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
+ *
+ * Separate out file-system wide checks from inode-specific permission
+ * checks.  In particular, union mounts want to check the read-only
+ * status of the top-level file system, not the lower.
+ */
+int sb_permission(struct super_block *sb, struct inode *inode, int mask)
+{
+	if (mask & MAY_WRITE) {
+		umode_t mode = inode->i_mode;
+
+		/*
+		 * Nobody gets write access to a read-only fs.
+		 */
+		if ((sb->s_flags & MS_RDONLY) &&
+		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
+			return -EROFS;
+	}
+	return 0;
+}
+
+/**
+ * inode_permission  -  check for access rights to a given inode
+ * @inode:	inode to check permission on
+ * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
+ *
+ * Used to check for read/write/execute permissions on an inode.
+ * We use "fsuid" for this, letting us set arbitrary permissions
+ * for filesystem access without changing the "normal" uids which
+ * are used for other things.
+ */
+int inode_permission(struct inode *inode, int mask)
+{
+	int retval;
+
+	retval = sb_permission(inode->i_sb, inode, mask);
+	if (retval)
+		return retval;
+	return __inode_permission(inode, mask);
+}
+
+/**
+ * path_permission - check for inode access rights depending on path
+ * @path: path of inode to check
+ * @parent_path: path of inode's parent
+ * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
+ *
+ * Like inode_permission, but used to check for permission when the
+ * file may potentially be copied up between union layers.
+ */
+
+int path_permission(struct path *path, struct path *parent_path, int mask)
+{
+	struct vfsmount *mnt;
+	int retval;
+
+	/* Catch some reversal of args */
+	BUG_ON(!S_ISDIR(parent_path->dentry->d_inode->i_mode));
+
+	if (IS_MNT_UNION(parent_path->mnt))
+		mnt = parent_path->mnt;
+	else
+		mnt = path->mnt;
+
+	retval = sb_permission(mnt->mnt_sb, path->dentry->d_inode, mask);
+	if (retval)
+		return retval;
+	return __inode_permission(path->dentry->d_inode, mask);
+}
+
+/**
  * file_permission  -  check for additional access rights to a given file
  * @file:	file to check access rights for
  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 4dae882..0b1c811 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2114,6 +2114,7 @@ extern sector_t bmap(struct inode *, sector_t);
 #endif
 extern int notify_change(struct dentry *, struct iattr *);
 extern int inode_permission(struct inode *, int);
+extern int path_permission(struct path *, struct path *, int);
 extern int generic_permission(struct inode *, int,
 		int (*check_acl)(struct inode *, int));
 
-- 
1.6.3.3

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/35] Union mounts - everything but the xattrs, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 01/35] VFS: Make lookup_hash() return a struct path, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 02/35] VFS: Add read-only users count to superblock, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 08/35] whiteout: tmpfs whiteout support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 10/35] whiteout: ext2 whiteout support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 11/35] whiteout: jffs2 whiteout support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 12/35] fallthru: Basic fallthru definitions, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 13/35] fallthru: ext2 fallthru support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 14/35] fallthru: jffs2 fallthru support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 15/35] fallthru: tmpfs fallthru support, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 20/35] union-mount: Implement union lookup, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 24/35] VFS: Split inode_permission() and create pat ..., Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 26/35] union-mount: In-kernel copyup routines, Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 28/35] union-mount: Implement union-aware link(), Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 29/35] union-mount: Implement union-aware rename(), Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 31/35] union-mount: Implement union-aware chown(), Valerie Aurora, (Thu Apr 15, 4:04 pm)
[PATCH 34/35] union-mount: Implement union-aware lchown(), Valerie Aurora, (Thu Apr 15, 4:04 pm)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, David Woodhouse, (Mon Apr 19, 6:02 am)
Re: [PATCH 11/35] whiteout: jffs2 whiteout support, David Woodhouse, (Mon Apr 19, 6:03 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jan Blunck, (Mon Apr 19, 6:23 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jan Blunck, (Mon Apr 19, 7:12 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Valerie Aurora, (Mon Apr 19, 7:23 am)
Re: [PATCH 11/35] whiteout: jffs2 whiteout support, Valerie Aurora, (Mon Apr 19, 7:26 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jan Blunck, (Wed Apr 21, 1:42 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jamie Lokier, (Wed Apr 21, 2:22 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jamie Lokier, (Wed Apr 21, 2:52 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Miklos Szeredi, (Wed Apr 21, 3:17 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jamie Lokier, (Wed Apr 21, 10:36 am)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Valerie Aurora, (Wed Apr 21, 2:38 pm)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, Jamie Lokier, (Wed Apr 21, 3:10 pm)
Re: [PATCH 00/35] Union mounts - everything but the xattrs, Valerie Aurora, (Wed Apr 21, 4:35 pm)
Re: [PATCH 13/35] fallthru: ext2 fallthru support, J. R. Okajima, (Thu Apr 22, 3:30 am)