Skip to contentSkip to conversation

Stacking support for inode_init_security

PATCH In review [email protected] 3 participants 8 messages Aug 22, 2005 – Aug 23, 2005

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Stacking support for inode_init_security

This is basically the same patch as I sent out Friday, except against 2.6.13-rc6-mm1 with stacker applied. It redefines security_inode_init_security() to pass a struct list_head to which security modules can attach their data. Filesystems can then loop through the results easily to store each of the xattrs and free the results.

This is useful both for the stacker LSM, and for any two (or more) LSMs which might want to cooperate even without stacker.

I've tested this under using Stephen Smalley's sample exploit originally motivating inode_init_security, as well as with a simple 'touch ab; ls -Z ab'. Several kernels have been compiled without a problem.

thanks,
-serge

--

 fs/ext2/xattr_security.c |   22 ++++++++++++++--------
 fs/ext3/xattr_security.c |   22 ++++++++++++++--------
 include/linux/security.h |   30 +++++++++++++++++++-----------
 mm/shmem.c               |    6 ++----
 security/dummy.c         |    2 +-
 security/selinux/hooks.c |   42 ++++++++++++++++++++++++++----------------
 6 files changed, 76 insertions(+), 48 deletions(-)
include/linux/security.h +19 -11
Index: linux-2.6.13-rc6-mm1/include/linux/security.h
===================================================================
@@ -161,6 +161,14 @@ struct swap_info_struct;
 
 #ifdef CONFIG_SECURITY
 
