Skip to contentSkip to conversation

vfss: support for a common kernel file loader

RFC PATCH Superseded Mimi Zohar 8 participants 50 messages Jan 18, 2016 – Jan 27, 2016

Subsystems (9)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mimi Zohar

author
#0 lore raw

v2 cover vfss: support for a common kernel file loader

For a while it was looked down upon to directly read files from Linux. These days there exists a few mechanisms in the kernel that do just this though to load a file into a local buffer. There are minor but important checks differences on each, we should take all the best practices from each of them, generalize them and make all places in the kernel that read a file use it.[1]

One difference is the method for opening the file. In some cases we have a file, while in other cases we have a pathname or a file descriptor.

Another difference is the security hook calls, or lack of them. In some versions there is a post file read hook, while in others there is a pre file read hook.

This patch set is the first attempt at resolving these differences. It does not attempt to merge the different methods of opening a file, but defines a single common kernel file read function with two wrappers. Although this patch set defines two new security hooks for pre and post file read, it does not attempt to merge the existing security hooks. That is left as future work.

Changelog v2:
- Combined the "ima: measuring/appraising files read by the kernel" patches with this patch set to simplify review.
- Split the "ima: measure and appraise kexec image and initramfs" patch to separate IMA from the kexec changes.

The latest version of these patches can be found in the next-kernel-read-v2 branch of:
git://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git

[1] Taken from Luis Rodriguez's wiki -
http://kernelnewbies.org/KernelProjects/common-kernel-loader

Mimi

Dmitry Kasatkin (3):
ima: separate 'security.ima' reading functionality from collect
ima: provide buffer hash calculation function
ima: load policy using path

Mimi Zohar (8):
vfs: define a generic function to read a file from the kernel
ima: calculate the hash of a buffer using aynchronous hash(ahash)
ima: define a new hook to measure and appraise a file already in
memory
kexec: replace call to copy_file_from_fd() with kernel version
firmware: replace call to fw_read_file_contents() with kernel version
module: replace copy_module_from_fd with kernel version
ima: measure and appraise the IMA policy itself
ima: require signed IMA policy

 Documentation/ABI/testing/ima_policy      |   2 +-
 drivers/base/firmware_class.c             |  48 ++++--------
 fs/exec.c                                 |  93 +++++++++++++++++++++++
 include/linux/fs.h                        |   3 +
 include/linux/ima.h                       |  17 ++++-
 include/linux/lsm_hooks.h                 |  19 +++++
 include/linux/security.h                  |  14 ++--
 kernel/kexec_file.c                       |  72 ++----------------
 kernel/module.c                           |  67 ++--------------
 security/integrity/iint.c                 |   1 +
 security/integrity/ima/ima.h              |  35 +++++----
 security/integrity/ima/ima_api.c          |  19 ++---
 security/integrity/ima/ima_appraise.c     |  45 +++++------
 security/integrity/ima/ima_crypto.c       | 120 ++++++++++++++++++++++++++++-
 security/integrity/ima/ima_fs.c           |  50 +++++++++++-
 security/integrity/ima/ima_init.c         |   2 +-
 security/integrity/ima/ima_main.c         |  52 +++++++++----
 security/integrity/ima/ima_policy.c       | 122 +++++++++++++++++++-----------
 security/integrity/ima/ima_template.c     |   2 -
 security/integrity/ima/ima_template_lib.c |   1 -
 security/integrity/integrity.h            |  14 ++--
 security/security.c                       |  46 +++++++----
 22 files changed, 540 insertions(+), 304 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

RFC PATCH v2 1/11 ima: separate 'security.ima' reading functionality from collect

From: Dmitry Kasatkin <[email protected]>

Instead of passing pointers to pointers to ima_collect_measurent() to read and return the 'security.ima' xattr value, this patch moves the functionality to the calling process_measurement() to directly read the xattr and pass only the hash algo to the ima_collect_measurement().

---
 security/integrity/ima/ima.h              | 15 +++++++--------
 security/integrity/ima/ima_api.c          | 15 +++------------
 security/integrity/ima/ima_appraise.c     | 25 ++++++++++++++-----------
 security/integrity/ima/ima_crypto.c       |  2 +-
 security/integrity/ima/ima_init.c         |  2 +-
 security/integrity/ima/ima_main.c         | 11 +++++++----
 security/integrity/ima/ima_template.c     |  2 --
 security/integrity/ima/ima_template_lib.c |  1 -
 8 files changed, 33 insertions(+), 40 deletions(-)
security/integrity/ima/ima.h +7 -8
index 585af61..fb8da36 100644
@@ -23,6 +23,7 @@
 #include <linux/hash.h>
 #include <linux/tpm.h>
 #include <linux/audit.h>
+#include <crypto/hash_info.h>
 
 #include "../integrity.h"
 
@@ -140,9 +141,7 @@ static inline unsigned long ima_hash_key(u8 *digest)
 int ima_get_action(struct inode *inode, int mask, int function);
 int ima_must_measure(struct inode *inode, int mask, int function);
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-			    struct file *file,
-			    struct evm_ima_xattr_data **xattr_value,
-			    int *xattr_len);
+			    struct file *file, enum hash_algo algo);
 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
 			   const unsigned char *filename,
 			   struct evm_ima_xattr_data *xattr_value,
@@ -188,8 +187,8 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 					   int func);
-void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
-		       struct ima_digest_data *hash);
+enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
+				 int xattr_len);
 int ima_read_xattr(struct dentry *dentry,
 		   struct evm_ima_xattr_data **xattr_value);
 
@@ -221,10 +220,10 @@ static inline enum integrity_status ima_get_cache_status(struct integrity_iint_c
 	return INTEGRITY_UNKNOWN;
 }
 
-static inline void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
-				     int xattr_len,
-				     struct ima_digest_data *hash)
+static inline enum hash_algo
+ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
 {
+	return ima_hash_algo;
 }
 
 static inline int ima_read_xattr(struct dentry *dentry,
security/integrity/ima/ima_api.c +3 -12
index 1d950fb..e7c7a5d 100644
@@ -18,7 +18,7 @@
 #include <linux/fs.h>
 #include <linux/xattr.h>
 #include <linux/evm.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 /*
@@ -188,9 +188,7 @@ int ima_get_action(struct inode *inode, int mask, int function)
  * Return 0 on success, error code otherwise
  */
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-			    struct file *file,
-			    struct evm_ima_xattr_data **xattr_value,
-			    int *xattr_len)
+			    struct file *file, enum hash_algo algo)
 {
 	const char *audit_cause = "failed";
 	struct inode *inode = file_inode(file);
@@ -201,9 +199,6 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
 		char digest[IMA_MAX_DIGEST_SIZE];
 	} hash;
 
-	if (xattr_value)
-		*xattr_len = ima_read_xattr(file->f_path.dentry, xattr_value);
-
 	if (!(iint->flags & IMA_COLLECTED)) {
 		u64 i_version = file_inode(file)->i_version;
 
@@ -213,11 +208,7 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
 			goto out;
 		}
 
-		/* use default hash algorithm */
-		hash.hdr.algo = ima_hash_algo;
-
-		if (xattr_value)
-			ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
+		hash.hdr.algo = algo;
 
 		result = ima_calc_file_hash(file, &hash.hdr);
 		if (!result) {
security/integrity/ima/ima_appraise.c +14 -11
index 1873b55..9c2b46b 100644
@@ -15,7 +15,6 @@
 #include <linux/magic.h>
 #include <linux/ima.h>
 #include <linux/evm.h>
-#include <crypto/hash_info.h>
 
 #include "ima.h"
 
@@ -130,36 +129,40 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	}
 }
 
-void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
-		       struct ima_digest_data *hash)
+enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
+				 int xattr_len)
 {
 	struct signature_v2_hdr *sig;
 
 	if (!xattr_value || xattr_len < 2)
-		return;
+		/* return default hash algo */
+		return ima_hash_algo;
 
 	switch (xattr_value->type) {
 	case EVM_IMA_XATTR_DIGSIG:
 		sig = (typeof(sig))xattr_value;
 		if (sig->version != 2 || xattr_len <= sizeof(*sig))
-			return;
-		hash->algo = sig->hash_algo;
+			return ima_hash_algo;
+		return sig->hash_algo;
 		break;
 	case IMA_XATTR_DIGEST_NG:
-		hash->algo = xattr_value->digest[0];
+		return xattr_value->digest[0];
 		break;
 	case IMA_XATTR_DIGEST:
 		/* this is for backward compatibility */
 		if (xattr_len == 21) {
 			unsigned int zero = 0;
 			if (!memcmp(&xattr_value->digest[16], &zero, 4))
-				hash->algo = HASH_ALGO_MD5;
+				return HASH_ALGO_MD5;
 			else
-				hash->algo = HASH_ALGO_SHA1;
+				return HASH_ALGO_SHA1;
 		} else if (xattr_len == 17)
-			hash->algo = HASH_ALGO_MD5;
+			return HASH_ALGO_MD5;
 		break;
 	}
+
+	/* return default hash algo */
+	return ima_hash_algo;
 }
 
 int ima_read_xattr(struct dentry *dentry,
@@ -296,7 +299,7 @@ void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
 	if (iint->flags & IMA_DIGSIG)
 		return;
 
-	rc = ima_collect_measurement(iint, file, NULL, NULL);
+	rc = ima_collect_measurement(iint, file, ima_hash_algo);
 	if (rc < 0)
 		return;
 
security/integrity/ima/ima_crypto.c +1 -1
index 6eb6293..fb30ce4 100644
@@ -24,7 +24,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <crypto/hash.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 struct ahash_completion {
security/integrity/ima/ima_init.c +1 -1
index bd79f25..5d679a6 100644
@@ -21,7 +21,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/err.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 /* name for boot aggregate entry */
security/integrity/ima/ima_main.c +7 -4
index c21f09b..d9fc463 100644
@@ -24,7 +24,6 @@
 #include <linux/slab.h>
 #include <linux/xattr.h>
 #include <linux/ima.h>
-#include <crypto/hash_info.h>
 
 #include "ima.h"
 
@@ -163,9 +162,10 @@ static int process_measurement(struct file *file, int mask, int function,
 	char *pathbuf = NULL;
 	const char *pathname = NULL;
 	int rc = -ENOMEM, action, must_appraise;
-	struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
+	struct evm_ima_xattr_data *xattr_value = NULL;
 	int xattr_len = 0;
 	bool violation_check;
+	enum hash_algo hash_algo;
 
 	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
 		return 0;
@@ -221,9 +221,12 @@ static int process_measurement(struct file *file, int mask, int function,
 	template_desc = ima_template_desc_current();
 	if ((action & IMA_APPRAISE_SUBMASK) ||
 		    strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
-		xattr_ptr = &xattr_value;
+		/* read 'security.ima' */
+		xattr_len = ima_read_xattr(file->f_path.dentry, &xattr_value);
 
-	rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
+	hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
+
+	rc = ima_collect_measurement(iint, file, hash_algo);
 	if (rc != 0) {
 		if (file->f_flags & O_DIRECT)
 			rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
security/integrity/ima/ima_template.c +0 -2
index 0b7404e..febd12e 100644
@@ -15,8 +15,6 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
-#include <crypto/hash_info.h>
-
 #include "ima.h"
 #include "ima_template_lib.h"
 
security/integrity/ima/ima_template_lib.c +0 -1
index 2934e3d..f9bae04 100644
@@ -12,7 +12,6 @@
  * File: ima_template_lib.c
  *      Library of supported template fields.
  */
-#include <crypto/hash_info.h>
 
 #include "ima_template_lib.h"
 
Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#2 lore raw

Re: [RFC PATCH v2 01/11] ima: separate 'security.ima' reading functionality from collect

Hi Mimi,

Please change

Thanks

Dmitry

Quoted 279 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

Instead of passing pointers to pointers to ima_collect_measurent() to read and return the 'security.ima' xattr value, this patch moves the functionality to the calling process_measurement() to directly read the xattr and pass only the hash algo to the ima_collect_measurement().

---
 security/integrity/ima/ima.h              | 15 +++++++--------
 security/integrity/ima/ima_api.c          | 15 +++------------
 security/integrity/ima/ima_appraise.c     | 25 ++++++++++++++-----------
 security/integrity/ima/ima_crypto.c       |  2 +-
 security/integrity/ima/ima_init.c         |  2 +-
 security/integrity/ima/ima_main.c         | 11 +++++++----
 security/integrity/ima/ima_template.c     |  2 --
 security/integrity/ima/ima_template_lib.c |  1 -
 8 files changed, 33 insertions(+), 40 deletions(-)
security/integrity/ima/ima.h +7 -8
index 585af61..fb8da36 100644
@@ -23,6 +23,7 @@
 #include <linux/hash.h>
 #include <linux/tpm.h>
 #include <linux/audit.h>
+#include <crypto/hash_info.h>
 
 #include "../integrity.h"
 
@@ -140,9 +141,7 @@ static inline unsigned long ima_hash_key(u8 *digest)
 int ima_get_action(struct inode *inode, int mask, int function);
 int ima_must_measure(struct inode *inode, int mask, int function);
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-                           struct file *file,
-                           struct evm_ima_xattr_data **xattr_value,
-                           int *xattr_len);
+                           struct file *file, enum hash_algo algo);
 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
                           const unsigned char *filename,
                           struct evm_ima_xattr_data *xattr_value,
@@ -188,8 +187,8 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
                                           int func);
-void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
-                      struct ima_digest_data *hash);
+enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
+                                int xattr_len);
 int ima_read_xattr(struct dentry *dentry,
                   struct evm_ima_xattr_data **xattr_value);
 
@@ -221,10 +220,10 @@ static inline enum integrity_status ima_get_cache_status(struct integrity_iint_c
        return INTEGRITY_UNKNOWN;
 }
 
-static inline void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
-                                    int xattr_len,
-                                    struct ima_digest_data *hash)
+static inline enum hash_algo
+ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
 {
+       return ima_hash_algo;
 }
 
 static inline int ima_read_xattr(struct dentry *dentry,
security/integrity/ima/ima_api.c +3 -12
index 1d950fb..e7c7a5d 100644
@@ -18,7 +18,7 @@
 #include <linux/fs.h>
 #include <linux/xattr.h>
 #include <linux/evm.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 /*
@@ -188,9 +188,7 @@ int ima_get_action(struct inode *inode, int mask, int function)
  * Return 0 on success, error code otherwise
  */
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-                           struct file *file,
-                           struct evm_ima_xattr_data **xattr_value,
-                           int *xattr_len)
+                           struct file *file, enum hash_algo algo)
 {
        const char *audit_cause = "failed";
        struct inode *inode = file_inode(file);
@@ -201,9 +199,6 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
                char digest[IMA_MAX_DIGEST_SIZE];
        } hash;
 
-       if (xattr_value)
-               *xattr_len = ima_read_xattr(file->f_path.dentry, xattr_value);
-
        if (!(iint->flags & IMA_COLLECTED)) {
                u64 i_version = file_inode(file)->i_version;
 
@@ -213,11 +208,7 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
                        goto out;
                }
 
-               /* use default hash algorithm */
-               hash.hdr.algo = ima_hash_algo;
-
-               if (xattr_value)
-                       ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
+               hash.hdr.algo = algo;
 
                result = ima_calc_file_hash(file, &hash.hdr);
                if (!result) {
security/integrity/ima/ima_appraise.c +14 -11
index 1873b55..9c2b46b 100644
@@ -15,7 +15,6 @@
 #include <linux/magic.h>
 #include <linux/ima.h>
 #include <linux/evm.h>
-#include <crypto/hash_info.h>
 
 #include "ima.h"
 
@@ -130,36 +129,40 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
        }
 }
 
