Initialize kernfs nodes with LSMs
Subsystems (2)
PATCH 1/1 Initialize kernfs nodes with 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):
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
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 than 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
maintainerRe: [PATCH] kernfs: hook inode initialization for LSMs
Quoted 50 lines · On Tue, Jun 09, 2015 at 12:11:41PM -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 than 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);
What can a developer do about this?
Quoted 17 lines
fs/kernfs/inode.c +6
-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)BUG (); } +if (parent) + error =security_inode_init_security (inode, parent->d_inode, + &parent->d_name,NULL ,NULL ); +unlock_new_inode (inode); +return error; }/**
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 333
Quoted 10 lines
fs/kernfs/inode.c +2
-0* 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.
What can we do about this?
Quoted 16 lines
fs/kernfs/inode.c +4
-2+* * 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 ))
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 356
Quoted 10 lines
fs/kernfs/inode.c +9
-1-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.
trailing whitespace :(
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 359
Quoted 3 lines
fs/kernfs/inode.c +3
-0+*/ +kernfs_evict_inode (inode); +return NULL ;
I don't know what to do here, it's a mess, this is really too deep it seems to properly unwind.
Quoted 32 lines
fs/kernfs/inode.c +2
-0+ } + }return inode; }fs/kernfs/kernfs-internal.h +11
-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. +*
fs/kernfs/kernfs-internal.h ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, · line 111
Quoted 10 lines
fs/kernfs/kernfs-internal.h +10
-0+* 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);
Why warn? What can we do about this?
thanks,
greg k-h
RE: [PATCH] kernfs: hook inode initialization for LSMs
Quoted 64 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Tuesday, June 9, 2015 2:09 PM
To: Roberts, William C
Cc: [email protected]
Subject: Re: [PATCH] kernfs: hook inode initialization for LSMsfs/kernfs/dir.c static struct dentry *kernfs_iop_lookup(struct
Quoted 25 lines · On Tue, Jun 09, 2015 at 12:11:41PM -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 than 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 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);What can a developer do about this?
This means that the node has a parent, but you didn't pass it as its argument. Doing a lookup internally, instead Of passing parent, results in a recursive lookup all the way to the root node as to get the inode for The parent one must do a kernfs_get_inode() on it, which can call the initialization routine, which calls Kernfs_get_inode(). What the developer should do is forward the inode from the parent so we can
Avoid the recursive lookup and any lookup in general. This is based on where this was getting called from, the parent inode was readily available. That WARN_ON is not even strict enough, read further down I mention it again. The goal is that We don't end up skipping LSM initialization by accident, ie kn has parent but they do not pass parent. Alternatively, We can move the warning (and make it proper) to right before the LSM hook and make sure that parent arg And kn->parent are either both specified or both unspecified ie XOR. !a != !b. Rather than litter the top level functions With this warning. Also, I chose warn_on because I didn't want it to be fatal, however, I am not arguing that its proper And am seeking alternative suggestions.
Quoted 20 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node
Quoted 7 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 12 lines
fs/kernfs/inode.c +5
-0BUG (); } +if (parent) + error =security_inode_init_security (inode, parent- d_inode, + &parent->d_name,NULL ,NULL ); +unlock_new_inode (inode); +return error; }
Quoted 13 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 333
Quoted 11 lines
fs/kernfs/inode.c +2
-0/** * 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. What can we do about this?
Forward the parent along if it has one
Quoted 17 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Quoted 11 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 5 lines
fs/kernfs/inode.c +2
-0+ struct dentry *parent) { +int error;struct inode *inode;
Quoted 14 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 356
Quoted 12 lines
fs/kernfs/inode.c +9
-2inode =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. trailing whitespace :(
Ok, strange checked this which checkpatch a few times... not sure where I introduced it....
Quoted 7 lines
fs/kernfs/inode.c static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) · line 359
Quoted 4 lines
fs/kernfs/inode.c +3
-0+*/ +kernfs_evict_inode (inode); +return NULL ;I don't know what to do here, it's a mess, this is really too deep it seems to properly unwind.
I initially had this as non-fatal. If the inode is released uninitialized by the LSM (SELinux in particular) it defaults to the "fs wide label" or whatever that LSM (not sure what others do) does. However, there really should be NO error, perhaps we can make this a BUG_ON() or something fatal. We can do printk and ignore, or just ignore. My take on it is, the lsm shouldn't really miss the boat on initializing the inode, and if they wanted to, they could make the initialization failure an oops or something. If the LSM isn't working how would the system reasonably carry on?
Quoted 41 lines
Quoted 11 lines
fs/kernfs/inode.c +2
-0+ } + }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 17 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:
Quoted 8 lines
fs/kernfs/kernfs-internal.h ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, · line 111
Quoted 6 lines
+ * Pointer to allocated inode on success, NULL on failure.
+ */fs/kernfs/kernfs-internal.h +4
-0+static inline struct inode *kernfs_get_root_inode (struct super_block *sb, +struct kernfs_node *kn) +{ +WARN_ON (kn->parent);Why warn? What can we do about this?
If it has a parent, you're not getting the root inode, which is what this helper should Be used for ie mount.c usage.
Kernfs_get_inode() should be used to retrieve either the root (!kn->parent && !parent) or Non-root (kn->parent && parent) cases. Perhaps my warn on in the first routine is not strong enough. We really wanted XOR.
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH] kernfs: hook inode initialization for LSMs
Quoted 71 lines · William C
Quoted 64 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Tuesday, June 9, 2015 2:09 PM
To: Roberts, William C
Cc: [email protected]
Subject: Re: [PATCH] kernfs: hook inode initialization for LSMsfs/kernfs/dir.c static struct dentry *kernfs_iop_lookup(struct
Quoted 25 lines · On Tue, Jun 09, 2015 at 12:11:41PM -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 than 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 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);What can a developer do about this?
Quoted 11 lines
This means that the node has a parent, but you didn't pass it as its argument. Doing a lookup internally, instead Of passing parent, results in a recursive lookup all the way to the root node as to get the inode for The parent one must do a
kernfs_get_inode()on it, which can call the initialization routine, which callsKernfs_get_inode(). What the developer should do is forward the inode from the parent so we can
Avoid the recursive lookup and any lookup in general. This is based on where this was getting called from, the parent inode was readily available. ThatWARN_ONis not even strict enough, read further down I mention it again. The goal is that We don't end up skipping LSM initialization by accident, ie kn has parent but they do not pass parent. Alternatively, We can move the warning (and make it proper) to right before the LSM hook and make sure that parent arg Andkn->parentare either both specified or both unspecified ie XOR. !a != !b. Rather than litter the top level functions With this warning. Also, I chosewarn_onbecause I didn't want it to be fatal, however, I am not arguing that its proper And am seeking alternative suggestions.
But again, what can a driver do about this? If this shouldn't happen, then don't test for it. This isn't something that a driver developer should care about, right? If not, then is it something that the sysfs core could be doing? If so, then fix the sysfs core.
greg k-h
RE: [PATCH] kernfs: hook inode initialization for LSMs
<snip>
Quoted 30 lines
Quoted 12 lines
Quoted 5 lines
fs/kernfs/inode.c int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
Quoted 4 lines
fs/kernfs/inode.c +1
-0+int error =0 ;kernfs_get (kn); inode->i_private = kn; 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);What can a developer do about this?
Quoted 11 lines
This means that the node has a parent, but you didn't pass it as its argument. Doing a lookup internally, instead Of passing parent, results in a recursive lookup all the way to the root node as to get the inode for The parent one must do a
kernfs_get_inode()on it, which can call the initialization routine, which callsKernfs_get_inode(). What the developer should do is forward the inode from the parent so we can Avoid the recursive lookup and any lookup in general. This is based on where this was getting called from, the parent inode was readily available. ThatWARN_ONis not even strict enough, read further down I mention it again. The goal is that We don't end up skipping LSM initialization by accident, ie kn has parent but they do not passparent. Alternatively, We can move the warning (and make it proper) to right before the LSM hook and make sure that parent arg And
kn->parentare either both specified or both unspecified ie XOR. !a != !b. Rather than litter the top level functions With this warning. Also, I chosewarn_onbecause I didn't want it to be fatal, however, I am not arguing that its proper And am seeking alternative suggestions.
Quoted 4 lines
But again, what can a driver do about this? If this shouldn't happen, then don't test for it. This isn't something that a driver developer should care about, right? If not, then is it something that the sysfs core could be doing? If so, then fix the sysfs core.
All of these modifications are to kernfs internal users and will not affect the users of kernfs (like cgroups and sysfs) from a modules perspective. In reality, the only Intended purpose was to keep someone from "in the future" accidentally modifying Kernfs and calling the wrong function. I guess serving that need is rather pointless.
With that said, I have been thinking about the "too deep to unwind" comment you made. I guess I don't follow. Eventually that kernfs node and inode would come back from the init routine To eventually be undone at later time when things are removed from the cache. The reversal Process for kernfs looks like the inode eviction routine kernfs_evict_inode(). Which only handles Some of it (handles kernfs + readying the inode for eviction), the ref on the inode needs to be put, so an iput() should end up calling iput_final() and evict(inode). Although, this is just from me browsing the code, I'd like to add a poison filename value to my test module that will cause an LSM failure and verify the failure case. The failure case, in theory should only be traversed on ENOMEM cases within the LSM itself. If there are more details I am missing, I'd be eager to Understand what they are.