Skip to contentSkip to conversation

ima: Linux Security Module implementation

PATCH New Reiner Sailer 1 participant 1 message May 20, 2005 Security (LSM)

lore
Share & tools
lore
#0 lore raw

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
diff -uprN linux-2.6.12-rc4/security/ima/ima.h linux-2.6.12-rc4-ima/security/ima/ima.h
@@ -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
diff -uprN linux-2.6.12-rc4/security/ima/Makefile linux-2.6.12-rc4-ima/security/ima/Makefile
@@ -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
diff -uprN linux-2.6.12-rc4/security/Kconfig linux-2.6.12-rc4-ima/security/Kconfig
@@ -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
diff -uprN linux-2.6.12-rc4/security/Makefile linux-2.6.12-rc4-ima/security/Makefile
@@ -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