-void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
-                      struct ima_digest_data *hash)
+enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
+                                int xattr_len)
 {
        struct signature_v2_hdr *sig;
 
        if (!xattr_value || xattr_len < 2)
-               return;
+               /* return default hash algo */
+               return ima_hash_algo;
 
        switch (xattr_value->type) {
        case EVM_IMA_XATTR_DIGSIG:
                sig = (typeof(sig))xattr_value;
                if (sig->version != 2 || xattr_len <= sizeof(*sig))
-                       return;
-               hash->algo = sig->hash_algo;
+                       return ima_hash_algo;
+               return sig->hash_algo;
                break;
        case IMA_XATTR_DIGEST_NG:
-               hash->algo = xattr_value->digest[0];
+               return xattr_value->digest[0];
                break;
        case IMA_XATTR_DIGEST:
                /* this is for backward compatibility */
                if (xattr_len == 21) {
                        unsigned int zero = 0;
                        if (!memcmp(&xattr_value->digest[16], &zero, 4))
-                               hash->algo = HASH_ALGO_MD5;
+                               return HASH_ALGO_MD5;
                        else
-                               hash->algo = HASH_ALGO_SHA1;
+                               return HASH_ALGO_SHA1;
                } else if (xattr_len == 17)
-                       hash->algo = HASH_ALGO_MD5;
+                       return HASH_ALGO_MD5;
                break;
        }
+
+       /* return default hash algo */
+       return ima_hash_algo;
 }
 
 int ima_read_xattr(struct dentry *dentry,
@@ -296,7 +299,7 @@ void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
        if (iint->flags & IMA_DIGSIG)
                return;
 
-       rc = ima_collect_measurement(iint, file, NULL, NULL);
+       rc = ima_collect_measurement(iint, file, ima_hash_algo);
        if (rc < 0)
                return;
 
security/integrity/ima/ima_crypto.c +1 -1
index 6eb6293..fb30ce4 100644
@@ -24,7 +24,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <crypto/hash.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 struct ahash_completion {
security/integrity/ima/ima_init.c +1 -1
index bd79f25..5d679a6 100644
@@ -21,7 +21,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/err.h>
-#include <crypto/hash_info.h>
+
 #include "ima.h"
 
 /* name for boot aggregate entry */
security/integrity/ima/ima_main.c +7 -4
index c21f09b..d9fc463 100644
@@ -24,7 +24,6 @@
 #include <linux/slab.h>
 #include <linux/xattr.h>
 #include <linux/ima.h>
-#include <crypto/hash_info.h>
 
 #include "ima.h"
 
@@ -163,9 +162,10 @@ static int process_measurement(struct file *file, int mask, int function,
        char *pathbuf = NULL;
        const char *pathname = NULL;
        int rc = -ENOMEM, action, must_appraise;
-       struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
+       struct evm_ima_xattr_data *xattr_value = NULL;
        int xattr_len = 0;
        bool violation_check;
+       enum hash_algo hash_algo;
 
        if (!ima_policy_flag || !S_ISREG(inode->i_mode))
                return 0;
@@ -221,9 +221,12 @@ static int process_measurement(struct file *file, int mask, int function,
        template_desc = ima_template_desc_current();
        if ((action & IMA_APPRAISE_SUBMASK) ||
                    strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
-               xattr_ptr = &xattr_value;
+               /* read 'security.ima' */
+               xattr_len = ima_read_xattr(file->f_path.dentry, &xattr_value);
 
-       rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
+       hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
+
+       rc = ima_collect_measurement(iint, file, hash_algo);
        if (rc != 0) {
                if (file->f_flags & O_DIRECT)
                        rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
security/integrity/ima/ima_template.c +0 -2
index 0b7404e..febd12e 100644
@@ -15,8 +15,6 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
-#include <crypto/hash_info.h>
-
 #include "ima.h"
 #include "ima_template_lib.h"
 
security/integrity/ima/ima_template_lib.c +0 -2
index 2934e3d..f9bae04 100644
@@ -12,7 +12,6 @@
  * File: ima_template_lib.c
  *      Library of supported template fields.
  */
-#include <crypto/hash_info.h>
 
 #include "ima_template_lib.h"
 
--

2.1.0

Signature · 2 lines
Thanks,
Dmitry
author
#3 lore raw

Re: [RFC PATCH v2 01/11] ima: separate 'security.ima' reading functionality from collect

Quoted 5 lines · Dmitry Kasatkin

Hi Mimi,

Please change

I'll make the change here and in the other patches as well.

Mimi

maintainer
#4 lore raw

Re: [RFC PATCH v2 01/11] ima: separate 'security.ima' reading functionality from collect

Quoted 11 lines · Mimi Zohar
Quoted 5 lines · Dmitry Kasatkin

Hi Mimi,

Please change

I'll make the change here and in the other patches as well.

Mimi

Thanks.

Signature · 2 lines
Thanks,
Dmitry
author
#5 lore raw

RFC PATCH v2 2/11 vfs: define a generic function to read a file from the kernel

In reply to Mimi Zohar

For a while it was looked down upon to directly read files from Linux. These days there exists a few mechanisms in the kernel that do just this though to load a file into a local buffer. There are minor but important checks differences on each. This patch set is the first attempt at resolving some of these differences.

This patch introduces a common function for reading files from the kernel with the corresponding security post-read hook and function.

Changelog v1:
- To simplify patch review, re-ordered patches

---
 fs/exec.c                 | 53 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h        |  1 +
 include/linux/lsm_hooks.h |  9 ++++++++
 include/linux/security.h  |  7 +++++++
 security/security.c       |  8 +++++++
 5 files changed, 78 insertions(+)
fs/exec.c +53 -0
index b06623a..6d623c2 100644
@@ -56,6 +56,7 @@
 #include <linux/pipe_fs_i.h>
 #include <linux/oom.h>
 #include <linux/compat.h>
+#include <linux/vmalloc.h>
 
 #include <asm/uaccess.h>
 #include <asm/mmu_context.h>
@@ -831,6 +832,58 @@ int kernel_read(struct file *file, loff_t offset,
 
 EXPORT_SYMBOL(kernel_read);
 
+int kernel_read_file(struct file *file, void **buf, loff_t *size,
+		     loff_t max_size)
+{
+	loff_t i_size, pos;
+	ssize_t bytes = 0;
+	int ret;
+
+	if (!S_ISREG(file_inode(file)->i_mode))
+		return -EINVAL;
+
+	i_size = i_size_read(file_inode(file));
+	if (max_size > 0 && i_size > max_size)
+		return -EFBIG;
+	if (i_size == 0)
+		return -EINVAL;
+
+	*buf = vmalloc(i_size);
+	if (!*buf)
+		return -ENOMEM;
+
+	pos = 0;
+	while (pos < i_size) {
+		bytes = kernel_read(file, pos, (char *)(*buf) + pos,
+				    i_size - pos);
+		if (bytes < 0) {
+			ret = bytes;
+			goto out;
+		}
+
+		if (bytes == 0)
+			break;
+		pos += bytes;
+	}
+
+	if (pos != i_size) {
+		ret = -EIO;
+		goto out;
+	}
+
+	ret = security_kernel_post_read_file(file, *buf, i_size);
+	if (!ret)
+		*size = pos;
+
+out:
+	if (ret < 0) {
+		vfree(*buf);
+		*buf = NULL;
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(kernel_read_file);
+
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
 {
 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
include/linux/fs.h +1 -0
index 3aa5142..93ca379 100644
@@ -2527,6 +2527,7 @@ static inline void i_readcount_inc(struct inode *inode)
 extern int do_pipe_flags(int *, int);
 
 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
+extern int kernel_read_file(struct file *, void **, loff_t *, loff_t);
 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
include/linux/lsm_hooks.h +9 -0
index 71969de..f82631c 100644
@@ -561,6 +561,13 @@
  *	the kernel module to load. If the module is being loaded from a blob,
  *	this argument will be NULL.
  *	Return 0 if permission is granted.
+ * @kernel_post_read_file:
+ *	Read a file specified by userspace.
+ *	@file contains the file structure pointing to the file being read
+ *	by the kernel.
+ *	@buf pointer to buffer containing the file contents.
+ *	@size length of the file contents.
+ *	Return 0 if permission is granted.
  * @task_fix_setuid:
  *	Update the module's state after setting one or more of the user
  *	identity attributes of the current process.  The @flags parameter
@@ -1457,6 +1464,7 @@ union security_list_options {
 	int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
 	int (*kernel_module_request)(char *kmod_name);
 	int (*kernel_module_from_file)(struct file *file);
+	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size);
 	int (*task_fix_setuid)(struct cred *new, const struct cred *old,
 				int flags);
 	int (*task_setpgid)(struct task_struct *p, pid_t pgid);
@@ -1716,6 +1724,7 @@ struct security_hook_heads {
 	struct list_head kernel_act_as;
 	struct list_head kernel_create_files_as;
 	struct list_head kernel_fw_from_file;
+	struct list_head kernel_post_read_file;
 	struct list_head kernel_module_request;
 	struct list_head kernel_module_from_file;
 	struct list_head task_fix_setuid;
include/linux/security.h +7 -0
index 4824a4c..f30f564 100644
@@ -301,6 +301,7 @@ int security_kernel_create_files_as(struct cred *new, struct inode *inode);
 int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
 int security_kernel_module_request(char *kmod_name);
 int security_kernel_module_from_file(struct file *file);
+int security_kernel_post_read_file(struct file *file, char *buf, loff_t size);
 int security_task_fix_setuid(struct cred *new, const struct cred *old,
 			     int flags);
 int security_task_setpgid(struct task_struct *p, pid_t pgid);
@@ -866,6 +867,12 @@ static inline int security_kernel_module_from_file(struct file *file)
 	return 0;
 }
 
+static inline int security_kernel_post_read_file(struct file *file,
+						 char *buf, loff_t size)
+{
+	return 0;
+}
+
 static inline int security_task_fix_setuid(struct cred *new,
 					   const struct cred *old,
 					   int flags)
security/security.c +8 -0
index e8ffd92..293bb4b 100644
@@ -910,6 +910,12 @@ int security_kernel_module_from_file(struct file *file)
 	return ima_module_check(file);
 }
 
+int security_kernel_post_read_file(struct file *file, char *buf, loff_t size)
+{
+	return = call_int_hook(kernel_post_read_file, 0, file, buf, size);
+}
+EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
+
 int security_task_fix_setuid(struct cred *new, const struct cred *old,
 			     int flags)
 {
@@ -1697,6 +1703,8 @@ struct security_hook_heads security_hook_heads = {
 		LIST_HEAD_INIT(security_hook_heads.kernel_module_request),
 	.kernel_module_from_file =
 		LIST_HEAD_INIT(security_hook_heads.kernel_module_from_file),
+	.kernel_post_read_file =
+		LIST_HEAD_INIT(security_hook_heads.kernel_post_read_file),
 	.task_fix_setuid =
 		LIST_HEAD_INIT(security_hook_heads.task_fix_setuid),
 	.task_setpgid =	LIST_HEAD_INIT(security_hook_heads.task_setpgid),
Signature · 1 line
2.1.0
Reviewed-by on v2 2/11
#6 lore raw

Re: [RFC PATCH v2 02/11] vfs: define a generic function to read a file from the kernel

Quoted 11 lines · Mimi Zohar
fs/exec.c +3 -0
index b06623a..6d623c2 100644
@@ -831,6 +832,58 @@ int kernel_read(struct file *file, loff_t offset,
 
 EXPORT_SYMBOL(kernel_read);
 
+int kernel_read_file(struct file *file, void **buf, loff_t *size,
+		     loff_t max_size)
+{

fs/exec.c int kernel_read(struct file *file, loff_t offset, · line 847

Quoted 10 lines
fs/exec.c +10 -0
+	loff_t i_size, pos;
+	ssize_t bytes = 0;
+	int ret;
+
+	if (!S_ISREG(file_inode(file)->i_mode))
+		return -EINVAL;
+
+	i_size = i_size_read(file_inode(file));
+	if (max_size > 0 && i_size > max_size)
+		return -EFBIG;

loff_t is a __kernel_loff_t, which in turn is a long long, and that's signed. We don't catch a negative value here, for max_size, we could return -EINVAL if its < 0.

fs/exec.c int kernel_read(struct file *file, loff_t offset, · line 849

Quoted 2 lines
fs/exec.c +2 -0
+	if (i_size == 0)
+		return -EINVAL;

Likewise for i_size. The setter of the size will depend on how the code calling this routine setup the struct file passed.

So how about adding a i_size <= 0 check here as well here? At least fw_read_file_contents() has historically done this, so if this generic read is going to skip that I'd like to see why. We're unifying so I rather be more pedantic.

Provided this is addressed feel free to peg:

Luis

author
#7 lore raw

Re: [RFC PATCH v2 02/11] vfs: define a generic function to read a file from the kernel

Quoted 33 lines · Luis R. Rodriguez
Quoted 11 lines · Mimi Zohar
fs/exec.c +3 -0
index b06623a..6d623c2 100644
@@ -831,6 +832,58 @@ int kernel_read(struct file *file, loff_t offset,
 
 EXPORT_SYMBOL(kernel_read);
 
+int kernel_read_file(struct file *file, void **buf, loff_t *size,
+		     loff_t max_size)
+{

fs/exec.c int kernel_read(struct file *file, loff_t offset, · line 847

Quoted 10 lines
fs/exec.c +10 -0
+	loff_t i_size, pos;
+	ssize_t bytes = 0;
+	int ret;
+
+	if (!S_ISREG(file_inode(file)->i_mode))
+		return -EINVAL;
+
+	i_size = i_size_read(file_inode(file));
+	if (max_size > 0 && i_size > max_size)
+		return -EFBIG;

loff_t is a __kernel_loff_t, which in turn is a long long, and that's signed. We don't catch a negative value here, for max_size, we could return -EINVAL if its < 0.

fs/exec.c int kernel_read(struct file *file, loff_t offset, · line 849

Quoted 2 lines
fs/exec.c +2 -0
+	if (i_size == 0)
+		return -EINVAL;

Likewise for i_size. The setter of the size will depend on how the code calling this routine setup the struct file passed.

Quoted 8 lines

So how about adding a i_size <= 0 check here as well here? At least fw_read_file_contents() has historically done this, so if this generic read is going to skip that I'd like to see why. We're unifying so I rather be more pedantic.

Provided this is addressed feel free to peg:

Don't know how I missed that. Thanks!

Mimi

author
#8 lore raw

RFC PATCH v2 3/11 ima: provide buffer hash calculation function

In reply to Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

This patch provides convenient buffer hash calculation function.

Changelog:
- rewrite to support loff_t sized buffers - Mimi
(based on Fenguang Wu's testing)

---
 security/integrity/ima/ima.h        |  2 ++
 security/integrity/ima/ima_crypto.c | 47 +++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
security/integrity/ima/ima.h +2 -0
index fb8da36..de53631 100644
@@ -107,6 +107,8 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
 			   const char *op, struct inode *inode,
 			   const unsigned char *filename);
 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
+int ima_calc_buffer_hash(const void *buf, loff_t len,
+			 struct ima_digest_data *hash);
 int ima_calc_field_array_hash(struct ima_field_data *field_data,
 			      struct ima_template_desc *desc, int num_fields,
 			      struct ima_digest_data *hash);
security/integrity/ima/ima_crypto.c +47 -0

53 lines not on this page · raw message

Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#9 lore raw

Re: [RFC PATCH v2 03/11] ima: provide buffer hash calculation function

Quoted 52 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

This patch provides convenient buffer hash calculation function.

Changelog:
- rewrite to support loff_t sized buffers - Mimi
(based on Fenguang Wu's testing)

---
 security/integrity/ima/ima.h        |  2 ++
 security/integrity/ima/ima_crypto.c | 47 +++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
security/integrity/ima/ima.h +2 -0
index fb8da36..de53631 100644
@@ -107,6 +107,8 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
                           const char *op, struct inode *inode,
                           const unsigned char *filename);
 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
+int ima_calc_buffer_hash(const void *buf, loff_t len,
+                        struct ima_digest_data *hash);
 int ima_calc_field_array_hash(struct ima_field_data *field_data,
                              struct ima_template_desc *desc, int num_fields,
                              struct ima_digest_data *hash);
security/integrity/ima/ima_crypto.c +16 -0
index fb30ce4..8d86281 100644
@@ -519,6 +519,53 @@ int ima_calc_field_array_hash(struct ima_field_data *field_data,
        return rc;
 }
 
+static int calc_buffer_shash_tfm(const void *buf, loff_t size,
+                               struct ima_digest_data *hash,
+                               struct crypto_shash *tfm)
+{
+       SHASH_DESC_ON_STACK(shash, tfm);
+       unsigned int len;
+       loff_t offset = 0;
+       int rc;
+
+       shash->tfm = tfm;
+       shash->flags = 0;
+
+       hash->length = crypto_shash_digestsize(tfm);
+
+       rc = crypto_shash_init(shash);
+       if (rc != 0)

security/integrity/ima/ima_crypto.c int ima_calc_field_array_hash(struct ima_field_data *field_data, · line 547

Quoted 10 lines
security/integrity/ima/ima_crypto.c +10 -0
+               return rc;
+
+       len = size < PAGE_SIZE ? size : PAGE_SIZE;
+       while (offset < size) {
+               rc = crypto_shash_update(shash, buf + offset, len);
+               if (rc)
+                       break;
+               offset += len;
+       }
+

Hello Mimi,

May be this was my earlier patch, but it seems to have a problem of accessing beyond end of buffer using the same len.
When buffer always padded by zeros it is not a problem, but it is a bug.

This seems to be better version.

   while (size) {
               len = size < PAGE_SIZE ? size : PAGE_SIZE;
               rc = crypto_shash_update(shash, buf, len);
               if (rc)
                       break;
               buf += len;
               size -= len;
    }

Please change my sign-of to: [email protected]

Thanks.
Dmitry

Quoted 27 lines
security/integrity/ima/ima_crypto.c +21 -1
+       if (!rc)
+               rc = crypto_shash_final(shash, hash->digest);
+       return rc;
+}
+
+int ima_calc_buffer_hash(const void *buf, loff_t len,
+                        struct ima_digest_data *hash)
+{
+       struct crypto_shash *tfm;
+       int rc;
+
+       tfm = ima_alloc_tfm(hash->algo);
+       if (IS_ERR(tfm))
+               return PTR_ERR(tfm);
+
+       rc = calc_buffer_shash_tfm(buf, len, hash, tfm);
+
+       ima_free_tfm(tfm);
+       return rc;
+}
+
 static void __init ima_pcrread(int idx, u8 *pcr)
 {
        if (!ima_used_chip)
--

2.1.0

Signature · 2 lines
Thanks,
Dmitry
author
#10 lore raw

Re: [RFC PATCH v2 03/11] ima: provide buffer hash calculation function

Quoted 81 lines · Dmitry Kasatkin
Quoted 52 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

This patch provides convenient buffer hash calculation function.

Changelog:
- rewrite to support loff_t sized buffers - Mimi
(based on Fenguang Wu's testing)

---
 security/integrity/ima/ima.h        |  2 ++
 security/integrity/ima/ima_crypto.c | 47 +++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
security/integrity/ima/ima.h +2 -0
index fb8da36..de53631 100644
@@ -107,6 +107,8 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
                           const char *op, struct inode *inode,
                           const unsigned char *filename);
 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
+int ima_calc_buffer_hash(const void *buf, loff_t len,
+                        struct ima_digest_data *hash);
 int ima_calc_field_array_hash(struct ima_field_data *field_data,
                              struct ima_template_desc *desc, int num_fields,
                              struct ima_digest_data *hash);
security/integrity/ima/ima_crypto.c +16 -0
index fb30ce4..8d86281 100644
@@ -519,6 +519,53 @@ int ima_calc_field_array_hash(struct ima_field_data *field_data,
        return rc;
 }
 
+static int calc_buffer_shash_tfm(const void *buf, loff_t size,
+                               struct ima_digest_data *hash,
+                               struct crypto_shash *tfm)
+{
+       SHASH_DESC_ON_STACK(shash, tfm);
+       unsigned int len;
+       loff_t offset = 0;
+       int rc;
+
+       shash->tfm = tfm;
+       shash->flags = 0;
+
+       hash->length = crypto_shash_digestsize(tfm);
+
+       rc = crypto_shash_init(shash);
+       if (rc != 0)

security/integrity/ima/ima_crypto.c int ima_calc_field_array_hash(struct ima_field_data *field_data, · line 547

Quoted 10 lines
security/integrity/ima/ima_crypto.c +10 -0
+               return rc;
+
+       len = size < PAGE_SIZE ? size : PAGE_SIZE;
+       while (offset < size) {
+               rc = crypto_shash_update(shash, buf + offset, len);
+               if (rc)
+                       break;
+               offset += len;
+       }
+

Hello Mimi,

May be this was my earlier patch, but it seems to have a problem of accessing beyond end of buffer using the same len.
When buffer always padded by zeros it is not a problem, but it is a bug.

This seems to be better version.

   while (size) {
               len = size < PAGE_SIZE ? size : PAGE_SIZE;
               rc = crypto_shash_update(shash, buf, len);
               if (rc)
                       break;
               buf += len;
               size -= len;
    }
Quoted 1 line

Please change my sign-of to: [email protected]

Good catch! I think I unfortunately introduce this bug. Thank you for the review!

Mimi

author
#11 lore raw

RFC PATCH v2 4/11 ima: calculate the hash of a buffer using aynchronous hash(ahash)

In reply to Mimi Zohar

Setting up ahash has some overhead. Only use ahash to calculate the hash of a buffer, if the buffer is larger than ima_ahash_minsize.

---
 security/integrity/ima/ima_crypto.c | 75 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 2 deletions(-)
security/integrity/ima/ima_crypto.c +73 -2
index 8d86281..82c4d3c 100644
@@ -519,6 +519,63 @@ int ima_calc_field_array_hash(struct ima_field_data *field_data,
 	return rc;
 }
 
+static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
+				  struct ima_digest_data *hash,
+				  struct crypto_ahash *tfm)
+{
+	struct ahash_request *req;
+	struct scatterlist sg;
+	struct ahash_completion res;
+	int rc, ahash_rc = 0;
+
+	hash->length = crypto_ahash_digestsize(tfm);
+
+	req = ahash_request_alloc(tfm, GFP_KERNEL);
+	if (!req)
+		return -ENOMEM;
+
+	init_completion(&res.completion);
+	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
+				   CRYPTO_TFM_REQ_MAY_SLEEP,
+				   ahash_complete, &res);
+
+	rc = ahash_wait(crypto_ahash_init(req), &res);
+	if (rc)
+		goto out;
+
+	sg_init_one(&sg, buf, len);
+	ahash_request_set_crypt(req, &sg, NULL, len);
+
+	ahash_rc = crypto_ahash_update(req);
+
+	/* wait for the update request to complete */
+	rc = ahash_wait(ahash_rc, &res);
+	if (!rc) {
+		ahash_request_set_crypt(req, NULL, hash->digest, 0);
+		rc = ahash_wait(crypto_ahash_final(req), &res);
+	}
+out:
+	ahash_request_free(req);
+	return rc;
+}
+
+static int calc_buffer_ahash(const void *buf, loff_t len,
+			     struct ima_digest_data *hash)
+{
+	struct crypto_ahash *tfm;
+	int rc;
+
+	tfm = ima_alloc_atfm(hash->algo);
+	if (IS_ERR(tfm))
+		return PTR_ERR(tfm);
+
+	rc = calc_buffer_ahash_atfm(buf, len, hash, tfm);
+
+	ima_free_atfm(tfm);
+
+	return rc;
+}
+
 static int calc_buffer_shash_tfm(const void *buf, loff_t size,
 				struct ima_digest_data *hash,
 				struct crypto_shash *tfm)
@@ -550,8 +607,8 @@ static int calc_buffer_shash_tfm(const void *buf, loff_t size,
 	return rc;
 }
 
-int ima_calc_buffer_hash(const void *buf, loff_t len,
-			 struct ima_digest_data *hash)
+static int calc_buffer_shash(const void *buf, loff_t len,
+			     struct ima_digest_data *hash)
 {
 	struct crypto_shash *tfm;
 	int rc;
@@ -566,6 +623,20 @@ int ima_calc_buffer_hash(const void *buf, loff_t len,
 	return rc;
 }
 
+int ima_calc_buffer_hash(const void *buf, loff_t len,
+			 struct ima_digest_data *hash)
+{
+	int rc;
+
+	if (ima_ahash_minsize && len >= ima_ahash_minsize) {
+		rc = calc_buffer_ahash(buf, len, hash);
+		if (!rc)
+			return 0;
+	}
+
+	return calc_buffer_shash(buf, len, hash);
+}
+
 static void __init ima_pcrread(int idx, u8 *pcr)
 {
 	if (!ima_used_chip)
Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#12 lore raw

RFC PATCH v2 5/11 ima: define a new hook to measure and appraise a file already in memory

In reply to Mimi Zohar

This patch defines a new IMA hook ima_hash_and_process_file() for measuring and appraising files read by the kernel. The caller loads the file into memory before calling this function, which calculates the hash followed by the normal IMA policy based processing.

To differentiate between callers of ima_hash_and_process_file() this patch introducees a policy identifier enumation named ima_policy_id.

Changelog v1:
- To simplify patch review, separate the IMA changes from the kexec and initramfs changes in "ima: measure and appraise kexec image and initramfs" patch. This patch contains just the IMA changes. The kexec and initramfs changes are with the rest of the kexec changes in "kexec: replace call to copy_file_from_fd() with kernel version".

---
 fs/exec.c                             |  4 +--
 include/linux/fs.h                    |  2 +-
 include/linux/ima.h                   | 14 ++++++++
 include/linux/lsm_hooks.h             |  4 ++-
 include/linux/security.h              |  6 ++--
 security/integrity/iint.c             |  1 +
 security/integrity/ima/ima.h          | 12 +++----
 security/integrity/ima/ima_api.c      |  6 ++--
 security/integrity/ima/ima_appraise.c |  4 +--
 security/integrity/ima/ima_main.c     | 40 ++++++++++++++++++-----
 security/integrity/ima/ima_policy.c   | 60 +++++++++++++++++++----------------
 security/integrity/integrity.h        |  7 ++--
 security/security.c                   |  6 ++--
 13 files changed, 110 insertions(+), 56 deletions(-)
fs/exec.c +2 -2
index 6d623c2..211b81c 100644
@@ -833,7 +833,7 @@ int kernel_read(struct file *file, loff_t offset,
 EXPORT_SYMBOL(kernel_read);
 
 int kernel_read_file(struct file *file, void **buf, loff_t *size,
-		     loff_t max_size)
+		     loff_t max_size, int policy_id)
 {
 	loff_t i_size, pos;
 	ssize_t bytes = 0;
@@ -871,7 +871,7 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 		goto out;
 	}
 
-	ret = security_kernel_post_read_file(file, *buf, i_size);
+	ret = security_kernel_post_read_file(file, *buf, i_size, policy_id);
 	if (!ret)
 		*size = pos;
 
include/linux/fs.h +1 -1
index 93ca379..9b1468c 100644
@@ -2527,7 +2527,7 @@ static inline void i_readcount_inc(struct inode *inode)
 extern int do_pipe_flags(int *, int);
 
 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
-extern int kernel_read_file(struct file *, void **, loff_t *, loff_t);
+extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, int);
 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
include/linux/ima.h +14 -0
index 120ccc5..ca76f60 100644
@@ -13,6 +13,10 @@
 #include <linux/fs.h>
 struct linux_binprm;
 
+enum ima_policy_id {
+	IMA_MAX_READ_CHECK
+};
+
 #ifdef CONFIG_IMA
 extern int ima_bprm_check(struct linux_binprm *bprm);
 extern int ima_file_check(struct file *file, int mask, int opened);
@@ -20,6 +24,9 @@ extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
 extern int ima_module_check(struct file *file);
 extern int ima_fw_from_file(struct file *file, char *buf, size_t size);
+extern int ima_hash_and_process_file(struct file *file,
+				     void *buf, loff_t size,
+				     enum ima_policy_id policy_id);
 
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
@@ -52,6 +59,13 @@ static inline int ima_fw_from_file(struct file *file, char *buf, size_t size)
 	return 0;
 }
 
+static inline int ima_hash_and_process_file(struct file *file,
+					    void *buf, loff_t size,
+					    enum ima_policy_id policy_id)
+{
+	return 0;
+}
+
 #endif /* CONFIG_IMA */
 
 #ifdef CONFIG_IMA_APPRAISE
include/linux/lsm_hooks.h +3 -1
index f82631c..4e6e2af 100644
@@ -567,6 +567,7 @@
  *	by the kernel.
  *	@buf pointer to buffer containing the file contents.
  *	@size length of the file contents.
+ *	@policy-id IMA policy identifier
  *	Return 0 if permission is granted.
  * @task_fix_setuid:
  *	Update the module's state after setting one or more of the user
@@ -1464,7 +1465,8 @@ union security_list_options {
 	int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
 	int (*kernel_module_request)(char *kmod_name);
 	int (*kernel_module_from_file)(struct file *file);
-	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size);
+	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
+				     int policy_id);
 	int (*task_fix_setuid)(struct cred *new, const struct cred *old,
 				int flags);
 	int (*task_setpgid)(struct task_struct *p, pid_t pgid);
include/linux/security.h +4 -2
index f30f564..44d8832 100644
@@ -301,7 +301,8 @@ int security_kernel_create_files_as(struct cred *new, struct inode *inode);
 int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
 int security_kernel_module_request(char *kmod_name);
 int security_kernel_module_from_file(struct file *file);
-int security_kernel_post_read_file(struct file *file, char *buf, loff_t size);
+int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
+				   int policy_id);
 int security_task_fix_setuid(struct cred *new, const struct cred *old,
 			     int flags);
 int security_task_setpgid(struct task_struct *p, pid_t pgid);
@@ -868,7 +869,8 @@ static inline int security_kernel_module_from_file(struct file *file)
 }
 
 static inline int security_kernel_post_read_file(struct file *file,
-						 char *buf, loff_t size)
+						 char *buf, loff_t size,
+						 int policy_id)
 {
 	return 0;
 }
security/integrity/iint.c +1 -0
index 8f1ab37..a41e073 100644
@@ -158,6 +158,7 @@ static void init_once(void *foo)
 	iint->ima_mmap_status = INTEGRITY_UNKNOWN;
 	iint->ima_bprm_status = INTEGRITY_UNKNOWN;
 	iint->ima_module_status = INTEGRITY_UNKNOWN;
+	iint->ima_read_status = INTEGRITY_UNKNOWN;
 	iint->evm_status = INTEGRITY_UNKNOWN;
 }
 
security/integrity/ima/ima.h +6 -6
index de53631..06bcc24 100644
@@ -20,6 +20,7 @@
 #include <linux/types.h>
 #include <linux/crypto.h>
 #include <linux/security.h>
+#include <linux/ima.h>
 #include <linux/hash.h>
 #include <linux/tpm.h>
 #include <linux/audit.h>
@@ -143,7 +144,8 @@ static inline unsigned long ima_hash_key(u8 *digest)
 int ima_get_action(struct inode *inode, int mask, int function);
 int ima_must_measure(struct inode *inode, int mask, int function);
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-			    struct file *file, enum hash_algo algo);
+			    struct file *file, void *buf, loff_t size,
+			    enum hash_algo algo);
 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
 			   const unsigned char *filename,
 			   struct evm_ima_xattr_data *xattr_value,
@@ -160,8 +162,7 @@ const char *ima_d_path(struct path *path, char **pathbuf);
 /* IMA policy related functions */
 enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, FIRMWARE_CHECK, POST_SETATTR };
 
-int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
-		     int flags);
+int ima_match_policy(struct inode *inode, int func, int mask, int flags);
 void ima_init_policy(void);
 void ima_update_policy(void);
 void ima_update_policy_flag(void);
@@ -185,7 +186,7 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
 			     struct file *file, const unsigned char *filename,
 			     struct evm_ima_xattr_data *xattr_value,
 			     int xattr_len, int opened);
-int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
+int ima_must_appraise(struct inode *inode, int mask, int func);
 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 					   int func);
@@ -205,8 +206,7 @@ static inline int ima_appraise_measurement(int func,
 	return INTEGRITY_UNKNOWN;
 }
 
-static inline int ima_must_appraise(struct inode *inode, int mask,
-				    enum ima_hooks func)
+static inline int ima_must_appraise(struct inode *inode, int mask, int func)
 {
 	return 0;
 }
security/integrity/ima/ima_api.c +4 -2
index e7c7a5d..b702e4b 100644
@@ -188,7 +188,8 @@ int ima_get_action(struct inode *inode, int mask, int function)
  * Return 0 on success, error code otherwise
  */
 int ima_collect_measurement(struct integrity_iint_cache *iint,
-			    struct file *file, enum hash_algo algo)
+			    struct file *file, void *buf, loff_t size,
+			    enum hash_algo algo)
 {
 	const char *audit_cause = "failed";
 	struct inode *inode = file_inode(file);
@@ -210,7 +211,8 @@ int ima_collect_measurement(struct integrity_iint_cache *iint,
 
 		hash.hdr.algo = algo;
 
-		result = ima_calc_file_hash(file, &hash.hdr);
+		result = (!buf) ?  ima_calc_file_hash(file, &hash.hdr) :
+			ima_calc_buffer_hash(buf, size, &hash.hdr);
 		if (!result) {
 			int length = sizeof(hash.hdr) + hash.hdr.length;
 			void *tmpbuf = krealloc(iint->ima_hash, length,
security/integrity/ima/ima_appraise.c +2 -2
index 9c2b46b..4edf47f 100644
@@ -36,7 +36,7 @@ __setup("ima_appraise=", default_appraise_setup);
  *
  * Return 1 to appraise
  */
-int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
+int ima_must_appraise(struct inode *inode, int mask, int func)
 {
 	if (!ima_appraise)
 		return 0;
@@ -299,7 +299,7 @@ void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
 	if (iint->flags & IMA_DIGSIG)
 		return;
 
-	rc = ima_collect_measurement(iint, file, ima_hash_algo);
+	rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
 	if (rc < 0)
 		return;
 
security/integrity/ima/ima_main.c +32 -8
index d9fc463..668cbc6 100644
@@ -153,8 +153,8 @@ void ima_file_free(struct file *file)
 	ima_check_last_writer(iint, inode, file);
 }
 
-static int process_measurement(struct file *file, int mask, int function,
-			       int opened)
+static int process_measurement(struct file *file, char *buf, loff_t size,
+			       int mask, int function, int opened)
 {
 	struct inode *inode = file_inode(file);
 	struct integrity_iint_cache *iint = NULL;
@@ -226,7 +226,7 @@ static int process_measurement(struct file *file, int mask, int function,
 
 	hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
 
-	rc = ima_collect_measurement(iint, file, hash_algo);
+	rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
 	if (rc != 0) {
 		if (file->f_flags & O_DIRECT)
 			rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
@@ -273,7 +273,8 @@ out:
 int ima_file_mmap(struct file *file, unsigned long prot)
 {
 	if (file && (prot & PROT_EXEC))
-		return process_measurement(file, MAY_EXEC, MMAP_CHECK, 0);
+		return process_measurement(file, NULL, 0, MAY_EXEC,
+					   MMAP_CHECK, 0);
 	return 0;
 }
 
@@ -292,7 +293,8 @@ int ima_file_mmap(struct file *file, unsigned long prot)
  */
 int ima_bprm_check(struct linux_binprm *bprm)
 {
-	return process_measurement(bprm->file, MAY_EXEC, BPRM_CHECK, 0);
+	return process_measurement(bprm->file, NULL, 0, MAY_EXEC,
+				   BPRM_CHECK, 0);
 }
 
 /**
@@ -307,7 +309,7 @@ int ima_bprm_check(struct linux_binprm *bprm)
  */
 int ima_file_check(struct file *file, int mask, int opened)
 {
-	return process_measurement(file,
+	return process_measurement(file, NULL, 0,
 				   mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
 				   FILE_CHECK, opened);
 }
@@ -332,7 +334,7 @@ int ima_module_check(struct file *file)
 #endif
 		return 0;	/* We rely on module signature checking */
 	}
-	return process_measurement(file, MAY_EXEC, MODULE_CHECK, 0);
+	return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
 }
 
 int ima_fw_from_file(struct file *file, char *buf, size_t size)
@@ -343,7 +345,29 @@ int ima_fw_from_file(struct file *file, char *buf, size_t size)
 			return -EACCES;	/* INTEGRITY_UNKNOWN */
 		return 0;
 	}
-	return process_measurement(file, MAY_EXEC, FIRMWARE_CHECK, 0);
+	return process_measurement(file, NULL, 0, MAY_EXEC, FIRMWARE_CHECK, 0);
+}
+
+/**
+ * ima_hash_and_process_file - in memory collect/appraise/audit measurement
+ * @file: pointer to the file to be measured/appraised/audit
+ * @buf: pointer to in memory file contents
+ * @size: size of in memory file contents
+ * @policy_id: caller identifier
+ *
+ * Measure/appraise/audit in memory file based on policy.  Policy rules
+ * are written in terms of a policy identifier.
+ */
+int ima_hash_and_process_file(struct file *file, void *buf, loff_t size,
+			      enum ima_policy_id policy_id)
+{
+	if (!file || !buf || size == 0) { /* should never happen */
+		if (ima_appraise & IMA_APPRAISE_ENFORCE)
+			return -EACCES;
+		return 0;
+	}
+
+	return process_measurement(file, buf, size, MAY_READ, policy_id, 0);
 }
 
 static int __init init_ima(void)
security/integrity/ima/ima_policy.c +32 -28

141 lines not on this page · raw message

security/integrity/integrity.h +5 -2
index 5efe2ec..9a0ea4c 100644
@@ -49,12 +49,14 @@
 #define IMA_MODULE_APPRAISED	0x00008000
 #define IMA_FIRMWARE_APPRAISE	0x00010000
 #define IMA_FIRMWARE_APPRAISED	0x00020000
+#define IMA_READ_APPRAISE	0x00040000
+#define IMA_READ_APPRAISED	0x00080000
 #define IMA_APPRAISE_SUBMASK	(IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
 				 IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE | \
-				 IMA_FIRMWARE_APPRAISE)
+				 IMA_FIRMWARE_APPRAISE | IMA_READ_APPRAISE)
 #define IMA_APPRAISED_SUBMASK	(IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
 				 IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED | \
-				 IMA_FIRMWARE_APPRAISED)
+				 IMA_FIRMWARE_APPRAISED | IMA_READ_APPRAISED)
 
 enum evm_ima_xattr_type {
 	IMA_XATTR_DIGEST = 0x01,
@@ -111,6 +113,7 @@ struct integrity_iint_cache {
 	enum integrity_status ima_bprm_status:4;
 	enum integrity_status ima_module_status:4;
 	enum integrity_status ima_firmware_status:4;
+	enum integrity_status ima_read_status:4;
 	enum integrity_status evm_status:4;
 	struct ima_digest_data *ima_hash;
 };
security/security.c +4 -2
index 293bb4b..49cacae 100644
@@ -910,9 +910,11 @@ int security_kernel_module_from_file(struct file *file)
 	return ima_module_check(file);
 }
 
-int security_kernel_post_read_file(struct file *file, char *buf, loff_t size)
+int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
+				   int policy_id)
 {
-	return = call_int_hook(kernel_post_read_file, 0, file, buf, size);
+	return = call_int_hook(kernel_post_read_file, 0, file, buf, size,
+			       policy_id);
 }
 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
 
Signature · 1 line
2.1.0
author
#13 lore raw

RFC PATCH v2 6/11 kexec: replace call to copy_file_from_fd() with kernel version

In reply to Mimi Zohar

This patch defines kernel_read_file_from_fd(), a wrapper for the VFS common kernel_read_file(), and replaces the kexec copy_file_from_fd() calls with the kernel_read_file_from_fd() wrapper.

Two new IMA policy identifiers named KEXEC_CHECK and INITRAMFS_CHECK are defined for measuring, appraising or auditing the kexec image and initramfs.

Changelog v1:
- re-order and squash the kexec patches

v3: ima: measure and appraise kexec image and initramfs (squashed)
- rename ima_read_hooks enumeration to ima_policy_id
- use kstat file size type loff_t, not size_t
- add union name "hooks" to fix sparse warning

v2:
- Calculate the file hash from the in memory buffer (suggested by Dave Young)
- Rename ima_read_and_process_file() to ima_hash_and_process_file()
- replace individual case statements with range:
KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1
v1:
- Instead of ima_read_and_process_file() allocating memory, the caller allocates and frees the memory.
- Moved the kexec measurement/appraisal call to copy_file_from_fd(). The same call now measures and appraises both the kexec image and initramfs.

---
 Documentation/ABI/testing/ima_policy  |  2 +-
 fs/exec.c                             | 15 ++++++++
 include/linux/fs.h                    |  1 +
 include/linux/ima.h                   |  2 +
 kernel/kexec_file.c                   | 72 ++++-------------------------------
 security/integrity/ima/ima.h          |  9 ++++-
 security/integrity/ima/ima_appraise.c |  7 ++++
 security/integrity/ima/ima_policy.c   | 27 ++++++++++---
 8 files changed, 64 insertions(+), 71 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index 0a378a8..e80f767 100644
@@ -26,7 +26,7 @@ Description:
 			option:	[[appraise_type=]] [permit_directio]
 
 		base: 	func:= [BPRM_CHECK][MMAP_CHECK][FILE_CHECK][MODULE_CHECK]
-				[FIRMWARE_CHECK]
+				[FIRMWARE_CHECK] [KEXEC_CHECK] [INITRAMFS_CHECK]
 			mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND]
 			       [[^]MAY_EXEC]
 			fsmagic:= hex value
fs/exec.c +15 -0
index 211b81c..a5ae51e 100644
@@ -884,6 +884,21 @@ out:
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     int policy_id)
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;
+
+	if (!f.file)
+		goto out;
+
+	ret = kernel_read_file(f.file, buf, size, max_size, policy_id);
+out:
+	fdput(f);
+	return ret;
+}
+
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
 {
 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
include/linux/fs.h +1 -0
index 9b1468c..9642623 100644
@@ -2528,6 +2528,7 @@ extern int do_pipe_flags(int *, int);
 
 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
 extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, int);
+extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t, int);
 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
include/linux/ima.h +2 -0
index ca76f60..ae91938 100644
@@ -14,6 +14,8 @@
 struct linux_binprm;
 
 enum ima_policy_id {
+	KEXEC_CHECK = 1,
+	INITRAMFS_CHECK,
 	IMA_MAX_READ_CHECK
 };
 
kernel/kexec_file.c +8 -64

105 lines not on this page · raw message

security/integrity/ima/ima.h +8 -1
index 06bcc24..b98dbd5 100644
@@ -160,7 +160,14 @@ void ima_free_template_entry(struct ima_template_entry *entry);
 const char *ima_d_path(struct path *path, char **pathbuf);
 
 /* IMA policy related functions */
-enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, FIRMWARE_CHECK, POST_SETATTR };
+enum ima_hooks {
+	FILE_CHECK = IMA_MAX_READ_CHECK,
+	MMAP_CHECK,
+	BPRM_CHECK,
+	MODULE_CHECK,
+	FIRMWARE_CHECK,
+	POST_SETATTR
+};
 
 int ima_match_policy(struct inode *inode, int func, int mask, int flags);
 void ima_init_policy(void);
security/integrity/ima/ima_appraise.c +7 -0
index 4edf47f..3adf937 100644
@@ -78,6 +78,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_module_status;
 	case FIRMWARE_CHECK:
 		return iint->ima_firmware_status;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return iint->ima_read_status;
 	case FILE_CHECK:
 	default:
 		return iint->ima_file_status;
@@ -100,6 +102,9 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
 	case FIRMWARE_CHECK:
 		iint->ima_firmware_status = status;
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		iint->ima_read_status = status;
+		break;
 	case FILE_CHECK:
 	default:
 		iint->ima_file_status = status;
@@ -122,6 +127,8 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	case FIRMWARE_CHECK:
 		iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		break;
 	case FILE_CHECK:
 	default:
 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
security/integrity/ima/ima_policy.c +22 -5
index 595e038..4711083 100644
@@ -306,6 +306,8 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
 		return IMA_MODULE_APPRAISE;
 	case FIRMWARE_CHECK:
 		return IMA_FIRMWARE_APPRAISE;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return IMA_READ_APPRAISE;
 	case FILE_CHECK:
 	default:
 		return IMA_FILE_APPRAISE;
@@ -614,6 +616,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.func = MMAP_CHECK;
 			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
 				entry->hooks.func = BPRM_CHECK;
+			else if (strcmp(args[0].from, "KEXEC_CHECK") == 0)
+				entry->hooks.policy_id = KEXEC_CHECK;
+			else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0)
+				entry->hooks.policy_id = INITRAMFS_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
@@ -867,7 +873,9 @@ static char *func_tokens[] = {
 	"BPRM_CHECK",
 	"MODULE_CHECK",
 	"FIRMWARE_CHECK",
-	"POST_SETATTR"
+	"POST_SETATTR",
+	"KEXEC_CHECK",
+	"INITRAMFS_CHECK",
 };
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
@@ -948,10 +956,19 @@ int ima_policy_show(struct seq_file *m, void *v)
 			seq_printf(m, pt(Opt_func), ft(func_post));
 			break;
 		default:
-			snprintf(tbuf, sizeof(tbuf), "%d",
-				 entry->hooks.func);
-			seq_printf(m, pt(Opt_func), tbuf);
-			break;
+			switch (entry->hooks.policy_id) {
+			case KEXEC_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_kexec));
+				break;
+			case INITRAMFS_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_initramfs));
+				break;
+			default:
+				snprintf(tbuf, sizeof(tbuf), "%d",
+					 entry->hooks.func);
+				seq_printf(m, pt(Opt_func), tbuf);
+				break;
+			}
 		}
 		seq_puts(m, " ");
 	}
