ima: Linux Security Module implementation
PATCH 1/1 ima: Linux Security Module implementation
This is the 3rd of 4 patches that constitute the IBM Integrity Measurement Architecture (IMA). This patch includes the main IMA functionality as a Linux Security Module.
This patch applies to the clean 2.6.12-rc4 test kernel.
---
security/ima/ima.h +227 -0
@@ -0,0 +1,227 @@ +/* +* Copyright (C) 2005 IBM Corporation +* +* Authors: +* Reiner Sailer <[email protected]> +* +* Maintained by: TBD +* +* LSM IBM Integrity Measurement Architecture. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation, version 2 of the +* License. +* +* File: ima.h +* defs +*/ +#ifndef __LINUX_IMA_H +#define __LINUX_IMA_H + +#include <linux/types.h> +#include <linux/fs.h> +#include <linux/security.h> + +#define ima_printk(level, format, arg...) \ + printk(level "ima (%s): " format ,__func__, ## arg) + +#define ima_error(format, arg...) \ + ima_printk(KERN_ERR, format, ## arg) + +#define ima_info(format, arg...) \ + ima_printk(KERN_INFO, format, ## arg) + +/* set during registering as lsm */ +extern unsigned char ima_terminating; + +#define IMA_MEASURE_MODULE_NAME "IMA" +#define IMA_MEASURE_PROC_NAME "ima" + +/* file systems we expect to change without +* our inode_permission hook being called (nfs, remote fs) */ +#define NFS_SUPER_MAGIC 0x6969 + +/* file systems we won't measure +(invalidate TPM PCR when executing one of these) */ +#define DEVFS_SUPER_MAGIC 0x1373 +#define PROC_SUPER_MAGIC 0x9fa0 +#define SYSFS_MAGIC 0x62656572 + +/* +* request structure fd: file descriptor +* label: sec label defined in user space or in kernel +* flags: store the hook that initiated measurement and more +*/ +#define FLAG_HOOK_MASK 0x0f +#define MMAP_MEASURE_FLAG 0x01 +#define MODULE_MEASURE_FLAG 0x02 +#define USER_MEASURE_FLAG 0x04 +struct measure_request { +int fd; +unsigned short label; +unsigned long flags; +}; + +#define MEASURE_HTABLE_SIZE 512 +/* key = lowest two bytes of inode_number */ +#define HASH_KEY (inode_number) ((inode_number) % MEASURE_HTABLE_SIZE) +#define SHA_KEY (sha_value) (((sha_value)[18] << 8 | (sha_value)[19]) % MEASURE_HTABLE_SIZE) +typedef enum {CLEAN ,DIRTY ,CHANGED }ima_entry_flags ; + +/* security structure appended to inodes */ +#define IMA_MAGIC 0x9999 +struct ima_inode { +unsigned short magic; +atomic_t measure_count;/* # processes currently using this file in measure-mode */ +ima_entry_flags dirty; +char *file_name;/* points to measure entry->fileName */ +}; + +/* security structure appended to measured files*/ +struct ima_file { +unsigned short magic;/* identify our struct format */ +char is_measuring;/* identify fds that are "measuring" */ +}; + +/* get/store security state information; +* if stacking were to be implemented, this would be the place */ +#define ima_get_inode_security (inode) \ + ((struct ima_inode *) ((inode)->i_security)) + +#define ima_store_inode_security (inode,sec_struct) \ + ((inode)->i_security = (sec_struct)) + +#define ima_get_file_security (file) \ + ((struct ima_file *) ((file)->f_security)) + +#define ima_store_file_security (file, sec_struct) \ + ((file)->f_security = (sec_struct)) + +#define ENTRY_MAXFILENAME 50 +struct measure_entry { +struct measure_request mr;/* keep info from measure request if applies */ +unsigned long inodeNr; +dev_t devId; +ima_entry_flags dirty; +u8 digest[20 ];/* sha1 measurement hash */ +char fileName[51 ];/* max first 50 characters of name + \0 */ +unsigned long fsMagic;/* file system magic (distinuish local/remote files) */ +struct super_block *superBlock;/* super block link (for umount-dirty flagging) */ +}; + +struct sha_entry { +struct sha_entry *next; +u8 *digest; +struct measure_entry *m_entry; +}; + +struct queue_entry { +struct queue_entry *next; +struct queue_entry *later; +struct measure_entry *entry; +}; + +extern struct queue_entry *first_measurement;/* for printing */ +extern struct queue_entry *latest_measurement;/* for adding */ + +/* hash table to keep fast access to past measurements +* uses one global lock for now (read/write) */ +extern struct semaphore h_table_mutex; + +struct h_table { +atomic_t len; +atomic_t sysfs; +atomic_t cleanInodeHits;/* times we find an inode clean when measuring */ +atomic_t cleanTableHits;/* times we find a clean htable hit */ +atomic_t dirtyTableHits;/* times we find a dirty htable hit */ +atomic_t changedFiles;/* times we realize a dirty marked entry really changed */ +unsigned int max_htable_size; +u8 terminating; +struct queue_entry *queue[MEASURE_HTABLE_SIZE ]; +atomic_t queueLen[MEASURE_HTABLE_SIZE ]; +spinlock_t lock; +}; + +struct sha_table { +atomic_t len; +unsigned int max_htable_size; +u8 terminating; +struct sha_entry *queue[MEASURE_HTABLE_SIZE ]; +atomic_t queueLen[MEASURE_HTABLE_SIZE ]; +spinlock_t lock; +}; + +/* configuration options*/ +extern int ima_test_mode; +extern int skip_boot_aggregate; +extern int ram_bypass_protection; +extern int hd_sd_bypass_protection; +extern int kmem_bypass_protection; +extern int mem_bypass_protection; + +static inline void read_configs (void ) +{ +#ifdef CONFIG_IMA_TEST_MODE + ima_test_mode =1 ; +#else + ima_test_mode =0 ; +#endif + +#ifdef CONFIG_IMA_SKIP_BOOT_AGGREGATE + skip_boot_aggregate =1 ; +#else + skip_boot_aggregate =0 ; +#endif + +#ifdef CONFIG_IMA_RAM_BYPASS_PROTECTION + ram_bypass_protection =1 ; +#else + ram_bypass_protection =0 ; +#endif + +#ifdef CONFIG_IMA_HD_SD_BYPASS_PROTECTION + hd_sd_bypass_protection =1 ; +#else + hd_sd_bypass_protection =0 ; +#endif + +#ifdef CONFIG_IMA_KMEM_BYPASS_PROTECTION + kmem_bypass_protection =1 ; +#else + kmem_bypass_protection =0 ; +#endif + +#ifdef CONFIG_IMA_MEM_BYPASS_PROTECTION + mem_bypass_protection =1 ; +#else + mem_bypass_protection =0 ; +#endif +} + +#ifdef CONFIG_TCG_TPM +struct tpm_chip ; + +extern ssize_t tpm_transmit (struct tpm_chip *chip,const char *buf,size_t bufsiz); + +extern struct tpm_chip *tpm_chip_lookup (int chip_num); +#else +struct tpm_chip { +char dummy; +}; + +static inline ssize_t tpm_transmit (struct tpm_chip *chip,const char *buf,size_t bufsiz) +{ +return 0 ; +} + +static inline struct tpm_chip *tpm_chip_lookup (int chip_num) +{ +return NULL ; +} +#endif + +/* general prototypes */ +void invalidate_pcr (char *); + +#endif
security/ima/ima_init.c +161 -0
161 lines not on this page · raw message
security/ima/ima_lsmhooks.c +242 -0
242 lines not on this page · raw message
security/ima/ima_main.c +596 -0
596 lines not on this page · raw message
security/ima/ima_proc.c +444 -0
444 lines not on this page · raw message
security/ima/ima_queue.c +241 -0
241 lines not on this page · raw message
security/ima/ima_sysfs.c +150 -0
150 lines not on this page · raw message
security/ima/ima_tpm_glue.c +105 -0
105 lines not on this page · raw message
security/ima/INSTALL +124 -0
124 lines not on this page · raw message
security/ima/Kconfig +103 -0
103 lines not on this page · raw message
security/ima/Makefile +12 -0
@@ -0,0 +1,12 @@ +# +# Makefile for the TCG run-time Measurements +# +# Author: [email protected] +# adapted to 2.6 kernel + +ifdef CONFIG_IMA_MEASURE +obj-$(CONFIG_IMA_MEASURE) += ima_init.o ima_sysfs.o ima_main.o ima_proc.o \ + ima_queue.o ima_lsmhooks.o ima_tpm_glue.o + +endif +
security/Kconfig +1 -0
@@ -86,6 +86,7 @@ config SECURITY_SECLVL If you are unsure how to answer this question, answer N. source security/selinux/Kconfig +source security/ima/Kconfig endmenu
security/Makefile +2 -0
@@ -4,6 +4,7 @@ obj-$(CONFIG_KEYS) += keys/ subdir-$(CONFIG_SECURITY_SELINUX) += selinux +subdir-$(CONFIG_IMA_MEASURE) += ima # if we don't select a security model, use the default capabilities ifneq ($(CONFIG_SECURITY),y)
@@ -14,6 +15,7 @@ endif obj-$(CONFIG_SECURITY) += security.o dummy.o # Must precede capability.o in order to stack properly. obj-$(CONFIG_SECURITY_SELINUX) += selinux/built-in.o +obj-$(CONFIG_IMA_MEASURE) += ima/built-in.o obj-$(CONFIG_SECURITY_CAPABILITIES) += commoncap.o capability.o obj-$(CONFIG_SECURITY_ROOTPLUG) += commoncap.o root_plug.o obj-$(CONFIG_SECURITY_SECLVL) += seclvl.o
security/ima/ima_sysfs.c line 7
Quoted 11 lines · Reiner Sailer
diff -uprN
linux-2.6.12-rc4/security/ima/ima_sysfs.clinux-2.6.12-rc4-ima/security/ima/ima_sysfs.csecurity/ima/ima_sysfs.c +7
-0@@ -0,0 +1,150 @@ +/* +* Copyright (C) 2005 IBM Corporation +* +* Authors: +* Reiner Sailer <[email protected]> +* +* Maintained by: TBD
Who is going to maintain this? That needs to be answered before we can even consider accepting this code.
Quoted 9 lines
security/ima/ima_sysfs.c +9
-0+* LSM IBM Integrity Measurement Architecture. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation, version 2 of the +* License. +* +* File: ima_sysfs.c +* sysfs interface to request measurements
security/ima/ima_sysfs.c line 27
Quoted 10 lines
security/ima/ima_sysfs.c +10
-0+* through instrumented user applications +*/ +#include <linux/config.h> +#include <linux/module.h> +#include <linux/fs.h> +#include <linux/file.h> +#include <linux/init.h> +#include <linux/spinlock.h> +#include <linux/kmod.h> +#include <linux/kobj_map.h>
Why do you need this .h file?
security/ima/ima_sysfs.c line 34
Quoted 7 lines
security/ima/ima_sysfs.c +7
-0+#include <linux/sysfs.h> +#include "ima.h" + +static struct subsystem security_subsys; + +atomic_t global_count_sysfs; +atomic_t global_count_sysfs_measure;
bad bad bad global variable names. Who initializes these atomic values?
security/ima/ima_sysfs.c line 35
Quoted 2 lines
security/ima/ima_sysfs.c +2
-0+ +int measure_file_exec (struct file *,const struct measure_request *);
Lousy global function name. And it should be in a header file somewhere, right?
security/ima/ima_sysfs.c line 46
Quoted 10 lines
security/ima/ima_sysfs.c +10
-0+ +static ssize_t security_attr_show (struct kobject *kobj, +struct attribute *attr,char *page) +{ +/* get security attribute */ +struct subsys_attribute *security_attr = + container_of(attr, struct subsys_attribute, attr); + +if (security_attr->show) + security_attr->show (&security_subsys, page);
Why does your show attribute not return the proper amount?
security/ima/ima_sysfs.c line 51
Quoted 5 lines
security/ima/ima_sysfs.c +5
-0+else +ima_error ("Attr method >show< not defined.\n" ); + +return (ssize_t )0 ; +}
No, return the value of the show function.
security/ima/ima_sysfs.c line 65
Quoted 13 lines
security/ima/ima_sysfs.c +13
-0+static ssize_t security_attr_store (struct kobject *kobj,struct attribute *attr, +const char *page,size_t count) +{ +/* get security attribute */ +struct subsys_attribute *security_attr = + container_of(attr, struct subsys_attribute, attr); +if (security_attr->store) + security_attr->store (&security_subsys, page, count); +else +ima_error ("Attr method >store< not defined.\n" ); + +return (ssize_t ) count; +}
Same issue here as above with the show attribute.
security/ima/ima_sysfs.c line 77
Quoted 11 lines
security/ima/ima_sysfs.c +11
-0+static struct sysfs_ops security_sysfs_ops = { + .show = &security_attr_show, + .store = &security_attr_store, +}; + +static ssize_t measurements_read (struct subsystem *sub,char *page) +{ +char *msg ="Hi There! Read is not supported :-)\n" ; +strncpy (page, msg,PAGE_SIZE ); +return (strlen (msg)); +}
No, just don't provide a read function, and set the mode properly, and you will not need this at all.
security/ima/ima_sysfs.c line 91
Quoted 13 lines
security/ima/ima_sysfs.c +13
-0+static ssize_t measurement_store (struct subsystem *sub,const char *page, +size_t count) +{ +struct measure_request *mr; +struct file *file; +int error = -EINVAL ; + +atomic_inc (&global_count_sysfs); +if (count !=sizeof (struct measure_request )) { +ima_error ("illegal request size (%d, expected %d).\n" , + count,sizeof (struct measure_request )); +return -EIO ; + }
Free form text is going to fit into a specific size?
security/ima/ima_sysfs.c line 94
Quoted 3 lines
security/ima/ima_sysfs.c +3
-0+ mr = (struct measure_request *) page; +if (mr->fd <0 ) +return -EBADF ;
Woah! You can't cast a structure to a ascii value. And I thought I had seen all of the ways you could abuse sysfs so far...
Please, sysfs is for:
- ascii values
- one value per file.
In fact, if you had read the sysfs documentation, it would have explained this. And people complain when there's no documentation, when in reality, it's just simply ignored anyway...
Do not attempt to do this, it will be rejected.
Quoted 9 lines
security/ima/ima_sysfs.c +9
-0+ file =fget (mr->fd); +if (!file) +return -EACCES ; + mr->flags = ((mr->flags) & (~FLAG_HOOK_MASK )) ||USER_MEASURE_FLAG ; +/* future: check inode->security to see if measure necessary */ +atomic_inc (&global_count_sysfs_measure); + error =measure_file_exec (file, mr); +fput (file); +if (error)
security/ima/ima_sysfs.c line 114
Quoted 10 lines
security/ima/ima_sysfs.c +10
-0+return error; + else + return (ssize_t) count; /* length of written data */ +} + +static struct subsys_attribute security_attr_measure = { + .attr = {.name ="measure" ,.mode =S_IRUGO |S_IWUGO }, + .show = measurements_read, + .store = measurement_store, +};
Use __ATTR() to properly set the module owner (right now you can open the sysfs file, remove the module, and then read from it. oops...)
security/ima/ima_sysfs.c line 119
Quoted 4 lines
security/ima/ima_sysfs.c +4
-0+static struct attribute *default_security_attrs[] = { + &security_attr_measure.attr, +NULL , +};
Ok, nice, but you got it wrong below...
security/ima/ima_sysfs.c line 124
Quoted 4 lines
security/ima/ima_sysfs.c +4
-0+static void security_object_release (struct kobject *kobj) +{ +return ; +}
Um, no. This is a HUGE RED FLAG that you did something wrong.
Quoted 6 lines
security/ima/ima_sysfs.c +6
-0+static struct kobj_type ktype_security = { + .release = security_object_release, + .sysfs_ops = &security_sysfs_ops, + .default_attrs = default_security_attrs, +}; +
security/ima/ima_sysfs.c line 141
Quoted 10 lines
security/ima/ima_sysfs.c +10
-0+/* declare security_subsys. */ +static decl_subsys(security, &ktype_security, NULL); + + +int ima_sysfs_init (void ) +{ +atomic_set (&global_count_sysfs,0 ); +atomic_set (&global_count_sysfs_measure,0 ); +subsystem_register (&security_subsys); +subsys_create_file (&security_subsys, &security_attr_measure);
Wait, you create a default attribute array, which will be automatically created, when you register a device in this subsystem. And then you create the same file again. Good thing you never checked that return value to catch the error the kernel is trying to tell you...
security/ima/ima_sysfs.c line 150
Quoted 9 lines
security/ima/ima_sysfs.c +9
-0+return 0 ; +} + +int ima_sysfs_remove (void ) +{ +subsys_remove_file (&security_subsys, &security_attr_measure); +subsystem_unregister (&security_subsys); +return 0 ; +}
Nope, that default attribute will take care of being removed automatically, you don't have to try to do it again (but I appreciate the effort.)
Wow, for such a small file, every single function was incorrect. And you abused sysfs in a new and intersting way that I didn't think was even possible. I think this is two new records you have set here, congratulations.
greg k-h
On 5/20/05, Greg KH <[email protected]> wrote:
...
Quoted 8 lines
Wow, for such a small file, every single function was incorrect. And you abused sysfs in a new and intersting way that I didn't think was even possible. I think this is two new records you have set here, congratulations.
greg k-h
never doubt that this should be the "quote of the week" for lwn.net ;-))
just kidding ;-))
regards,
aq
Quoted 4 lines · Reiner Sailer
security/ima/ima.h +4
-0+/* security structure appended to inodes */ +#define IMA_MAGIC 0x9999 +struct ima_inode { +unsigned short magic;
security/ima/ima.h line 85
Quoted 10 lines
security/ima/ima.h +10
-0+atomic_t measure_count;/* # processes currently using this file in measure-mode */ +ima_entry_flags dirty; +char *file_name;/* points to measure entry->fileName */ +}; + +/* security structure appended to measured files*/ +struct ima_file { +unsigned short magic;/* identify our struct format */ +char is_measuring;/* identify fds that are "measuring" */ +};
magic values for structures protect you from nothing. Do not use them.
Quoted 6 lines
security/ima/ima_tpm_glue.c +6
-0+static u32 decode_u32 (u8 * buf) +{ +u32 val = buf[0 ]; + val = (val <<8 ) | (u8 ) buf[1 ]; + val = (val <<8 ) | (u8 ) buf[2 ]; + val = (val <<8 ) | (u8 ) buf[3 ];
security/ima/ima_tpm_glue.c line 56
Quoted 10 lines
security/ima/ima_tpm_glue.c +10
-0+return val; +} + +static void encode_u32 (u8 * buf,u32 val) +{ + buf[0 ] = (u8 ) val >>24 ; + buf[1 ] = (u8 ) val >>16 ; + buf[2 ] = (u8 ) val >>8 ; + buf[3 ] = (u8 ) val >>0 ; +}
Hm, what's wrong with the standard kernel functions to do this kind of thing?
security/ima/INSTALL
Quoted 3 lines
diff -uprN linux-2.6.12-rc4/security/ima/INSTALL linux-2.6.12-rc4-ima/security/ima/INSTALL
security/ima/INSTALL +0
-0
Kernel directories do not get a INSTALL file. Stuff like that goes into the Documentation/ directory.
greg k-h