+struct xattr_data {
+	struct list_head list;
+	char *name;
+	void *value;
+	int len;
+};
+
+
 /**
  * struct security_operations - main security structure
  *
@@ -343,9 +351,13 @@ struct swap_info_struct;
  *	then it should return -EOPNOTSUPP to skip this processing.
  *	@inode contains the inode structure of the newly created inode.
  *	@dir contains the inode structure of the parent directory.
- *	@name will be set to the allocated name suffix (e.g. selinux).
- *	@value will be set to the allocated attribute value.
- *	@len will be set to the length of the value.
+ *	@head, if not null, points to a listhead to which to append a
+ *		newly allocated struct xattr_data with the following data:
+ *		@data->name will be set to the allocated name suffix
+ * 			(e.g. selinux).
+ *		@data->value will be set to the allocated attribute value.
+ *		@data->len will be set to the length of the value.
+ *		@data->list is used to add the data to the list_head
  *	Returns 0 if @name and @value have been successfully set,
  *		-EOPNOTSUPP if no security attribute is needed, or
  *		-ENOMEM on memory allocation failure.
@@ -1146,7 +1158,7 @@ struct security_operations {
 	int (*inode_alloc_security) (struct inode *inode);	
 	void (*inode_free_security) (struct inode *inode);
 	int (*inode_init_security) (struct inode *inode, struct inode *dir,
-				    char **name, void **value, size_t *len);
+				    struct list_head *head);
 	int (*inode_create) (struct inode *dir,
 	                     struct dentry *dentry, int mode);
 	int (*inode_link) (struct dentry *old_dentry,
@@ -1497,13 +1509,11 @@ static inline void security_inode_free (
 
 static inline int security_inode_init_security (struct inode *inode,
 						struct inode *dir,
-						char **name,
-						void **value,
-						size_t *len)
+						struct list_head *head)
 {
 	if (unlikely (IS_PRIVATE (inode)))
 		return -EOPNOTSUPP;
-	return security_ops->inode_init_security (inode, dir, name, value, len);
+	return security_ops->inode_init_security (inode, dir, head);
 }
 	
 static inline int security_inode_create (struct inode *dir,
@@ -2186,9 +2196,7 @@ static inline void security_inode_free (
 
 static inline int security_inode_init_security (struct inode *inode,
 						struct inode *dir,
-						char **name,
-						void **value,
-						size_t *len)
+						struct list_head *head)
 {
 	return -EOPNOTSUPP;
 }
mm/shmem.c +2 -4
Index: linux-2.6.13-rc6-mm1/mm/shmem.c
===================================================================
@@ -1611,8 +1611,7 @@ shmem_mknod(struct inode *dir, struct de
 	int error = -ENOSPC;
 
 	if (inode) {
-		error = security_inode_init_security(inode, dir, NULL, NULL,
-						     NULL);
+		error = security_inode_init_security(inode, dir, NULL);
 		if (error) {
 			if (error != -EOPNOTSUPP) {
 				iput(inode);
@@ -1758,8 +1757,7 @@ static int shmem_symlink(struct inode *d
 	if (!inode)
 		return -ENOSPC;
 
-	error = security_inode_init_security(inode, dir, NULL, NULL,
-					     NULL);
+	error = security_inode_init_security(inode, dir, NULL);
 	if (error) {
 		if (error != -EOPNOTSUPP) {
 			iput(inode);
security/dummy.c +1 -1
Index: linux-2.6.13-rc6-mm1/security/dummy.c
===================================================================
@@ -259,7 +259,7 @@ static void dummy_inode_free_security (s
 }
 
 static int dummy_inode_init_security (struct inode *inode, struct inode *dir,
-				      char **name, void **value, size_t *len)
+				      struct list_head *head)
 {
 	return -EOPNOTSUPP;
 }
security/selinux/hooks.c +26 -16

63 lines not on this page · raw message

fs/ext2/xattr_security.c +14 -8
Index: linux-2.6.13-rc6-mm1/fs/ext2/xattr_security.c
===================================================================
@@ -50,20 +50,26 @@ int
 ext2_init_security(struct inode *inode, struct inode *dir)
 {
 	int err;
-	size_t len;
-	void *value;
-	char *name;
+	struct xattr_data *p, *n;
+	LIST_HEAD(head);
 
-	err = security_inode_init_security(inode, dir, &name, &value, &len);
+	err = security_inode_init_security(inode, dir, &head);
 	if (err) {
 		if (err == -EOPNOTSUPP)
 			return 0;
 		return err;
 	}
-	err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
-			     name, value, len, 0);
-	kfree(name);
-	kfree(value);
+
+	list_for_each_entry_safe(p, n, &head, list) {
+		if (!err)
+			err = ext2_xattr_set(inode,
+				EXT2_XATTR_INDEX_SECURITY,
+				p->name, p->value, p->len, 0);
+		list_del(&p->list);
+		kfree(p->name);
+		kfree(p->value);
+		kfree(p);
+	}
 	return err;
 }
 
fs/ext3/xattr_security.c +14 -8
Index: linux-2.6.13-rc6-mm1/fs/ext3/xattr_security.c
===================================================================
@@ -52,20 +52,26 @@ int
 ext3_init_security(handle_t *handle, struct inode *inode, struct inode *dir)
 {
 	int err;
-	size_t len;
-	void *value;
-	char *name;
+	struct xattr_data *p, *n;
+	LIST_HEAD(head);
 
-	err = security_inode_init_security(inode, dir, &name, &value, &len);
+	err = security_inode_init_security(inode, dir, &head);
 	if (err) {
 		if (err == -EOPNOTSUPP)
 			return 0;
 		return err;
 	}
-	err = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_SECURITY,
-				    name, value, len, 0);
-	kfree(name);
-	kfree(value);
+
+	list_for_each_entry_safe(p, n, &head, list) {
+		if (!err)
+			err = ext3_xattr_set_handle(handle, inode,
+				EXT3_XATTR_INDEX_SECURITY,
+				p->name, p->value, p->len, 0);
+		list_del(&p->list);
+		kfree(p->name);
+		kfree(p->value);
+		kfree(p);
+	}
 	return err;
 }
 

-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf

#1 lore raw

This patch, against the 2.6.13-rc6-mm1 stacker, defines the inode_init_security() hook.

thanks,
-serge

--

 stacker.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+)
security/stacker.c +35 -0
Index: linux-2.6.13-rc6-mm1/security/stacker.c
===================================================================
@@ -443,6 +443,40 @@ static void stacker_inode_free_security 
 		security_disown_value(h);
 }
 
+static int stacker_inode_init_security(struct inode *inode, struct inode *dir,
+				       struct list_head *head)
+{
+	int ret, succ_ret = -EOPNOTSUPP;
+	struct xattr_data *p, *n;
+	struct module_entry *m;
+
+	rcu_read_lock();
+	stack_for_each_entry(m, &stacked_modules, lsm_list) {
+		if (!m->module_operations.inode_init_security)
+			continue;
+		rcu_read_unlock();
+		ret = m->module_operations.inode_init_security(inode,dir,head);
+		rcu_read_lock();
+		if (ret && ret != -EOPNOTSUPP)
+			goto out_free_data;
+		if (ret == 0)
+			succ_ret = 0;
+	}
+	rcu_read_unlock();
+	return succ_ret;
+
+out_free_data:
+	if (!head)
+		return ret;
+	list_for_each_entry_safe(p, n, head, list) {
+		list_del(&p->list);
+		kfree(p->value);
+		kfree(p->name);
+		kfree(p);
+	}
+	return ret;
+}
+
 static int stacker_inode_create (struct inode *inode, struct dentry *dentry,
 			       int mask)
 {
@@ -1315,6 +1349,7 @@ static struct security_operations stacke
 
 	.inode_alloc_security		= stacker_inode_alloc_security,
 	.inode_free_security		= stacker_inode_free_security,
+	.inode_init_security		= stacker_inode_init_security,
 	.inode_create			= stacker_inode_create,
 	.inode_link			= stacker_inode_link,
 	.inode_unlink			= stacker_inode_unlink,
#2 lore raw
In reply to [email protected]

The previous patch used int instead of size_t for the xattr value length. A new patch just for include/linux/security.h is appended.

thanks,
-serge

--

  include/linux/security.h |   30 +++++++++++++++++++-----------
include/linux/security.h +19 -11
Index: linux-2.6.13-rc6-mm1/include/linux/security.h
===================================================================
@@ -161,6 +161,14 @@ struct swap_info_struct;
 
 #ifdef CONFIG_SECURITY
 
+struct xattr_data {
+	struct list_head list;
+	char *name;
+	void *value;
+	size_t len;
+};
+
+
 /**
  * struct security_operations - main security structure
  *
@@ -343,9 +351,13 @@ struct swap_info_struct;
  *	then it should return -EOPNOTSUPP to skip this processing.
  *	@inode contains the inode structure of the newly created inode.
  *	@dir contains the inode structure of the parent directory.
- *	@name will be set to the allocated name suffix (e.g. selinux).
- *	@value will be set to the allocated attribute value.
- *	@len will be set to the length of the value.
+ *	@head, if not null, points to a listhead to which to append a
+ *		newly allocated struct xattr_data with the following data:
+ *		@data->name will be set to the allocated name suffix
+ * 			(e.g. selinux).
+ *		@data->value will be set to the allocated attribute value.
+ *		@data->len will be set to the length of the value.
+ *		@data->list is used to add the data to the list_head
  *	Returns 0 if @name and @value have been successfully set,
  *		-EOPNOTSUPP if no security attribute is needed, or
  *		-ENOMEM on memory allocation failure.
@@ -1146,7 +1158,7 @@ struct security_operations {
 	int (*inode_alloc_security) (struct inode *inode);	
 	void (*inode_free_security) (struct inode *inode);
 	int (*inode_init_security) (struct inode *inode, struct inode *dir,
-				    char **name, void **value, size_t *len);
+				    struct list_head *head);
 	int (*inode_create) (struct inode *dir,
 	                     struct dentry *dentry, int mode);
 	int (*inode_link) (struct dentry *old_dentry,
@@ -1497,13 +1509,11 @@ static inline void security_inode_free (
 
 static inline int security_inode_init_security (struct inode *inode,
 						struct inode *dir,
-						char **name,
-						void **value,
-						size_t *len)
+						struct list_head *head)
 {
 	if (unlikely (IS_PRIVATE (inode)))
 		return -EOPNOTSUPP;
-	return security_ops->inode_init_security (inode, dir, name, value, len);
+	return security_ops->inode_init_security (inode, dir, head);
 }
 	
 static inline int security_inode_create (struct inode *dir,
@@ -2186,9 +2196,7 @@ static inline void security_inode_free (
 
 static inline int security_inode_init_security (struct inode *inode,
 						struct inode *dir,
-						char **name,
-						void **value,
-						size_t *len)
+						struct list_head *head)
 {
 	return -EOPNOTSUPP;
 }

-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf

#3 lore raw
In reply to [email protected]

This patch adds two stackable test LSMs which only define inode_init_security(). Any file created while these modules are loaded should have the xattrs ("security.name1", "value1") and ("security.name2", "value2").

thanks,
-serge

--

 testinitsec1.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 testinitsec2.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 150 insertions(+)
security/testinitsec1.c new file +75 -0
Index: linux-2.6.13-rc6-mm1/security/testinitsec1.c
===================================================================
@@ -0,0 +1,75 @@
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/security.h>
+
+#define VALUE "value1"
+#define NAME "name1"
+#define MY_NAME "testinitsec1"
+static int test_init_security(struct inode *inode, struct inode *dir,
+				       struct list_head *head)
+{
+	char *namep = NULL, *valuep = NULL;
+	struct xattr_data *data = NULL;
+
+	if (!head)
+		return 0;
+
+	data = kmalloc(sizeof(struct xattr_data), GFP_KERNEL);
+	if (!data)
+		goto err;
+	namep = kmalloc(32, GFP_KERNEL);
+	if (!namep)
+		goto err;
+	valuep = kmalloc(32, GFP_KERNEL);
+	if (!valuep)
+		goto err;
+
+	strcpy(namep, NAME);
+	strcpy(valuep, VALUE);
+	data->name = namep;
+	data->value = valuep;
+	data->len = strlen(VALUE);
+	INIT_LIST_HEAD(&data->list);
+	list_add_tail(&data->list, head);
+	return 0;
+err:
+	kfree(namep);
+	kfree(valuep);
+	kfree(data);
+	return -ENOMEM;
+}
+
+static struct security_operations testlsm_security_ops = {
+	.owner =			THIS_MODULE,
+
+	.inode_init_security =		test_init_security,
+};
+
+static int __init testlsm_init (void)
+{
+	if (mod_reg_security (MY_NAME, &testlsm_security_ops, NULL)) {
+		printk (KERN_INFO "Failure registering testlsm "
+			" module with primary security module.\n");
+		return -EINVAL;
+	}
+	return 0;
+}
+
+
+static void __exit testlsm_exit (void)
+{
+	if (unregister_security (&testlsm_security_ops)) {
+		printk (KERN_INFO "Failure unregistering testlsm "
+			"module with the kernel\n");
+	}
+	printk (KERN_INFO "init_security test module removed\n");
+}
+
+security_initcall (testlsm_init);
+module_exit (testlsm_exit);
+
+MODULE_DESCRIPTION("inode_initsecurity test LSM module");
+MODULE_LICENSE("GPL");
+
security/testinitsec2.c new file +75 -0

75 lines not on this page · raw message

#4 lore raw
Quoted 7 lines · Quoting [email protected] ([email protected]):

This patch adds two stackable test LSMs which only define inode_init_security(). Any file created while these modules are loaded should have the xattrs ("security.name1", "value1") and ("security.name2", "value2").

thanks,
-serge

I'd forgotten a 'quilt add'. The following trivial patch against security/Makefile is necessary to actually compile the test LSMs...

thanks,
-serge

security/Makefile +1 -0
Index: linux-2.6.13-rc6-mm1/security/Makefile
===================================================================
@@ -19,3 +19,4 @@ obj-$(CONFIG_SECURITY_CAPABILITIES)	+= c
 obj-$(CONFIG_SECURITY_CAP_STACK)	+= commoncap.o cap_stack.o
 obj-$(CONFIG_SECURITY_ROOTPLUG)		+= commoncap.o root_plug.o
 obj-$(CONFIG_SECURITY_SECLVL)		+= seclvl.o
+obj-m					+= testinitsec1.o testinitsec2.o
#5 lore raw
Quoted 22 lines · On Mon, Aug 22, 2005 at 11:52:09AM -0500, [email protected] wrote:
Quoted 7 lines · Quoting [email protected] ([email protected]):

This patch adds two stackable test LSMs which only define inode_init_security(). Any file created while these modules are loaded should have the xattrs ("security.name1", "value1") and ("security.name2", "value2").

thanks,
-serge

I'd forgotten a 'quilt add'. The following trivial patch against security/Makefile is necessary to actually compile the test LSMs...

thanks,
-serge

Index: linux-2.6.13-rc6-mm1/security/Makefile
===================================================================

security/Makefile obj-$(CONFIG_SECURITY_CAPABILITIES) += c · line 22

Quoted 4 lines
security/Makefile +1 -0
@@ -19,3 +19,4 @@ obj-$(CONFIG_SECURITY_CAPABILITIES)	+= c
 obj-$(CONFIG_SECURITY_CAP_STACK)	+= commoncap.o cap_stack.o
 obj-$(CONFIG_SECURITY_ROOTPLUG)		+= commoncap.o root_plug.o
 obj-$(CONFIG_SECURITY_SECLVL)		+= seclvl.o
+obj-m					+= testinitsec1.o testinitsec2.o

You want to build these modules always and always modular?

No matter whether the security subsystem is built modular or static?

No matter whether the user has enabled or completely disabled the security subsystem?

...

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#6 lore raw
Quoted 6 lines · * Adrian Bunk ([email protected]) wrote:

You want to build these modules always and always modular?

No matter whether the security subsystem is built modular or static?

No matter whether the user has enabled or completely disabled the security subsystem?

The modules are just for testing, and shouldn't be added at all.

#7 lore raw
Quoted 9 lines · Chris Wright
Quoted 6 lines · * Adrian Bunk ([email protected]) wrote:

You want to build these modules always and always modular?

No matter whether the security subsystem is built modular or static?

No matter whether the user has enabled or completely disabled the security subsystem?

The modules are just for testing, and shouldn't be added at all.

Right, in no way should the testing modules be upstreamed :)

thanks,
-serge