Signature · 1 line
2.1.0
#14 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 6 lines · Mimi Zohar
fs/exec.c +0 -0
index 211b81c..a5ae51e 100644
@@ -884,6 +884,21 @@ out:
 }

fs/exec.c out: · line 894

Quoted 10 lines
fs/exec.c +8 -0
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     int policy_id)
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;
+
+	if (!f.file)
+		goto out;

It is no need to call the fdput, if f.file is NULL. It is better to return it directly.

fs/exec.c out: · line 901

Quoted 7 lines
fs/exec.c +7 -0
+
+	ret = kernel_read_file(f.file, buf, size, max_size, policy_id);
+out:
+	fdput(f);
+	return ret;
+}
+

Thanks
Minfei

#15 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

In reply to Mimi Zohar
Quoted 13 lines · Mimi Zohar
fs/exec.c +5 -0
index 211b81c..a5ae51e 100644
@@ -884,6 +884,21 @@ out:
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     int policy_id)
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

fs/exec.c out: · line 901

Quoted 10 lines
fs/exec.c +10 -0
+
+	if (!f.file)
+		goto out;
+
+	ret = kernel_read_file(f.file, buf, size, max_size, policy_id);
+out:
+	fdput(f);
+	return ret;
+}
+

Don't you need to EXPORT_SYMBOL_GPL() here as well?

security/integrity/ima/ima_appraise.c enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, · line 82

Quoted 10 lines
security/integrity/ima/ima_appraise.c +2 -0
index 4edf47f..3adf937 100644
@@ -78,6 +78,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_module_status;
 	case FIRMWARE_CHECK:
 		return iint->ima_firmware_status;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return iint->ima_read_status;

I didn't get the memo that we're OK to use compiler specific extensions like this. I'm sure if you are using it its not the first case, just want to be sure we are aware of possible issues if some compiler doesn't support this.

If we don't have a precedence can we just avoid its use?

Cc'd Julia in case this might be of interest for Coccinelle to grok.

security/integrity/ima/ima_appraise.c static void ima_set_cache_status(struct integrity_iint_cache *iint, · line 110

Quoted 13 lines
security/integrity/ima/ima_appraise.c +3 -0
 	case FILE_CHECK:
 	default:
 		return iint->ima_file_status;
@@ -100,6 +102,9 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
 	case FIRMWARE_CHECK:
 		iint->ima_firmware_status = status;
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		iint->ima_read_status = status;
+		break;
 	case FILE_CHECK:
 	default:
 		iint->ima_file_status = status;

Likewise.

security/integrity/ima/ima_appraise.c static void ima_cache_flags(struct integrity_iint_cache *iint, int func) · line 134

Quoted 9 lines
security/integrity/ima/ima_appraise.c +2 -0
@@ -122,6 +127,8 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	case FIRMWARE_CHECK:
 		iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		break;
 	case FILE_CHECK:
 	default:
 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);

Likewise.

security/integrity/ima/ima_policy.c static int get_subaction(struct ima_rule_entry *rule, int func) · line 313

Quoted 13 lines
security/integrity/ima/ima_policy.c +2 -0
index 595e038..4711083 100644
@@ -306,6 +306,8 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
 		return IMA_MODULE_APPRAISE;
 	case FIRMWARE_CHECK:
 		return IMA_FIRMWARE_APPRAISE;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return IMA_READ_APPRAISE;
 	case FILE_CHECK:
 	default:
 		return IMA_FILE_APPRAISE;

Likewise.

Quoted 14 lines
security/integrity/ima/ima_policy.c +6 -4
@@ -948,10 +956,19 @@ int ima_policy_show(struct seq_file *m, void *v)
 			seq_printf(m, pt(Opt_func), ft(func_post));
 			break;
 		default:
-			snprintf(tbuf, sizeof(tbuf), "%d",
-				 entry->hooks.func);
-			seq_printf(m, pt(Opt_func), tbuf);
-			break;
+			switch (entry->hooks.policy_id) {
+			case KEXEC_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_kexec));
+				break;
+			case INITRAMFS_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_initramfs));

security/integrity/ima/ima_policy.c int ima_policy_show(struct seq_file *m, void *v) · line 974

Quoted 10 lines
security/integrity/ima/ima_policy.c +7 -0
+				break;
+			default:
+				snprintf(tbuf, sizeof(tbuf), "%d",
+					 entry->hooks.func);
+				seq_printf(m, pt(Opt_func), tbuf);
+				break;
+			}
 		}
 		seq_puts(m, " ");
 	}

Two switches wrapped tend to lead to messy and hard to read code, is using a function here better?

Luis

#16 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 17 lines · Luis R. Rodriguez

security/integrity/ima/ima_appraise.c enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, · line 82

Quoted 10 lines · Mimi Zohar
security/integrity/ima/ima_appraise.c +2 -0
index 4edf47f..3adf937 100644
@@ -78,6 +78,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_module_status;
 	case FIRMWARE_CHECK:
 		return iint->ima_firmware_status;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return iint->ima_read_status;

