kernfs: hook inode initialization for LSMs
Subsystems (2)
RFC PATCH 1/1 kernfs: hook inode initialization for LSMs
From: William Roberts <[email protected]>
On the Android side of the house, were required to label each and every sysfs file with a specific label. sysfs often has transient files, and this uncovered a limitation in SELinux. In most filesystems the new inode inherits from the parents label, but in sysfs, this was not the case. The new inode would inherit the syfs wide "default" label. Because of this, many hacks in userspace were performed, but alas there were many races and so it didn't work well. The reason sysfs differed from other filesystems was because the inode creation for kernfs was missing the security callback.
This patch adds that security callback, however, is not yet ready to apply. Since sysfs differs from other filesystems in that their is no inode create implementation, since create is not supported, the inode's are generated in the lookup operation. We added the hook there, however the error code path for unwind is unclear.
I would like to get some comments on the design (is lookup ok) as well as the unwind paths on lsm initialization error, see the XXX in fs/kernfs/inode.c.
William Roberts (1):
[RFC] kernfs: hook inode initialization for LSMs
fs/kernfs/dir.c | 2 +- fs/kernfs/inode.c | 34 ++++++++++++++++++++++++++++++---- fs/kernfs/kernfs-internal.h | 26 +++++++++++++++++++++++++- fs/kernfs/mount.c | 2 +- 4 files changed, 57 insertions(+), 7 deletions(-)
Signature · 1 line
1.9.1
RFC PATCH 1/1 kernfs: hook inode initialization for LSMs
From: William Roberts <[email protected]>
This corrects issues in client filesystems, like sysfs, where LSMs missed the opportunity to initialize inodes. In the case of SELinux this prevented newly created files from inheriting their parent's label. This has posed many issues on Android where transient sysfs files were defaulting to the sysfs wide label, rather than their parent, which is the normal behavior for other filesystems. Without this, access controls for these files had to be granted on the file system wide label, rather then a file specific label. The result of which over-privilege the caller but also can cause CTS failures.
--- fs/kernfs/dir.c | 2 +- fs/kernfs/inode.c | 34 ++++++++++++++++++++++++++++++---- fs/kernfs/kernfs-internal.h | 26 +++++++++++++++++++++++++- fs/kernfs/mount.c | 2 +- 4 files changed, 57 insertions(+), 7 deletions(-)
fs/kernfs/dir.c +1 -1
@@ -809,7 +809,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, dentry->d_fsdata = kn;/* attach dentry and inode */ - inode =kernfs_get_inode (dir->i_sb, kn); + inode =kernfs_get_inode (dir->i_sb, kn, dentry->d_parent);if (!inode) { ret =ERR_PTR (-ENOMEM ); goto out_unlock;
fs/kernfs/inode.c +30 -4
@@ -281,8 +281,10 @@ int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,return 0 ; } -static void kernfs_init_inode (struct kernfs_node *kn,struct inode *inode) +static int kernfs_init_inode (struct kernfs_node *kn,struct inode *inode, +struct dentry *parent) { +int error =0 ;kernfs_get (kn); inode->i_private = kn; inode->i_mapping->a_ops = &kernfs_aops;
@@ -291,6 +293,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)set_default_inode_attr (inode, kn->mode);kernfs_refresh_inode (kn, inode); +WARN_ON (kn->parent && !parent); +/* initialize inode according to type */ switch (kernfs_type (kn)) {case KERNFS_DIR :
@@ -308,31 +312,53 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)BUG (); } +if (parent) + error =security_inode_init_security (inode, parent->d_inode, + &parent->d_name,NULL ,NULL ); +unlock_new_inode (inode); +return error; }/** * kernfs_get_inode - get inode for kernfs_node * @sb: super block * @kn: kernfs_node to allocate inode for +* @parent:The parent dentry or NULL if root node * * Get inode for @kn. If such inode doesn't exist, a new inode is * allocated and basics are initialized. New inode is returned * locked. * +* WARN: If parent is null but @kn is not the root node. +* * LOCKING: * Kernel thread context (may sleep). * * RETURNS: * Pointer to allocated inode on success, NULL on failure. */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn) +struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn, +struct dentry *parent) { +int error;struct inode *inode; inode =iget_locked (sb, kn->ino); -if (inode && (inode->i_state &I_NEW )) -kernfs_init_inode (kn, inode); +if (inode && (inode->i_state &I_NEW )) { + error =kernfs_init_inode (kn, inode, parent); +if (unlikely (error)) { +/* +* XXX +* What is the proper error sequence here? +* Should we kernfs_put() and iput() or +* just run the kernfs_evict_inode() +* routine. +*/ +kernfs_evict_inode (inode); +return NULL ; + } + }return inode; }
fs/kernfs/kernfs-internal.h +25 -1
@@ -76,7 +76,8 @@ extern struct kmem_cache *kernfs_node_cache;/* * inode.c */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn); +struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn, +struct dentry *parent);void kernfs_evict_inode (struct inode *inode);int kernfs_iop_permission (struct inode *inode,int mask);int kernfs_iop_setattr (struct dentry *dentry,struct iattr *iattr);
@@ -89,6 +90,29 @@ ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, size_t size);ssize_t kernfs_iop_listxattr (struct dentry *dentry,char *buf,size_t size); +/** +* kernfs_get_root_inode - get root inode for kernfs_node +* @sb: super block +* @kn: kernfs_node to allocate inode for +* +* SEE: kernfs_get_inode() for more details +* +* WARN: if @kn is not the root node, ie has a parent. +* +* LOCKING: +* Kernel thread context (may sleep). +* +* RETURNS: +* Pointer to allocated inode on success, NULL on failure. +*/ +static inline struct inode *kernfs_get_root_inode (struct super_block *sb, +struct kernfs_node *kn) +{ +WARN_ON (kn->parent); +return kernfs_get_inode (sb, kn,NULL ); +} + +/* * dir.c */
fs/kernfs/mount.c +1 -1
@@ -77,7 +77,7 @@ static int kernfs_fill_super(struct super_block *sb, unsigned long magic)/* get root inode, initialize and unlock it */ mutex_lock (&kernfs_mutex); - inode =kernfs_get_inode (sb, info->root->kn); + inode =kernfs_get_root_inode (sb, info->root->kn);mutex_unlock (&kernfs_mutex);if (!inode) {pr_debug ("kernfs: could not get root inode\n" );
Signature · 1 line
1.9.1
Greg KH
maintainerQuoted 50 lines · On Fri, May 22, 2015 at 11:25:36AM -0700, [email protected] wrote:
From: William Roberts <[email protected]>
This corrects issues in client filesystems, like sysfs, where LSMs missed the opportunity to initialize inodes. In the case of SELinux this prevented newly created files from inheriting their parent's label. This has posed many issues on Android where transient sysfs files were defaulting to the sysfs wide label, rather than their parent, which is the normal behavior for other filesystems. Without this, access controls for these files had to be granted on the file system wide label, rather then a file specific label. The result of which over-privilege the caller but also can cause CTS failures.
Signed-off-by William Roberts <[email protected]>--- fs/kernfs/dir.c | 2 +- fs/kernfs/inode.c | 34 ++++++++++++++++++++++++++++++---- fs/kernfs/kernfs-internal.h | 26 +++++++++++++++++++++++++- fs/kernfs/mount.c | 2 +- 4 files changed, 57 insertions(+), 7 deletions(-)fs/kernfs/dir.c +1
-1@@ -809,7 +809,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir, dentry->d_fsdata = kn;/* attach dentry and inode */ - inode =kernfs_get_inode (dir->i_sb, kn); + inode =kernfs_get_inode (dir->i_sb, kn, dentry->d_parent);if (!inode) { ret =ERR_PTR (-ENOMEM ); goto out_unlock;fs/kernfs/inode.c +3
-1@@ -281,8 +281,10 @@ int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,return 0 ; } -static void kernfs_init_inode (struct kernfs_node *kn,struct inode *inode) +static int kernfs_init_inode (struct kernfs_node *kn,struct inode *inode, +struct dentry *parent) { +int error =0 ;kernfs_get (kn); inode->i_private = kn; inode->i_mapping->a_ops = &kernfs_aops;
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 296
Quoted 4 lines
fs/kernfs/inode.c +1
-0@@ -291,6 +293,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)set_default_inode_attr (inode, kn->mode);kernfs_refresh_inode (kn, inode); +WARN_ON (kn->parent && !parent);
Why? What is this going to tell us, or anyone else if it triggers?
Quoted 44 lines
fs/kernfs/inode.c +12
-3+/* initialize inode according to type */ switch (kernfs_type (kn)) {case KERNFS_DIR :@@ -308,31 +312,53 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)BUG (); } +if (parent) + error =security_inode_init_security (inode, parent->d_inode, + &parent->d_name,NULL ,NULL ); +unlock_new_inode (inode); +return error; }/** * kernfs_get_inode - get inode for kernfs_node * @sb: super block * @kn: kernfs_node to allocate inode for +* @parent:The parent dentry or NULL if root node * * Get inode for @kn. If such inode doesn't exist, a new inode is * allocated and basics are initialized. New inode is returned * locked. * +* WARN: If parent is null but @kn is not the root node. +* * LOCKING: * Kernel thread context (may sleep). * * RETURNS: * Pointer to allocated inode on success, NULL on failure. */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn) +struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn, +struct dentry *parent) { +int error;struct inode *inode; inode =iget_locked (sb, kn->ino); -if (inode && (inode->i_state &I_NEW )) -kernfs_init_inode (kn, inode);
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 357
Quoted 10 lines
fs/kernfs/inode.c +10
-0+if (inode && (inode->i_state &I_NEW )) { + error =kernfs_init_inode (kn, inode, parent); +if (unlikely (error)) { +/* +* XXX +* What is the proper error sequence here? +* Should we kernfs_put() and iput() or +* just run the kernfs_evict_inode() +* routine. +*/
How can kernfs_init_inode now fail? What does LSM expect us to do with that information, was it just a failure in the LSM, or something that we now are supposed to not allow to have happen within the filesystem?
Quoted 35 lines
fs/kernfs/inode.c +4
-0+kernfs_evict_inode (inode); +return NULL ; + } + }return inode; }fs/kernfs/kernfs-internal.h +12
-1@@ -76,7 +76,8 @@ extern struct kmem_cache *kernfs_node_cache;/* * inode.c */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn); +struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn, +struct dentry *parent);void kernfs_evict_inode (struct inode *inode);int kernfs_iop_permission (struct inode *inode,int mask);int kernfs_iop_setattr (struct dentry *dentry,struct iattr *iattr);@@ -89,6 +90,29 @@ ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, size_t size);ssize_t kernfs_iop_listxattr (struct dentry *dentry,char *buf,size_t size); +/** +* kernfs_get_root_inode - get root inode for kernfs_node +* @sb: super block +* @kn: kernfs_node to allocate inode for +* +* SEE: kernfs_get_inode() for more details +* +* WARN: if @kn is not the root node, ie has a parent. +* +* LOCKING:
fs/kernfs/kernfs-internal.h ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, · line 112
Quoted 10 lines
fs/kernfs/kernfs-internal.h +10
-0+* Kernel thread context (may sleep). +* +* RETURNS: +* Pointer to allocated inode on success, NULL on failure. +*/ +static inline struct inode *kernfs_get_root_inode (struct super_block *sb, +struct kernfs_node *kn) +{ +WARN_ON (kn->parent); +return kernfs_get_inode (sb, kn,NULL );
Why is this whole function even needed?
thanks,
greg k-h
Quoted 10 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Saturday, May 23, 2015 8:15 AM
To: Roberts, William C
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] [RFC] kernfs: hook inode initialization for LSMs
Quoted 11 lines
Quoted 10 lines · On Fri, May 22, 2015 at 11:25:36AM -0700, [email protected] wrote:
From: William Roberts <[email protected]>
This corrects issues in client filesystems, like sysfs, where LSMs missed the opportunity to initialize inodes. In the case of SELinux this prevented newly created files from inheriting their parent's label. This has posed many issues on Android where transient sysfs files were defaulting to the sysfs wide label, rather than their parent, which is the normal behavior for other filesystems. Without this, access controls for these files had to be granted on the file system wide label, rather then a file specific label. The result of
Just noticed this "then --> than" grammar issue. I will fix in a finalized patch after addressing any concerns/comments.
Quoted 43 lines
fs/kernfs/dir.c static struct dentry *kernfs_iop_lookup(struct
Quoted 15 lines
which over-privilege the caller but also can cause CTS failures.
Signed-off-by William Roberts <[email protected]>--- fs/kernfs/dir.c | 2 +- fs/kernfs/inode.c | 34 ++++++++++++++++++++++++++++++---- fs/kernfs/kernfs-internal.h | 26 +++++++++++++++++++++++++- fs/kernfs/mount.c | 2 +- 4 files changed, 57 insertions(+), 7 deletions(-)fs/kernfs/dir.c index fffca95..096379e renamed from fs/kernfs/dir.c +0
-0100644
fs/kernfs/dir.c +0
-0@@ -809,7 +809,7 @@ static struct dentry *kernfs_iop_lookup(structinode *dir,
fs/kernfs/inode.c int kernfs_iop_getattr(struct vfsmount *mnt,
Quoted 13 lines
fs/kernfs/dir.c +1
-1dentry->d_fsdata = kn;/* attach dentry and inode */ - inode =kernfs_get_inode (dir->i_sb, kn); + inode =kernfs_get_inode (dir->i_sb, kn, dentry->d_parent);if (!inode) { ret =ERR_PTR (-ENOMEM ); goto out_unlock;fs/kernfs/inode.c index renamed from fs/kernfs/inode.c +0
-02da8493..d37b8ff 100644
fs/kernfs/inode.c +0
-0@@ -281,8 +281,10 @@ int kernfs_iop_getattr(struct vfsmount *mnt,struct dentry *dentry,
Quoted 11 lines
fs/kernfs/inode.c +3
-1return 0 ; } -static void kernfs_init_inode (struct kernfs_node *kn,struct inode *inode) +static int kernfs_init_inode (struct kernfs_node *kn,struct inode *inode, +struct dentry *parent) { +int error =0 ;kernfs_get (kn); inode->i_private = kn;Quoted 1 line
inode->i_mapping->a_ops=&kernfs_aops; @@ -291,6 +293,8 @@static
Quoted 7 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 296
Quoted 5 lines
void
kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)fs/kernfs/inode.c +1
-0set_default_inode_attr (inode, kn->mode);kernfs_refresh_inode (kn, inode); +WARN_ON (kn->parent && !parent);Why? What is this going to tell us, or anyone else if it triggers?
It was to catch the case when you think you're passing the root node, and there is no parent, but you're actually passing something further down that has a parent. The LSM's initialize the root node in a separate hook, and this routine was used to get both the root node and sub-nodes. We don't want callers to specify a NULL parent, when one truly exists, and bypass the LSM inode initialization.
In a previous version of the patch, I attempted to look up the kernfs_node's parent. However, it can result in sysfs-level deep recursion when you look up the parent but It's a cache miss, and thus you need to initialize a node, which then results in another lookup. The goal with this design was to minimize lookup overhead since the caller had the parent inode in-hand.
Quoted 55 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node
Quoted 6 lines
fs/kernfs/inode.c +1
-0+/* initialize inode according to type */ switch (kernfs_type (kn)) {case KERNFS_DIR :@@ -308,31 +312,53 @@ static void kernfs_init_inode(struct kernfs_node*kn, struct inode *inode)
Quoted 23 lines
fs/kernfs/inode.c +7
-0BUG (); } +if (parent) + error =security_inode_init_security (inode, parent- d_inode, + &parent->d_name,NULL ,NULL ); +unlock_new_inode (inode); +return error; }/** * kernfs_get_inode - get inode for kernfs_node * @sb: super block * @kn: kernfs_node to allocate inode for +* @parent:The parent dentry or NULL if root node * * Get inode for @kn. If such inode doesn't exist, a new inode is * allocated and basics are initialized. New inode is returned * locked. * +* WARN: If parent is null but @kn is not the root node. fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Quoted 10 lines
fs/kernfs/inode.c +2
-1+* * LOCKING: * Kernel thread context (may sleep). * * RETURNS: * Pointer to allocated inode on success, NULL on failure. */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn) +struct inode *kernfs_get_inode(struct super_block *sb, struct
kernfs_node*kn,Quoted 14 lines
fs/kernfs/inode.c +8
-2+ struct dentry *parent) { +int error;struct inode *inode; inode =iget_locked (sb, kn->ino); -if (inode && (inode->i_state &I_NEW )) -kernfs_init_inode (kn, inode); +if (inode && (inode->i_state &I_NEW )) { + error =kernfs_init_inode (kn, inode, parent); +if (unlikely (error)) { +/* +* XXX +* What is the proper error sequence here?
Quoted 8 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 357
Quoted 4 lines
fs/kernfs/inode.c +4
-0+* Should we kernfs_put() and iput() or +* just run the kernfs_evict_inode() +* routine. +*/ How can
kernfs_init_inodenow fail? What does LSM expect us to do with that information, was it just a failure in the LSM, or something that we now are supposed to not allow to have happen within the filesystem?
No it's really just a failure in the LSM. I initially left this as a non-fatal failure, however, changed it during code review. My original thought was in the case of failure the node is created with the default sysfs file label, and policy will not allow access to it, however for other creations of policy, perhaps you don't want the node to exist if the LSM cannot label it. So because of that reason, if the LSM fails, we wouldn't the node to appear in sysfs.
Quoted 44 lines
Quoted 13 lines
fs/kernfs/inode.c +4
-0+kernfs_evict_inode (inode); +return NULL ; + } + }fs/kernfs/inode.c +0
-0return inode; }fs/kernfs/kernfs-internal.h +0
-0fs/kernfs/kernfs-internal.h extern struct kmem_cache *kernfs_node_cache; · line 80
Quoted 6 lines
fs/kernfs/kernfs-internal.h +1
-1@@ -76,7 +76,8 @@ extern struct kmem_cache *kernfs_node_cache;/* * inode.c */ -struct inode *kernfs_get_inode (struct super_block *sb,struct kernfs_node *kn); +struct inode *kernfs_get_inode(struct super_block *sb, struct
kernfs_node*kn,Quoted 5 lines
fs/kernfs/kernfs-internal.h +1
-0+ struct dentry *parent);void kernfs_evict_inode (struct inode *inode);int
kernfs_iop_permission(struct inode *inode, int mask); intkernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr); @@ -89,6 +90,29 @@ssize_tkernfs_iop_getxattr(struct dentry *dentry, constchar *name, void *buf,
Quoted 18 lines
size_tsize);
ssize_tkernfs_iop_listxattr(struct dentry *dentry, char *buf,size_t
size);+/**
+ *kernfs_get_root_inode- get root inode forkernfs_node
+ * @sb: super block
+ * @kn:kernfs_nodeto allocate inode for
+ *
+ * SEE:kernfs_get_inode()for more details
+ *
+ * WARN: if @kn is not the root node, ie has a parent.
+ *
+ * LOCKING:
+ * Kernel thread context (may sleep).
+ *
+ * RETURNS:
+ * Pointer to allocated inode on success, NULL on failure.
Quoted 8 lines
fs/kernfs/kernfs-internal.h ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, · line 112
Quoted 6 lines
+ */
fs/kernfs/kernfs-internal.h +5
-0+static inline struct inode *kernfs_get_root_inode (struct super_block *sb, +struct kernfs_node *kn) +{ +WARN_ON (kn->parent); +return kernfs_get_inode (sb, kn,NULL );Why is this whole function even needed?
It's not, but a comment during review stated that passing NULL as the 3rd argument in the mount.c case was awkward, which I could see. But I am not attached to this change.
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerQuoted 4 lines · On Fri, May 22, 2015 at 11:25:35AM -0700, [email protected] wrote:
From: William Roberts <[email protected]>
On the Android side of the house, were required to label each and every sysfs file with a specific label.
"required"?
And what label are you having to use?
Quoted 5 lines
sysfs often has transient
files, and this uncovered a limitation in SELinux. In most filesystems the new inode inherits from the parents label, but in sysfs, this was not the case. The new inode would inherit the syfs wide "default" label.
Do you really want to build up a list of SELinux policies for sysfs files/directories, only to see them need to be redone when the tree moves things around in the future?
What type of "rules" are you using here for sysfs to handle the dynamic-ness of the tree properly?
thanks,
greg k-h
Quoted 11 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Saturday, May 23, 2015 8:12 AM
To: Roberts, William C
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [RFC] [PATCH] kernfs: hook inode initialization for LSMsOn Fri, May 22, 2015 at 11:25:35AM -0700, [email protected] wrote:
Quoted 8 lines
Quoted 4 lines
From: William Roberts <[email protected]>
On the Android side of the house, were required to label each and every sysfs file with a specific label.
"required"?
And what label are you having to use?
Yes, required by CTS. A check occurs on submission of the platform to google To ensure some basic sanity, this is the CTS tests. Part of this test is to ensure that domains are not given write access to all of sysfs. In Android, we define a specific label to each sysfs node, and grant access to that for that specific domain.
Quoted 10 lines
Quoted 6 lines
sysfs often has transient
files, and this uncovered a limitation in SELinux. In most filesystems the new inode inherits from the parents label, but in sysfs, this was not the case. The new inode would inherit the syfs wide "default" label.Do you really want to build up a list of SELinux policies for sysfs files/directories, only to see them need to be redone when the tree moves things around in the future?
That's not really an issue for us. We have only a handful of rules, and the sysfs nodes change from device to device. Our intent is not to label all of sysfs independently, just the few nodes that we need write access too. As an example, on the Nexus 7 or "hammerhead" device we have 13 distinctly separately labeled sysfs file entries including the global sysfs label.
Quoted 3 lines
What type of "rules" are you using here for sysfs to handle the dynamic-ness of the tree properly?
I may not understand what you're asking here properly. In the case of a dynamic sysfs file, we may have labeled its parent directory a specific label and allowed access to that label type, however, when the child is created dynamically, it doesn't get that label and defaults to the sysfs wide one.
Quoted 4 lines
thanks,
greg k-h