I didn't get the memo that we're OK to use compiler specific extensions like this. I'm sure if you are using it its not the first case, just want to be sure we are aware of possible issues if some compiler doesn't support this.

Quoted 1 line

If we don't have a precedence can we just avoid its use?

This has sailed:

dtor@dtor-ws:~$ grep -rl 'case.*\.\.\..*:' kernel/work/drivers/ | wc -l
98

Thanks.

Signature · 1 line
Dmitry
#17 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

In reply to Mimi Zohar

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?
- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

Do you have a simple usage documentation about how to test it?

Quoted 58 lines · Mimi Zohar

This patch defines kernel_read_file_from_fd(), a wrapper for the VFS common kernel_read_file(), and replaces the kexec copy_file_from_fd() calls with the kernel_read_file_from_fd() wrapper.

Two new IMA policy identifiers named KEXEC_CHECK and INITRAMFS_CHECK are defined for measuring, appraising or auditing the kexec image and initramfs.

Changelog v1:
- re-order and squash the kexec patches

v3: ima: measure and appraise kexec image and initramfs (squashed)
- rename ima_read_hooks enumeration to ima_policy_id
- use kstat file size type loff_t, not size_t
- add union name "hooks" to fix sparse warning

v2:
- Calculate the file hash from the in memory buffer (suggested by Dave Young)
- Rename ima_read_and_process_file() to ima_hash_and_process_file()
- replace individual case statements with range:
KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1
v1:
- Instead of ima_read_and_process_file() allocating memory, the caller allocates and frees the memory.
- Moved the kexec measurement/appraisal call to copy_file_from_fd(). The same call now measures and appraises both the kexec image and initramfs.

---
 Documentation/ABI/testing/ima_policy  |  2 +-
 fs/exec.c                             | 15 ++++++++
 include/linux/fs.h                    |  1 +
 include/linux/ima.h                   |  2 +
 kernel/kexec_file.c                   | 72 ++++-------------------------------
 security/integrity/ima/ima.h          |  9 ++++-
 security/integrity/ima/ima_appraise.c |  7 ++++
 security/integrity/ima/ima_policy.c   | 27 ++++++++++---
 8 files changed, 64 insertions(+), 71 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index 0a378a8..e80f767 100644
@@ -26,7 +26,7 @@ Description:
 			option:	[[appraise_type=]] [permit_directio]
 
 		base: 	func:= [BPRM_CHECK][MMAP_CHECK][FILE_CHECK][MODULE_CHECK]
-				[FIRMWARE_CHECK]
+				[FIRMWARE_CHECK] [KEXEC_CHECK] [INITRAMFS_CHECK]
 			mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND]
 			       [[^]MAY_EXEC]
 			fsmagic:= hex value
fs/exec.c +0 -0
index 211b81c..a5ae51e 100644

fs/exec.c out: · line 888

Quoted 5 lines
fs/exec.c +2 -0
@@ -884,6 +884,21 @@ out:
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     int policy_id)

Though this is only used in kexec now, it looks more a general function, move it to general code should be fine along with kernel_read_file

fs/exec.c out: · line 891

Quoted 3 lines
fs/exec.c +3 -0
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

-EBADF looks better?

Quoted 30 lines
fs/exec.c +10 -0
+
+	if (!f.file)
+		goto out;
+
+	ret = kernel_read_file(f.file, buf, size, max_size, policy_id);
+out:
+	fdput(f);
+	return ret;
+}
+
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
 {
 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
include/linux/fs.h +1 -0
index 9b1468c..9642623 100644
@@ -2528,6 +2528,7 @@ extern int do_pipe_flags(int *, int);
 
 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
 extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, int);
+extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t, int);
 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
include/linux/ima.h +0 -0
index ca76f60..ae91938 100644

include/linux/ima.h line 18

Quoted 5 lines
include/linux/ima.h +2 -0
@@ -14,6 +14,8 @@
 struct linux_binprm;
 
 enum ima_policy_id {
+	KEXEC_CHECK = 1,
+	INITRAMFS_CHECK,

Change to below should be better:
KEXEC_KERNEL_CHECK
KEXEC_INITRAMFS_CHECK

Quoted 235 lines
include/linux/ima.h +0 -0
 	IMA_MAX_READ_CHECK
 };
 
kernel/kexec_file.c +8 -64
index b70ada0..f7c3ce4 100644
@@ -18,6 +18,7 @@
 #include <linux/kexec.h>
 #include <linux/mutex.h>
 #include <linux/list.h>
+#include <linux/ima.h>
 #include <crypto/hash.h>
 #include <crypto/sha.h>
 #include <linux/syscalls.h>
@@ -33,65 +34,6 @@ size_t __weak kexec_purgatory_size = 0;
 
 static int kexec_calculate_store_digests(struct kimage *image);
 
-static int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len)
-{
-	struct fd f = fdget(fd);
-	int ret;
-	struct kstat stat;
-	loff_t pos;
-	ssize_t bytes = 0;
-
-	if (!f.file)
-		return -EBADF;
-
-	ret = vfs_getattr(&f.file->f_path, &stat);
-	if (ret)
-		goto out;
-
-	if (stat.size > INT_MAX) {
-		ret = -EFBIG;
-		goto out;
-	}
-
-	/* Don't hand 0 to vmalloc, it whines. */
-	if (stat.size == 0) {
-		ret = -EINVAL;
-		goto out;
-	}
-
-	*buf = vmalloc(stat.size);
-	if (!*buf) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	pos = 0;
-	while (pos < stat.size) {
-		bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,
-				    stat.size - pos);
-		if (bytes < 0) {
-			vfree(*buf);
-			ret = bytes;
-			goto out;
-		}
-
-		if (bytes == 0)
-			break;
-		pos += bytes;
-	}
-
-	if (pos != stat.size) {
-		ret = -EBADF;
-		vfree(*buf);
-		goto out;
-	}
-
-	*buf_len = pos;
-out:
-	fdput(f);
-	return ret;
-}
-
 /* Architectures can provide this probe function */
 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
 					 unsigned long buf_len)
@@ -180,16 +122,17 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 {
 	int ret = 0;
 	void *ldata;
+	loff_t size;
 
-	ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,
-				&image->kernel_buf_len);
+	ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
+				       &size, INT_MAX, KEXEC_CHECK);
 	if (ret)
 		return ret;
+	image->kernel_buf_len = size;
 
 	/* Call arch image probe handlers */
 	ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
 					    image->kernel_buf_len);
-
 	if (ret)
 		goto out;
 
@@ -204,10 +147,11 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
 #endif
 	/* It is possible that there no initramfs is being loaded */
 	if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
-		ret = copy_file_from_fd(initrd_fd, &image->initrd_buf,
-					&image->initrd_buf_len);
+		ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
+					       &size, INT_MAX, INITRAMFS_CHECK);
 		if (ret)
 			goto out;
+		image->initrd_buf_len = size;
 	}
 
 	if (cmdline_len) {
security/integrity/ima/ima.h +8 -1
index 06bcc24..b98dbd5 100644
@@ -160,7 +160,14 @@ void ima_free_template_entry(struct ima_template_entry *entry);
 const char *ima_d_path(struct path *path, char **pathbuf);
 
 /* IMA policy related functions */
-enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, FIRMWARE_CHECK, POST_SETATTR };
+enum ima_hooks {
+	FILE_CHECK = IMA_MAX_READ_CHECK,
+	MMAP_CHECK,
+	BPRM_CHECK,
+	MODULE_CHECK,
+	FIRMWARE_CHECK,
+	POST_SETATTR
+};
 
 int ima_match_policy(struct inode *inode, int func, int mask, int flags);
 void ima_init_policy(void);
security/integrity/ima/ima_appraise.c +7 -0
index 4edf47f..3adf937 100644
@@ -78,6 +78,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_module_status;
 	case FIRMWARE_CHECK:
 		return iint->ima_firmware_status;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return iint->ima_read_status;
 	case FILE_CHECK:
 	default:
 		return iint->ima_file_status;
@@ -100,6 +102,9 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
 	case FIRMWARE_CHECK:
 		iint->ima_firmware_status = status;
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		iint->ima_read_status = status;
+		break;
 	case FILE_CHECK:
 	default:
 		iint->ima_file_status = status;
@@ -122,6 +127,8 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	case FIRMWARE_CHECK:
 		iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
 		break;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		break;
 	case FILE_CHECK:
 	default:
 		iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
security/integrity/ima/ima_policy.c +22 -5
index 595e038..4711083 100644
@@ -306,6 +306,8 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
 		return IMA_MODULE_APPRAISE;
 	case FIRMWARE_CHECK:
 		return IMA_FIRMWARE_APPRAISE;
+	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
+		return IMA_READ_APPRAISE;
 	case FILE_CHECK:
 	default:
 		return IMA_FILE_APPRAISE;
@@ -614,6 +616,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.func = MMAP_CHECK;
 			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
 				entry->hooks.func = BPRM_CHECK;
+			else if (strcmp(args[0].from, "KEXEC_CHECK") == 0)
+				entry->hooks.policy_id = KEXEC_CHECK;
+			else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0)
+				entry->hooks.policy_id = INITRAMFS_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
@@ -867,7 +873,9 @@ static char *func_tokens[] = {
 	"BPRM_CHECK",
 	"MODULE_CHECK",
 	"FIRMWARE_CHECK",
-	"POST_SETATTR"
+	"POST_SETATTR",
+	"KEXEC_CHECK",
+	"INITRAMFS_CHECK",
 };
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
@@ -948,10 +956,19 @@ int ima_policy_show(struct seq_file *m, void *v)
 			seq_printf(m, pt(Opt_func), ft(func_post));
 			break;
 		default:
-			snprintf(tbuf, sizeof(tbuf), "%d",
-				 entry->hooks.func);
-			seq_printf(m, pt(Opt_func), tbuf);
-			break;
+			switch (entry->hooks.policy_id) {
+			case KEXEC_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_kexec));
+				break;
+			case INITRAMFS_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_initramfs));
+				break;
+			default:
+				snprintf(tbuf, sizeof(tbuf), "%d",
+					 entry->hooks.func);
+				seq_printf(m, pt(Opt_func), tbuf);
+				break;
+			}
 		}
 		seq_puts(m, " ");
 	}
Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#18 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 8 lines
Quoted 8 lines
fs/exec.c +0 -0
index 211b81c..a5ae51e 100644
@@ -884,6 +884,21 @@ out:
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
 
Quoted 5 lines

fs/exec.c out: · line 888

Quoted 2 lines
fs/exec.c +2 -0
+int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
+			     int policy_id)

Though this is only used in kexec now, it looks more a general function, move it to general code should be fine along with kernel_read_file

Oops, seems I misread exec.c as kexec.c

author
#19 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

In reply to Dave Young
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

Quoted 3 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall. On systems without MODULE_SIG_FORCE, the IMA policy would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

The pe format is supported on x86. Why require the pe file signature format on all platforms?

Quoted 1 line

Do you have a simple usage documentation about how to test it?

The wiki[1] and ima-evm-ctl package[2] have directions for enabling IMA/IMA-appraisal.

To include just the kexec image and initramfs file hashes in the IMA measurement list, create a file containing the following IMA policy rules. "cat" the policy and redirect the output
to /sys/kernel/security/ima/policy. After loading the kexec image and
initramfs, the IMA measurements will be included in the measurement list (/sys/kernel/security/ima/ascii_runtime_measurements)

IMA policy:
measure func=KEXEC_CHECK
measure func=INITRAMFS_CHECK

Appraising the kexec image and initramfs is a bit more complicated as it requires creating a key, which is signed by a key on the system keyring, and loading the key onto the trusted IMA keyring. To simplify testing, without CONFIG_IMA_TRUSTED_KEYRING enabled, the key being loaded onto the IMA keyring does not need to be signed. The evmctl man page[2] contains directions for creating and loading the key onto the IMA keyring.

To appraise just the kexec image and initramfs files, add the following two rules to the IMA policy and load the policy as before. (The policy can only be loaded once per boot, unless IMA_WRITE_POLICY is configured. With the default appraisal policy, the policy would need to signed.) Sign the kexec image and initramfs with evmctl before loading them.

# evmctl ima_sign -k <private key> -a sha256 <VM image>
# evmctl ima_sign -k <private key> -a sha256 <initramfs>

IMA appraise policy:
appraise func=KEXEC_CHECK appraise_type=imasig appraise func=INITRAMFS_CHECK appraise_type=imasig

[1] http://sourceforge.net/p/linux-ima/wiki/Home
[2] http://linux-ima.sourceforge.net/evmctl.1.html

Quoted 5 lines

fs/exec.c out: · line 891

Quoted 3 lines
fs/exec.c +3 -0
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

-EBADF looks better?

Sure.

Mimi

#20 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 9 lines · Mimi Zohar
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

That's a bit unfair to your work here, this actually paves the way for not only IMA but also other LSMs to vet for the kexec/initramfs given LSM hooks are used now on a common kernel read set of functions.

Quoted 10 lines
Quoted 4 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall. On systems without MODULE_SIG_FORCE, the IMA policy would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Right, so what this approach has revealed really is that architecturally MODULE_SIG_FORCE should have been an LSM but its not, its also hard to make it an LSM. Now with LSM stacking this might make more sense but that requires work and who knows when and if that will happen. In the meantime we'll live with the fact that enabling MODULE_SIG_FORCE means you want to stack on top of the LSMs you have enabled, the MODULE_SIG_FORCE functionality being all kernel related and perhaps easier to manage / set.

Luis

author
#21 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 6 lines · Luis R. Rodriguez
Quoted 5 lines · Mimi Zohar
Quoted 4 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

That's a bit unfair to your work here, this actually paves the way for not only IMA but also other LSMs to vet for the kexec/initramfs given LSM hooks are used now on a common kernel read set of functions.

Right, I responded to his questions about IMA and not the bigger picture.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall.

Sorry for jumping back and forth between security hooks. Similarly, for the kernel module hook, it prevents the original kernel module syscall as well.

Quoted 9 lines

On systems without MODULE_SIG_FORCE, the IMA policy

Quoted 2 lines

would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Right, so what this approach has revealed really is that architecturally MODULE_SIG_FORCE should have been an LSM but its not, its also hard to make it an LSM. Now with LSM stacking this might make more sense but that requires work and who knows when and if that will happen.

I kind of lost you here. A new mini LSM would require file signatures of an existing type or would it be a new method for verifying file signatures?

Quoted 4 lines

In the meantime we'll live
with the fact that enabling MODULE_SIG_FORCE means you want to stack on top of the LSMs you have enabled, the MODULE_SIG_FORCE functionality being all kernel related and perhaps easier to manage / set.

As I see it, with MODULE_SIG_FORCE, IMA-appraisal could relax its own policy knowing that only signed kernel modules are loaded. Without MODULE_SIG_FORCE enabled, then IMA-appraisal needs to do the enforcing, of course based on policy.

Mimi

#22 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 10 lines · Mimi Zohar
Quoted 9 lines · Luis R. Rodriguez
Quoted 8 lines · Mimi Zohar
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Yes, without IMA configured or an IMA policy, it is the same as before.

That's a bit unfair to your work here, this actually paves the way for not only IMA but also other LSMs to vet for the kexec/initramfs given LSM hooks are used now on a common kernel read set of functions.

Right, I responded to his questions about IMA and not the bigger picture.

Indeed, its just the bigger picture helps validate your work further.

Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall.

Sorry for jumping back and forth between security hooks. Similarly, for the kernel module hook, it prevents the original kernel module syscall as well.

This is indeed an important feature. I know people who don't use IMA won't care, but people who do should, likewise its also important for the prospects in design of LSMs if they wanted to consider the prospects of this evaluation for any of the file types we are now clearly defining.

Quoted 5 lines
Quoted 5 lines

On systems without MODULE_SIG_FORCE, the IMA policy

Quoted 2 lines

would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Quoted 8 lines
Quoted 4 lines

Right, so what this approach has revealed really is that architecturally MODULE_SIG_FORCE should have been an LSM but its not, its also hard to make it an LSM. Now with LSM stacking this might make more sense but that requires work and who knows when and if that will happen.

I kind of lost you here. A new mini LSM would require file signatures of an existing type or would it be a new method for verifying file signatures?

No the way I see it is this is a feature for signature verification with all security mechanisms built-in to the kernel. Clearly there is a difference for how signatures are defined between file types: modules have signatures built-in to the file, meanwhile for other files this might be detached (that will be the case for firmware, sysdata). This has yet to be considered for kexec / initramfs but it shouldn't' be too hard now to consider that. Its also why firmware signatures is using a separate kconfig option.

Quoted 9 lines
Quoted 4 lines

In the meantime we'll live
with the fact that enabling MODULE_SIG_FORCE means you want to stack on top of the LSMs you have enabled, the MODULE_SIG_FORCE functionality being all kernel related and perhaps easier to manage / set.

As I see it, with MODULE_SIG_FORCE, IMA-appraisal could relax its own policy knowing that only signed kernel modules are loaded. Without MODULE_SIG_FORCE enabled, then IMA-appraisal needs to do the enforcing, of course based on policy.

Sure, up to the LSMs, which begs the question of how an LSM would codify such stacking assumptions. IIRC stacking will just let the code stack, but I am not sure if we have a clean way to know: hey the kernel vetted this file's signature itself, just fyi. Other than #idef'ery or relying on the .config. What I just described is the case for say IMA and kernel module firmware signing (note both have separate kconfigs), the same question still applies to ways LSM stacking can provide a set of security requirements each could have addressed. All in a clean way.

Luis

#23 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

In reply to Mimi Zohar

Hi, Mimi

Quoted 14 lines · Mimi Zohar
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

Quoted 3 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

Quoted 5 lines

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall. On systems without MODULE_SIG_FORCE, the IMA policy would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Hmm, enabling policy is in userspace (initramfs?) so it may not be good enough for secure boot case. IMA can be used as a uniform method for kexec kernel signature verification for !UEFI or !secure-boot case.

Quoted 3 lines

The pe format is supported on x86. Why require the pe file signature format on all platforms?

For secure boot purpose, an uefi bootable kernel (as an uefi applicatioin) require it to be a pe file.

But for !secure-boot it is not mandatory.

Quoted 34 lines
Quoted 2 lines

Do you have a simple usage documentation about how to test it?

The wiki[1] and ima-evm-ctl package[2] have directions for enabling IMA/IMA-appraisal.

To include just the kexec image and initramfs file hashes in the IMA measurement list, create a file containing the following IMA policy rules. "cat" the policy and redirect the output
to /sys/kernel/security/ima/policy. After loading the kexec image and
initramfs, the IMA measurements will be included in the measurement list (/sys/kernel/security/ima/ascii_runtime_measurements)

IMA policy:
measure func=KEXEC_CHECK
measure func=INITRAMFS_CHECK

Appraising the kexec image and initramfs is a bit more complicated as it requires creating a key, which is signed by a key on the system keyring, and loading the key onto the trusted IMA keyring. To simplify testing, without CONFIG_IMA_TRUSTED_KEYRING enabled, the key being loaded onto the IMA keyring does not need to be signed. The evmctl man page[2] contains directions for creating and loading the key onto the IMA keyring.

To appraise just the kexec image and initramfs files, add the following two rules to the IMA policy and load the policy as before. (The policy can only be loaded once per boot, unless IMA_WRITE_POLICY is configured. With the default appraisal policy, the policy would need to signed.) Sign the kexec image and initramfs with evmctl before loading them.

# evmctl ima_sign -k <private key> -a sha256 <VM image>
# evmctl ima_sign -k <private key> -a sha256 <initramfs>
Quoted 6 lines

IMA appraise policy:
appraise func=KEXEC_CHECK appraise_type=imasig appraise func=INITRAMFS_CHECK appraise_type=imasig

[1] http://sourceforge.net/p/linux-ima/wiki/Home
[2] http://linux-ima.sourceforge.net/evmctl.1.html

Thank you, will try

Quoted 9 lines
Quoted 6 lines

fs/exec.c out: · line 891

Quoted 4 lines
fs/exec.c +3 -0
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

-EBADF looks better?

Sure.

Seems you missed another comment about the policy id name? can the name be like below?
KEXEC_KERNEL_CHECK
KEXEC_INITRAMFS_CHECK

Thanks
Dave

author
#24 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Hi Dave,

Quoted 23 lines · Dave Young

Hi, Mimi

Quoted 14 lines · Mimi Zohar
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

Quoted 3 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

Quoted 5 lines

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall. On systems without MODULE_SIG_FORCE, the IMA policy would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Quoted 3 lines

Hmm, enabling policy is in userspace (initramfs?) so it may not be good enough for secure boot case. IMA can be used as a uniform method for kexec kernel signature verification for !UEFI or !secure-boot case.

Normally, the kernel is booted with a builtin policy. The policy, if
it is being replaced, is normally replaced in the initramfs. This patch
set introduces the concept of a signed policy. Refer to the last 3
patches.

Quoted 8 lines
Quoted 3 lines

The pe format is supported on x86. Why require the pe file signature format on all platforms?

For secure boot purpose, an uefi bootable kernel (as an uefi applicatioin) require it to be a pe file.

But for !secure-boot it is not mandatory.

It would be more appropriate to say that "UEFI secure boot" requires a pe file, as opposed to "secure boot" in general.

Quoted 49 lines
Quoted 33 lines
Quoted 1 line

Do you have a simple usage documentation about how to test it?

The wiki[1] and ima-evm-ctl package[2] have directions for enabling IMA/IMA-appraisal.

To include just the kexec image and initramfs file hashes in the IMA measurement list, create a file containing the following IMA policy rules. "cat" the policy and redirect the output
to /sys/kernel/security/ima/policy. After loading the kexec image and
initramfs, the IMA measurements will be included in the measurement list (/sys/kernel/security/ima/ascii_runtime_measurements)

IMA policy:
measure func=KEXEC_CHECK
measure func=INITRAMFS_CHECK

Appraising the kexec image and initramfs is a bit more complicated as it requires creating a key, which is signed by a key on the system keyring, and loading the key onto the trusted IMA keyring. To simplify testing, without CONFIG_IMA_TRUSTED_KEYRING enabled, the key being loaded onto the IMA keyring does not need to be signed. The evmctl man page[2] contains directions for creating and loading the key onto the IMA keyring.

To appraise just the kexec image and initramfs files, add the following two rules to the IMA policy and load the policy as before. (The policy can only be loaded once per boot, unless IMA_WRITE_POLICY is configured. With the default appraisal policy, the policy would need to signed.) Sign the kexec image and initramfs with evmctl before loading them.

# evmctl ima_sign -k <private key> -a sha256 <VM image>
# evmctl ima_sign -k <private key> -a sha256 <initramfs>
Quoted 6 lines

IMA appraise policy:
appraise func=KEXEC_CHECK appraise_type=imasig appraise func=INITRAMFS_CHECK appraise_type=imasig

[1] http://sourceforge.net/p/linux-ima/wiki/Home
[2] http://linux-ima.sourceforge.net/evmctl.1.html

Thank you, will try

Quoted 7 lines
Quoted 6 lines

fs/exec.c out: · line 891

Quoted 4 lines
fs/exec.c +3 -0
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

-EBADF looks better?

Quoted 6 lines
Quoted 2 lines

Sure.

Seems you missed another comment about the policy id name? can the name be like below?
KEXEC_KERNEL_CHECK
KEXEC_INITRAMFS_CHECK

Luis suggested making the enumeration more generic, not IMA specific. I suggested the following:

enum kernel_read_file_id {
READING_KEXEC_IMAGE = 1,
READING_KEXEC_INITRAMFS,
READING_FIRMWARE,
READING_MODULE,
READING_POLICY,
READING_MAX_ID
};

Mimi

#25 lore raw

Re: [RFC PATCH v2 06/11] kexec: replace call to copy_file_from_fd() with kernel version

Quoted 26 lines · Mimi Zohar

Hi Dave,

Quoted 23 lines · Dave Young

Hi, Mimi

Quoted 19 lines · Mimi Zohar
Quoted 6 lines · Dave Young

Hi, Mimi

Besides of code issues, I have several thing to be understand:

What is the effect to kexec behavior with this patchset?
- without IMA enabled (kconfig or kernel cmdline) it will be same as before?

Yes, without IMA configured or an IMA policy, it is the same as before.

Quoted 3 lines

- with IMA enabled for kernel bzImage, kexec_file_load will check both ima signature and original pe file signature, those two mechanisms are somehow duplicated. I'm not sure if we need both for bzImage.

IMA provides a uniform method of measuring and appraising all files on the system, based on policy. The IMA policy could prevent the original kexec syscall. On systems without MODULE_SIG_FORCE, the IMA policy would require an IMA signature as well. (The current patch would require both, even when MODULE_SIG_FORCE is enabled.)

Quoted 8 lines
Quoted 3 lines

Hmm, enabling policy is in userspace (initramfs?) so it may not be good enough for secure boot case. IMA can be used as a uniform method for kexec kernel signature verification for !UEFI or !secure-boot case.

Normally, the kernel is booted with a builtin policy. The policy, if
it is being replaced, is normally replaced in the initramfs. This patch
set introduces the concept of a signed policy. Refer to the last 3
patches.

But one can still disable ima via kernel cmdline. I'm not objecting this patch I just think that it can not replace the kexec signature verification that we are using for UEFI secure boot.

I think they can coexist consider their different design.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

The pe format is supported on x86. Why require the pe file signature format on all platforms?

Quoted 7 lines
Quoted 4 lines

For secure boot purpose, an uefi bootable kernel (as an uefi applicatioin) require it to be a pe file.

But for !secure-boot it is not mandatory.

It would be more appropriate to say that "UEFI secure boot" requires a pe file, as opposed to "secure boot" in general.

Fair enough and agreed.

Quoted 60 lines
Quoted 50 lines
Quoted 34 lines
Quoted 2 lines

Do you have a simple usage documentation about how to test it?

The wiki[1] and ima-evm-ctl package[2] have directions for enabling IMA/IMA-appraisal.

To include just the kexec image and initramfs file hashes in the IMA measurement list, create a file containing the following IMA policy rules. "cat" the policy and redirect the output
to /sys/kernel/security/ima/policy. After loading the kexec image and
initramfs, the IMA measurements will be included in the measurement list (/sys/kernel/security/ima/ascii_runtime_measurements)

IMA policy:
measure func=KEXEC_CHECK
measure func=INITRAMFS_CHECK

Appraising the kexec image and initramfs is a bit more complicated as it requires creating a key, which is signed by a key on the system keyring, and loading the key onto the trusted IMA keyring. To simplify testing, without CONFIG_IMA_TRUSTED_KEYRING enabled, the key being loaded onto the IMA keyring does not need to be signed. The evmctl man page[2] contains directions for creating and loading the key onto the IMA keyring.

To appraise just the kexec image and initramfs files, add the following two rules to the IMA policy and load the policy as before. (The policy can only be loaded once per boot, unless IMA_WRITE_POLICY is configured. With the default appraisal policy, the policy would need to signed.) Sign the kexec image and initramfs with evmctl before loading them.

# evmctl ima_sign -k <private key> -a sha256 <VM image>
# evmctl ima_sign -k <private key> -a sha256 <initramfs>
Quoted 6 lines

IMA appraise policy:
appraise func=KEXEC_CHECK appraise_type=imasig appraise func=INITRAMFS_CHECK appraise_type=imasig

[1] http://sourceforge.net/p/linux-ima/wiki/Home
[2] http://linux-ima.sourceforge.net/evmctl.1.html

Thank you, will try

Quoted 7 lines
Quoted 6 lines

fs/exec.c out: · line 891

Quoted 4 lines
fs/exec.c +3 -0
+{
+	struct fd f = fdget(fd);
+	int ret = -ENOEXEC;

-EBADF looks better?

Quoted 6 lines
Quoted 2 lines

Sure.

Seems you missed another comment about the policy id name? can the name be like below?
KEXEC_KERNEL_CHECK
KEXEC_INITRAMFS_CHECK

Luis suggested making the enumeration more generic, not IMA specific. I suggested the following:

Quoted 8 lines

enum kernel_read_file_id {
READING_KEXEC_IMAGE = 1,
READING_KEXEC_INITRAMFS,
READING_FIRMWARE,
READING_MODULE,
READING_POLICY,
READING_MAX_ID
};

It is even better.

Quoted 3 lines

Mimi

Thanks
Dave

author
#26 lore raw

RFC PATCH v2 7/11 firmware: replace call to fw_read_file_contents() with kernel version

In reply to Mimi Zohar

Replace fw_read_file_contents() for reading a file with the common VFS kernel_read_file() function. A benefit of calling kernel_read_file() to read the firmware is the firmware is read only once, instead of once for measuring/appraising the firmware and again for reading the file contents into memory.

This patch retains the kernel_fw_from_file() hook, which is called from security_kernel_post_read_file(), but removes the sercurity_kernel_fw_from_file() function.

Changelog:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)

---
 drivers/base/firmware_class.c         | 48 ++++++++++-------------------------
 include/linux/ima.h                   |  7 +----
 include/linux/security.h              |  8 +-----
 security/integrity/ima/ima.h          |  1 -
 security/integrity/ima/ima_appraise.c |  8 ------
 security/integrity/ima/ima_main.c     | 18 +++++--------
 security/integrity/ima/ima_policy.c   | 26 +++++++++----------
 security/integrity/integrity.h        | 11 +++-----
 security/security.c                   | 28 ++++++++++----------
 9 files changed, 54 insertions(+), 101 deletions(-)
drivers/base/firmware_class.c +13 -35
index 8524450..cc175f1 100644
@@ -29,6 +29,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/reboot.h>
 #include <linux/security.h>
+#include <linux/ima.h>
 
 #include <generated/utsrelease.h>
 
@@ -291,40 +292,10 @@ static const char * const fw_path[] = {
 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
 
-static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
-{
-	int size;
-	char *buf;
-	int rc;
-
-	if (!S_ISREG(file_inode(file)->i_mode))
-		return -EINVAL;
-	size = i_size_read(file_inode(file));
-	if (size <= 0)
-		return -EINVAL;
-	buf = vmalloc(size);
-	if (!buf)
-		return -ENOMEM;
-	rc = kernel_read(file, 0, buf, size);
-	if (rc != size) {
-		if (rc > 0)
-			rc = -EIO;
-		goto fail;
-	}
-	rc = security_kernel_fw_from_file(file, buf, size);
-	if (rc)
-		goto fail;
-	fw_buf->data = buf;
-	fw_buf->size = size;
-	return 0;
-fail:
-	vfree(buf);
-	return rc;
-}
-
 static int fw_get_filesystem_firmware(struct device *device,
 				       struct firmware_buf *buf)
 {
+	loff_t size;
 	int i, len;
 	int rc = -ENOENT;
 	char *path;
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
 		file = filp_open(path, O_RDONLY, 0);
 		if (IS_ERR(file))
 			continue;
-		rc = fw_read_file_contents(file, buf);
+
+		buf->size = 0;
+		rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+				      FIRMWARE_CHECK);
 		fput(file);
 		if (rc)
 			dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
 				path, rc);
-		else
+		else {
+			buf->size = (size_t) size;
 			break;
+		}
 	}
 	__putname(path);
 
@@ -685,8 +661,10 @@ static ssize_t firmware_loading_store(struct device *dev,
 				dev_err(dev, "%s: map pages failed\n",
 					__func__);
 			else
-				rc = security_kernel_fw_from_file(NULL,
-						fw_buf->data, fw_buf->size);
+				rc = security_kernel_post_read_file(NULL,
+							       fw_buf->data,
+							       fw_buf->size,
+							       FIRMWARE_CHECK);
 
 			/*
 			 * Same logic as fw_load_abort, only the DONE bit
include/linux/ima.h +1 -6
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
 	KEXEC_CHECK = 1,
 	INITRAMFS_CHECK,
+	FIRMWARE_CHECK,
 	IMA_MAX_READ_CHECK
 };
 
@@ -25,7 +26,6 @@ extern int ima_file_check(struct file *file, int mask, int opened);
 extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
 extern int ima_module_check(struct file *file);
-extern int ima_fw_from_file(struct file *file, char *buf, size_t size);
 extern int ima_hash_and_process_file(struct file *file,
 				     void *buf, loff_t size,
 				     enum ima_policy_id policy_id);
@@ -56,11 +56,6 @@ static inline int ima_module_check(struct file *file)
 	return 0;
 }
 
-static inline int ima_fw_from_file(struct file *file, char *buf, size_t size)
-{
-	return 0;
-}
-
 static inline int ima_hash_and_process_file(struct file *file,
 					    void *buf, loff_t size,
 					    enum ima_policy_id policy_id)
include/linux/security.h +1 -7
index 44d8832..51f3bc6 100644
@@ -28,6 +28,7 @@
 #include <linux/err.h>
 #include <linux/string.h>
 #include <linux/mm.h>
+#include <linux/ima.h>
 
 struct linux_binprm;
 struct cred;
@@ -298,7 +299,6 @@ int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp);
 void security_transfer_creds(struct cred *new, const struct cred *old);
 int security_kernel_act_as(struct cred *new, u32 secid);
 int security_kernel_create_files_as(struct cred *new, struct inode *inode);
-int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
 int security_kernel_module_request(char *kmod_name);
 int security_kernel_module_from_file(struct file *file);
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
@@ -852,12 +852,6 @@ static inline int security_kernel_create_files_as(struct cred *cred,
 	return 0;
 }
 
-static inline int security_kernel_fw_from_file(struct file *file,
-					       char *buf, size_t size)
-{
-	return 0;
-}
-
 static inline int security_kernel_module_request(char *kmod_name)
 {
 	return 0;
security/integrity/ima/ima.h +0 -1
index b98dbd5..520c7b4 100644
@@ -165,7 +165,6 @@ enum ima_hooks {
 	MMAP_CHECK,
 	BPRM_CHECK,
 	MODULE_CHECK,
-	FIRMWARE_CHECK,
 	POST_SETATTR
 };
 
security/integrity/ima/ima_appraise.c +0 -8
index 3adf937..57b1ad1 100644
@@ -76,8 +76,6 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_bprm_status;
 	case MODULE_CHECK:
 		return iint->ima_module_status;
-	case FIRMWARE_CHECK:
-		return iint->ima_firmware_status;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		return iint->ima_read_status;
 	case FILE_CHECK:
@@ -99,9 +97,6 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
 	case MODULE_CHECK:
 		iint->ima_module_status = status;
 		break;
-	case FIRMWARE_CHECK:
-		iint->ima_firmware_status = status;
-		break;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		iint->ima_read_status = status;
 		break;
@@ -124,9 +119,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	case MODULE_CHECK:
 		iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
 		break;
-	case FIRMWARE_CHECK:
-		iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
-		break;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		break;
 	case FILE_CHECK:
security/integrity/ima/ima_main.c +7 -11
index 668cbc6..1251882 100644
@@ -337,17 +337,6 @@ int ima_module_check(struct file *file)
 	return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
 }
 
-int ima_fw_from_file(struct file *file, char *buf, size_t size)
-{
-	if (!file) {
-		if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
-		    (ima_appraise & IMA_APPRAISE_ENFORCE))
-			return -EACCES;	/* INTEGRITY_UNKNOWN */
-		return 0;
-	}
-	return process_measurement(file, NULL, 0, MAY_EXEC, FIRMWARE_CHECK, 0);
-}
-
 /**
  * ima_hash_and_process_file - in memory collect/appraise/audit measurement
  * @file: pointer to the file to be measured/appraised/audit
@@ -361,6 +350,13 @@ int ima_fw_from_file(struct file *file, char *buf, size_t size)
 int ima_hash_and_process_file(struct file *file, void *buf, loff_t size,
 			      enum ima_policy_id policy_id)
 {
+	if (!file && policy_id == FIRMWARE_CHECK) {
+		if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
+		    (ima_appraise & IMA_APPRAISE_ENFORCE))
+			return -EACCES;	/* INTEGRITY_UNKNOWN */
+		return 0;
+	}
+
 	if (!file || !buf || size == 0) { /* should never happen */
 		if (ima_appraise & IMA_APPRAISE_ENFORCE)
 			return -EACCES;
security/integrity/ima/ima_policy.c +13 -13
index 4711083..dbd7aa1 100644
@@ -102,7 +102,8 @@ static struct ima_rule_entry original_measurement_rules[] = {
 	{.action = MEASURE, .hooks.func = FILE_CHECK, .mask = MAY_READ,
 	 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
 	{.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
-	{.action = MEASURE, .hooks.func = FIRMWARE_CHECK, .flags = IMA_FUNC},
+	{.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
+	 .flags = IMA_FUNC},
 };
 
 static struct ima_rule_entry default_measurement_rules[] = {
@@ -115,7 +116,8 @@ static struct ima_rule_entry default_measurement_rules[] = {
 	{.action = MEASURE, .hooks.func = FILE_CHECK, .mask = MAY_READ,
 	 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
 	{.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
-	{.action = MEASURE, .hooks.func = FIRMWARE_CHECK, .flags = IMA_FUNC},
+	{.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
+	 .flags = IMA_FUNC},
 };
 
 static struct ima_rule_entry default_appraise_rules[] = {
@@ -304,8 +306,6 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
 		return IMA_BPRM_APPRAISE;
 	case MODULE_CHECK:
 		return IMA_MODULE_APPRAISE;
-	case FIRMWARE_CHECK:
-		return IMA_FIRMWARE_APPRAISE;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		return IMA_READ_APPRAISE;
 	case FILE_CHECK:
@@ -609,8 +609,6 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.func = FILE_CHECK;
 			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
 				entry->hooks.func = MODULE_CHECK;
-			else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
-				entry->hooks.func = FIRMWARE_CHECK;
 			else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
 				|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
 				entry->hooks.func = MMAP_CHECK;
@@ -620,6 +618,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.policy_id = KEXEC_CHECK;
 			else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0)
 				entry->hooks.policy_id = INITRAMFS_CHECK;
+			else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
+				entry->hooks.policy_id = FIRMWARE_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
@@ -776,7 +776,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 		result = -EINVAL;
 	else if (entry->hooks.func == MODULE_CHECK)
 		temp_ima_appraise |= IMA_APPRAISE_MODULES;
-	else if (entry->hooks.func == FIRMWARE_CHECK)
+	else if (entry->hooks.policy_id == FIRMWARE_CHECK)
 		temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
 	audit_log_format(ab, "res=%d", !result);
 	audit_log_end(ab);
@@ -863,8 +863,8 @@ static char *mask_tokens[] = {
 
 enum {
 	func_file = 0, func_mmap, func_bprm,
-	func_module, func_firmware, func_post,
-	func_kexec, func_initramfs
+	func_module, func_post,
+	func_kexec, func_initramfs, func_firmware
 };
 
 static char *func_tokens[] = {
@@ -872,10 +872,10 @@ static char *func_tokens[] = {
 	"MMAP_CHECK",
 	"BPRM_CHECK",
 	"MODULE_CHECK",
-	"FIRMWARE_CHECK",
 	"POST_SETATTR",
 	"KEXEC_CHECK",
 	"INITRAMFS_CHECK",
+	"FIRMWARE_CHECK"
 };
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
@@ -949,9 +949,6 @@ int ima_policy_show(struct seq_file *m, void *v)
 		case MODULE_CHECK:
 			seq_printf(m, pt(Opt_func), ft(func_module));
 			break;
-		case FIRMWARE_CHECK:
-			seq_printf(m, pt(Opt_func), ft(func_firmware));
-			break;
 		case POST_SETATTR:
 			seq_printf(m, pt(Opt_func), ft(func_post));
 			break;
@@ -963,6 +960,9 @@ int ima_policy_show(struct seq_file *m, void *v)
 			case INITRAMFS_CHECK:
 				seq_printf(m, pt(Opt_func), ft(func_initramfs));
 				break;
+			case FIRMWARE_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_firmware));
+				break;
 			default:
 				snprintf(tbuf, sizeof(tbuf), "%d",
 					 entry->hooks.func);
security/integrity/integrity.h +4 -7
index 9a0ea4c..75334cd 100644
@@ -47,16 +47,14 @@
 #define IMA_BPRM_APPRAISED	0x00002000
 #define IMA_MODULE_APPRAISE	0x00004000
 #define IMA_MODULE_APPRAISED	0x00008000
-#define IMA_FIRMWARE_APPRAISE	0x00010000
-#define IMA_FIRMWARE_APPRAISED	0x00020000
-#define IMA_READ_APPRAISE	0x00040000
-#define IMA_READ_APPRAISED	0x00080000
+#define IMA_READ_APPRAISE	0x00010000
+#define IMA_READ_APPRAISED	0x00020000
 #define IMA_APPRAISE_SUBMASK	(IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
 				 IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE | \
-				 IMA_FIRMWARE_APPRAISE | IMA_READ_APPRAISE)
+				 IMA_READ_APPRAISE)
 #define IMA_APPRAISED_SUBMASK	(IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
 				 IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED | \
-				 IMA_FIRMWARE_APPRAISED | IMA_READ_APPRAISED)
+				 IMA_READ_APPRAISED)
 
 enum evm_ima_xattr_type {
 	IMA_XATTR_DIGEST = 0x01,
@@ -112,7 +110,6 @@ struct integrity_iint_cache {
 	enum integrity_status ima_mmap_status:4;
 	enum integrity_status ima_bprm_status:4;
 	enum integrity_status ima_module_status:4;
-	enum integrity_status ima_firmware_status:4;
 	enum integrity_status ima_read_status:4;
 	enum integrity_status evm_status:4;
 	struct ima_digest_data *ima_hash;
security/security.c +15 -13
index 49cacae..a391ce4 100644
@@ -884,17 +884,6 @@ int security_kernel_create_files_as(struct cred *new, struct inode *inode)
 	return call_int_hook(kernel_create_files_as, 0, new, inode);
 }
 
-int security_kernel_fw_from_file(struct file *file, char *buf, size_t size)
-{
-	int ret;
-
-	ret = call_int_hook(kernel_fw_from_file, 0, file, buf, size);
-	if (ret)
-		return ret;
-	return ima_fw_from_file(file, buf, size);
-}
-EXPORT_SYMBOL_GPL(security_kernel_fw_from_file);
-
 int security_kernel_module_request(char *kmod_name)
 {
 	return call_int_hook(kernel_module_request, 0, kmod_name);
@@ -913,8 +902,21 @@ int security_kernel_module_from_file(struct file *file)
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
 				   int policy_id)
 {
-	return = call_int_hook(kernel_post_read_file, 0, file, buf, size,
-			       policy_id);
+	int ret = 0;
+
+	switch (policy_id) {
+	case FIRMWARE_CHECK:
+		ret = call_int_hook(kernel_fw_from_file, 0, file, buf, size);
+		break;
+	default:
+		ret = call_int_hook(kernel_post_read_file, 0, file, buf, size,
+				    policy_id);
+		break;
+	}
+	if (ret)
+		return ret;
+
+	return ima_hash_and_process_file(file, buf, size, policy_id);
 }
 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
 
Signature · 1 line
2.1.0
Reviewed-by on v2 7/11
#27 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

Quoted 10 lines · Mimi Zohar

Replace fw_read_file_contents() for reading a file with the common VFS kernel_read_file() function. A benefit of calling kernel_read_file() to read the firmware is the firmware is read only once, instead of once for measuring/appraising the firmware and again for reading the file contents into memory.

This patch retains the kernel_fw_from_file() hook, which is called from security_kernel_post_read_file(), but removes the sercurity_kernel_fw_from_file() function.

Quoted 5 lines

Changelog:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)

-Kees

Quoted 431 lines
---
 drivers/base/firmware_class.c         | 48 ++++++++++-------------------------
 include/linux/ima.h                   |  7 +----
 include/linux/security.h              |  8 +-----
 security/integrity/ima/ima.h          |  1 -
 security/integrity/ima/ima_appraise.c |  8 ------
 security/integrity/ima/ima_main.c     | 18 +++++--------
 security/integrity/ima/ima_policy.c   | 26 +++++++++----------
 security/integrity/integrity.h        | 11 +++-----
 security/security.c                   | 28 ++++++++++----------
 9 files changed, 54 insertions(+), 101 deletions(-)
drivers/base/firmware_class.c +13 -35
index 8524450..cc175f1 100644
@@ -29,6 +29,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/reboot.h>
 #include <linux/security.h>
+#include <linux/ima.h>
 
 #include <generated/utsrelease.h>
 
@@ -291,40 +292,10 @@ static const char * const fw_path[] = {
 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
 
-static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
-{
-       int size;
-       char *buf;
-       int rc;
-
-       if (!S_ISREG(file_inode(file)->i_mode))
-               return -EINVAL;
-       size = i_size_read(file_inode(file));
-       if (size <= 0)
-               return -EINVAL;
-       buf = vmalloc(size);
-       if (!buf)
-               return -ENOMEM;
-       rc = kernel_read(file, 0, buf, size);
-       if (rc != size) {
-               if (rc > 0)
-                       rc = -EIO;
-               goto fail;
-       }
-       rc = security_kernel_fw_from_file(file, buf, size);
-       if (rc)
-               goto fail;
-       fw_buf->data = buf;
-       fw_buf->size = size;
-       return 0;
-fail:
-       vfree(buf);
-       return rc;
-}
-
 static int fw_get_filesystem_firmware(struct device *device,
                                       struct firmware_buf *buf)
 {
+       loff_t size;
        int i, len;
        int rc = -ENOENT;
        char *path;
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
                file = filp_open(path, O_RDONLY, 0);
                if (IS_ERR(file))
                        continue;
-               rc = fw_read_file_contents(file, buf);
+
+               buf->size = 0;
+               rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+                                     FIRMWARE_CHECK);
                fput(file);
                if (rc)
                        dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n",
                                path, rc);
-               else
+               else {
+                       buf->size = (size_t) size;
                        break;
+               }
        }
        __putname(path);
 
@@ -685,8 +661,10 @@ static ssize_t firmware_loading_store(struct device *dev,
                                dev_err(dev, "%s: map pages failed\n",
                                        __func__);
                        else
-                               rc = security_kernel_fw_from_file(NULL,
-                                               fw_buf->data, fw_buf->size);
+                               rc = security_kernel_post_read_file(NULL,
+                                                              fw_buf->data,
+                                                              fw_buf->size,
+                                                              FIRMWARE_CHECK);
 
                        /*
                         * Same logic as fw_load_abort, only the DONE bit
include/linux/ima.h +1 -6
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
        KEXEC_CHECK = 1,
        INITRAMFS_CHECK,
+       FIRMWARE_CHECK,
        IMA_MAX_READ_CHECK
 };
 
@@ -25,7 +26,6 @@ extern int ima_file_check(struct file *file, int mask, int opened);
 extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
 extern int ima_module_check(struct file *file);
-extern int ima_fw_from_file(struct file *file, char *buf, size_t size);
 extern int ima_hash_and_process_file(struct file *file,
                                     void *buf, loff_t size,
                                     enum ima_policy_id policy_id);
@@ -56,11 +56,6 @@ static inline int ima_module_check(struct file *file)
        return 0;
 }
 
-static inline int ima_fw_from_file(struct file *file, char *buf, size_t size)
-{
-       return 0;
-}
-
 static inline int ima_hash_and_process_file(struct file *file,
                                            void *buf, loff_t size,
                                            enum ima_policy_id policy_id)
include/linux/security.h +1 -7
index 44d8832..51f3bc6 100644
@@ -28,6 +28,7 @@
 #include <linux/err.h>
 #include <linux/string.h>
 #include <linux/mm.h>
+#include <linux/ima.h>
 
 struct linux_binprm;
 struct cred;
@@ -298,7 +299,6 @@ int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp);
 void security_transfer_creds(struct cred *new, const struct cred *old);
 int security_kernel_act_as(struct cred *new, u32 secid);
 int security_kernel_create_files_as(struct cred *new, struct inode *inode);
-int security_kernel_fw_from_file(struct file *file, char *buf, size_t size);
 int security_kernel_module_request(char *kmod_name);
 int security_kernel_module_from_file(struct file *file);
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
@@ -852,12 +852,6 @@ static inline int security_kernel_create_files_as(struct cred *cred,
        return 0;
 }
 
-static inline int security_kernel_fw_from_file(struct file *file,
-                                              char *buf, size_t size)
-{
-       return 0;
-}
-
 static inline int security_kernel_module_request(char *kmod_name)
 {
        return 0;
security/integrity/ima/ima.h +0 -1
index b98dbd5..520c7b4 100644
@@ -165,7 +165,6 @@ enum ima_hooks {
        MMAP_CHECK,
        BPRM_CHECK,
        MODULE_CHECK,
-       FIRMWARE_CHECK,
        POST_SETATTR
 };
 
security/integrity/ima/ima_appraise.c +0 -8
index 3adf937..57b1ad1 100644
@@ -76,8 +76,6 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
                return iint->ima_bprm_status;
        case MODULE_CHECK:
                return iint->ima_module_status;
-       case FIRMWARE_CHECK:
-               return iint->ima_firmware_status;
        case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
                return iint->ima_read_status;
        case FILE_CHECK:
@@ -99,9 +97,6 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
        case MODULE_CHECK:
                iint->ima_module_status = status;
                break;
-       case FIRMWARE_CHECK:
-               iint->ima_firmware_status = status;
-               break;
        case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
                iint->ima_read_status = status;
                break;
@@ -124,9 +119,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
        case MODULE_CHECK:
                iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
                break;
-       case FIRMWARE_CHECK:
-               iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
-               break;
        case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
                break;
        case FILE_CHECK:
security/integrity/ima/ima_main.c +7 -11
index 668cbc6..1251882 100644
@@ -337,17 +337,6 @@ int ima_module_check(struct file *file)
        return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
 }
 
-int ima_fw_from_file(struct file *file, char *buf, size_t size)
-{
-       if (!file) {
-               if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
-                   (ima_appraise & IMA_APPRAISE_ENFORCE))
-                       return -EACCES; /* INTEGRITY_UNKNOWN */
-               return 0;
-       }
-       return process_measurement(file, NULL, 0, MAY_EXEC, FIRMWARE_CHECK, 0);
-}
-
 /**
  * ima_hash_and_process_file - in memory collect/appraise/audit measurement
  * @file: pointer to the file to be measured/appraised/audit
@@ -361,6 +350,13 @@ int ima_fw_from_file(struct file *file, char *buf, size_t size)
 int ima_hash_and_process_file(struct file *file, void *buf, loff_t size,
                              enum ima_policy_id policy_id)
 {
+       if (!file && policy_id == FIRMWARE_CHECK) {
+               if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
+                   (ima_appraise & IMA_APPRAISE_ENFORCE))
+                       return -EACCES; /* INTEGRITY_UNKNOWN */
+               return 0;
+       }
+
        if (!file || !buf || size == 0) { /* should never happen */
                if (ima_appraise & IMA_APPRAISE_ENFORCE)
                        return -EACCES;
security/integrity/ima/ima_policy.c +13 -13
index 4711083..dbd7aa1 100644
@@ -102,7 +102,8 @@ static struct ima_rule_entry original_measurement_rules[] = {
        {.action = MEASURE, .hooks.func = FILE_CHECK, .mask = MAY_READ,
         .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
        {.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
-       {.action = MEASURE, .hooks.func = FIRMWARE_CHECK, .flags = IMA_FUNC},
+       {.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
+        .flags = IMA_FUNC},
 };
 
 static struct ima_rule_entry default_measurement_rules[] = {
@@ -115,7 +116,8 @@ static struct ima_rule_entry default_measurement_rules[] = {
        {.action = MEASURE, .hooks.func = FILE_CHECK, .mask = MAY_READ,
         .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
        {.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
-       {.action = MEASURE, .hooks.func = FIRMWARE_CHECK, .flags = IMA_FUNC},
+       {.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
+        .flags = IMA_FUNC},
 };
 
 static struct ima_rule_entry default_appraise_rules[] = {
@@ -304,8 +306,6 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
                return IMA_BPRM_APPRAISE;
        case MODULE_CHECK:
                return IMA_MODULE_APPRAISE;
-       case FIRMWARE_CHECK:
-               return IMA_FIRMWARE_APPRAISE;
        case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
                return IMA_READ_APPRAISE;
        case FILE_CHECK:
@@ -609,8 +609,6 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
                                entry->hooks.func = FILE_CHECK;
                        else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
                                entry->hooks.func = MODULE_CHECK;
-                       else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
-                               entry->hooks.func = FIRMWARE_CHECK;
                        else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
                                || (strcmp(args[0].from, "MMAP_CHECK") == 0))
                                entry->hooks.func = MMAP_CHECK;
@@ -620,6 +618,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
                                entry->hooks.policy_id = KEXEC_CHECK;
                        else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0)
                                entry->hooks.policy_id = INITRAMFS_CHECK;
+                       else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
+                               entry->hooks.policy_id = FIRMWARE_CHECK;
                        else
                                result = -EINVAL;
                        if (!result)
@@ -776,7 +776,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
                result = -EINVAL;
        else if (entry->hooks.func == MODULE_CHECK)
                temp_ima_appraise |= IMA_APPRAISE_MODULES;
-       else if (entry->hooks.func == FIRMWARE_CHECK)
+       else if (entry->hooks.policy_id == FIRMWARE_CHECK)
                temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
        audit_log_format(ab, "res=%d", !result);
        audit_log_end(ab);
@@ -863,8 +863,8 @@ static char *mask_tokens[] = {
 
 enum {
        func_file = 0, func_mmap, func_bprm,
-       func_module, func_firmware, func_post,
-       func_kexec, func_initramfs
+       func_module, func_post,
+       func_kexec, func_initramfs, func_firmware
 };
 
 static char *func_tokens[] = {
@@ -872,10 +872,10 @@ static char *func_tokens[] = {
        "MMAP_CHECK",
        "BPRM_CHECK",
        "MODULE_CHECK",
-       "FIRMWARE_CHECK",
        "POST_SETATTR",
        "KEXEC_CHECK",
        "INITRAMFS_CHECK",
+       "FIRMWARE_CHECK"
 };
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
@@ -949,9 +949,6 @@ int ima_policy_show(struct seq_file *m, void *v)
                case MODULE_CHECK:
                        seq_printf(m, pt(Opt_func), ft(func_module));
                        break;
-               case FIRMWARE_CHECK:
-                       seq_printf(m, pt(Opt_func), ft(func_firmware));
-                       break;
                case POST_SETATTR:
                        seq_printf(m, pt(Opt_func), ft(func_post));
                        break;
@@ -963,6 +960,9 @@ int ima_policy_show(struct seq_file *m, void *v)
                        case INITRAMFS_CHECK:
                                seq_printf(m, pt(Opt_func), ft(func_initramfs));
                                break;
+                       case FIRMWARE_CHECK:
+                               seq_printf(m, pt(Opt_func), ft(func_firmware));
+                               break;
                        default:
                                snprintf(tbuf, sizeof(tbuf), "%d",
                                         entry->hooks.func);
security/integrity/integrity.h +4 -7
index 9a0ea4c..75334cd 100644
@@ -47,16 +47,14 @@
 #define IMA_BPRM_APPRAISED     0x00002000
 #define IMA_MODULE_APPRAISE    0x00004000
 #define IMA_MODULE_APPRAISED   0x00008000
-#define IMA_FIRMWARE_APPRAISE  0x00010000
-#define IMA_FIRMWARE_APPRAISED 0x00020000
-#define IMA_READ_APPRAISE      0x00040000
-#define IMA_READ_APPRAISED     0x00080000
+#define IMA_READ_APPRAISE      0x00010000
+#define IMA_READ_APPRAISED     0x00020000
 #define IMA_APPRAISE_SUBMASK   (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
                                 IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE | \
-                                IMA_FIRMWARE_APPRAISE | IMA_READ_APPRAISE)
+                                IMA_READ_APPRAISE)
 #define IMA_APPRAISED_SUBMASK  (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
                                 IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED | \
-                                IMA_FIRMWARE_APPRAISED | IMA_READ_APPRAISED)
+                                IMA_READ_APPRAISED)
 
 enum evm_ima_xattr_type {
        IMA_XATTR_DIGEST = 0x01,
@@ -112,7 +110,6 @@ struct integrity_iint_cache {
        enum integrity_status ima_mmap_status:4;
        enum integrity_status ima_bprm_status:4;
        enum integrity_status ima_module_status:4;
-       enum integrity_status ima_firmware_status:4;
        enum integrity_status ima_read_status:4;
        enum integrity_status evm_status:4;
        struct ima_digest_data *ima_hash;
security/security.c +15 -14
index 49cacae..a391ce4 100644
@@ -884,17 +884,6 @@ int security_kernel_create_files_as(struct cred *new, struct inode *inode)
        return call_int_hook(kernel_create_files_as, 0, new, inode);
 }
 
-int security_kernel_fw_from_file(struct file *file, char *buf, size_t size)
-{
-       int ret;
-
-       ret = call_int_hook(kernel_fw_from_file, 0, file, buf, size);
-       if (ret)
-               return ret;
-       return ima_fw_from_file(file, buf, size);
-}
-EXPORT_SYMBOL_GPL(security_kernel_fw_from_file);
-
 int security_kernel_module_request(char *kmod_name)
 {
        return call_int_hook(kernel_module_request, 0, kmod_name);
@@ -913,8 +902,21 @@ int security_kernel_module_from_file(struct file *file)
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
                                   int policy_id)
 {
-       return = call_int_hook(kernel_post_read_file, 0, file, buf, size,
-                              policy_id);
+       int ret = 0;
+
+       switch (policy_id) {
+       case FIRMWARE_CHECK:
+               ret = call_int_hook(kernel_fw_from_file, 0, file, buf, size);
+               break;
+       default:
+               ret = call_int_hook(kernel_post_read_file, 0, file, buf, size,
+                                   policy_id);
+               break;
+       }
+       if (ret)
+               return ret;
+
+       return ima_hash_and_process_file(file, buf, size, policy_id);
 }
 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
 
--

2.1.0

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security
author
#28 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

Quoted 11 lines · Kees Cook
Quoted 10 lines · Mimi Zohar

Replace fw_read_file_contents() for reading a file with the common VFS kernel_read_file() function. A benefit of calling kernel_read_file() to read the firmware is the firmware is read only once, instead of once for measuring/appraising the firmware and again for reading the file contents into memory.

This patch retains the kernel_fw_from_file() hook, which is called from security_kernel_post_read_file(), but removes the sercurity_kernel_fw_from_file() function.

Quoted 7 lines
Quoted 5 lines

Changelog:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)

Thanks!

Mimi

#29 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

In reply to Mimi Zohar
Quoted 82 lines · Mimi Zohar

Replace fw_read_file_contents() for reading a file with the common VFS kernel_read_file() function. A benefit of calling kernel_read_file() to read the firmware is the firmware is read only once, instead of once for measuring/appraising the firmware and again for reading the file contents into memory.

This patch retains the kernel_fw_from_file() hook, which is called from security_kernel_post_read_file(), but removes the sercurity_kernel_fw_from_file() function.

Changelog:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)

---
 drivers/base/firmware_class.c         | 48 ++++++++++-------------------------
 include/linux/ima.h                   |  7 +----
 include/linux/security.h              |  8 +-----
 security/integrity/ima/ima.h          |  1 -
 security/integrity/ima/ima_appraise.c |  8 ------
 security/integrity/ima/ima_main.c     | 18 +++++--------
 security/integrity/ima/ima_policy.c   | 26 +++++++++----------
 security/integrity/integrity.h        | 11 +++-----
 security/security.c                   | 28 ++++++++++----------
 9 files changed, 54 insertions(+), 101 deletions(-)
drivers/base/firmware_class.c +2 -31
index 8524450..cc175f1 100644
@@ -29,6 +29,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/reboot.h>
 #include <linux/security.h>
+#include <linux/ima.h>
 
 #include <generated/utsrelease.h>
 
@@ -291,40 +292,10 @@ static const char * const fw_path[] = {
 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
 
-static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
-{
-	int size;
-	char *buf;
-	int rc;
-
-	if (!S_ISREG(file_inode(file)->i_mode))
-		return -EINVAL;
-	size = i_size_read(file_inode(file));
-	if (size <= 0)
-		return -EINVAL;
-	buf = vmalloc(size);
-	if (!buf)
-		return -ENOMEM;
-	rc = kernel_read(file, 0, buf, size);
-	if (rc != size) {
-		if (rc > 0)
-			rc = -EIO;
-		goto fail;
-	}
-	rc = security_kernel_fw_from_file(file, buf, size);
-	if (rc)
-		goto fail;
-	fw_buf->data = buf;
-	fw_buf->size = size;
-	return 0;
-fail:
-	vfree(buf);
-	return rc;
-}
-
 static int fw_get_filesystem_firmware(struct device *device,
 				       struct firmware_buf *buf)
 {
+	loff_t size;
 	int i, len;
 	int rc = -ENOENT;
 	char *path;

drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 327

Quoted 8 lines
drivers/base/firmware_class.c +4 -1
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
 		file = filp_open(path, O_RDONLY, 0);
 		if (IS_ERR(file))
 			continue;
-		rc = fw_read_file_contents(file, buf);
+
+		buf->size = 0;
+		rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+				      FIRMWARE_CHECK);

The way kernel firmware signing was implemented was that we'd first read the foo.sig (or whatever extension we use). The same kernel_read_file() would be used if this gets applied so this would still works well with that. Of course folks using IMA and their own security policy would just disable the kernel fw signing facility.

include/linux/ima.h struct linux_binprm; · line 21

Quoted 11 lines
include/linux/ima.h +1 -0
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
 	KEXEC_CHECK = 1,
 	INITRAMFS_CHECK,
+	FIRMWARE_CHECK,
 	IMA_MAX_READ_CHECK
 };

The only thing that is worth questioning here in light of kernel fw signing is what int policy_id do we use? Would we be OK to add FIRMWARE_SIG_CHECK later While at it, perhaps kernel_read_file() last argument should be enum ima_policy_id then. If the policy_id is going to be used elsewhere outside of IMA then perhaps ima.h is not the best place for it ? Would fs.h for type of file be OK ? Then we'd have a list of known file types the kernel reads.

Luis

#30 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

Quoted 108 lines · Luis R. Rodriguez
Quoted 82 lines · Mimi Zohar

Replace fw_read_file_contents() for reading a file with the common VFS kernel_read_file() function. A benefit of calling kernel_read_file() to read the firmware is the firmware is read only once, instead of once for measuring/appraising the firmware and again for reading the file contents into memory.

This patch retains the kernel_fw_from_file() hook, which is called from security_kernel_post_read_file(), but removes the sercurity_kernel_fw_from_file() function.

Changelog:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)

---
 drivers/base/firmware_class.c         | 48 ++++++++++-------------------------
 include/linux/ima.h                   |  7 +----
 include/linux/security.h              |  8 +-----
 security/integrity/ima/ima.h          |  1 -
 security/integrity/ima/ima_appraise.c |  8 ------
 security/integrity/ima/ima_main.c     | 18 +++++--------
 security/integrity/ima/ima_policy.c   | 26 +++++++++----------
 security/integrity/integrity.h        | 11 +++-----
 security/security.c                   | 28 ++++++++++----------
 9 files changed, 54 insertions(+), 101 deletions(-)
drivers/base/firmware_class.c +2 -31
index 8524450..cc175f1 100644
@@ -29,6 +29,7 @@
 #include <linux/syscore_ops.h>
 #include <linux/reboot.h>
 #include <linux/security.h>
+#include <linux/ima.h>
 
 #include <generated/utsrelease.h>
 
@@ -291,40 +292,10 @@ static const char * const fw_path[] = {
 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
 MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
 
-static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf)
-{
-     int size;
-     char *buf;
-     int rc;
-
-     if (!S_ISREG(file_inode(file)->i_mode))
-             return -EINVAL;
-     size = i_size_read(file_inode(file));
-     if (size <= 0)
-             return -EINVAL;
-     buf = vmalloc(size);
-     if (!buf)
-             return -ENOMEM;
-     rc = kernel_read(file, 0, buf, size);
-     if (rc != size) {
-             if (rc > 0)
-                     rc = -EIO;
-             goto fail;
-     }
-     rc = security_kernel_fw_from_file(file, buf, size);
-     if (rc)
-             goto fail;
-     fw_buf->data = buf;
-     fw_buf->size = size;
-     return 0;
-fail:
-     vfree(buf);
-     return rc;
-}
-
 static int fw_get_filesystem_firmware(struct device *device,
                                     struct firmware_buf *buf)
 {
+     loff_t size;
      int i, len;
      int rc = -ENOENT;
      char *path;

drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 327

Quoted 8 lines
drivers/base/firmware_class.c +4 -1
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
              file = filp_open(path, O_RDONLY, 0);
              if (IS_ERR(file))
                      continue;
-             rc = fw_read_file_contents(file, buf);
+
+             buf->size = 0;
+             rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+                                   FIRMWARE_CHECK);

The way kernel firmware signing was implemented was that we'd first read the foo.sig (or whatever extension we use). The same kernel_read_file() would be used if this gets applied so this would still works well with that. Of course folks using IMA and their own security policy would just disable the kernel fw signing facility.

Quoted 10 lines
include/linux/ima.h +1 -0
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
      KEXEC_CHECK = 1,
      INITRAMFS_CHECK,
+     FIRMWARE_CHECK,
      IMA_MAX_READ_CHECK
Quoted 8 lines

include/linux/ima.h struct linux_binprm; · line 21

Quoted 1 line
include/linux/ima.h +0 -0
 };

The only thing that is worth questioning here in light of kernel fw signing is what int policy_id do we use? Would we be OK to add FIRMWARE_SIG_CHECK later While at it, perhaps kernel_read_file() last argument should be enum ima_policy_id then. If the policy_id is going to be used elsewhere outside of IMA then perhaps ima.h is not the best place for it ? Would fs.h for type of file be OK ? Then we'd have a list of known file types the kernel reads.

Actually your patch #9 "ima: load policy using path" defines kernel_read_file_from_path and since the firmware code uses a path this code would be much cleaner if instead you used that. It'd mean more code sharing and would make firmware code cleaner. Could you re-order that to go first and then later this as its first user? Perhaps add the helper for the firmware patch.

Luis

author
#31 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

Quoted 14 lines · Luis R. Rodriguez
Quoted 4 lines · Luis R. Rodriguez
Quoted 4 lines
drivers/base/firmware_class.c +0 -0
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
              file = filp_open(path, O_RDONLY, 0);
              if (IS_ERR(file))
                      continue;
Quoted 8 lines

drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 327

Quoted 5 lines
drivers/base/firmware_class.c +4 -1
-             rc = fw_read_file_contents(file, buf);
+
+             buf->size = 0;
+             rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+                                   FIRMWARE_CHECK);

The way kernel firmware signing was implemented was that we'd first read the foo.sig (or whatever extension we use).

Was there a reason for using a detached signature and not using the same method as kernel modules?

Quoted 4 lines

The same kernel_read_file() would be

Quoted 3 lines

used if this gets applied so this would still works well with that. Of course folks using IMA and their own security policy would just disable the kernel fw signing facility.

Right, support for not measuring/appraising the firmware and sig would be supported in the policy.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
include/linux/ima.h +1 -0
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
      KEXEC_CHECK = 1,
      INITRAMFS_CHECK,
+     FIRMWARE_CHECK,
      IMA_MAX_READ_CHECK
Quoted 8 lines
Quoted 8 lines

include/linux/ima.h struct linux_binprm; · line 21

Quoted 1 line
include/linux/ima.h +0 -0
 };

The only thing that is worth questioning here in light of kernel fw signing is what int policy_id do we use? Would we be OK to add FIRMWARE_SIG_CHECK later While at it, perhaps kernel_read_file() last argument should be enum ima_policy_id then. If the policy_id is going to be used elsewhere outside of IMA then perhaps ima.h is not the best place for it ? Would fs.h for type of file be OK ? Then we'd have a list of known file types the kernel reads.

I would definitely prefer the enumeration be defined at the VFS layer. For example,

enum kernel_read_file_id {
READING_KEXEC_IMAGE,
READING_KEXEC_INITRAMFS,
READING_FIRMWARE,
READING_FIRMWARE_SIG,
READING_MAX_ID
};

Agreed, the last field of kernel_read_file() and the wrappers should be the enumeration.

Quoted 6 lines

Actually your patch #9 "ima: load policy using path" defines kernel_read_file_from_path and since the firmware code uses a path this code would be much cleaner if instead you used that. It'd mean more code sharing and would make firmware code cleaner. Could you re-order that to go first and then later this as its first user? Perhaps add the helper for the firmware patch.

Thanks, I missed that. I'll include this change in the next version.

Mimi

#32 lore raw

Re: [RFC PATCH v2 07/11] firmware: replace call to fw_read_file_contents() with kernel version

Quoted 16 lines · Mimi Zohar
Quoted 14 lines · Luis R. Rodriguez
Quoted 4 lines · Luis R. Rodriguez
Quoted 4 lines
drivers/base/firmware_class.c +0 -0
@@ -350,13 +321,18 @@ static int fw_get_filesystem_firmware(struct device *device,
              file = filp_open(path, O_RDONLY, 0);
              if (IS_ERR(file))
                      continue;
Quoted 8 lines

drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 327

Quoted 5 lines
drivers/base/firmware_class.c +4 -1
-             rc = fw_read_file_contents(file, buf);
+
+             buf->size = 0;
+             rc = kernel_read_file(file, &buf->data, &size, INT_MAX,
+                                   FIRMWARE_CHECK);

The way kernel firmware signing was implemented was that we'd first read the foo.sig (or whatever extension we use).

Quoted 2 lines

Was there a reason for using a detached signature and not using the same method as kernel modules?

Yes, the firmware might have different license. Its also easier for users to use standard mechanisms to verify.

Quoted 7 lines
Quoted 4 lines

The same kernel_read_file() would be

Quoted 3 lines

used if this gets applied so this would still works well with that. Of course folks using IMA and their own security policy would just disable the kernel fw signing facility.

Right, support for not measuring/appraising the firmware and sig would be supported in the policy.

OK!

Quoted 22 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
include/linux/ima.h +1 -0
index ae91938..0a7f039 100644
@@ -16,6 +16,7 @@ struct linux_binprm;
 enum ima_policy_id {
      KEXEC_CHECK = 1,
      INITRAMFS_CHECK,
+     FIRMWARE_CHECK,
      IMA_MAX_READ_CHECK
Quoted 8 lines
Quoted 8 lines

include/linux/ima.h struct linux_binprm; · line 21

Quoted 1 line
include/linux/ima.h +0 -0
 };

The only thing that is worth questioning here in light of kernel fw signing is what int policy_id do we use? Would we be OK to add FIRMWARE_SIG_CHECK later While at it, perhaps kernel_read_file() last argument should be enum ima_policy_id then. If the policy_id is going to be used elsewhere outside of IMA then perhaps ima.h is not the best place for it ? Would fs.h for type of file be OK ? Then we'd have a list of known file types the kernel reads.

I would definitely prefer the enumeration be defined at the VFS layer. For example,

Quoted 7 lines

enum kernel_read_file_id {
READING_KEXEC_IMAGE,
READING_KEXEC_INITRAMFS,
READING_FIRMWARE,
READING_FIRMWARE_SIG,
READING_MAX_ID
};

Looks good to me. Please add a kdoc section to force us to have to document each one.

Quoted 2 lines

Agreed, the last field of kernel_read_file() and the wrappers should be the enumeration.

Great.

Quoted 8 lines
Quoted 6 lines

Actually your patch #9 "ima: load policy using path" defines kernel_read_file_from_path and since the firmware code uses a path this code would be much cleaner if instead you used that. It'd mean more code sharing and would make firmware code cleaner. Could you re-order that to go first and then later this as its first user? Perhaps add the helper for the firmware patch.

Thanks, I missed that. I'll include this change in the next version.

Great.

Luis

author
#33 lore raw

RFC PATCH v2 8/11 module: replace copy_module_from_fd with kernel version

In reply to Mimi Zohar

This patch replaces the module copy_module_from_fd() call with the VFS common kernel_read_file_from_fd() function. Instead of reading the kernel module twice, once for measuring/appraising and then loading the kernel module, the file is read once.

This patch defines a new security hook named security_kernel_read_file(), which is called before reading the file. For now, call the module security hook from security_kernel_read_file until the LSMs have been converted to use the kernel_read_file hook.

This patch retains the kernel_module_from_file hook, but removes the security_kernel_module_from_file() function.

---
 fs/exec.c                             |  4 +++
 include/linux/ima.h                   |  1 +
 include/linux/lsm_hooks.h             |  8 +++++
 include/linux/security.h              |  3 +-
 kernel/module.c                       | 67 ++++-------------------------------
 security/integrity/ima/ima.h          |  1 -
 security/integrity/ima/ima_appraise.c |  7 ----
 security/integrity/ima/ima_main.c     |  5 ++-
 security/integrity/ima/ima_policy.c   | 16 ++++-----
 security/integrity/integrity.h        | 12 +++----
 security/security.c                   | 12 +++++--
 11 files changed, 47 insertions(+), 89 deletions(-)
fs/exec.c +4 -0
index a5ae51e..3524e5f 100644
@@ -842,6 +842,10 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 	if (!S_ISREG(file_inode(file)->i_mode))
 		return -EINVAL;
 
+	ret = security_kernel_read_file(file, policy_id);
+	if (ret)
+		return ret;
+
 	i_size = i_size_read(file_inode(file));
 	if (max_size > 0 && i_size > max_size)
 		return -EFBIG;
include/linux/ima.h +1 -0
index 0a7f039..eec5e2b 100644
@@ -17,6 +17,7 @@ enum ima_policy_id {
 	KEXEC_CHECK = 1,
 	INITRAMFS_CHECK,
 	FIRMWARE_CHECK,
+	MODULE_CHECK,
 	IMA_MAX_READ_CHECK
 };
 
include/linux/lsm_hooks.h +8 -0
index 4e6e2af..9915310 100644
@@ -561,6 +561,12 @@
  *	the kernel module to load. If the module is being loaded from a blob,
  *	this argument will be NULL.
  *	Return 0 if permission is granted.
+ * @kernel_read_file:
+ *      Read a file specified by userspace.
+ *	@file contains the file structure pointing to the file being read
+ *	by the kernel.
+ *	@policy_id contains IMA policy identifier.
+ *	Return 0 if permission is granted.
  * @kernel_post_read_file:
  *	Read a file specified by userspace.
  *	@file contains the file structure pointing to the file being read
@@ -1465,6 +1471,7 @@ union security_list_options {
 	int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
 	int (*kernel_module_request)(char *kmod_name);
 	int (*kernel_module_from_file)(struct file *file);
+	int (*kernel_read_file)(struct file *file, int policy_id);
 	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
 				     int policy_id);
 	int (*task_fix_setuid)(struct cred *new, const struct cred *old,
@@ -1726,6 +1733,7 @@ struct security_hook_heads {
 	struct list_head kernel_act_as;
 	struct list_head kernel_create_files_as;
 	struct list_head kernel_fw_from_file;
+	struct list_head kernel_read_file;
 	struct list_head kernel_post_read_file;
 	struct list_head kernel_module_request;
 	struct list_head kernel_module_from_file;
include/linux/security.h +2 -1
index 51f3bc6..6d005b3 100644
@@ -301,6 +301,7 @@ int security_kernel_act_as(struct cred *new, u32 secid);
 int security_kernel_create_files_as(struct cred *new, struct inode *inode);
 int security_kernel_module_request(char *kmod_name);
 int security_kernel_module_from_file(struct file *file);
+int security_kernel_read_file(struct file *file, int policy_id);
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
 				   int policy_id);
 int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -857,7 +858,7 @@ static inline int security_kernel_module_request(char *kmod_name)
 	return 0;
 }
 
-static inline int security_kernel_module_from_file(struct file *file)
+static inline int security_kernel_read_file(struct file *file, int policy_id)
 {
 	return 0;
 }
kernel/module.c +7 -60

94 lines not on this page · raw message

security/integrity/ima/ima.h +0 -1
index 520c7b4..fc31ba2 100644
@@ -164,7 +164,6 @@ enum ima_hooks {
 	FILE_CHECK = IMA_MAX_READ_CHECK,
 	MMAP_CHECK,
 	BPRM_CHECK,
-	MODULE_CHECK,
 	POST_SETATTR
 };
 
security/integrity/ima/ima_appraise.c +0 -7
index 57b1ad1..6b3e30a 100644
@@ -74,8 +74,6 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
 		return iint->ima_mmap_status;
 	case BPRM_CHECK:
 		return iint->ima_bprm_status;
-	case MODULE_CHECK:
-		return iint->ima_module_status;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		return iint->ima_read_status;
 	case FILE_CHECK:
@@ -94,8 +92,6 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,
 	case BPRM_CHECK:
 		iint->ima_bprm_status = status;
 		break;
-	case MODULE_CHECK:
-		iint->ima_module_status = status;
 		break;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		iint->ima_read_status = status;
@@ -116,9 +112,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
 	case BPRM_CHECK:
 		iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
 		break;
-	case MODULE_CHECK:
-		iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
-		break;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		break;
 	case FILE_CHECK:
security/integrity/ima/ima_main.c +4 -1
index 1251882..107e6a7 100644
@@ -334,7 +334,7 @@ int ima_module_check(struct file *file)
 #endif
 		return 0;	/* We rely on module signature checking */
 	}
-	return process_measurement(file, NULL, 0, MAY_EXEC, MODULE_CHECK, 0);
+	return 0;
 }
 
 /**
@@ -357,6 +357,9 @@ int ima_hash_and_process_file(struct file *file, void *buf, loff_t size,
 		return 0;
 	}
 
+	if (!file && policy_id == MODULE_CHECK) /* MODULE_SIG_FORCE enabled */
+		return 0;
+
 	if (!file || !buf || size == 0) { /* should never happen */
 		if (ima_appraise & IMA_APPRAISE_ENFORCE)
 			return -EACCES;
security/integrity/ima/ima_policy.c +7 -9
index dbd7aa1..dbfd26b 100644
@@ -101,7 +101,7 @@ static struct ima_rule_entry original_measurement_rules[] = {
 	 .flags = IMA_FUNC | IMA_MASK},
 	{.action = MEASURE, .hooks.func = FILE_CHECK, .mask = MAY_READ,
 	 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
-	{.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
+	{.action = MEASURE, .hooks.policy_id = MODULE_CHECK, .flags = IMA_FUNC},
 	{.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
 	 .flags = IMA_FUNC},
 };
@@ -304,8 +304,6 @@ static int get_subaction(struct ima_rule_entry *rule, int func)
 		return IMA_MMAP_APPRAISE;
 	case BPRM_CHECK:
 		return IMA_BPRM_APPRAISE;
-	case MODULE_CHECK:
-		return IMA_MODULE_APPRAISE;
 	case KEXEC_CHECK ... IMA_MAX_READ_CHECK - 1:
 		return IMA_READ_APPRAISE;
 	case FILE_CHECK:
@@ -607,8 +605,6 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 			/* PATH_CHECK is for backwards compat */
 			else if (strcmp(args[0].from, "PATH_CHECK") == 0)
 				entry->hooks.func = FILE_CHECK;
-			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
-				entry->hooks.func = MODULE_CHECK;
 			else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
 				|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
 				entry->hooks.func = MMAP_CHECK;
@@ -620,6 +616,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.policy_id = INITRAMFS_CHECK;
 			else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
 				entry->hooks.policy_id = FIRMWARE_CHECK;
+			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
+				entry->hooks.policy_id = MODULE_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
@@ -774,7 +772,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 	}
 	if (!result && (entry->action == UNKNOWN))
 		result = -EINVAL;
-	else if (entry->hooks.func == MODULE_CHECK)
+	else if (entry->hooks.policy_id == MODULE_CHECK)
 		temp_ima_appraise |= IMA_APPRAISE_MODULES;
 	else if (entry->hooks.policy_id == FIRMWARE_CHECK)
 		temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
@@ -946,9 +944,6 @@ int ima_policy_show(struct seq_file *m, void *v)
 		case BPRM_CHECK:
 			seq_printf(m, pt(Opt_func), ft(func_bprm));
 			break;
-		case MODULE_CHECK:
-			seq_printf(m, pt(Opt_func), ft(func_module));
-			break;
 		case POST_SETATTR:
 			seq_printf(m, pt(Opt_func), ft(func_post));
 			break;
@@ -963,6 +958,9 @@ int ima_policy_show(struct seq_file *m, void *v)
 			case FIRMWARE_CHECK:
 				seq_printf(m, pt(Opt_func), ft(func_firmware));
 				break;
+			case MODULE_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_module));
+				break;
 			default:
 				snprintf(tbuf, sizeof(tbuf), "%d",
 					 entry->hooks.func);
security/integrity/integrity.h +4 -8
index 75334cd..97fb5c2 100644
@@ -45,16 +45,12 @@
 #define IMA_MMAP_APPRAISED	0x00000800
 #define IMA_BPRM_APPRAISE	0x00001000
 #define IMA_BPRM_APPRAISED	0x00002000
-#define IMA_MODULE_APPRAISE	0x00004000
-#define IMA_MODULE_APPRAISED	0x00008000
-#define IMA_READ_APPRAISE	0x00010000
-#define IMA_READ_APPRAISED	0x00020000
+#define IMA_READ_APPRAISE	0x00004000
+#define IMA_READ_APPRAISED	0x00008000
 #define IMA_APPRAISE_SUBMASK	(IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
-				 IMA_BPRM_APPRAISE | IMA_MODULE_APPRAISE | \
-				 IMA_READ_APPRAISE)
+				 IMA_BPRM_APPRAISE | IMA_READ_APPRAISE)
 #define IMA_APPRAISED_SUBMASK	(IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
-				 IMA_BPRM_APPRAISED | IMA_MODULE_APPRAISED | \
-				 IMA_READ_APPRAISED)
+				 IMA_BPRM_APPRAISED | IMA_READ_APPRAISED)
 
 enum evm_ima_xattr_type {
 	IMA_XATTR_DIGEST = 0x01,
security/security.c +10 -2
index a391ce4..fa8a9e8 100644
@@ -889,11 +889,17 @@ int security_kernel_module_request(char *kmod_name)
 	return call_int_hook(kernel_module_request, 0, kmod_name);
 }
 
-int security_kernel_module_from_file(struct file *file)
+int security_kernel_read_file(struct file *file, int policy_id)
 {
 	int ret;
 
-	ret = call_int_hook(kernel_module_from_file, 0, file);
+	switch (policy_id) {
+	case MODULE_CHECK:
+		ret = call_int_hook(kernel_module_from_file, 0, file);
+		break;
+	default:
+		ret = call_int_hook(kernel_read_file, 0, file, policy_id);
+	}
 	if (ret)
 		return ret;
 	return ima_module_check(file);
@@ -1707,6 +1713,8 @@ struct security_hook_heads security_hook_heads = {
 		LIST_HEAD_INIT(security_hook_heads.kernel_module_request),
 	.kernel_module_from_file =
 		LIST_HEAD_INIT(security_hook_heads.kernel_module_from_file),
+	.kernel_read_file =
+		LIST_HEAD_INIT(security_hook_heads.kernel_read_file),
 	.kernel_post_read_file =
 		LIST_HEAD_INIT(security_hook_heads.kernel_post_read_file),
 	.task_fix_setuid =
Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#34 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 5 lines · Mimi Zohar

This patch replaces the module copy_module_from_fd() call with the VFS common kernel_read_file_from_fd() function. Instead of reading the kernel module twice, once for measuring/appraising and then loading the kernel module, the file is read once.

Quoted 7 lines

This patch defines a new security hook named security_kernel_read_file(), which is called before reading the file. For now, call the module security hook from security_kernel_read_file until the LSMs have been converted to use the kernel_read_file hook.

This patch retains the kernel_module_from_file hook, but removes the security_kernel_module_from_file() function.

I think it would help if your cover letter and this patch described a bit that some LSMs either prefer to read / check / appraise files prior to loading and some other prefer to do that later. You could explain the LSM hook preferences and what they do. Then here you can explain how this one prefers a hook early, but acknowledge that the other one still exists.

So:

kernel_read_file() {
	...
	security_kernel_read_file();
	...
	security_kernel_post_read_file();
	...
}

include/linux/lsm_hooks.h union security_list_options { · line 1477

Quoted 12 lines
include/linux/lsm_hooks.h +1 -0
index 4e6e2af..9915310 100644
@@ -1465,6 +1471,7 @@ union security_list_options {
 	int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
 	int (*kernel_module_request)(char *kmod_name);
 	int (*kernel_module_from_file)(struct file *file);
+	int (*kernel_read_file)(struct file *file, int policy_id);
 	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
 				     int policy_id);
 	int (*task_fix_setuid)(struct cred *new, const struct cred *old,

Is the goal to eventually kill the other LSM hooks and just keep the file one? If so where is that done in this series? It was not clear.

Luis

author
#35 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 14 lines · Luis R. Rodriguez
Quoted 5 lines · Mimi Zohar

This patch replaces the module copy_module_from_fd() call with the VFS common kernel_read_file_from_fd() function. Instead of reading the kernel module twice, once for measuring/appraising and then loading the kernel module, the file is read once.

Quoted 7 lines

This patch defines a new security hook named security_kernel_read_file(), which is called before reading the file. For now, call the module security hook from security_kernel_read_file until the LSMs have been converted to use the kernel_read_file hook.

This patch retains the kernel_module_from_file hook, but removes the security_kernel_module_from_file() function.

Quoted 6 lines

I think it would help if your cover letter and this patch described a bit that some LSMs either prefer to read / check / appraise files prior to loading and some other prefer to do that later. You could explain the LSM hook preferences and what they do. Then here you can explain how this one prefers a hook early, but acknowledge that the other one still exists.

Before this patch set, IMA measured/appraised/audited a file before allowing it to be accessed, causing the file in some cases to be read
twice. This patch set changes that. Files are read into memory and
then measured/appraised/audited.

It's been a while since this hook was added. As I recall, Kees added the pre module hook to limit loading kernel modules to only those filesystems that were mounted read-only. I would have to look at each of the LSMs to see how they're using the hooks.

Quoted 10 lines

So:

kernel_read_file() {
	...
	security_kernel_read_file();
	...
	security_kernel_post_read_file();
	...
}
Quoted 15 lines

include/linux/lsm_hooks.h union security_list_options { · line 1477

Quoted 12 lines
include/linux/lsm_hooks.h +1 -0
index 4e6e2af..9915310 100644
@@ -1465,6 +1471,7 @@ union security_list_options {
 	int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
 	int (*kernel_module_request)(char *kmod_name);
 	int (*kernel_module_from_file)(struct file *file);
+	int (*kernel_read_file)(struct file *file, int policy_id);
 	int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
 				     int policy_id);
 	int (*task_fix_setuid)(struct cred *new, const struct cred *old,

Is the goal to eventually kill the other LSM hooks and just keep the file one? If so where is that done in this series? It was not clear.

As mentioned in the cover letter, consolidating the LSM hooks is not covered in this patch set. I was under the impression that not only were we defining a common kernel read file function, but that we were also consolidating the pre and post security hooks as well. By defining the pre and post security hooks in this patch set, it permits each of
the LSMs to migrate to the new hooks independently of each other. Lets
ask the LSM maintainers what they think.

Paul, Casey, Kees, Jon, Tetsuo does it make sense to consolidate the module, firmware, and kexec pre and post security hooks and have just
one set of pre and post security kernel_read_file hook instead? Does
it make sense for this patch set to define the new hooks to allow the LSMs to migrate to it independently of each other?

Mimi

#36 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 5 lines · Mimi Zohar

Paul, Casey, Kees, Jon, Tetsuo does it make sense to consolidate the module, firmware, and kexec pre and post security hooks and have just
one set of pre and post security kernel_read_file hook instead? Does
it make sense for this patch set to define the new hooks to allow the LSMs to migrate to it independently of each other?

Well, as usual, the easiest way to both get solid feedback and actually get a change accepted is to post patches to the affected LSMs. Probably not what you wanted to hear, but at least I'm honest :)

Signature · 2 lines
paul moore
security @ redhat
author
#37 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 10 lines · Paul Moore
Quoted 5 lines · Mimi Zohar

Paul, Casey, Kees, Jon, Tetsuo does it make sense to consolidate the module, firmware, and kexec pre and post security hooks and have just
one set of pre and post security kernel_read_file hook instead? Does
it make sense for this patch set to define the new hooks to allow the LSMs to migrate to it independently of each other?

Well, as usual, the easiest way to both get solid feedback and actually get a change accepted is to post patches to the affected LSMs. Probably not what you wanted to hear, but at least I'm honest :)

Unless I'm misreading the code, it might be a lot simpler than I thought. Of the three LSM hooks kernel_module_request, kernel_module_from_file, and kernel_fw_from_file, the only upstreamed LSM on any of these hooks is SELinux, which is only on the kernel_module_request hook.

After converting the SELinux kernel_module_request hook to use the new
kernel_read_file(), do I then remove the three hooks? Are we
concerned about "minor" LSMs that have not been upstreamed that might be using these hooks?

Mimi

#38 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 18 lines · Mimi Zohar
Quoted 10 lines · Paul Moore
Quoted 5 lines · Mimi Zohar

Paul, Casey, Kees, Jon, Tetsuo does it make sense to consolidate the module, firmware, and kexec pre and post security hooks and have just
one set of pre and post security kernel_read_file hook instead? Does
it make sense for this patch set to define the new hooks to allow the LSMs to migrate to it independently of each other?

Well, as usual, the easiest way to both get solid feedback and actually get a change accepted is to post patches to the affected LSMs. Probably not what you wanted to hear, but at least I'm honest :)

Unless I'm misreading the code, it might be a lot simpler than I thought. Of the three LSM hooks kernel_module_request, kernel_module_from_file, and kernel_fw_from_file, the only upstreamed LSM on any of these hooks is SELinux, which is only on the kernel_module_request hook.

Quoted 4 lines

After converting the SELinux kernel_module_request hook to use the new
kernel_read_file(), do I then remove the three hooks? Are we
concerned about "minor" LSMs that have not been upstreamed that might be using these hooks?

You can't worry about code that isn't upstream; if this change breaks something that hasn't been merged, then the burden lies on the out-of-tree developers to change their code.

Signature · 2 lines
paul moore
security @ redhat
#39 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

In reply to Mimi Zohar
Quoted 18 lines · Mimi Zohar
Quoted 10 lines · Paul Moore
Quoted 5 lines · Mimi Zohar

Paul, Casey, Kees, Jon, Tetsuo does it make sense to consolidate the module, firmware, and kexec pre and post security hooks and have just
one set of pre and post security kernel_read_file hook instead? Does
it make sense for this patch set to define the new hooks to allow the LSMs to migrate to it independently of each other?

Well, as usual, the easiest way to both get solid feedback and actually get a change accepted is to post patches to the affected LSMs. Probably not what you wanted to hear, but at least I'm honest :)

Unless I'm misreading the code, it might be a lot simpler than I thought. Of the three LSM hooks kernel_module_request, kernel_module_from_file, and kernel_fw_from_file, the only upstreamed LSM on any of these hooks is SELinux, which is only on the kernel_module_request hook.

Quoted 4 lines

After converting the SELinux kernel_module_request hook to use the new
kernel_read_file(), do I then remove the three hooks? Are we
concerned about "minor" LSMs that have not been upstreamed that might be using these hooks?

It should be easy for me to port my LSM to use the new hook. No objections in consolidating things.

(Which reminds me to resend my LSM again...)

-Kees

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security
#40 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

In reply to Mimi Zohar
Quoted 22 lines · Mimi Zohar
Quoted 14 lines · Luis R. Rodriguez
Quoted 5 lines · Mimi Zohar

This patch replaces the module copy_module_from_fd() call with the VFS common kernel_read_file_from_fd() function. Instead of reading the kernel module twice, once for measuring/appraising and then loading the kernel module, the file is read once.

Quoted 7 lines

This patch defines a new security hook named security_kernel_read_file(), which is called before reading the file. For now, call the module security hook from security_kernel_read_file until the LSMs have been converted to use the kernel_read_file hook.

This patch retains the kernel_module_from_file hook, but removes the security_kernel_module_from_file() function.

Quoted 6 lines

I think it would help if your cover letter and this patch described a bit that some LSMs either prefer to read / check / appraise files prior to loading and some other prefer to do that later. You could explain the LSM hook preferences and what they do. Then here you can explain how this one prefers a hook early, but acknowledge that the other one still exists.

Quoted 4 lines

Before this patch set, IMA measured/appraised/audited a file before allowing it to be accessed, causing the file in some cases to be read
twice. This patch set changes that. Files are read into memory and
then measured/appraised/audited.

Sounds like this could help also with performance, has any preliminary benchmarking been done to see the effect ?

Quoted 4 lines

It's been a while since this hook was added. As I recall, Kees added the pre module hook to limit loading kernel modules to only those filesystems that were mounted read-only. I would have to look at each of the LSMs to see how they're using the hooks.

Sure.

Quoted 26 lines
Quoted 10 lines

So:

kernel_read_file() {
      ...
      security_kernel_read_file();
      ...
      security_kernel_post_read_file();
      ...
}
Quoted 15 lines

include/linux/lsm_hooks.h union security_list_options { · line 1477

Quoted 12 lines
include/linux/lsm_hooks.h +1 -0
index 4e6e2af..9915310 100644
@@ -1465,6 +1471,7 @@ union security_list_options {
    int (*kernel_fw_from_file)(struct file *file, char *buf, size_t size);
    int (*kernel_module_request)(char *kmod_name);
    int (*kernel_module_from_file)(struct file *file);
+   int (*kernel_read_file)(struct file *file, int policy_id);
    int (*kernel_post_read_file)(struct file *file, char *buf, loff_t size,
                                 int policy_id);
    int (*task_fix_setuid)(struct cred *new, const struct cred *old,

Is the goal to eventually kill the other LSM hooks and just keep the file one? If so where is that done in this series? It was not clear.

Quoted 2 lines

As mentioned in the cover letter, consolidating the LSM hooks is not covered in this patch set.

Sorry I missed that after I started reviewing.

Quoted 3 lines

I was under the impression that not only
were we defining a common kernel read file function, but that we were also consolidating the pre and post security hooks as well.

Sure.

Quoted 4 lines

By defining
the pre and post security hooks in this patch set, it permits each of
the LSMs to migrate to the new hooks independently of each other. Lets
ask the LSM maintainers what they think.

I see -- yeah making this a 2 step thing makes sense, so long as the maintainers can later expect / understand what would be done in a second patch set. Breaking this down in two patch sets makes sense. It should also mean there might be fun benchmarks on gains provided there were considerable IO savings by not opening files twice.

Luis

author
#41 lore raw

Re: [RFC PATCH v2 08/11] module: replace copy_module_from_fd with kernel version

Quoted 23 lines · Luis R. Rodriguez
Quoted 22 lines · Mimi Zohar
Quoted 20 lines · Luis R. Rodriguez
Quoted 5 lines · Mimi Zohar

This patch replaces the module copy_module_from_fd() call with the VFS common kernel_read_file_from_fd() function. Instead of reading the kernel module twice, once for measuring/appraising and then loading the kernel module, the file is read once.

Quoted 7 lines

This patch defines a new security hook named security_kernel_read_file(), which is called before reading the file. For now, call the module security hook from security_kernel_read_file until the LSMs have been converted to use the kernel_read_file hook.

This patch retains the kernel_module_from_file hook, but removes the security_kernel_module_from_file() function.

I think it would help if your cover letter and this patch described a bit that some LSMs either prefer to read / check / appraise files prior to loading and some other prefer to do that later. You could explain the LSM hook preferences and what they do. Then here you can explain how this one prefers a hook early, but acknowledge that the other one still exists.

Quoted 7 lines
Quoted 4 lines

Before this patch set, IMA measured/appraised/audited a file before allowing it to be accessed, causing the file in some cases to be read
twice. This patch set changes that. Files are read into memory and
then measured/appraised/audited.

Sounds like this could help also with performance, has any preliminary benchmarking been done to see the effect ?

In general, IMA's pre-reading a file has negligible performance impact,
if any. Dmitry's LinuxCon 2013 Europe talk "Integrity Protection
Solutions in Linux" had some performance statistics. I'm not sure this change will have much of a performance impact.

Quoted 8 lines
Quoted 4 lines

By defining
the pre and post security hooks in this patch set, it permits each of
the LSMs to migrate to the new hooks independently of each other. Lets
ask the LSM maintainers what they think.

I see -- yeah making this a 2 step thing makes sense, so long as the maintainers can later expect / understand what would be done in a second patch set. Breaking this down in two patch sets makes sense.

I'll defer adding the pre and post security hooks to the subsequent patch set.

Mimi

author
#42 lore raw

RFC PATCH v2 9/11 ima: load policy using path

In reply to Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

We currently cannot do appraisal or signature vetting of IMA policies since we currently can only load IMA policies by writing the contents of the policy directly in, as follows:

cat policy-file > <securityfs>/ima/policy

If we provide the kernel the path to the IMA policy so it can load the policy itself it'd be able to later appraise or vet the file signature if it has one. This patch adds support to load the IMA policy with a given path as follows:

echo /etc/ima/ima_policy > /sys/kernel/security/ima/policy

This patch defines kernel_read_file_from_path(), a wrapper for the VFS common kernel_read_file(), and replaces the integrity_read_file() with a call to the kernel_read_file_from_path() wrapper.

Changelog v3:
- after re-ordering the patches, replace calling integrity_kernel_read() to read the file with kernel_read_file_from_path() (Mimi)

Changelog v2:
- Patch description re-written by Luis R. Rodriguez

---
 fs/exec.c                       | 21 ++++++++++++++++++++
 include/linux/fs.h              |  1 +
 include/linux/ima.h             |  1 +
 security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++--
 4 files changed, 64 insertions(+), 2 deletions(-)
fs/exec.c +21 -0
index 3524e5f..5731b40 100644
@@ -903,6 +903,27 @@ out:
 	return ret;
 }
 
+int kernel_read_file_from_path(char *path, void **buf, loff_t *size,
+			       loff_t max_size, int policy_id)
+{
+	struct file *file;
+	int ret;
+
+	if (!path || !*path)
+		return -EINVAL;
+
+	file = filp_open(path, O_RDONLY, 0);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		pr_err("Unable to open file: %s (%d)", path, ret);
+		return ret;
+	}
+
+	ret = kernel_read_file(file, buf, size, max_size, policy_id);
+	fput(file);
+	return ret;
+}
+
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
 {
 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
include/linux/fs.h +1 -0
index 9642623..79b1172 100644
@@ -2529,6 +2529,7 @@ extern int do_pipe_flags(int *, int);
 extern int kernel_read(struct file *, loff_t, char *, unsigned long);
 extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, int);
 extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t, int);
+extern int kernel_read_file_from_path(char *, void **, loff_t *, loff_t, int);
 extern ssize_t kernel_write(struct file *, const char *, size_t, loff_t);
 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
 extern struct file * open_exec(const char *);
include/linux/ima.h +1 -0
index eec5e2b..164d918 100644
@@ -18,6 +18,7 @@ enum ima_policy_id {
 	INITRAMFS_CHECK,
 	FIRMWARE_CHECK,
 	MODULE_CHECK,
+	POLICY_CHECK,
 	IMA_MAX_READ_CHECK
 };
 
security/integrity/ima/ima_fs.c +41 -2

62 lines not on this page · raw message

Signature · 1 line
2.1.0
#43 lore raw

Re: [RFC PATCH v2 09/11] ima: load policy using path

Quoted 17 lines · Mimi Zohar
fs/exec.c +11 -0
@@ -903,6 +903,27 @@ out:
 	return ret;
 }
 
+int kernel_read_file_from_path(char *path, void **buf, loff_t *size,
+			       loff_t max_size, int policy_id)
+{
+	struct file *file;
+	int ret;
+
+	if (!path || !*path)
+		return -EINVAL;
+
+	file = filp_open(path, O_RDONLY, 0);
+	if (IS_ERR(file)) {

fs/exec.c out: · line 926

Quoted 10 lines
fs/exec.c +10 -0
+		ret = PTR_ERR(file);
+		pr_err("Unable to open file: %s (%d)", path, ret);
+		return ret;
+	}
+
+	ret = kernel_read_file(file, buf, size, max_size, policy_id);
+	fput(file);
+	return ret;
+}
+

EXPORT_SYMBOL_GPL() needed.

Luis

author
#44 lore raw

Re: [RFC PATCH v2 09/11] ima: load policy using path

Quoted 22 lines · Luis R. Rodriguez
Quoted 21 lines · Mimi Zohar
fs/exec.c +15 -0
@@ -903,6 +903,27 @@ out:
 	return ret;
 }
 
+int kernel_read_file_from_path(char *path, void **buf, loff_t *size,
+			       loff_t max_size, int policy_id)
+{
+	struct file *file;
+	int ret;
+
+	if (!path || !*path)
+		return -EINVAL;
+
+	file = filp_open(path, O_RDONLY, 0);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		pr_err("Unable to open file: %s (%d)", path, ret);
+		return ret;
+	}
Quoted 8 lines

fs/exec.c out: · line 926

Quoted 6 lines
fs/exec.c +6 -0
+
+	ret = kernel_read_file(file, buf, size, max_size, policy_id);
+	fput(file);
+	return ret;
+}
+

EXPORT_SYMBOL_GPL() needed.

Yes. Thank you for reviewing all the patches!

Mimi

#45 lore raw

Re: [RFC PATCH v2 09/11] ima: load policy using path

In reply to Mimi Zohar
Quoted 25 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

echo /etc/ima/ima_policy > /sys/kernel/security/ima/policy

 fs/exec.c                       | 21 ++++++++++++++++++++
fs/exec.c +11 -0
index 3524e5f..5731b40 100644
@@ -903,6 +903,27 @@ out:
 	return ret;
 }
 
+int kernel_read_file_from_path(char *path, void **buf, loff_t *size,
+			       loff_t max_size, int policy_id)
+{
+	struct file *file;
+	int ret;
+
+	if (!path || !*path)
+		return -EINVAL;
+
+	file = filp_open(path, O_RDONLY, 0);
+	if (IS_ERR(file)) {

fs/exec.c out: · line 926

Quoted 10 lines
fs/exec.c +10 -0
+		ret = PTR_ERR(file);
+		pr_err("Unable to open file: %s (%d)", path, ret);
+		return ret;
+	}
+
+	ret = kernel_read_file(file, buf, size, max_size, policy_id);
+	fput(file);
+	return ret;
+}
+

I like this strategy, and can see it being further generalized in areas of the kernel where we may want self policy / file loading appraisal. Those policies are now defined through LSMs, in IMA here you do your own vetting but -- it seems we might better want instead to enable easily *any* LSM to do its vetting. In this case I suppose that's still possible given kernel_read_file() is used and that in turn would have a pre LSM hook and post-LSM hook.

Is that right? If so then it should just be a matter of all other areas of the kernel to use kernel_read_file_from_path() or kernel_read_file() to take advantage of similar strategies. Or is there more work? The other obvious alternative for the heavy handed users would be to just use the future sysdata helpers, that in turn would use kernel_read_file_from_path() and still if a distribution wants can also in the future get kernel signature verification.

Luis

author
#46 lore raw

RFC PATCH v2 10/11 ima: measure and appraise the IMA policy itself

In reply to Mimi Zohar

This patch adds support for measuring and appraising the IMA policy itself.

---
 security/integrity/ima/ima.h        |  1 +
 security/integrity/ima/ima_fs.c     |  9 ++++++++-
 security/integrity/ima/ima_policy.c | 14 ++++++++++++--
 3 files changed, 21 insertions(+), 3 deletions(-)
security/integrity/ima/ima.h +1 -0
index fc31ba2..e8f111b 100644
@@ -185,6 +185,7 @@ int ima_policy_show(struct seq_file *m, void *v);
 #define IMA_APPRAISE_LOG	0x04
 #define IMA_APPRAISE_MODULES	0x08
 #define IMA_APPRAISE_FIRMWARE	0x10
+#define IMA_APPRAISE_POLICY	0x20
 
 #ifdef CONFIG_IMA_APPRAISE
 int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
security/integrity/ima/ima_fs.c +8 -1
index fe8b16b..57c6b2e 100644
@@ -325,7 +325,14 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 
 	if (data[0] == '/')
 		result = ima_read_policy(data);
-	else
+	else if (ima_appraise & IMA_APPRAISE_POLICY) {
+		pr_err("IMA: signed policy required\n");
+		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
+				    "policy_update", "signed policy required",
+				    1, 0);
+		if (ima_appraise & IMA_APPRAISE_ENFORCE)
+			result = -EACCES;
+	} else
 		result = ima_parse_add_rule(data);
 	mutex_unlock(&ima_write_mutex);
 out_free:
security/integrity/ima/ima_policy.c +12 -2
index dbfd26b..7a63760 100644
@@ -118,6 +118,7 @@ static struct ima_rule_entry default_measurement_rules[] = {
 	{.action = MEASURE, .hooks.func = MODULE_CHECK, .flags = IMA_FUNC},
 	{.action = MEASURE, .hooks.policy_id = FIRMWARE_CHECK,
 	 .flags = IMA_FUNC},
+	{.action = MEASURE, .hooks.policy_id = POLICY_CHECK, .flags = IMA_FUNC},
 };
 
 static struct ima_rule_entry default_appraise_rules[] = {
@@ -618,6 +619,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->hooks.policy_id = FIRMWARE_CHECK;
 			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
 				entry->hooks.policy_id = MODULE_CHECK;
+			else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
+				entry->hooks.policy_id = POLICY_CHECK;
 			else
 				result = -EINVAL;
 			if (!result)
@@ -776,6 +779,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 		temp_ima_appraise |= IMA_APPRAISE_MODULES;
 	else if (entry->hooks.policy_id == FIRMWARE_CHECK)
 		temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
+	else if (entry->hooks.policy_id == POLICY_CHECK)
+		temp_ima_appraise |= IMA_APPRAISE_POLICY;
 	audit_log_format(ab, "res=%d", !result);
 	audit_log_end(ab);
 	return result;
@@ -862,7 +867,8 @@ static char *mask_tokens[] = {
 enum {
 	func_file = 0, func_mmap, func_bprm,
 	func_module, func_post,
-	func_kexec, func_initramfs, func_firmware
+	func_kexec, func_initramfs, func_firmware,
+	func_policy
 };
 
 static char *func_tokens[] = {
@@ -873,7 +879,8 @@ static char *func_tokens[] = {
 	"POST_SETATTR",
 	"KEXEC_CHECK",
 	"INITRAMFS_CHECK",
-	"FIRMWARE_CHECK"
+	"FIRMWARE_CHECK",
+	"POLICY_CHECK"
 };
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
@@ -961,6 +968,9 @@ int ima_policy_show(struct seq_file *m, void *v)
 			case MODULE_CHECK:
 				seq_printf(m, pt(Opt_func), ft(func_module));
 				break;
+			case POLICY_CHECK:
+				seq_printf(m, pt(Opt_func), ft(func_policy));
+				break;
 			default:
 				snprintf(tbuf, sizeof(tbuf), "%d",
 					 entry->hooks.func);
Signature · 7 lines
2.1.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#47 lore raw

RFC PATCH v2 11/11 ima: require signed IMA policy

In reply to Mimi Zohar

Require the IMA policy to be signed when additional rules can be added.

Changelog v2:
- add union name "hooks" to fix sparse warning
v1:
- initialize the policy flag
- include IMA_APPRAISE_POLICY in the policy flag

---
 security/integrity/ima/ima_policy.c | 7 +++++++
 1 file changed, 7 insertions(+)
security/integrity/ima/ima_policy.c +7 -0
index 7a63760..327e691 100644
@@ -133,6 +133,10 @@ static struct ima_rule_entry default_appraise_rules[] = {
 	{.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
 	{.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
 	{.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
+#ifdef CONFIG_IMA_WRITE_POLICY
+	{.action = APPRAISE, .hooks.policy_id = POLICY_CHECK,
+	.flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
+#endif
 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
 	{.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
 #else
@@ -414,9 +418,12 @@ void __init ima_init_policy(void)
 	for (i = 0; i < appraise_entries; i++) {
 		list_add_tail(&default_appraise_rules[i].list,
 			      &ima_default_rules);
+		if (default_appraise_rules[i].hooks.policy_id == POLICY_CHECK)
+			temp_ima_appraise |= IMA_APPRAISE_POLICY;
 	}
 
 	ima_rules = &ima_default_rules;
+	ima_update_policy_flag();
 }
 
 /* Make sure we have a valid policy, at least containing some rules. */
Signature · 1 line
2.1.0
#48 lore raw
In reply to Mimi Zohar
Quoted 27 lines · Mimi Zohar

For a while it was looked down upon to directly read files from Linux. These days there exists a few mechanisms in the kernel that do just this though to load a file into a local buffer. There are minor but important checks differences on each, we should take all the best practices from each of them, generalize them and make all places in the kernel that read a file use it.[1]

One difference is the method for opening the file. In some cases we have a file, while in other cases we have a pathname or a file descriptor.

Another difference is the security hook calls, or lack of them. In some versions there is a post file read hook, while in others there is a pre file read hook.

This patch set is the first attempt at resolving these differences. It does not attempt to merge the different methods of opening a file, but defines a single common kernel file read function with two wrappers. Although this patch set defines two new security hooks for pre and post file read, it does not attempt to merge the existing security hooks. That is left as future work.

Changelog v2:
- Combined the "ima: measuring/appraising files read by the kernel" patches with this patch set to simplify review.
- Split the "ima: measure and appraise kexec image and initramfs" patch to separate IMA from the kexec changes.

Quoted 6 lines

The latest version of these patches can be found in the next-kernel-read-v2 branch of:
git://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git

[1] Taken from Luis Rodriguez's wiki -
http://kernelnewbies.org/KernelProjects/common-kernel-loader

Did 0-day bot get a chance to test this tree? If not can it be added ?

Luis

author
#49 lore raw
Quoted 11 lines · Luis R. Rodriguez
Quoted 7 lines · Mimi Zohar

The latest version of these patches can be found in the next-kernel-read-v2 branch of:
git://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git

[1] Taken from Luis Rodriguez's wiki -
http://kernelnewbies.org/KernelProjects/common-kernel-loader

Did 0-day bot get a chance to test this tree? If not can it be added ?

Yes, I get the notifications.

Mimi