vfs: support for a common kernel file loader
Subsystems (9)
v3 cover vfs: 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 attempts to resolve 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. In addition, as none of the upstreamed LSMs define either a kernel_module_from_file or a kernel_fw_from_file hook, this patch set removes these hooks and the associated functions. The ima_module_check() and ima_fw_from_file() functions are renamed and called from the pre and post kernel_read_file security functions respectively.
Changelog:
- Rebased on top of some of the "firmware_class: extensible firmware API" patches posted by Luis.
- Removed the kernel_module_from_file and kernel_fw_from_file security hooks and functions.
- Defined "kernel_read_file_id" enumeration, independently of "ima_hooks".
- Split patches for ease of review.
The latest version of these patches can be found in the next-kernel-read-v3 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
David Howells (1):
firmware: fold successful fw read early
Dmitry Kasatkin (3):
ima: separate 'security.ima' reading functionality from collect
ima: provide buffer hash calculation function
ima: load policy using path
Luis R. Rodriguez (2):
firmware: simplify dev_*() print messages for generic helpers
firmware: move completing fw into a helper
Mimi Zohar (16):
ima: refactor ima_policy_show() to display "ima_hooks" rules
ima: use "ima_hooks" enum as function argument
vfs: define a generic function to read a file from the kernel
vfs: define kernel_read_file_id enumeration
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
vfs: define kernel_read_file_from_path
firmware: replace call to fw_read_file_contents() with kernel version
security: define kernel_read_file hook
vfs: define kernel_copy_file_from_fd()
module: replace copy_module_from_fd with kernel version
ima: remove firmware and module specific cached status info
kexec: replace call to copy_file_from_fd() with kernel version
ima: support for kexec image and initramfs
ima: measure and appraise the IMA policy itself
ima: require signed IMA policy
Documentation/ABI/testing/ima_policy | 2 +- drivers/base/firmware_class.c | 76 ++++++------------- fs/exec.c | 95 +++++++++++++++++++++++ include/linux/fs.h | 15 ++++ include/linux/ima.h | 10 ++- include/linux/lsm_hooks.h | 35 ++++----- include/linux/security.h | 16 ++-- kernel/kexec_file.c | 73 +++--------------- kernel/module.c | 68 ++--------------- security/integrity/iint.c | 4 +- security/integrity/ima/ima.h | 49 ++++++++---- security/integrity/ima/ima_api.c | 25 +++---- security/integrity/ima/ima_appraise.c | 69 ++++++++--------- 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 | 104 +++++++++++++++++++------- security/integrity/ima/ima_policy.c | 107 +++++++++++++++++--------- security/integrity/ima/ima_template.c | 2 - security/integrity/ima/ima_template_lib.c | 1 - security/integrity/integrity.h | 15 ++-- security/security.c | 32 ++++---- 22 files changed, 605 insertions(+), 365 deletions(-)
Signature · 1 line
2.1.0
PATCH v3 1/22 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
@@ -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_creturn 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 · 1 line
2.1.0
PATCH v3 2/22 ima: refactor ima_policy_show() to display "ima_hooks" rules
In reply to Mimi ZoharDefine and call a function to display the "ima_hooks" rules.
--- security/integrity/ima/ima_policy.c | 63 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 27 deletions(-)
security/integrity/ima/ima_policy.c +36 -27
@@ -903,6 +903,40 @@ void ima_policy_stop(struct seq_file *m, void *v)#define mt (token) mask_tokens[token]#define ft (token) func_tokens[token] +/* +* policy_func_show - display the ima_hooks policy rule +*/ +static void policy_func_show (struct seq_file *m,enum ima_hooks func) +{ +char tbuf[64 ] = {0 ,}; + +switch (func) { +case FILE_CHECK : +seq_printf (m,pt (Opt_func),ft (func_file)); +break ; +case MMAP_CHECK : +seq_printf (m,pt (Opt_func),ft (func_mmap)); +break ; +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 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 ; +default : +snprintf (tbuf,sizeof (tbuf),"%d" , func); +seq_printf (m,pt (Opt_func), tbuf); +break ; + } +seq_puts (m," " ); +} +int ima_policy_show (struct seq_file *m,void *v) {struct ima_rule_entry *entry = v;
@@ -924,33 +958,8 @@ int ima_policy_show(struct seq_file *m, void *v)seq_puts (m," " ); -if (entry->flags &IMA_FUNC ) { -switch (entry->func) { -case FILE_CHECK : -seq_printf (m,pt (Opt_func),ft (func_file)); -break ; -case MMAP_CHECK : -seq_printf (m,pt (Opt_func),ft (func_mmap)); -break ; -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 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 ; -default : -snprintf (tbuf,sizeof (tbuf),"%d" , entry->func); -seq_printf (m,pt (Opt_func), tbuf); -break ; - } -seq_puts (m," " ); - } +if (entry->flags &IMA_FUNC ) +policy_func_show (m, entry->func);if (entry->flags &IMA_MASK ) {if (entry->mask &MAY_EXEC )
Signature · 1 line
2.1.0
Re: [PATCH v3 02/22] ima: refactor ima_policy_show() to display "ima_hooks" rules
Quoted 3 lines · Mimi Zohar
Define and call a function to display the "
ima_hooks" rules.Signed-off-by Mimi Zohar <[email protected]>
Quoted 92 lines
--- security/integrity/ima/ima_policy.c | 63 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 27 deletions(-)security/integrity/ima/ima_policy.c +36
-27@@ -903,6 +903,40 @@ void ima_policy_stop(struct seq_file *m, void *v)#define mt (token) mask_tokens[token]#define ft (token) func_tokens[token] +/* +* policy_func_show - display the ima_hooks policy rule +*/ +static void policy_func_show (struct seq_file *m,enum ima_hooks func) +{ +char tbuf[64 ] = {0 ,}; + +switch (func) { +case FILE_CHECK : +seq_printf (m,pt (Opt_func),ft (func_file)); +break ; +case MMAP_CHECK : +seq_printf (m,pt (Opt_func),ft (func_mmap)); +break ; +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 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 ; +default : +snprintf (tbuf,sizeof (tbuf),"%d" , func); +seq_printf (m,pt (Opt_func), tbuf); +break ; + } +seq_puts (m," " ); +} +int ima_policy_show (struct seq_file *m,void *v) {struct ima_rule_entry *entry = v;@@ -924,33 +958,8 @@ int ima_policy_show(struct seq_file *m, void *v)seq_puts (m," " ); -if (entry->flags &IMA_FUNC ) { -switch (entry->func) { -case FILE_CHECK : -seq_printf (m,pt (Opt_func),ft (func_file)); -break ; -case MMAP_CHECK : -seq_printf (m,pt (Opt_func),ft (func_mmap)); -break ; -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 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 ; -default : -snprintf (tbuf,sizeof (tbuf),"%d" , entry->func); -seq_printf (m,pt (Opt_func), tbuf); -break ; - } -seq_puts (m," " ); - } +if (entry->flags &IMA_FUNC ) +policy_func_show (m, entry->func);if (entry->flags &IMA_MASK ) {if (entry->mask &MAY_EXEC )Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH v3 02/22] ima: refactor ima_policy_show() to display "ima_hooks" rules
In reply to Mimi ZoharQuoted 3 lines · Mimi Zohar
Define and call a function to display the "
ima_hooks" rules.Signed-off-by Mimi Zohar <[email protected]>
Quoted 88 lines
--- security/integrity/ima/ima_policy.c | 63 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 27 deletions(-)security/integrity/ima/ima_policy.c +36
-28@@ -903,6 +903,40 @@ void ima_policy_stop(struct seq_file *m, void *v)#define mt (token) mask_tokens[token]#define ft (token) func_tokens[token] +/* +* policy_func_show - display the ima_hooks policy rule +*/ +static void policy_func_show (struct seq_file *m,enum ima_hooks func) +{ +char tbuf[64 ] = {0 ,}; + +switch (func) { +case FILE_CHECK : +seq_printf (m,pt (Opt_func),ft (func_file)); +break ; +case MMAP_CHECK : +seq_printf (m,pt (Opt_func),ft (func_mmap)); +break ; +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 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 ; +default : +snprintf (tbuf,sizeof (tbuf),"%d" , func); +seq_printf (m,pt (Opt_func), tbuf); +break ; + } +seq_puts (m," " ); +} +int ima_policy_show (struct seq_file *m,void *v) {struct ima_rule_entry *entry = v;@@ -924,33 +958,8 @@ int ima_policy_show(struct seq_file *m, void *v)seq_puts (m," " ); -if (entry->flags &IMA_FUNC ) { -switch (entry->func) { -case FILE_CHECK : -seq_printf (m,pt (Opt_func),ft (func_file)); -break ; -case MMAP_CHECK : -seq_printf (m,pt (Opt_func),ft (func_mmap)); -break ; -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 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 ; -default : -snprintf (tbuf,sizeof (tbuf),"%d" , entry->func); -seq_printf (m,pt (Opt_func), tbuf); -break ; - } -seq_puts (m," " ); - } +if (entry->flags &IMA_FUNC ) +policy_func_show (m, entry->func);if (entry->flags &IMA_MASK ) {if (entry->mask &MAY_EXEC ) --2.1.0
Signature · 2 lines
Thanks, Dmitry
PATCH v3 3/22 ima: use "ima_hooks" enum as function argument
In reply to Mimi ZoharCleanup the function arguments by using "ima_hooks" enumerator as needed.
--- security/integrity/ima/ima.h | 25 +++++++++++++++++-------- security/integrity/ima/ima_api.c | 6 +++--- security/integrity/ima/ima_appraise.c | 13 +++++++------ security/integrity/ima/ima_main.c | 14 +++++++------- security/integrity/ima/ima_policy.c | 6 +++--- 5 files changed, 37 insertions(+), 27 deletions(-)
security/integrity/ima/ima.h +17 -8
@@ -137,9 +137,18 @@ static inline unsigned long ima_hash_key(u8 *digest)return hash_long (*digest,IMA_HASH_BITS ); } +enum ima_hooks { +FILE_CHECK =1 , +MMAP_CHECK , +BPRM_CHECK , +MODULE_CHECK , +FIRMWARE_CHECK , +POST_SETATTR +}; +/* LIM API function definitions */ -int ima_get_action (struct inode *inode,int mask,int function); -int ima_must_measure (struct inode *inode,int mask,int function); +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func); +int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func);int ima_collect_measurement (struct integrity_iint_cache *iint,struct file *file,enum hash_algo algo); void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
@@ -156,8 +165,6 @@ 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 }; -int ima_match_policy (struct inode *inode,enum ima_hooks func,int mask,int flags);void ima_init_policy (void );
@@ -179,21 +186,22 @@ int ima_policy_show(struct seq_file *m, void *v);#define IMA_APPRAISE_FIRMWARE 0x10#ifdef CONFIG_IMA_APPRAISE -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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);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); +enum ima_hooks func);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);#else -static inline int ima_appraise_measurement(int func, +static inline int ima_appraise_measurement(enum ima_hooks func, struct integrity_iint_cache *iint, struct file *file, const unsigned char *filename,
@@ -215,7 +223,8 @@ static inline void ima_update_xattr(struct integrity_iint_cache *iint, }static inline enum integrity_status ima_get_cache_status (struct integrity_iint_cache - *iint,int func) + *iint, +enum ima_hooks func) {return INTEGRITY_UNKNOWN ; }
security/integrity/ima/ima_api.c +3 -3
@@ -156,7 +156,7 @@ err_out:* ima_get_action - appraise & measure decision based on policy. * @inode: pointer to inode to measure * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) -* @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) +* @func: caller identifier * * The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic=
@@ -168,13 +168,13 @@ err_out:* Returns IMA_MEASURE, IMA_APPRAISE mask. * */ -int ima_get_action (struct inode *inode,int mask,int function) +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func) {int flags =IMA_MEASURE |IMA_AUDIT |IMA_APPRAISE ; flags &= ima_policy_flag; -return ima_match_policy (inode, function, mask, flags); +return ima_match_policy (inode, func, mask, flags); }/*
security/integrity/ima/ima_appraise.c +7 -6
@@ -67,7 +67,7 @@ static int ima_fix_xattr(struct dentry *dentry,/* Return specific func appraised cached result */ enum integrity_status ima_get_cache_status (struct integrity_iint_cache *iint, -int func) +enum ima_hooks func) {switch (func) {case MMAP_CHECK :
@@ -85,7 +85,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, }static void ima_set_cache_status (struct integrity_iint_cache *iint, -int func,enum integrity_status status) +enum ima_hooks func, +enum integrity_status status) {switch (func) {case MMAP_CHECK :
@@ -103,11 +104,11 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,case FILE_CHECK :default : iint->ima_file_status = status; -break ; } } -static void ima_cache_flags (struct integrity_iint_cache *iint,int func) +static void ima_cache_flags (struct integrity_iint_cache *iint, +enum ima_hooks func) {switch (func) {case MMAP_CHECK :
@@ -125,7 +126,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)case FILE_CHECK :default : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); -break ; } }
@@ -185,7 +185,8 @@ int ima_read_xattr(struct dentry *dentry,* * Return 0 on success, error code otherwise */ -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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)
security/integrity/ima/ima_main.c +7 -7
@@ -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,int mask, +enum ima_hooks func,int opened) {struct inode *inode =file_inode (file);struct integrity_iint_cache *iint =NULL ;
@@ -174,8 +174,8 @@ static int process_measurement(struct file *file, int mask, int function,* bitmask based on the appraise/audit/measurement policy. * Included is the appraise submask. */ - action =ima_get_action (inode, mask, function); - violation_check = ((function ==FILE_CHECK || function ==MMAP_CHECK ) && + action =ima_get_action (inode, mask, func); + violation_check = ((func ==FILE_CHECK || func ==MMAP_CHECK ) && (ima_policy_flag &IMA_MEASURE ));if (!action && !violation_check)return 0 ;
@@ -184,7 +184,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Is the appraise rule hook specific? */ if (action &IMA_FILE_APPRAISE ) - function =FILE_CHECK ; + func =FILE_CHECK ;mutex_lock (&inode->i_mutex);
@@ -214,7 +214,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Nothing to do, just return existing appraised status */ if (!action) {if (must_appraise) - rc =ima_get_cache_status (iint, function); + rc =ima_get_cache_status (iint, func); goto out_digsig; }
@@ -240,7 +240,7 @@ static int process_measurement(struct file *file, int mask, int function,ima_store_measurement (iint, file, pathname, xattr_value, xattr_len);if (action &IMA_APPRAISE_SUBMASK ) - rc =ima_appraise_measurement (function, iint, file, pathname, + rc =ima_appraise_measurement (func, iint, file, pathname, xattr_value, xattr_len, opened);if (action &IMA_AUDIT )ima_audit_measurement (iint, pathname);
security/integrity/ima/ima_policy.c +3 -3
@@ -207,8 +207,8 @@ static void ima_lsm_update_rules(void)* * Returns true on rule match, false on failure. */ -static bool ima_match_rules (struct ima_rule_entry *rule, -struct inode *inode,enum ima_hooks func,int mask) +static bool ima_match_rules (struct ima_rule_entry *rule,struct inode *inode, +enum ima_hooks func,int mask) {struct task_struct *tsk = current;const struct cred *cred =current_cred ();
@@ -289,7 +289,7 @@ retry:* In addition to knowing that we need to appraise the file in general, * we need to differentiate between calling hooks, for hook specific rules. */ -static int get_subaction (struct ima_rule_entry *rule,int func) +static int get_subaction (struct ima_rule_entry *rule,enum ima_hooks func) {if (!(rule->flags &IMA_FUNC ))return IMA_FILE_APPRAISE ;
Signature · 1 line
2.1.0
Re: [PATCH v3 03/22] ima: use "ima_hooks" enum as function argument
Quoted 3 lines · Mimi Zohar
Cleanup the function arguments by using "
ima_hooks" enumerator as needed.Signed-off-by Mimi Zohar <[email protected]>
Quoted 246 lines
--- security/integrity/ima/ima.h | 25 +++++++++++++++++-------- security/integrity/ima/ima_api.c | 6 +++--- security/integrity/ima/ima_appraise.c | 13 +++++++------ security/integrity/ima/ima_main.c | 14 +++++++------- security/integrity/ima/ima_policy.c | 6 +++--- 5 files changed, 37 insertions(+), 27 deletions(-)security/integrity/ima/ima.h +17
-8@@ -137,9 +137,18 @@ static inline unsigned long ima_hash_key(u8 *digest)return hash_long (*digest,IMA_HASH_BITS ); } +enum ima_hooks { +FILE_CHECK =1 , +MMAP_CHECK , +BPRM_CHECK , +MODULE_CHECK , +FIRMWARE_CHECK , +POST_SETATTR +}; +/* LIM API function definitions */ -int ima_get_action (struct inode *inode,int mask,int function); -int ima_must_measure (struct inode *inode,int mask,int function); +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func); +int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func);int ima_collect_measurement (struct integrity_iint_cache *iint,struct file *file,enum hash_algo algo); void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,@@ -156,8 +165,6 @@ 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 }; -int ima_match_policy (struct inode *inode,enum ima_hooks func,int mask,int flags);void ima_init_policy (void );@@ -179,21 +186,22 @@ int ima_policy_show(struct seq_file *m, void *v);#define IMA_APPRAISE_FIRMWARE 0x10#ifdef CONFIG_IMA_APPRAISE -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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);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); +enum ima_hooks func);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);#else -static inline int ima_appraise_measurement(int func, +static inline int ima_appraise_measurement(enum ima_hooks func, struct integrity_iint_cache *iint, struct file *file, const unsigned char *filename,@@ -215,7 +223,8 @@ static inline void ima_update_xattr(struct integrity_iint_cache *iint, }static inline enum integrity_status ima_get_cache_status (struct integrity_iint_cache - *iint,int func) + *iint, +enum ima_hooks func) {return INTEGRITY_UNKNOWN ; }security/integrity/ima/ima_api.c +3
-3@@ -156,7 +156,7 @@ err_out:* ima_get_action - appraise & measure decision based on policy. * @inode: pointer to inode to measure * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) -* @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) +* @func: caller identifier * * The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic= @@ -168,13 +168,13 @@ err_out:* Returns IMA_MEASURE, IMA_APPRAISE mask. * */ -int ima_get_action (struct inode *inode,int mask,int function) +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func) {int flags =IMA_MEASURE |IMA_AUDIT |IMA_APPRAISE ; flags &= ima_policy_flag; -return ima_match_policy (inode, function, mask, flags); +return ima_match_policy (inode, func, mask, flags); }/* security/integrity/ima/ima_appraise.c +7
-6@@ -67,7 +67,7 @@ static int ima_fix_xattr(struct dentry *dentry,/* Return specific func appraised cached result */ enum integrity_status ima_get_cache_status (struct integrity_iint_cache *iint, -int func) +enum ima_hooks func) {switch (func) {case MMAP_CHECK :@@ -85,7 +85,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, }static void ima_set_cache_status (struct integrity_iint_cache *iint, -int func,enum integrity_status status) +enum ima_hooks func, +enum integrity_status status) {switch (func) {case MMAP_CHECK :@@ -103,11 +104,11 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,case FILE_CHECK :default : iint->ima_file_status = status; -break ; } } -static void ima_cache_flags (struct integrity_iint_cache *iint,int func) +static void ima_cache_flags (struct integrity_iint_cache *iint, +enum ima_hooks func) {switch (func) {case MMAP_CHECK :@@ -125,7 +126,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)case FILE_CHECK :default : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); -break ; } }@@ -185,7 +185,8 @@ int ima_read_xattr(struct dentry *dentry,* * Return 0 on success, error code otherwise */ -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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)security/integrity/ima/ima_main.c +7
-7@@ -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,int mask, +enum ima_hooks func,int opened) {struct inode *inode =file_inode (file);struct integrity_iint_cache *iint =NULL ;@@ -174,8 +174,8 @@ static int process_measurement(struct file *file, int mask, int function,* bitmask based on the appraise/audit/measurement policy. * Included is the appraise submask. */ - action =ima_get_action (inode, mask, function); - violation_check = ((function ==FILE_CHECK || function ==MMAP_CHECK ) && + action =ima_get_action (inode, mask, func); + violation_check = ((func ==FILE_CHECK || func ==MMAP_CHECK ) && (ima_policy_flag &IMA_MEASURE ));if (!action && !violation_check)return 0 ;@@ -184,7 +184,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Is the appraise rule hook specific? */ if (action &IMA_FILE_APPRAISE ) - function =FILE_CHECK ; + func =FILE_CHECK ;mutex_lock (&inode->i_mutex);@@ -214,7 +214,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Nothing to do, just return existing appraised status */ if (!action) {if (must_appraise) - rc =ima_get_cache_status (iint, function); + rc =ima_get_cache_status (iint, func); goto out_digsig; }@@ -240,7 +240,7 @@ static int process_measurement(struct file *file, int mask, int function,ima_store_measurement (iint, file, pathname, xattr_value, xattr_len);if (action &IMA_APPRAISE_SUBMASK ) - rc =ima_appraise_measurement (function, iint, file, pathname, + rc =ima_appraise_measurement (func, iint, file, pathname, xattr_value, xattr_len, opened);if (action &IMA_AUDIT )ima_audit_measurement (iint, pathname);security/integrity/ima/ima_policy.c +3
-3@@ -207,8 +207,8 @@ static void ima_lsm_update_rules(void)* * Returns true on rule match, false on failure. */ -static bool ima_match_rules (struct ima_rule_entry *rule, -struct inode *inode,enum ima_hooks func,int mask) +static bool ima_match_rules (struct ima_rule_entry *rule,struct inode *inode, +enum ima_hooks func,int mask) {struct task_struct *tsk = current;const struct cred *cred =current_cred ();@@ -289,7 +289,7 @@ retry:* In addition to knowing that we need to appraise the file in general, * we need to differentiate between calling hooks, for hook specific rules. */ -static int get_subaction (struct ima_rule_entry *rule,int func) +static int get_subaction (struct ima_rule_entry *rule,enum ima_hooks func) {if (!(rule->flags &IMA_FUNC ))return IMA_FILE_APPRAISE ;Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH v3 03/22] ima: use "ima_hooks" enum as function argument
In reply to Mimi ZoharQuoted 3 lines · Mimi Zohar
Cleanup the function arguments by using "
ima_hooks" enumerator as needed.Signed-off-by Mimi Zohar <[email protected]>
Quoted 242 lines
--- security/integrity/ima/ima.h | 25 +++++++++++++++++-------- security/integrity/ima/ima_api.c | 6 +++--- security/integrity/ima/ima_appraise.c | 13 +++++++------ security/integrity/ima/ima_main.c | 14 +++++++------- security/integrity/ima/ima_policy.c | 6 +++--- 5 files changed, 37 insertions(+), 27 deletions(-)security/integrity/ima/ima.h +17
-8@@ -137,9 +137,18 @@ static inline unsigned long ima_hash_key(u8 *digest)return hash_long (*digest,IMA_HASH_BITS ); } +enum ima_hooks { +FILE_CHECK =1 , +MMAP_CHECK , +BPRM_CHECK , +MODULE_CHECK , +FIRMWARE_CHECK , +POST_SETATTR +}; +/* LIM API function definitions */ -int ima_get_action (struct inode *inode,int mask,int function); -int ima_must_measure (struct inode *inode,int mask,int function); +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func); +int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func);int ima_collect_measurement (struct integrity_iint_cache *iint,struct file *file,enum hash_algo algo); void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,@@ -156,8 +165,6 @@ 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 }; -int ima_match_policy (struct inode *inode,enum ima_hooks func,int mask,int flags);void ima_init_policy (void );@@ -179,21 +186,22 @@ int ima_policy_show(struct seq_file *m, void *v);#define IMA_APPRAISE_FIRMWARE 0x10#ifdef CONFIG_IMA_APPRAISE -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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);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); +enum ima_hooks func);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);#else -static inline int ima_appraise_measurement(int func, +static inline int ima_appraise_measurement(enum ima_hooks func, struct integrity_iint_cache *iint, struct file *file, const unsigned char *filename,@@ -215,7 +223,8 @@ static inline void ima_update_xattr(struct integrity_iint_cache *iint, }static inline enum integrity_status ima_get_cache_status (struct integrity_iint_cache - *iint,int func) + *iint, +enum ima_hooks func) {return INTEGRITY_UNKNOWN ; }security/integrity/ima/ima_api.c +3
-3@@ -156,7 +156,7 @@ err_out:* ima_get_action - appraise & measure decision based on policy. * @inode: pointer to inode to measure * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) -* @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) +* @func: caller identifier * * The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic= @@ -168,13 +168,13 @@ err_out:* Returns IMA_MEASURE, IMA_APPRAISE mask. * */ -int ima_get_action (struct inode *inode,int mask,int function) +int ima_get_action (struct inode *inode,int mask,enum ima_hooks func) {int flags =IMA_MEASURE |IMA_AUDIT |IMA_APPRAISE ; flags &= ima_policy_flag; -return ima_match_policy (inode, function, mask, flags); +return ima_match_policy (inode, func, mask, flags); }/* security/integrity/ima/ima_appraise.c +7
-6@@ -67,7 +67,7 @@ static int ima_fix_xattr(struct dentry *dentry,/* Return specific func appraised cached result */ enum integrity_status ima_get_cache_status (struct integrity_iint_cache *iint, -int func) +enum ima_hooks func) {switch (func) {case MMAP_CHECK :@@ -85,7 +85,8 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, }static void ima_set_cache_status (struct integrity_iint_cache *iint, -int func,enum integrity_status status) +enum ima_hooks func, +enum integrity_status status) {switch (func) {case MMAP_CHECK :@@ -103,11 +104,11 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint,case FILE_CHECK :default : iint->ima_file_status = status; -break ; } } -static void ima_cache_flags (struct integrity_iint_cache *iint,int func) +static void ima_cache_flags (struct integrity_iint_cache *iint, +enum ima_hooks func) {switch (func) {case MMAP_CHECK :@@ -125,7 +126,6 @@ static void ima_cache_flags(struct integrity_iint_cache *iint, int func)case FILE_CHECK :default : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); -break ; } }@@ -185,7 +185,8 @@ int ima_read_xattr(struct dentry *dentry,* * Return 0 on success, error code otherwise */ -int ima_appraise_measurement (int func,struct integrity_iint_cache *iint, +int ima_appraise_measurement (enum ima_hooks 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)security/integrity/ima/ima_main.c +7
-7@@ -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,int mask, +enum ima_hooks func,int opened) {struct inode *inode =file_inode (file);struct integrity_iint_cache *iint =NULL ;@@ -174,8 +174,8 @@ static int process_measurement(struct file *file, int mask, int function,* bitmask based on the appraise/audit/measurement policy. * Included is the appraise submask. */ - action =ima_get_action (inode, mask, function); - violation_check = ((function ==FILE_CHECK || function ==MMAP_CHECK ) && + action =ima_get_action (inode, mask, func); + violation_check = ((func ==FILE_CHECK || func ==MMAP_CHECK ) && (ima_policy_flag &IMA_MEASURE ));if (!action && !violation_check)return 0 ;@@ -184,7 +184,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Is the appraise rule hook specific? */ if (action &IMA_FILE_APPRAISE ) - function =FILE_CHECK ; + func =FILE_CHECK ;mutex_lock (&inode->i_mutex);@@ -214,7 +214,7 @@ static int process_measurement(struct file *file, int mask, int function,/* Nothing to do, just return existing appraised status */ if (!action) {if (must_appraise) - rc =ima_get_cache_status (iint, function); + rc =ima_get_cache_status (iint, func); goto out_digsig; }@@ -240,7 +240,7 @@ static int process_measurement(struct file *file, int mask, int function,ima_store_measurement (iint, file, pathname, xattr_value, xattr_len);if (action &IMA_APPRAISE_SUBMASK ) - rc =ima_appraise_measurement (function, iint, file, pathname, + rc =ima_appraise_measurement (func, iint, file, pathname, xattr_value, xattr_len, opened);if (action &IMA_AUDIT )ima_audit_measurement (iint, pathname);security/integrity/ima/ima_policy.c +3
-4@@ -207,8 +207,8 @@ static void ima_lsm_update_rules(void)* * Returns true on rule match, false on failure. */ -static bool ima_match_rules (struct ima_rule_entry *rule, -struct inode *inode,enum ima_hooks func,int mask) +static bool ima_match_rules (struct ima_rule_entry *rule,struct inode *inode, +enum ima_hooks func,int mask) {struct task_struct *tsk = current;const struct cred *cred =current_cred ();@@ -289,7 +289,7 @@ retry:* In addition to knowing that we need to appraise the file in general, * we need to differentiate between calling hooks, for hook specific rules. */ -static int get_subaction (struct ima_rule_entry *rule,int func) +static int get_subaction (struct ima_rule_entry *rule,enum ima_hooks func) {if (!(rule->flags &IMA_FUNC ))return IMA_FILE_APPRAISE ; --2.1.0
Signature · 2 lines
Thanks, Dmitry
PATCH v3 4/22 firmware: simplify dev_*() print messages for generic helpers
In reply to Mimi ZoharFrom: "Luis R. Rodriguez" <[email protected]>
Simplify a few of the *generic* shared dev_warn() and dev_dbg() print messages for three reasons:
0) Historically firmware_class code was added to help get device driver firmware binaries but these days request_firmware*() helpers are being repurposed for general *system data* needed by the kernel.
1) This will also help generalize shared code as much as possible later in the future in consideration for a new extensible firmware API which will enable to separate usermode helper code out as much as possible.
2) Kees Cook pointed out the the prints already have the device associated as dev_*() helpers are used, that should help identify the user and case in which the helpers are used. That should provide enough context and simplifies the messages further.
v4: generalize debug/warn messages even further as suggested by
Kees Cook.
--- drivers/base/firmware_class.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/base/firmware_class.c +4 -4
@@ -353,15 +353,15 @@ static int fw_get_filesystem_firmware(struct device *device, rc =fw_read_file_contents (file, buf);fput (file);if (rc) -dev_warn (device,"firmware, attempted to load %s, but failed with error %d\n" , - path, rc); +dev_warn (device,"loading %s failed with error %d\n" , + path, rc);else break ; }__putname (path);if (!rc) { -dev_dbg (device,"firmware: direct-loading firmware %s\n" , +dev_dbg (device,"direct-loading %s\n" , buf->fw_id);mutex_lock (&fw_lock);set_bit (FW_STATUS_DONE , &buf->status);
@@ -1051,7 +1051,7 @@ _request_firmware_prepare(struct firmware **firmware_p, const char *name, }if (fw_get_builtin_firmware (firmware, name)) { -dev_dbg (device,"firmware: using built-in firmware %s\n" , name); +dev_dbg (device,"using built-in %s\n" , name);return 0 ;/* assigned */ }
Signature · 1 line
2.1.0
Re: [PATCH v3 04/22] firmware: simplify dev_*() print messages for generic helpers
Quoted 23 lines · Mimi Zohar
From: "Luis R. Rodriguez" <[email protected]>
Simplify a few of the *generic* shared
dev_warn()anddev_dbg()print messages for three reasons:0) Historically
firmware_classcode was added to help get device driver firmware binaries but these days request_firmware*() helpers are being repurposed for general *system data* needed by the kernel.1) This will also help generalize shared code as much as possible later in the future in consideration for a new extensible firmware API which will enable to separate usermode helper code out as much as possible.
2) Kees Cook pointed out the the prints already have the device associated as dev_*() helpers are used, that should help identify the user and case in which the helpers are used. That should provide enough context and simplifies the messages further.
v4: generalize debug/warn messages even further as suggested by
Kees Cook.
Quoted 14 lines
Cc Rusty Russell <[email protected]>Cc Andrew Morton <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc David Howells <[email protected]>Cc Kees Cook <[email protected]>Cc Casey Schaufler <[email protected]>Cc Ming Lei <[email protected]>Cc Takashi Iwai <[email protected]>Cc Vojtěch Pavlík <[email protected]>Cc Kyle McMartin <[email protected]>Cc Matthew Garrett <[email protected]>Signed-off-by Luis R. Rodriguez <[email protected]>Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 39 lines
--- drivers/base/firmware_class.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/base/firmware_class.c +4
-5@@ -353,15 +353,15 @@ static int fw_get_filesystem_firmware(struct device *device, rc =fw_read_file_contents (file, buf);fput (file);if (rc) -dev_warn (device,"firmware, attempted to load %s, but failed with error %d\n" , - path, rc); +dev_warn (device,"loading %s failed with error %d\n" , + path, rc);else break ; }__putname (path);if (!rc) { -dev_dbg (device,"firmware: direct-loading firmware %s\n" , +dev_dbg (device,"direct-loading %s\n" , buf->fw_id);mutex_lock (&fw_lock);set_bit (FW_STATUS_DONE , &buf->status);@@ -1051,7 +1051,7 @@ _request_firmware_prepare(struct firmware **firmware_p, const char *name, }if (fw_get_builtin_firmware (firmware, name)) { -dev_dbg (device,"firmware: using built-in firmware %s\n" , name); +dev_dbg (device,"using built-in %s\n" , name);return 0 ;/* assigned */ } --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v3 5/22 firmware: move completing fw into a helper
In reply to Mimi ZoharFrom: "Luis R. Rodriguez" <[email protected]>
This will be re-used later through a new extensible interface.
--- drivers/base/firmware_class.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/base/firmware_class.c +10 -4
@@ -322,6 +322,15 @@ fail:return rc; } +static void fw_finish_direct_load (struct device *device, +struct firmware_buf *buf) +{ +mutex_lock (&fw_lock); +set_bit (FW_STATUS_DONE , &buf->status); +complete_all (&buf->completion); +mutex_unlock (&fw_lock); +} +static int fw_get_filesystem_firmware (struct device *device,struct firmware_buf *buf) {
@@ -363,10 +372,7 @@ static int fw_get_filesystem_firmware(struct device *device,if (!rc) {dev_dbg (device,"direct-loading %s\n" , buf->fw_id); -mutex_lock (&fw_lock); -set_bit (FW_STATUS_DONE , &buf->status); -complete_all (&buf->completion); -mutex_unlock (&fw_lock); +fw_finish_direct_load (device, buf); }return rc;
Signature · 1 line
2.1.0
Re: [PATCH v3 05/22] firmware: move completing fw into a helper
Quoted 7 lines · Mimi Zohar
From: "Luis R. Rodriguez" <[email protected]>
This will be re-used later through a new extensible interface.
Reviewed-by Josh Boyer <[email protected]>Signed-off-by Luis R. Rodriguez <[email protected]>Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 39 lines
--- drivers/base/firmware_class.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/base/firmware_class.c +10
-5@@ -322,6 +322,15 @@ fail:return rc; } +static void fw_finish_direct_load (struct device *device, +struct firmware_buf *buf) +{ +mutex_lock (&fw_lock); +set_bit (FW_STATUS_DONE , &buf->status); +complete_all (&buf->completion); +mutex_unlock (&fw_lock); +} +static int fw_get_filesystem_firmware (struct device *device,struct firmware_buf *buf) {@@ -363,10 +372,7 @@ static int fw_get_filesystem_firmware(struct device *device,if (!rc) {dev_dbg (device,"direct-loading %s\n" , buf->fw_id); -mutex_lock (&fw_lock); -set_bit (FW_STATUS_DONE , &buf->status); -complete_all (&buf->completion); -mutex_unlock (&fw_lock); +fw_finish_direct_load (device, buf); }return rc; --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v3 6/22 firmware: fold successful fw read early
In reply to Mimi ZoharFrom: David Howells <[email protected]>
We'll be folding in some more checks on fw_read_file_contents(), this will make the success case easier to follow.
--- drivers/base/firmware_class.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/base/firmware_class.c +7 -9
@@ -361,20 +361,18 @@ static int fw_get_filesystem_firmware(struct device *device,continue ; rc =fw_read_file_contents (file, buf);fput (file); -if (rc) +if (rc ==0 ) { +dev_dbg (device,"direct-loading %s\n" , + buf->fw_id); +fw_finish_direct_load (device, buf); + goto out; + }else dev_warn (device,"loading %s failed with error %d\n" , path, rc); -else -break ; } +out:__putname (path); -if (!rc) { -dev_dbg (device,"direct-loading %s\n" , - buf->fw_id); -fw_finish_direct_load (device, buf); - } -return rc; }
Signature · 1 line
2.1.0
Re: [PATCH v3 06/22] firmware: fold successful fw read early
Quoted 34 lines · Mimi Zohar
From: David Howells <[email protected]>
We'll be folding in some more checks on
fw_read_file_contents(), this will make the success case easier to follow.Reviewed-by Josh Boyer <[email protected]>Signed-off-by David Howells <[email protected]>Signed-off-by Luis R. Rodriguez <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- drivers/base/firmware_class.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)drivers/base/firmware_class.c +7
-3@@ -361,20 +361,18 @@ static int fw_get_filesystem_firmware(struct device *device,continue ; rc =fw_read_file_contents (file, buf);fput (file); -if (rc) +if (rc ==0 ) { +dev_dbg (device,"direct-loading %s\n" , + buf->fw_id); +fw_finish_direct_load (device, buf); + goto out; + }else dev_warn (device,"loading %s failed with error %d\n" , path, rc); -else -break ; } +out:
drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 377
Quoted 10 lines
drivers/base/firmware_class.c +0
-6__putname (path); -if (!rc) { -dev_dbg (device,"direct-loading %s\n" , - buf->fw_id); -fw_finish_direct_load (device, buf); - } -return rc; }
Looking at this code, why does this use "break":
len = snprintf(path, PATH_MAX, "%s/%s",
fw_path[i], buf->fw_id);
if (len >= PATH_MAX) {
rc = -ENAMETOOLONG;
break;
}Shouldn't that emit a warning, set rc, and continue?
Regardless, I think this is more readable. Adding an "out" target at the end of a for loop seems weird, given "break" existing. :)
rc = fw_read_file_contents(file, buf);
fput(file);- if (rc)
+ if (rc) {
dev_warn(device, "loading %s failed with error %d\n",
path, rc);
+ continue;
+ }
+ dev_dbg(device, "direct-loading %s\n", buf->fw_id);
+ fw_finish_direct_load(device, buf);
- else
- break;
+ break;
}
__putname(path);-
- if (!rc) {
- dev_dbg(device, "direct-loading %s\n",
- buf->fw_id);
- fw_finish_direct_load(device, buf);
- }
-
return rc;
}
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 06/22] firmware: fold successful fw read early
Quoted 48 lines · Kees Cook
Quoted 34 lines · Mimi Zohar
From: David Howells <[email protected]>
We'll be folding in some more checks on
fw_read_file_contents(), this will make the success case easier to follow.Reviewed-by Josh Boyer <[email protected]>Signed-off-by David Howells <[email protected]>Signed-off-by Luis R. Rodriguez <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- drivers/base/firmware_class.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)drivers/base/firmware_class.c +7
-3@@ -361,20 +361,18 @@ static int fw_get_filesystem_firmware(struct device *device,continue ; rc =fw_read_file_contents (file, buf);fput (file); -if (rc) +if (rc ==0 ) { +dev_dbg (device,"direct-loading %s\n" , + buf->fw_id); +fw_finish_direct_load (device, buf); + goto out; + }else dev_warn (device,"loading %s failed with error %d\n" , path, rc); -else -break ; } +out:drivers/base/firmware_class.c static int fw_get_filesystem_firmware(struct device *device, · line 377
Quoted 10 lines
drivers/base/firmware_class.c +0
-6__putname (path); -if (!rc) { -dev_dbg (device,"direct-loading %s\n" , - buf->fw_id); -fw_finish_direct_load (device, buf); - } -return rc; }Looking at this code, why does this use "break":
Quoted 8 lines
len = snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); if (len >= PATH_MAX) { rc = -ENAMETOOLONG; break; }Shouldn't that emit a warning, set rc, and continue?
Yes but this is a separate piece of code, and that's a functional change but I welcome the change for sure. This could be done separately.
Quoted 2 lines
Regardless, I think this is more readable. Adding an "out" target at the end of a for loop seems weird, given "break" existing. :)
Good call, goto mixed with while loops can be get iffy. I'm fine with this change as a replacement. To be fair we should also note both patches (the one submitted and yours below) also make a small functional change so that the loop continues over all other possible directories in the fw_path in case of failure with the first directory used as a base. If we wanted to be pedantic and careful we could split the functional change to not enable to continue in case of failure onto the next directory, and instead require that as a separate patch. I'll leave it to Mimi to decide.
Quoted 15 lines
rc = fw_read_file_contents(file, buf); fput(file);(quoted diff) +6
-3- if (rc) + if (rc) { dev_warn(device, "loading %s failed with error %d\n", path, rc); + continue; + } + dev_dbg(device, "direct-loading %s\n", buf->fw_id); + fw_finish_direct_load(device, buf); - else - break; + break; }
Review
Quoted 10 lines
(quoted diff) +0
-7__putname(path); - - if (!rc) { - dev_dbg(device, "direct-loading %s\n", - buf->fw_id); - fw_finish_direct_load(device, buf); - } - return rc; }
Can you provided a Signed-offy-by? If so consider my Acked-by.
Luis
PATCH v3 7/22 vfs: define a generic function to read a file from the kernel
In reply to Mimi ZoharFor 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 v3:
- additional bounds checking - Luis
v2:
- 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 | 7 +++++++ 5 files changed, 77 insertions(+)
fs/exec.c +53 -0
@@ -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) || max_size <0 ) +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
@@ -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
@@ -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
@@ -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 +7 -0
@@ -910,6 +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) +{ +return call_int_hook (kernel_post_read_file,0 , file, buf, size); +} +int security_task_fix_setuid (struct cred *new,const struct cred *old,int flags) {
@@ -1697,6 +1702,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
Re: [PATCH v3 07/22] vfs: define a generic function to read a file from the kernel
Quoted 9 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. 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.
Quoted 7 lines
Changelog v3:
- additional bounds checking - Luis
v2:
- To simplify patch review, re-ordered patchesSigned-off-by Mimi Zohar <[email protected]>Reviewed-by Luis R. Rodriguez <[email protected]>
-Kees
Quoted 178 lines
--- fs/exec.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + include/linux/lsm_hooks.h | 9 ++++++++ include/linux/security.h | 7 +++++++ security/security.c | 7 +++++++ 5 files changed, 77 insertions(+)fs/exec.c +53
-0@@ -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) || max_size <0 ) +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@@ -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@@ -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@@ -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 +7
-1@@ -910,6 +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) +{ +return call_int_hook (kernel_post_read_file,0 , file, buf, size); +} +int security_task_fix_setuid (struct cred *new,const struct cred *old,int flags) {@@ -1697,6 +1702,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), --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
PATCH v3 8/22 vfs: define kernel_read_file_id enumeration
In reply to Mimi ZoharTo differentiate between the kernel_read_file() callers, this patch defines a new enumeration named kernel_read_file_id and includes the caller identifier as an argument.
Subsequent patches define READING_KEXEC_IMAGE, READING_KEXEC_INITRAMFS, READING_FIRMWARE, READING_MODULE, and READING_POLICY.
Changelog v3:
- Replace the IMA specific enumeration with a generic one.
--- fs/exec.c | 4 ++-- include/linux/fs.h | 7 ++++++- include/linux/lsm_hooks.h | 4 +++- include/linux/security.h | 7 +++++-- security/security.c | 5 +++-- 5 files changed, 19 insertions(+), 8 deletions(-)
fs/exec.c +2 -2
@@ -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,enum kernel_read_file_id 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, id);if (!ret) *size = pos;
include/linux/fs.h +6 -1
@@ -2526,8 +2526,13 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); +enum kernel_read_file_id { +READING_MAX_ID +}; +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 , +enum kernel_read_file_id );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 +3 -1
@@ -567,6 +567,7 @@* by the kernel. * @buf pointer to buffer containing the file contents. * @size length of the file contents. +* @id kernel read file 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, +enum kernel_read_file_id 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 +5 -2
@@ -28,6 +28,7 @@#include <linux/err.h> #include <linux/string.h> #include <linux/mm.h> +#include <linux/fs.h> struct linux_binprm ;struct cred ;
@@ -301,7 +302,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, +enum kernel_read_file_id 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 +870,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, +enum kernel_read_file_id id) {return 0 ; }
security/security.c +3 -2
@@ -910,9 +910,10 @@ 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, +enum kernel_read_file_id 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, id); } int security_task_fix_setuid(struct cred *new, const struct cred *old,
Signature · 1 line
2.1.0
Re: [PATCH v3 08/22] vfs: define kernel_read_file_id enumeration
Quoted 11 lines · Mimi Zohar
To differentiate between the
kernel_read_file()callers, this patch defines a new enumeration namedkernel_read_file_idand includes the caller identifier as an argument.Subsequent patches define
READING_KEXEC_IMAGE,READING_KEXEC_INITRAMFS,READING_FIRMWARE,READING_MODULE, andREADING_POLICY.Changelog v3:
- Replace the IMA specific enumeration with a generic one.Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 123 lines
--- fs/exec.c | 4 ++-- include/linux/fs.h | 7 ++++++- include/linux/lsm_hooks.h | 4 +++- include/linux/security.h | 7 +++++-- security/security.c | 5 +++-- 5 files changed, 19 insertions(+), 8 deletions(-)fs/exec.c +2
-2@@ -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,enum kernel_read_file_id 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, id);if (!ret) *size = pos;include/linux/fs.h +6
-1@@ -2526,8 +2526,13 @@ static inline void i_readcount_inc(struct inode *inode)#endif extern int do_pipe_flags (int *,int ); +enum kernel_read_file_id { +READING_MAX_ID +}; +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 , +enum kernel_read_file_id );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 +3
-1@@ -567,6 +567,7 @@* by the kernel. * @buf pointer to buffer containing the file contents. * @size length of the file contents. +* @id kernel read file 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, +enum kernel_read_file_id 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 +5
-2@@ -28,6 +28,7 @@#include <linux/err.h> #include <linux/string.h> #include <linux/mm.h> +#include <linux/fs.h> struct linux_binprm ;struct cred ;@@ -301,7 +302,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, +enum kernel_read_file_id 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 +870,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, +enum kernel_read_file_id id) {return 0 ; }security/security.c +3
-3@@ -910,9 +910,10 @@ 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, +enum kernel_read_file_id 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, id); } int security_task_fix_setuid(struct cred *new, const struct cred *old, --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 08/22] vfs: define kernel_read_file_id enumeration
In reply to Mimi ZoharQuoted 11 lines · Mimi Zohar
To differentiate between the
kernel_read_file()callers, this patch defines a new enumeration namedkernel_read_file_idand includes the caller identifier as an argument.Subsequent patches define
READING_KEXEC_IMAGE,READING_KEXEC_INITRAMFS,READING_FIRMWARE,READING_MODULE, andREADING_POLICY.Changelog v3:
- Replace the IMA specific enumeration with a generic one.Signed-off-by Mimi Zohar <[email protected]>
Luis
PATCH v3 9/22 ima: provide buffer hash calculation function
In reply to Mimi ZoharFrom: Dmitry Kasatkin <[email protected]>
This patch provides convenient buffer hash calculation function.
Changelog v3:
- fix while hash calculation - Dmitry
v1:
- 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
@@ -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 · 1 line
2.1.0
PATCH v3 10/22 ima: calculate the hash of a buffer using aynchronous hash(ahash)
In reply to Mimi ZoharSetting 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
@@ -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 · 1 line
2.1.0
Re: [PATCH v3 10/22] ima: calculate the hash of a buffer using aynchronous hash(ahash)
Quoted 4 lines · 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.Signed-off-by Mimi Zohar <[email protected]>
Quoted 107 lines
--- security/integrity/ima/ima_crypto.c | 75 ++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-)security/integrity/ima/ima_crypto.c +73
-3@@ -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) --2.1.0
Signature · 2 lines
Thanks, Dmitry
PATCH v3 11/22 ima: define a new hook to measure and appraise a file already in memory
In reply to Mimi ZoharThis patch defines a new IMA hook ima_post_read_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.
Changelog v3:
- rename ima_hash_and_process_file() to ima_post_read_file()
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".
--- include/linux/ima.h | 8 +++++++ include/linux/security.h | 1 + security/integrity/ima/ima.h | 4 +++- security/integrity/ima/ima_api.c | 6 +++-- security/integrity/ima/ima_appraise.c | 2 +- security/integrity/ima/ima_main.c | 45 ++++++++++++++++++++++++++++------- security/integrity/ima/ima_policy.c | 1 + security/integrity/integrity.h | 7 ++++-- security/security.c | 7 +++++- 9 files changed, 66 insertions(+), 15 deletions(-)
include/linux/ima.h +8 -0
@@ -20,6 +20,8 @@ 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_post_read_file (struct file *file,void *buf,loff_t size, +enum kernel_read_file_id id);#else static inline int ima_bprm_check (struct linux_binprm *bprm)
@@ -52,6 +54,12 @@ static inline int ima_fw_from_file(struct file *file, char *buf, size_t size) return 0; } +static inline int ima_post_read_file (struct file *file,void *buf,loff_t size, +enum kernel_read_file_id id) +{ +return 0 ; +} +#endif /* CONFIG_IMA */ #ifdef CONFIG_IMA_APPRAISE
include/linux/security.h +1 -0
@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/capability.h> +#include <linux/fs.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/string.h>
security/integrity/ima/ima.h +3 -1
@@ -19,6 +19,7 @@#include <linux/types.h> #include <linux/crypto.h> +#include <linux/fs.h> #include <linux/security.h> #include <linux/hash.h> #include <linux/tpm.h>
@@ -152,7 +153,8 @@ enum ima_hooks {int ima_get_action (struct inode *inode,int mask,enum ima_hooks func);int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func);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,
security/integrity/ima/ima_api.c +4 -2
@@ -188,7 +188,8 @@ int ima_get_action(struct inode *inode, int mask, enum ima_hooks func)* 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 +1 -1
@@ -300,7 +300,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 +37 -8
87 lines not on this page · raw message
security/integrity/ima/ima_policy.c +1 -0
@@ -12,6 +12,7 @@*/ #include <linux/module.h> #include <linux/list.h> +#include <linux/fs.h> #include <linux/security.h> #include <linux/magic.h> #include <linux/parser.h>
security/integrity/integrity.h +5 -2
@@ -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 +6 -1
@@ -913,7 +913,12 @@ int security_kernel_module_from_file(struct file *file)int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id) { -return call_int_hook (kernel_post_read_file,0 , file, buf, size, id); +int ret; + + ret =call_int_hook (kernel_post_read_file,0 , file, buf, size, id); +if (ret) +return ret; +return ima_post_read_file (file, buf, size, id); } int security_task_fix_setuid(struct cred *new, const struct cred *old,
Signature · 1 line
2.1.0
Re: [PATCH v3 11/22] ima: define a new hook to measure and appraise a file already in memory
Quoted 8 lines · Mimi Zohar
This patch defines a new IMA hook
ima_post_read_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.Changelog v3:
- renameima_hash_and_process_file()toima_post_read_file()
Quoted 8 lines
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 tocopy_file_from_fd()with kernel version".Signed-off-by Mimi Zohar <[email protected]>
Quoted 269 lines
--- include/linux/ima.h | 8 +++++++ include/linux/security.h | 1 + security/integrity/ima/ima.h | 4 +++- security/integrity/ima/ima_api.c | 6 +++-- security/integrity/ima/ima_appraise.c | 2 +- security/integrity/ima/ima_main.c | 45 ++++++++++++++++++++++++++++------- security/integrity/ima/ima_policy.c | 1 + security/integrity/integrity.h | 7 ++++-- security/security.c | 7 +++++- 9 files changed, 66 insertions(+), 15 deletions(-)include/linux/ima.h +8
-0@@ -20,6 +20,8 @@ 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_post_read_file (struct file *file,void *buf,loff_t size, +enum kernel_read_file_id id);#else static inline int ima_bprm_check (struct linux_binprm *bprm)@@ -52,6 +54,12 @@ static inline int ima_fw_from_file(struct file *file, char *buf, size_t size) return 0; } +static inline int ima_post_read_file (struct file *file,void *buf,loff_t size, +enum kernel_read_file_id id) +{ +return 0 ; +} +#endif /* CONFIG_IMA */ #ifdef CONFIG_IMA_APPRAISE include/linux/security.h +1
-0@@ -24,6 +24,7 @@#include <linux/key.h> #include <linux/capability.h> +#include <linux/fs.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/string.h> security/integrity/ima/ima.h +3
-1@@ -19,6 +19,7 @@#include <linux/types.h> #include <linux/crypto.h> +#include <linux/fs.h> #include <linux/security.h> #include <linux/hash.h> #include <linux/tpm.h> @@ -152,7 +153,8 @@ enum ima_hooks {int ima_get_action (struct inode *inode,int mask,enum ima_hooks func);int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func);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,security/integrity/ima/ima_api.c +4
-2@@ -188,7 +188,8 @@ int ima_get_action(struct inode *inode, int mask, enum ima_hooks func)* 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 +1
-1@@ -300,7 +300,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 +37
-8@@ -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, -enum ima_hooks func,int opened) +static int process_measurement (struct file *file,char *buf,loff_t size, +int mask,enum ima_hooks func,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, 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,34 @@ 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_post_read_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 +* @read_id: caller identifier +* +* Measure/appraise/audit in memory file based on policy. Policy rules +* are written in terms of a policy identifier. +* +* On success return 0. On integrity appraisal error, assuming the file +* is in policy and IMA-appraisal is in enforcing mode, return -EACCES. +*/ +int ima_post_read_file (struct file *file,void *buf,loff_t size, +enum kernel_read_file_id read_id) +{ +enum ima_hooks func =FILE_CHECK ; + +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 , func,0 ); }static int __init init_ima (void )security/integrity/ima/ima_policy.c +1
-0@@ -12,6 +12,7 @@*/ #include <linux/module.h> #include <linux/list.h> +#include <linux/fs.h> #include <linux/security.h> #include <linux/magic.h> #include <linux/parser.h> security/integrity/integrity.h +5
-2@@ -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 +6
-2@@ -913,7 +913,12 @@ int security_kernel_module_from_file(struct file *file)int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id) { -return call_int_hook (kernel_post_read_file,0 , file, buf, size, id); +int ret; + + ret =call_int_hook (kernel_post_read_file,0 , file, buf, size, id); +if (ret) +return ret; +return ima_post_read_file (file, buf, size, id); } int security_task_fix_setuid(struct cred *new, const struct cred *old, --2.1.0
Signature · 2 lines
Thanks, Dmitry
PATCH v3 12/22 vfs: define kernel_read_file_from_path
In reply to Mimi ZoharThis patch defines kernel_read_file_from_path(), a wrapper for the VFS common kernel_read_file().
Changelog:
- Separated from the IMA patch
--- fs/exec.c | 22 ++++++++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 24 insertions(+)
fs/exec.c +22 -0
@@ -884,6 +884,28 @@ out: }EXPORT_SYMBOL_GPL (kernel_read_file); +int kernel_read_file_from_path (char *path,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id 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, id); +fput (file); +return ret; +} +EXPORT_SYMBOL_GPL (kernel_read_file_from_path); +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 +2 -0
@@ -2533,6 +2533,8 @@ enum kernel_read_file_id {extern int kernel_read (struct file *,loff_t ,char *,unsigned long );extern int kernel_read_file (struct file *,void **,loff_t *,loff_t ,enum kernel_read_file_id ); +extern int kernel_read_file_from_path (char *,void **,loff_t *,loff_t , +enum kernel_read_file_id );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 *);
Signature · 1 line
2.1.0
Re: [PATCH v3 12/22] vfs: define kernel_read_file_from_path
Quoted 7 lines · Mimi Zohar
This patch defines
kernel_read_file_from_path(), a wrapper for the VFS commonkernel_read_file().Changelog:
- Separated from the IMA patchSigned-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 54 lines
--- fs/exec.c | 22 ++++++++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 24 insertions(+)fs/exec.c +22
-0@@ -884,6 +884,28 @@ out: }EXPORT_SYMBOL_GPL (kernel_read_file); +int kernel_read_file_from_path (char *path,void **buf,loff_t *size, +loff_t max_size,enum kernel_read_file_id 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, id); +fput (file); +return ret; +} +EXPORT_SYMBOL_GPL (kernel_read_file_from_path); +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 +2
-1@@ -2533,6 +2533,8 @@ enum kernel_read_file_id {extern int kernel_read (struct file *,loff_t ,char *,unsigned long );extern int kernel_read_file (struct file *,void **,loff_t *,loff_t ,enum kernel_read_file_id ); +extern int kernel_read_file_from_path (char *,void **,loff_t *,loff_t , +enum kernel_read_file_id );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 *); --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 12/22] vfs: define kernel_read_file_from_path
In reply to Mimi ZoharQuoted 7 lines · Mimi Zohar
This patch defines
kernel_read_file_from_path(), a wrapper for the VFS commonkernel_read_file().Changelog:
- Separated from the IMA patchSigned-off-by Mimi Zohar <[email protected]>
Luis
PATCH v3 13/22 firmware: replace call to fw_read_file_contents() with kernel version
In reply to Mimi ZoharReplace the fw_read_file_contents with kernel_file_read_from_path().
Although none of the upstreamed LSMs define a kernel_fw_from_file hook, IMA is called by the security function to prevent unsigned firmware from being loaded and to measure/appraise signed firmware, based on policy.
Instead of reading the firmware twice, once for measuring/appraising the firmware and again for reading the firmware contents into memory, the kernel_post_read_file() security hook calculates the file hash based on the in memory file buffer. The firmware is read once.
This patch removes the LSM kernel_fw_from_file() hook and security call.
Changelog v3:
- remove kernel_fw_from_file hook
- use kernel_file_read_from_path() - requested by Luis v2:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)
--- drivers/base/firmware_class.c | 48 +++++++-------------------------------- include/linux/fs.h | 1 + include/linux/ima.h | 6 ----- include/linux/lsm_hooks.h | 11 --------- include/linux/security.h | 7 ------ security/integrity/ima/ima_main.c | 21 ++++++++--------- security/security.c | 13 ----------- 7 files changed, 19 insertions(+), 88 deletions(-)
drivers/base/firmware_class.c +8 -40
@@ -291,37 +291,6 @@ 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 void fw_finish_direct_load (struct device *device,struct firmware_buf *buf) {
@@ -334,6 +303,7 @@ static void fw_finish_direct_load(struct device *device,static int fw_get_filesystem_firmware (struct device *device,struct firmware_buf *buf) { +loff_t size;int i, len;int rc = -ENOENT ;char *path;
@@ -343,8 +313,6 @@ static int fw_get_filesystem_firmware(struct device *device,return -ENOMEM ;for (i =0 ; i <ARRAY_SIZE (fw_path); i++) { -struct file *file; -/* skip the unset customized path */ if (!fw_path[i][0 ])continue ;
@@ -356,12 +324,11 @@ static int fw_get_filesystem_firmware(struct device *device,break ; } - file =filp_open (path,O_RDONLY ,0 ); -if (IS_ERR (file)) -continue ; - rc =fw_read_file_contents (file, buf); -fput (file); + buf->size =0 ; + rc =kernel_read_file_from_path (path, &buf->data, &size, +INT_MAX ,READING_FIRMWARE );if (rc ==0 ) { + buf->size = (size_t ) size;dev_dbg (device,"direct-loading %s\n" , buf->fw_id);fw_finish_direct_load (device, buf);
@@ -689,8 +656,9 @@ 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, +READING_FIRMWARE );/* * Same logic as fw_load_abort, only the DONE bit
include/linux/fs.h +1 -0
@@ -2527,6 +2527,7 @@ static inline void i_readcount_inc(struct inode *inode)extern int do_pipe_flags (int *,int );enum kernel_read_file_id { +READING_FIRMWARE =1 ,READING_MAX_ID };
include/linux/ima.h +0 -6
@@ -19,7 +19,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_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);
@@ -49,11 +48,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_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id) {
include/linux/lsm_hooks.h +0 -11
@@ -541,15 +541,6 @@* @inode points to the inode to use as a reference. * The current task must be the one that nominated @inode. * Return 0 if successful. -* @kernel_fw_from_file: -* Load firmware from userspace (not called for built-in firmware). -* @file contains the file structure pointing to the file containing -* the firmware to load. This argument will be NULL if the firmware -* was loaded via the uevent-triggered blob-based interface exposed -* by CONFIG_FW_LOADER_USER_HELPER. -* @buf pointer to buffer containing firmware contents. -* @size length of the firmware contents. -* Return 0 if permission is granted. * @kernel_module_request: * Ability to trigger the kernel to automatically upcall to userspace for * userspace to load a kernel module with the given name.
@@ -1462,7 +1453,6 @@ union security_list_options {void (*cred_transfer)(struct cred *new,const struct cred *old);int (*kernel_act_as)(struct cred *new,u32 secid);int (*kernel_create_files_as)(struct cred *new,struct inode *inode); -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,
@@ -1725,7 +1715,6 @@ struct security_hook_heads {struct list_head cred_transfer;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;
include/linux/security.h +0 -7
@@ -300,7 +300,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,
@@ -854,12 +853,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_main.c +10 -11
@@ -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_post_read_file - in memory collect/appraise/audit measurement * @file: pointer to the file to be measured/appraised/audit
@@ -366,12 +355,22 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size, {enum ima_hooks func =FILE_CHECK ; +if (!file && read_id ==READING_FIRMWARE ) { +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 ;return 0 ; } +if (read_id ==READING_FIRMWARE ) + func =FIRMWARE_CHECK ; +return process_measurement (file, buf, size,MAY_READ , func,0 ); }
security/security.c +0 -13
@@ -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);
@@ -1702,8 +1691,6 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.kernel_act_as), .kernel_create_files_as = LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as), - .kernel_fw_from_file = - LIST_HEAD_INIT(security_hook_heads.kernel_fw_from_file), .kernel_module_request = LIST_HEAD_INIT(security_hook_heads.kernel_module_request), .kernel_module_from_file =
Signature · 1 line
2.1.0
Re: [PATCH v3 13/22] firmware: replace call to fw_read_file_contents() with kernel version
Quoted 13 lines · Mimi Zohar
Replace the
fw_read_file_contentswithkernel_file_read_from_path().Although none of the upstreamed LSMs define a
kernel_fw_from_filehook, IMA is called by the security function to prevent unsigned firmware from being loaded and to measure/appraise signed firmware, based on policy.Instead of reading the firmware twice, once for measuring/appraising the firmware and again for reading the firmware contents into memory, the
kernel_post_read_file()security hook calculates the file hash based on the in memory file buffer. The firmware is read once.This patch removes the LSM
kernel_fw_from_file()hook and security call.
Quoted 8 lines
Changelog v3:
- removekernel_fw_from_filehook
- usekernel_file_read_from_path()- requested by Luis v2:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 274 lines
--- drivers/base/firmware_class.c | 48 +++++++-------------------------------- include/linux/fs.h | 1 + include/linux/ima.h | 6 ----- include/linux/lsm_hooks.h | 11 --------- include/linux/security.h | 7 ------ security/integrity/ima/ima_main.c | 21 ++++++++--------- security/security.c | 13 ----------- 7 files changed, 19 insertions(+), 88 deletions(-)drivers/base/firmware_class.c +8
-40@@ -291,37 +291,6 @@ 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 void fw_finish_direct_load (struct device *device,struct firmware_buf *buf) {@@ -334,6 +303,7 @@ static void fw_finish_direct_load(struct device *device,static int fw_get_filesystem_firmware (struct device *device,struct firmware_buf *buf) { +loff_t size;int i, len;int rc = -ENOENT ;char *path;@@ -343,8 +313,6 @@ static int fw_get_filesystem_firmware(struct device *device,return -ENOMEM ;for (i =0 ; i <ARRAY_SIZE (fw_path); i++) { -struct file *file; -/* skip the unset customized path */ if (!fw_path[i][0 ])continue ;@@ -356,12 +324,11 @@ static int fw_get_filesystem_firmware(struct device *device,break ; } - file =filp_open (path,O_RDONLY ,0 ); -if (IS_ERR (file)) -continue ; - rc =fw_read_file_contents (file, buf); -fput (file); + buf->size =0 ; + rc =kernel_read_file_from_path (path, &buf->data, &size, +INT_MAX ,READING_FIRMWARE );if (rc ==0 ) { + buf->size = (size_t ) size;dev_dbg (device,"direct-loading %s\n" , buf->fw_id);fw_finish_direct_load (device, buf);@@ -689,8 +656,9 @@ 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, +READING_FIRMWARE );/* * Same logic as fw_load_abort, only the DONE bit include/linux/fs.h +1
-0@@ -2527,6 +2527,7 @@ static inline void i_readcount_inc(struct inode *inode)extern int do_pipe_flags (int *,int );enum kernel_read_file_id { +READING_FIRMWARE =1 ,READING_MAX_ID };include/linux/ima.h +0
-6@@ -19,7 +19,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_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);@@ -49,11 +48,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_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id) {include/linux/lsm_hooks.h +0
-11@@ -541,15 +541,6 @@* @inode points to the inode to use as a reference. * The current task must be the one that nominated @inode. * Return 0 if successful. -* @kernel_fw_from_file: -* Load firmware from userspace (not called for built-in firmware). -* @file contains the file structure pointing to the file containing -* the firmware to load. This argument will be NULL if the firmware -* was loaded via the uevent-triggered blob-based interface exposed -* by CONFIG_FW_LOADER_USER_HELPER. -* @buf pointer to buffer containing firmware contents. -* @size length of the firmware contents. -* Return 0 if permission is granted. * @kernel_module_request: * Ability to trigger the kernel to automatically upcall to userspace for * userspace to load a kernel module with the given name. @@ -1462,7 +1453,6 @@ union security_list_options {void (*cred_transfer)(struct cred *new,const struct cred *old);int (*kernel_act_as)(struct cred *new,u32 secid);int (*kernel_create_files_as)(struct cred *new,struct inode *inode); -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,@@ -1725,7 +1715,6 @@ struct security_hook_heads {struct list_head cred_transfer;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;include/linux/security.h +0
-7@@ -300,7 +300,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,@@ -854,12 +853,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_main.c +10
-11@@ -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_post_read_file - in memory collect/appraise/audit measurement * @file: pointer to the file to be measured/appraised/audit @@ -366,12 +355,22 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size, {enum ima_hooks func =FILE_CHECK ; +if (!file && read_id ==READING_FIRMWARE ) { +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 ;return 0 ; } +if (read_id ==READING_FIRMWARE ) + func =FIRMWARE_CHECK ; +return process_measurement (file, buf, size,MAY_READ , func,0 ); }security/security.c +0
-14@@ -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);@@ -1702,8 +1691,6 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.kernel_act_as), .kernel_create_files_as = LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as), - .kernel_fw_from_file = - LIST_HEAD_INIT(security_hook_heads.kernel_fw_from_file), .kernel_module_request = LIST_HEAD_INIT(security_hook_heads.kernel_module_request), .kernel_module_from_file = --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 13/22] firmware: replace call to fw_read_file_contents() with kernel version
In reply to Mimi ZoharQuoted 13 lines · Mimi Zohar
Replace the
fw_read_file_contentswithkernel_file_read_from_path().Although none of the upstreamed LSMs define a
kernel_fw_from_filehook, IMA is called by the security function to prevent unsigned firmware from being loaded and to measure/appraise signed firmware, based on policy.Instead of reading the firmware twice, once for measuring/appraising the firmware and again for reading the firmware contents into memory, the
kernel_post_read_file()security hook calculates the file hash based on the in memory file buffer. The firmware is read once.This patch removes the LSM
kernel_fw_from_file()hook and security call.
Quoted 8 lines
Changelog v3:
- removekernel_fw_from_filehook
- usekernel_file_read_from_path()- requested by Luis v2:
- reordered and squashed firmware patches
- fix MAX firmware size (Kees Cook)Signed-off-by Mimi Zohar <[email protected]>
Luis
PATCH v3 14/22 security: define kernel_read_file hook
In reply to Mimi ZoharThe kernel_read_file security hook is called prior to reading the file into memory.
--- fs/exec.c | 4 ++++ include/linux/ima.h | 6 ++++++ include/linux/lsm_hooks.h | 8 ++++++++ include/linux/security.h | 7 +++++++ security/integrity/ima/ima_main.c | 16 ++++++++++++++++ security/security.c | 12 ++++++++++++ 6 files changed, 53 insertions(+)
fs/exec.c +4 -0
@@ -842,6 +842,10 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (!S_ISREG (file_inode (file)->i_mode) || max_size <0 )return -EINVAL ; + ret =security_kernel_read_file (file, 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 +6 -0
@@ -19,6 +19,7 @@ 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_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);
@@ -48,6 +49,11 @@ static inline int ima_module_check(struct file *file)return 0 ; } +static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id) {
include/linux/lsm_hooks.h +8 -0
@@ -552,6 +552,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. +* @id kernel read file 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
@@ -1455,6 +1461,7 @@ union security_list_options {int (*kernel_create_files_as)(struct cred *new,struct inode *inode);int (*kernel_module_request)(char *kmod_name);int (*kernel_module_from_file)(struct file *file); +int (*kernel_read_file)(struct file *file,enum kernel_read_file_id id);int (*kernel_post_read_file)(struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int (*task_fix_setuid)(struct cred *new, const struct cred *old,
@@ -1715,6 +1722,7 @@ struct security_hook_heads {struct list_head cred_transfer;struct list_head kernel_act_as;struct list_head kernel_create_files_as; +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 +7 -0
@@ -302,6 +302,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,enum kernel_read_file_id id);int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int security_task_fix_setuid(struct cred *new, const struct cred *old,
@@ -863,6 +864,12 @@ static inline int security_kernel_module_from_file(struct file *file)return 0 ; } +static inline int security_kernel_read_file (struct file *file, +enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id)
security/integrity/ima/ima_main.c +16 -0
@@ -338,6 +338,22 @@ int ima_module_check(struct file *file) }/** +* ima_read_file - pre-measure/appraise hook decision based on policy +* @file: pointer to the file to be measured/appraised/audit +* @read_id: caller identifier +* +* Permit reading a file based on policy. The policy rules are written +* in terms of the policy identifier. Appraising the integrity of +* a file requires a file descriptor. +* +* For permission return 0, otherwise return -EACCES. +*/ +int ima_read_file (struct file *file,enum kernel_read_file_id read_id) +{ +return 0 ; +} + +/** * ima_post_read_file - in memory collect/appraise/audit measurement * @file: pointer to the file to be measured/appraised/audit * @buf: pointer to in memory file contents
security/security.c +12 -0
@@ -899,6 +899,16 @@ int security_kernel_module_from_file(struct file *file)return ima_module_check (file); } +int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) +{ +int ret; + + ret =call_int_hook (kernel_read_file,0 , file, id); +if (ret) +return ret; +return ima_read_file (file, id); +} +int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id) {
@@ -1695,6 +1705,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 · 1 line
2.1.0
Re: [PATCH v3 14/22] security: define kernel_read_file hook
Quoted 4 lines · Mimi Zohar
The
kernel_read_filesecurity hook is called prior to reading the file into memory.Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 166 lines
--- fs/exec.c | 4 ++++ include/linux/ima.h | 6 ++++++ include/linux/lsm_hooks.h | 8 ++++++++ include/linux/security.h | 7 +++++++ security/integrity/ima/ima_main.c | 16 ++++++++++++++++ security/security.c | 12 ++++++++++++ 6 files changed, 53 insertions(+)fs/exec.c +4
-0@@ -842,6 +842,10 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (!S_ISREG (file_inode (file)->i_mode) || max_size <0 )return -EINVAL ; + ret =security_kernel_read_file (file, 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 +6
-0@@ -19,6 +19,7 @@ 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_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);@@ -48,6 +49,11 @@ static inline int ima_module_check(struct file *file)return 0 ; } +static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id) {include/linux/lsm_hooks.h +8
-0@@ -552,6 +552,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. +* @id kernel read file 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 @@ -1455,6 +1461,7 @@ union security_list_options {int (*kernel_create_files_as)(struct cred *new,struct inode *inode);int (*kernel_module_request)(char *kmod_name);int (*kernel_module_from_file)(struct file *file); +int (*kernel_read_file)(struct file *file,enum kernel_read_file_id id);int (*kernel_post_read_file)(struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int (*task_fix_setuid)(struct cred *new, const struct cred *old,@@ -1715,6 +1722,7 @@ struct security_hook_heads {struct list_head cred_transfer;struct list_head kernel_act_as;struct list_head kernel_create_files_as; +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 +7
-0@@ -302,6 +302,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,enum kernel_read_file_id id);int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int security_task_fix_setuid(struct cred *new, const struct cred *old,@@ -863,6 +864,12 @@ static inline int security_kernel_module_from_file(struct file *file)return 0 ; } +static inline int security_kernel_read_file (struct file *file, +enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id)security/integrity/ima/ima_main.c +16
-0@@ -338,6 +338,22 @@ int ima_module_check(struct file *file) }/** +* ima_read_file - pre-measure/appraise hook decision based on policy +* @file: pointer to the file to be measured/appraised/audit +* @read_id: caller identifier +* +* Permit reading a file based on policy. The policy rules are written +* in terms of the policy identifier. Appraising the integrity of +* a file requires a file descriptor. +* +* For permission return 0, otherwise return -EACCES. +*/ +int ima_read_file (struct file *file,enum kernel_read_file_id read_id) +{ +return 0 ; +} + +/** * ima_post_read_file - in memory collect/appraise/audit measurement * @file: pointer to the file to be measured/appraised/audit * @buf: pointer to in memory file contents security/security.c +12
-1@@ -899,6 +899,16 @@ int security_kernel_module_from_file(struct file *file)return ima_module_check (file); } +int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) +{ +int ret; + + ret =call_int_hook (kernel_read_file,0 , file, id); +if (ret) +return ret; +return ima_read_file (file, id); +} +int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id) {@@ -1695,6 +1705,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 = --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 14/22] security: define kernel_read_file hook
In reply to Mimi ZoharQuoted 4 lines · Mimi Zohar
The
kernel_read_filesecurity hook is called prior to reading the file into memory.Signed-off-by Mimi Zohar <[email protected]>
Luis
Re: [PATCH v3 14/22] security: define kernel_read_file hook
In reply to Mimi ZoharQuoted 4 lines · Mimi Zohar
The
kernel_read_filesecurity hook is called prior to reading the file into memory.Signed-off-by Mimi Zohar <[email protected]>
Being able to deny the read prior to performing any real work makes a lot of sense.
Quoted 155 lines
--- fs/exec.c | 4 ++++ include/linux/ima.h | 6 ++++++ include/linux/lsm_hooks.h | 8 ++++++++ include/linux/security.h | 7 +++++++ security/integrity/ima/ima_main.c | 16 ++++++++++++++++ security/security.c | 12 ++++++++++++ 6 files changed, 53 insertions(+)fs/exec.c +4
-0@@ -842,6 +842,10 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,if (!S_ISREG (file_inode (file)->i_mode) || max_size <0 )return -EINVAL ; + ret =security_kernel_read_file (file, 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 +6
-0@@ -19,6 +19,7 @@ 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_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);@@ -48,6 +49,11 @@ static inline int ima_module_check(struct file *file)return 0 ; } +static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id) {include/linux/lsm_hooks.h +8
-0@@ -552,6 +552,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. +* @id kernel read file 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 @@ -1455,6 +1461,7 @@ union security_list_options {int (*kernel_create_files_as)(struct cred *new,struct inode *inode);int (*kernel_module_request)(char *kmod_name);int (*kernel_module_from_file)(struct file *file); +int (*kernel_read_file)(struct file *file,enum kernel_read_file_id id);int (*kernel_post_read_file)(struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int (*task_fix_setuid)(struct cred *new, const struct cred *old,@@ -1715,6 +1722,7 @@ struct security_hook_heads {struct list_head cred_transfer;struct list_head kernel_act_as;struct list_head kernel_create_files_as; +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 +7
-0@@ -302,6 +302,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,enum kernel_read_file_id id);int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id); int security_task_fix_setuid(struct cred *new, const struct cred *old,@@ -863,6 +864,12 @@ static inline int security_kernel_module_from_file(struct file *file)return 0 ; } +static inline int security_kernel_read_file (struct file *file, +enum kernel_read_file_id id) +{ +return 0 ; +} +static inline int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id)security/integrity/ima/ima_main.c +16
-0@@ -338,6 +338,22 @@ int ima_module_check(struct file *file) }/** +* ima_read_file - pre-measure/appraise hook decision based on policy +* @file: pointer to the file to be measured/appraised/audit +* @read_id: caller identifier +* +* Permit reading a file based on policy. The policy rules are written +* in terms of the policy identifier. Appraising the integrity of +* a file requires a file descriptor. +* +* For permission return 0, otherwise return -EACCES. +*/ +int ima_read_file (struct file *file,enum kernel_read_file_id read_id) +{ +return 0 ; +} + +/** * ima_post_read_file - in memory collect/appraise/audit measurement * @file: pointer to the file to be measured/appraised/audit * @buf: pointer to in memory file contents security/security.c +10
-0@@ -899,6 +899,16 @@ int security_kernel_module_from_file(struct file *file)return ima_module_check (file); } +int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) +{ +int ret; + + ret =call_int_hook (kernel_read_file,0 , file, id); +if (ret) +return ret; +return ima_read_file (file, id); +} +int security_kernel_post_read_file (struct file *file,char *buf,loff_t size,enum kernel_read_file_id id) {
security/security.c struct security_hook_heads security_hook_heads = { · line 1712
Quoted 8 lines
security/security.c +2
-0@@ -1695,6 +1705,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 =
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Re: [PATCH v3 14/22] security: define kernel_read_file hook
Quoted 10 lines · Casey Schaufler
Quoted 4 lines · Mimi Zohar
The
kernel_read_filesecurity hook is called prior to reading the file into memory.Signed-off-by Mimi Zohar <[email protected]>Acked-by Casey Schaufler <[email protected]>Being able to deny the read prior to performing any real work makes a lot of sense.
Thanks, Casey!
Mimi
PATCH v3 15/22 vfs: define kernel_copy_file_from_fd()
In reply to Mimi ZoharThis patch defines kernel_read_file_from_fd(), a wrapper for the VFS common kernel_read_file().
Changelog:
- Separated from the kernel modules patch
--- fs/exec.c | 16 ++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 18 insertions(+)
fs/exec.c +16 -0
@@ -910,6 +910,22 @@ int kernel_read_file_from_path(char *path, void **buf, loff_t *size, }EXPORT_SYMBOL_GPL (kernel_read_file_from_path); +int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +struct fd f =fdget (fd); +int ret = -EBADF ; + +if (!f.file) + goto out; + + ret =kernel_read_file (f.file, buf, size, max_size, id); +out: +fdput (f); +return ret; +} +EXPORT_SYMBOL_GPL (kernel_read_file_from_fd); +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 +2 -0
@@ -2536,6 +2536,8 @@ extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, enum kernel_read_file_id);extern int kernel_read_file_from_path (char *,void **,loff_t *,loff_t ,enum kernel_read_file_id ); +extern int kernel_read_file_from_fd (int ,void **,loff_t *,loff_t , +enum kernel_read_file_id );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 *);
Signature · 1 line
2.1.0
Re: [PATCH v3 15/22] vfs: define kernel_copy_file_from_fd()
Quoted 7 lines · Mimi Zohar
This patch defines
kernel_read_file_from_fd(), a wrapper for the VFS commonkernel_read_file().Changelog:
- Separated from the kernel modules patchSigned-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 48 lines
--- fs/exec.c | 16 ++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 18 insertions(+)fs/exec.c +16
-0@@ -910,6 +910,22 @@ int kernel_read_file_from_path(char *path, void **buf, loff_t *size, }EXPORT_SYMBOL_GPL (kernel_read_file_from_path); +int kernel_read_file_from_fd (int fd,void **buf,loff_t *size,loff_t max_size, +enum kernel_read_file_id id) +{ +struct fd f =fdget (fd); +int ret = -EBADF ; + +if (!f.file) + goto out; + + ret =kernel_read_file (f.file, buf, size, max_size, id); +out: +fdput (f); +return ret; +} +EXPORT_SYMBOL_GPL (kernel_read_file_from_fd); +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 +2
-1@@ -2536,6 +2536,8 @@ extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, enum kernel_read_file_id);extern int kernel_read_file_from_path (char *,void **,loff_t *,loff_t ,enum kernel_read_file_id ); +extern int kernel_read_file_from_fd (int ,void **,loff_t *,loff_t , +enum kernel_read_file_id );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 *); --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 15/22] vfs: define kernel_copy_file_from_fd()
In reply to Mimi ZoharQuoted 7 lines · Mimi Zohar
This patch defines
kernel_read_file_from_fd(), a wrapper for the VFS commonkernel_read_file().Changelog:
- Separated from the kernel modules patchSigned-off-by Mimi Zohar <[email protected]>
Luis
PATCH v3 16/22 module: replace copy_module_from_fd with kernel version
In reply to Mimi ZoharReplace copy_module_from_fd() with kernel_read_file_from_fd().
Although none of the upstreamed LSMs define a kernel_module_from_file hook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules.
The security function security_kernel_module_from_file() was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-read kernel_read_file() security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to the kernel_post_read_file() security hook.
This patch removes the security_kernel_module_from_file() hook and security call.
--- include/linux/fs.h | 1 + include/linux/ima.h | 6 ---- include/linux/lsm_hooks.h | 7 ---- include/linux/security.h | 5 --- kernel/module.c | 68 +++++---------------------------------- security/integrity/ima/ima_main.c | 35 ++++++++------------ security/security.c | 12 ------- 7 files changed, 22 insertions(+), 112 deletions(-)
include/linux/fs.h +1 -0
@@ -2528,6 +2528,7 @@ extern int do_pipe_flags(int *, int);enum kernel_read_file_id {READING_FIRMWARE =1 , +READING_MODULE ,READING_MAX_ID };
include/linux/ima.h +0 -6
@@ -18,7 +18,6 @@ extern int ima_bprm_check(struct linux_binprm *bprm);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_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);
@@ -44,11 +43,6 @@ static inline int ima_file_mmap(struct file *file, unsigned long prot)return 0 ; } -static inline int ima_module_check (struct file *file) -{ -return 0 ; -} -static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) {return 0 ;
include/linux/lsm_hooks.h +0 -7
@@ -546,12 +546,6 @@* userspace to load a kernel module with the given name. * @kmod_name name of the module requested by the kernel * Return 0 if successful. -* @kernel_module_from_file: -* Load a kernel module from userspace. -* @file contains the file structure pointing to the file containing -* 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
@@ -1725,7 +1719,6 @@ struct security_hook_heads {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;struct list_head task_fix_setuid;struct list_head task_setpgid;struct list_head task_getpgid;
include/linux/security.h +0 -5
@@ -859,11 +859,6 @@ static inline int security_kernel_module_request(char *kmod_name)return 0 ; } -static inline int security_kernel_module_from_file (struct file *file) -{ -return 0 ; -} -static inline int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) {
kernel/module.c +8 -60
95 lines not on this page · raw message
security/integrity/ima/ima_main.c +13 -22
@@ -316,28 +316,6 @@ int ima_file_check(struct file *file, int mask, int opened)EXPORT_SYMBOL_GPL (ima_file_check);/** -* ima_module_check - based on policy, collect/store/appraise measurement. -* @file: pointer to the file to be measured/appraised -* -* Measure/appraise kernel modules based on policy. -* -* On success return 0. On integrity appraisal error, assuming the file -* is in policy and IMA-appraisal is in enforcing mode, return -EACCES. -*/ -int ima_module_check (struct file *file) -{ -if (!file) { -#ifndef CONFIG_MODULE_SIG_FORCE -if ((ima_appraise &IMA_APPRAISE_MODULES ) && - (ima_appraise &IMA_APPRAISE_ENFORCE )) -return -EACCES ;/* INTEGRITY_UNKNOWN */ -#endif -return 0 ;/* We rely on module signature checking */ - } -return process_measurement (file,NULL ,0 ,MAY_EXEC ,MODULE_CHECK ,0 ); -} - -/** * ima_read_file - pre-measure/appraise hook decision based on policy * @file: pointer to the file to be measured/appraised/audit * @read_id: caller identifier
@@ -350,6 +328,14 @@ int ima_module_check(struct file *file)*/ int ima_read_file (struct file *file,enum kernel_read_file_id read_id) { +if (!file && read_id ==READING_MODULE ) { +#ifndef CONFIG_MODULE_SIG_FORCE +if ((ima_appraise &IMA_APPRAISE_MODULES ) && + (ima_appraise &IMA_APPRAISE_ENFORCE )) +return -EACCES ;/* INTEGRITY_UNKNOWN */ +#endif +return 0 ;/* We rely on module signature checking */ + }return 0 ; }
@@ -378,6 +364,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } +if (!file && read_id ==READING_MODULE )/* MODULE_SIG_FORCE enabled */ +return 0 ; +if (!file && (!buf || size ==0 )) {/* should never happen */ if (ima_appraise &IMA_APPRAISE_ENFORCE )return -EACCES ;
@@ -386,6 +375,8 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,if (read_id ==READING_FIRMWARE ) func =FIRMWARE_CHECK ; +else if (read_id ==READING_MODULE ) + func =MODULE_CHECK ;return process_measurement (file, buf, size,MAY_READ , func,0 ); }
security/security.c +0 -12
@@ -889,16 +889,6 @@ 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 ret; - - ret =call_int_hook (kernel_module_from_file,0 , file); -if (ret) -return ret; -return ima_module_check (file); -} -int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) {int ret;
@@ -1703,8 +1693,6 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as), .kernel_module_request = 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 =
Signature · 1 line
2.1.0
Re: [PATCH v3 16/22] module: replace copy_module_from_fd with kernel version
Quoted 15 lines · Mimi Zohar
Replace
copy_module_from_fd()withkernel_read_file_from_fd().Although none of the upstreamed LSMs define a
kernel_module_from_filehook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules.The security function
security_kernel_module_from_file()was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-readkernel_read_file()security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to thekernel_post_read_file()security hook.
Quoted 4 lines
This patch removes the
security_kernel_module_from_file()hook and security call.Signed-off-by Mimi Zohar <[email protected]>
I'm really liking this consolidation. :)
-Kees
Quoted 290 lines
--- include/linux/fs.h | 1 + include/linux/ima.h | 6 ---- include/linux/lsm_hooks.h | 7 ---- include/linux/security.h | 5 --- kernel/module.c | 68 +++++---------------------------------- security/integrity/ima/ima_main.c | 35 ++++++++------------ security/security.c | 12 ------- 7 files changed, 22 insertions(+), 112 deletions(-)include/linux/fs.h +1
-0@@ -2528,6 +2528,7 @@ extern int do_pipe_flags(int *, int);enum kernel_read_file_id {READING_FIRMWARE =1 , +READING_MODULE ,READING_MAX_ID };include/linux/ima.h +0
-6@@ -18,7 +18,6 @@ extern int ima_bprm_check(struct linux_binprm *bprm);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_read_file (struct file *file,enum kernel_read_file_id id);extern int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id id);@@ -44,11 +43,6 @@ static inline int ima_file_mmap(struct file *file, unsigned long prot)return 0 ; } -static inline int ima_module_check (struct file *file) -{ -return 0 ; -} -static inline int ima_read_file (struct file *file,enum kernel_read_file_id id) {return 0 ;include/linux/lsm_hooks.h +0
-7@@ -546,12 +546,6 @@* userspace to load a kernel module with the given name. * @kmod_name name of the module requested by the kernel * Return 0 if successful. -* @kernel_module_from_file: -* Load a kernel module from userspace. -* @file contains the file structure pointing to the file containing -* 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 @@ -1725,7 +1719,6 @@ struct security_hook_heads {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;struct list_head task_fix_setuid;struct list_head task_setpgid;struct list_head task_getpgid;include/linux/security.h +0
-5@@ -859,11 +859,6 @@ static inline int security_kernel_module_request(char *kmod_name)return 0 ; } -static inline int security_kernel_module_from_file (struct file *file) -{ -return 0 ; -} -static inline int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) {kernel/module.c +8
-60@@ -2665,7 +2665,7 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,if (info->len <sizeof (*(info->hdr)))return -ENOEXEC ; - err =security_kernel_module_from_file (NULL ); + err =security_kernel_read_file (NULL ,READING_MODULE );if (err)return err;@@ -2683,63 +2683,6 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,return 0 ; } -/* Sets info->hdr and info->len. */ -static int copy_module_from_fd (int fd,struct load_info *info) -{ -struct fd f =fdget (fd); -int err; -struct kstat stat; -loff_t pos; -ssize_t bytes =0 ; - -if (!f.file) -return -ENOEXEC ; - - err =security_kernel_module_from_file (f.file); -if (err) - goto out; - - err =vfs_getattr (&f.file->f_path, &stat); -if (err) - goto out; - -if (stat.size >INT_MAX ) { - err = -EFBIG ; - goto out; - } - -/* Don't hand 0 to vmalloc, it whines. */ -if (stat.size ==0 ) { - err = -EINVAL ; - goto out; - } - - info->hdr =vmalloc (stat.size); -if (!info->hdr) { - err = -ENOMEM ; - goto out; - } - - pos =0 ; -while (pos < stat.size) { - bytes =kernel_read (f.file, pos, (char *)(info->hdr) + pos, - stat.size - pos); -if (bytes <0 ) { -vfree (info->hdr); - err = bytes; - goto out; - } -if (bytes ==0 ) -break ; - pos += bytes; - } - info->len = pos; - -out: -fdput (f); -return err; -} -static void free_copy (struct load_info *info) {vfree (info->hdr);@@ -3602,8 +3545,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod, SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) { -int err;struct load_info info = { }; +loff_t size; +void *hdr; +int err; err =may_init_module ();if (err)@@ -3615,9 +3560,12 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags) |MODULE_INIT_IGNORE_VERMAGIC))return -EINVAL ; - err =copy_module_from_fd (fd, &info); + err =kernel_read_file_from_fd (fd, &hdr, &size,INT_MAX , +READING_MODULE );if (err)return err; + info.hdr = hdr; + info.len = size;return load_module (&info, uargs, flags); }security/integrity/ima/ima_main.c +13
-22@@ -316,28 +316,6 @@ int ima_file_check(struct file *file, int mask, int opened)EXPORT_SYMBOL_GPL (ima_file_check);/** -* ima_module_check - based on policy, collect/store/appraise measurement. -* @file: pointer to the file to be measured/appraised -* -* Measure/appraise kernel modules based on policy. -* -* On success return 0. On integrity appraisal error, assuming the file -* is in policy and IMA-appraisal is in enforcing mode, return -EACCES. -*/ -int ima_module_check (struct file *file) -{ -if (!file) { -#ifndef CONFIG_MODULE_SIG_FORCE -if ((ima_appraise &IMA_APPRAISE_MODULES ) && - (ima_appraise &IMA_APPRAISE_ENFORCE )) -return -EACCES ;/* INTEGRITY_UNKNOWN */ -#endif -return 0 ;/* We rely on module signature checking */ - } -return process_measurement (file,NULL ,0 ,MAY_EXEC ,MODULE_CHECK ,0 ); -} - -/** * ima_read_file - pre-measure/appraise hook decision based on policy * @file: pointer to the file to be measured/appraised/audit * @read_id: caller identifier @@ -350,6 +328,14 @@ int ima_module_check(struct file *file)*/ int ima_read_file (struct file *file,enum kernel_read_file_id read_id) { +if (!file && read_id ==READING_MODULE ) { +#ifndef CONFIG_MODULE_SIG_FORCE +if ((ima_appraise &IMA_APPRAISE_MODULES ) && + (ima_appraise &IMA_APPRAISE_ENFORCE )) +return -EACCES ;/* INTEGRITY_UNKNOWN */ +#endif +return 0 ;/* We rely on module signature checking */ + }return 0 ; }@@ -378,6 +364,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } +if (!file && read_id ==READING_MODULE )/* MODULE_SIG_FORCE enabled */ +return 0 ; +if (!file && (!buf || size ==0 )) {/* should never happen */ if (ima_appraise &IMA_APPRAISE_ENFORCE )return -EACCES ;@@ -386,6 +375,8 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,if (read_id ==READING_FIRMWARE ) func =FIRMWARE_CHECK ; +else if (read_id ==READING_MODULE ) + func =MODULE_CHECK ;return process_measurement (file, buf, size,MAY_READ , func,0 ); }security/security.c +0
-13@@ -889,16 +889,6 @@ 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 ret; - - ret =call_int_hook (kernel_module_from_file,0 , file); -if (ret) -return ret; -return ima_module_check (file); -} -int security_kernel_read_file (struct file *file,enum kernel_read_file_id id) {int ret;@@ -1703,8 +1693,6 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.kernel_create_files_as), .kernel_module_request = 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 = --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 16/22] module: replace copy_module_from_fd with kernel version
In reply to Mimi ZoharQuoted 15 lines · Mimi Zohar
Replace
copy_module_from_fd()withkernel_read_file_from_fd().Although none of the upstreamed LSMs define a
kernel_module_from_filehook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules.The security function
security_kernel_module_from_file()was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-readkernel_read_file()security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to thekernel_post_read_file()security hook.
Quoted 4 lines
This patch removes the
security_kernel_module_from_file()hook and security call.Signed-off-by Mimi Zohar <[email protected]>
Luis
Re: [PATCH v3 16/22] module: replace copy_module_from_fd with kernel version
Quoted 16 lines · Luis R. Rodriguez
Quoted 15 lines · Mimi Zohar
Replace
copy_module_from_fd()withkernel_read_file_from_fd().Although none of the upstreamed LSMs define a
kernel_module_from_filehook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules.The security function
security_kernel_module_from_file()was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-readkernel_read_file()security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to thekernel_post_read_file()security hook.
Quoted 6 lines
Quoted 4 lines
This patch removes the
security_kernel_module_from_file()hook and security call.Signed-off-by Mimi Zohar <[email protected]>
Thank you for reviewing the patches!
Mimi
PATCH v3 17/22 ima: remove firmware and module specific cached status info
In reply to Mimi ZoharEach time a file is read by the kernel, the file should be re-measured and the file signature re-appraised, based on policy. As there is no need to preserve the status information, this patch replaces the firmware and module specific cache status with a generic one named read_file.
This change simplifies adding support for other files read by the kernel.
--- security/integrity/iint.c | 4 ++-- security/integrity/ima/ima.h | 3 ++- security/integrity/ima/ima_appraise.c | 35 ++++++++++++++++------------------- security/integrity/ima/ima_policy.c | 9 ++++----- security/integrity/integrity.h | 16 ++++------------ 5 files changed, 28 insertions(+), 39 deletions(-)
security/integrity/iint.c +2 -2
@@ -77,7 +77,7 @@ static void iint_free(struct integrity_iint_cache *iint) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 ;kmem_cache_free (iint_cache, iint); }
@@ -157,7 +157,7 @@ static void init_once(void *foo) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 +2 -1
@@ -144,9 +144,10 @@ enum ima_hooks {FILE_CHECK =1 ,MMAP_CHECK ,BPRM_CHECK , +POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , - POST_SETATTR + MAX_CHECK };/* LIM API function definitions */
security/integrity/ima/ima_appraise.c +16 -19
@@ -74,13 +74,12 @@ 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 FIRMWARE_CHECK : -return iint->ima_firmware_status;case FILE_CHECK : -default : +case POST_SETATTR :return iint->ima_file_status; + case MODULE_CHECK ... MAX_CHECK - 1: +default : +return iint->ima_read_status; } }
@@ -95,15 +94,14 @@ 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 FIRMWARE_CHECK : - iint->ima_firmware_status = status; -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->ima_file_status = status; +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->ima_read_status = status; +break ; } }
@@ -117,15 +115,14 @@ static void ima_cache_flags(struct integrity_iint_cache *iint,case BPRM_CHECK : iint->flags |= (IMA_BPRM_APPRAISED |IMA_APPRAISED );break ; -case MODULE_CHECK : - iint->flags |= (IMA_MODULE_APPRAISED |IMA_APPRAISED ); -break ; -case FIRMWARE_CHECK : - iint->flags |= (IMA_FIRMWARE_APPRAISED |IMA_APPRAISED ); -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->flags |= (IMA_READ_APPRAISED |IMA_APPRAISED ); +break ; } }
security/integrity/ima/ima_policy.c +4 -5
@@ -300,13 +300,12 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)return IMA_MMAP_APPRAISE ;case BPRM_CHECK :return IMA_BPRM_APPRAISE ; -case MODULE_CHECK : -return IMA_MODULE_APPRAISE ; -case FIRMWARE_CHECK : -return IMA_FIRMWARE_APPRAISE ;case FILE_CHECK : -default : +case POST_SETATTR :return IMA_FILE_APPRAISE ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : +return IMA_READ_APPRAISE ; } }
security/integrity/integrity.h +4 -12
@@ -45,18 +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_FIRMWARE_APPRAISE 0x00010000 -#define IMA_FIRMWARE_APPRAISED 0x00020000 -#define IMA_READ_APPRAISE 0x00040000 -#define IMA_READ_APPRAISED 0x00080000 +#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_FIRMWARE_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_FIRMWARE_APPRAISED | IMA_READ_APPRAISED) + IMA_BPRM_APPRAISED | IMA_READ_APPRAISED)enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,
@@ -111,8 +105,6 @@ struct integrity_iint_cache { enum integrity_status ima_file_status:4; 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;
Signature · 1 line
2.1.0
Re: [PATCH v3 17/22] ima: remove firmware and module specific cached status info
Quoted 8 lines · Mimi Zohar
Each time a file is read by the kernel, the file should be re-measured and the file signature re-appraised, based on policy. As there is no need to preserve the status information, this patch replaces the firmware and module specific cache status with a generic one named
read_file.This change simplifies adding support for other files read by the kernel.
Signed-off-by Mimi Zohar <[email protected]>
Quoted 57 lines
--- security/integrity/iint.c | 4 ++-- security/integrity/ima/ima.h | 3 ++- security/integrity/ima/ima_appraise.c | 35 ++++++++++++++++------------------- security/integrity/ima/ima_policy.c | 9 ++++----- security/integrity/integrity.h | 16 ++++------------ 5 files changed, 28 insertions(+), 39 deletions(-)security/integrity/iint.c +2
-2@@ -77,7 +77,7 @@ static void iint_free(struct integrity_iint_cache *iint) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 ;kmem_cache_free (iint_cache, iint); }@@ -157,7 +157,7 @@ static void init_once(void *foo) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 +2
-1@@ -144,9 +144,10 @@ enum ima_hooks {FILE_CHECK =1 ,MMAP_CHECK ,BPRM_CHECK , +POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , - POST_SETATTR + MAX_CHECK };/* LIM API function definitions */ security/integrity/ima/ima_appraise.c +0
-3@@ -74,13 +74,12 @@ 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 FIRMWARE_CHECK :
security/integrity/ima/ima_appraise.c enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, · line 84
Quoted 10 lines
security/integrity/ima/ima_appraise.c +4
-2-return iint->ima_firmware_status;case FILE_CHECK : -default : +case POST_SETATTR :return iint->ima_file_status; + case MODULE_CHECK ... MAX_CHECK - 1: +default : +return iint->ima_read_status; } }
I assume you used "case MODULE_CHECK ... MAX_CHECK - 1:" for readability?
Quoted 109 lines
security/integrity/ima/ima_appraise.c +12
-14@@ -95,15 +94,14 @@ 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 FIRMWARE_CHECK : - iint->ima_firmware_status = status; -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->ima_file_status = status; +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->ima_read_status = status; +break ; } }@@ -117,15 +115,14 @@ static void ima_cache_flags(struct integrity_iint_cache *iint,case BPRM_CHECK : iint->flags |= (IMA_BPRM_APPRAISED |IMA_APPRAISED );break ; -case MODULE_CHECK : - iint->flags |= (IMA_MODULE_APPRAISED |IMA_APPRAISED ); -break ; -case FIRMWARE_CHECK : - iint->flags |= (IMA_FIRMWARE_APPRAISED |IMA_APPRAISED ); -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->flags |= (IMA_READ_APPRAISED |IMA_APPRAISED ); +break ; } }security/integrity/ima/ima_policy.c +4
-5@@ -300,13 +300,12 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)return IMA_MMAP_APPRAISE ;case BPRM_CHECK :return IMA_BPRM_APPRAISE ; -case MODULE_CHECK : -return IMA_MODULE_APPRAISE ; -case FIRMWARE_CHECK : -return IMA_FIRMWARE_APPRAISE ;case FILE_CHECK : -default : +case POST_SETATTR :return IMA_FILE_APPRAISE ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : +return IMA_READ_APPRAISE ; } }security/integrity/integrity.h +4
-12@@ -45,18 +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_FIRMWARE_APPRAISE 0x00010000 -#define IMA_FIRMWARE_APPRAISED 0x00020000 -#define IMA_READ_APPRAISE 0x00040000 -#define IMA_READ_APPRAISED 0x00080000 +#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_FIRMWARE_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_FIRMWARE_APPRAISED | IMA_READ_APPRAISED) + IMA_BPRM_APPRAISED | IMA_READ_APPRAISED)enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,@@ -111,8 +105,6 @@ struct integrity_iint_cache { enum integrity_status ima_file_status:4; 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;Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Dmitry Kasatkin
maintainerRe: [PATCH v3 17/22] ima: remove firmware and module specific cached status info
In reply to Mimi ZoharQuoted 61 lines · Mimi Zohar
Each time a file is read by the kernel, the file should be re-measured and the file signature re-appraised, based on policy. As there is no need to preserve the status information, this patch replaces the firmware and module specific cache status with a generic one named
read_file.This change simplifies adding support for other files read by the kernel.
Signed-off-by Mimi Zohar <[email protected]>--- security/integrity/iint.c | 4 ++-- security/integrity/ima/ima.h | 3 ++- security/integrity/ima/ima_appraise.c | 35 ++++++++++++++++------------------- security/integrity/ima/ima_policy.c | 9 ++++----- security/integrity/integrity.h | 16 ++++------------ 5 files changed, 28 insertions(+), 39 deletions(-)security/integrity/iint.c +2
-2@@ -77,7 +77,7 @@ static void iint_free(struct integrity_iint_cache *iint) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 ;kmem_cache_free (iint_cache, iint); }@@ -157,7 +157,7 @@ static void init_once(void *foo) iint->ima_file_status =INTEGRITY_UNKNOWN ; 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 +2
-1@@ -144,9 +144,10 @@ enum ima_hooks {FILE_CHECK =1 ,MMAP_CHECK ,BPRM_CHECK , +POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , - POST_SETATTR + MAX_CHECK };/* LIM API function definitions */ security/integrity/ima/ima_appraise.c +0
-0@@ -74,13 +74,12 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,return iint->ima_mmap_status;case BPRM_CHECK :
security/integrity/ima/ima_appraise.c enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, · line 80
Quoted 10 lines
security/integrity/ima/ima_appraise.c +2
-5return iint->ima_bprm_status; -case MODULE_CHECK : -return iint->ima_module_status; -case FIRMWARE_CHECK : -return iint->ima_firmware_status;case FILE_CHECK : -default : +case POST_SETATTR :return iint->ima_file_status; + case MODULE_CHECK ... MAX_CHECK - 1:
Will LLVM clang handles this range?
Otherwise it can be just like:
case MODULE_CHECK ... MAX_CHECK :
Quoted 110 lines
security/integrity/ima/ima_appraise.c +14
-14+default : +return iint->ima_read_status; } }@@ -95,15 +94,14 @@ 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 FIRMWARE_CHECK : - iint->ima_firmware_status = status; -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->ima_file_status = status; +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->ima_read_status = status; +break ; } }@@ -117,15 +115,14 @@ static void ima_cache_flags(struct integrity_iint_cache *iint,case BPRM_CHECK : iint->flags |= (IMA_BPRM_APPRAISED |IMA_APPRAISED );break ; -case MODULE_CHECK : - iint->flags |= (IMA_MODULE_APPRAISED |IMA_APPRAISED ); -break ; -case FIRMWARE_CHECK : - iint->flags |= (IMA_FIRMWARE_APPRAISED |IMA_APPRAISED ); -break ;case FILE_CHECK : -default : +case POST_SETATTR : iint->flags |= (IMA_FILE_APPRAISED |IMA_APPRAISED ); +break ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : + iint->flags |= (IMA_READ_APPRAISED |IMA_APPRAISED ); +break ; } }security/integrity/ima/ima_policy.c +4
-5@@ -300,13 +300,12 @@ static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)return IMA_MMAP_APPRAISE ;case BPRM_CHECK :return IMA_BPRM_APPRAISE ; -case MODULE_CHECK : -return IMA_MODULE_APPRAISE ; -case FIRMWARE_CHECK : -return IMA_FIRMWARE_APPRAISE ;case FILE_CHECK : -default : +case POST_SETATTR :return IMA_FILE_APPRAISE ; + case MODULE_CHECK ... MAX_CHECK - 1: +default : +return IMA_READ_APPRAISE ; } }security/integrity/integrity.h +4
-13@@ -45,18 +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_FIRMWARE_APPRAISE 0x00010000 -#define IMA_FIRMWARE_APPRAISED 0x00020000 -#define IMA_READ_APPRAISE 0x00040000 -#define IMA_READ_APPRAISED 0x00080000 +#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_FIRMWARE_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_FIRMWARE_APPRAISED | IMA_READ_APPRAISED) + IMA_BPRM_APPRAISED | IMA_READ_APPRAISED)enum evm_ima_xattr_type {IMA_XATTR_DIGEST =0x01 ,@@ -111,8 +105,6 @@ struct integrity_iint_cache { enum integrity_status ima_file_status:4; 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; --2.1.0
Signature · 2 lines
Thanks, Dmitry
Re: [PATCH v3 17/22] ima: remove firmware and module specific cached status info
Quoted 20 lines · Dmitry Kasatkin
Quoted 7 lines
security/integrity/ima/ima_appraise.c +0
-0@@ -74,13 +74,12 @@ enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,return iint->ima_mmap_status;case BPRM_CHECK :security/integrity/ima/ima_appraise.c enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, · line 80
Quoted 10 lines
security/integrity/ima/ima_appraise.c +2
-5return iint->ima_bprm_status; -case MODULE_CHECK : -return iint->ima_module_status; -case FIRMWARE_CHECK : -return iint->ima_firmware_status;case FILE_CHECK : -default : +case POST_SETATTR :return iint->ima_file_status; + case MODULE_CHECK ... MAX_CHECK - 1:Will LLVM clang handles this range?
Quoted 3 lines
Otherwise it can be just like:
case
MODULE_CHECK...MAX_CHECK:
Yes, my test program compiled fine with clang. Similar usage exists in the kernel (eg. fs/afs/callback.c).
Mimi
PATCH v3 18/22 kexec: replace call to copy_file_from_fd() with kernel version
In reply to Mimi ZoharReplace copy_file_from_fd() with kernel_read_file_from_fd().
Two new identifiers named READING_KEXEC_IMAGE and READING_KEXEC_INITRAMFS are defined for measuring, appraising or auditing the kexec image and initramfs.
Changelog v3:
- return -EBADF, not -ENOEXEC
- identifier change
- moved copy_file_from_fd() to a separate patch
- defer support for IMA
v1:
- re-order and squash the kexec patches
v0: 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
- 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
- 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.
--- include/linux/fs.h | 2 ++ kernel/kexec_file.c | 73 +++++++---------------------------------------------- 2 files changed, 11 insertions(+), 64 deletions(-)
include/linux/fs.h +2 -0
@@ -2529,6 +2529,8 @@ extern int do_pipe_flags(int *, int);enum kernel_read_file_id {READING_FIRMWARE =1 ,READING_MODULE , +READING_KEXEC_IMAGE , +READING_KEXEC_INITRAMFS ,READING_MAX_ID };
kernel/kexec_file.c +9 -64
106 lines not on this page · raw message
Signature · 1 line
2.1.0
Re: [PATCH v3 18/22] kexec: replace call to copy_file_from_fd() with kernel version
Quoted 27 lines · Mimi Zohar
Replace
copy_file_from_fd()withkernel_read_file_from_fd().Two new identifiers named
READING_KEXEC_IMAGEandREADING_KEXEC_INITRAMFSare defined for measuring, appraising or auditing the kexec image and initramfs.Changelog v3:
- return-EBADF, not-ENOEXEC
- identifier change
- movedcopy_file_from_fd()to a separate patch
- defer support for IMA
v1:
- re-order and squash the kexec patches
v0: ima: measure and appraise kexec image and initramfs (squashed)
- renameima_read_hooksenumeration toima_policy_id
- use kstat file size typeloff_t, notsize_t
- add union name "hooks" to fix sparse warning
- Calculate the file hash from the in memory buffer (suggested by Dave Young)
- Renameima_read_and_process_file()toima_hash_and_process_file()
- replace individual case statements with range:
KEXEC_CHECK...IMA_MAX_READ_CHECK- 1
- Instead ofima_read_and_process_file()allocating memory, the caller allocates and frees the memory.
- Moved the kexec measurement/appraisal call tocopy_file_from_fd(). The same call now measures and appraises both the kexec image and initramfs.
Quoted 1 line
Signed-off-by Mimi Zohar <[email protected]>
-Kees
Quoted 135 lines
--- include/linux/fs.h | 2 ++ kernel/kexec_file.c | 73 +++++++---------------------------------------------- 2 files changed, 11 insertions(+), 64 deletions(-)include/linux/fs.h +2
-0@@ -2529,6 +2529,8 @@ extern int do_pipe_flags(int *, int);enum kernel_read_file_id {READING_FIRMWARE =1 ,READING_MODULE , +READING_KEXEC_IMAGE , +READING_KEXEC_INITRAMFS ,READING_MAX_ID };kernel/kexec_file.c +9
-65@@ -18,6 +18,7 @@#include <linux/kexec.h> #include <linux/mutex.h> #include <linux/list.h> +#include <linux/fs.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 ,READING_KEXEC_IMAGE );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,12 @@ 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 , +READING_KEXEC_INITRAMFS );if (ret) goto out; + image->initrd_buf_len = size; }if (cmdline_len) { --2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v3 18/22] kexec: replace call to copy_file_from_fd() with kernel version
In reply to Mimi ZoharQuoted 27 lines · Mimi Zohar
Replace
copy_file_from_fd()withkernel_read_file_from_fd().Two new identifiers named
READING_KEXEC_IMAGEandREADING_KEXEC_INITRAMFSare defined for measuring, appraising or auditing the kexec image and initramfs.Changelog v3:
- return-EBADF, not-ENOEXEC
- identifier change
- movedcopy_file_from_fd()to a separate patch
- defer support for IMA
v1:
- re-order and squash the kexec patches
v0: ima: measure and appraise kexec image and initramfs (squashed)
- renameima_read_hooksenumeration toima_policy_id
- use kstat file size typeloff_t, notsize_t
- add union name "hooks" to fix sparse warning
- Calculate the file hash from the in memory buffer (suggested by Dave Young)
- Renameima_read_and_process_file()toima_hash_and_process_file()
- replace individual case statements with range:
KEXEC_CHECK...IMA_MAX_READ_CHECK- 1
- Instead ofima_read_and_process_file()allocating memory, the caller allocates and frees the memory.
- Moved the kexec measurement/appraisal call tocopy_file_from_fd(). The same call now measures and appraises both the kexec image and initramfs.
Quoted 1 line
Signed-off-by Mimi Zohar <[email protected]>
Luis
Re: [PATCH v3 18/22] kexec: replace call to copy_file_from_fd() with kernel version
In reply to Mimi ZoharQuoted 150 lines · Mimi Zohar
Replace
copy_file_from_fd()withkernel_read_file_from_fd().Two new identifiers named
READING_KEXEC_IMAGEandREADING_KEXEC_INITRAMFSare defined for measuring, appraising or auditing the kexec image and initramfs.Changelog v3:
- return-EBADF, not-ENOEXEC
- identifier change
- movedcopy_file_from_fd()to a separate patch
- defer support for IMA
v1:
- re-order and squash the kexec patches
v0: ima: measure and appraise kexec image and initramfs (squashed)
- renameima_read_hooksenumeration toima_policy_id
- use kstat file size typeloff_t, notsize_t
- add union name "hooks" to fix sparse warning
- Calculate the file hash from the in memory buffer (suggested by Dave Young)
- Renameima_read_and_process_file()toima_hash_and_process_file()
- replace individual case statements with range:
KEXEC_CHECK...IMA_MAX_READ_CHECK- 1
- Instead ofima_read_and_process_file()allocating memory, the caller allocates and frees the memory.
- Moved the kexec measurement/appraisal call tocopy_file_from_fd(). The same call now measures and appraises both the kexec image and initramfs.Signed-off-by Mimi Zohar <[email protected]>--- include/linux/fs.h | 2 ++ kernel/kexec_file.c | 73 +++++++---------------------------------------------- 2 files changed, 11 insertions(+), 64 deletions(-)include/linux/fs.h +2
-0@@ -2529,6 +2529,8 @@ extern int do_pipe_flags(int *, int);enum kernel_read_file_id {READING_FIRMWARE =1 ,READING_MODULE , +READING_KEXEC_IMAGE , +READING_KEXEC_INITRAMFS ,READING_MAX_ID };kernel/kexec_file.c +5
-63@@ -18,6 +18,7 @@#include <linux/kexec.h> #include <linux/mutex.h> #include <linux/list.h> +#include <linux/fs.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 ,READING_KEXEC_IMAGE );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,12 @@ 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,
kernel/kexec_file.c kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, · line 158
Quoted 10 lines
kernel/kexec_file.c +4
-1- &image->initrd_buf_len); + ret =kernel_read_file_from_fd (initrd_fd, &image->initrd_buf, + &size,INT_MAX , +READING_KEXEC_INITRAMFS );if (ret) goto out; + image->initrd_buf_len = size; }if (cmdline_len) {
Thanks
Dave
PATCH v3 19/22 ima: support for kexec image and initramfs
In reply to Mimi ZoharAdd IMA policy support for measuring/appraising the kexec image and initramfs.
Moving the enumeration to the vfs layer simplified the patches, allowing the IMA changes, for the most part, to be separated from the other changes. Unfortunately, passing either a kernel_read_file_id or a ima_hooks enumeration within IMA is messy.
Option 1: duplicate kernel_read_file enumeration in ima_hooks
enum kernel_read_file_id {
...
READING_KEXEC_IMAGE,
READING_KEXEC_INITRAMFS,
READING_MAX_IDenum ima_hooks {
...
KEXEC_CHECK
INITRAMFS_CHECKOption 2: define ima_hooks as extension of kernel_read_file
eg: enum ima_hooks {
FILE_CHECK = READING_MAX_ID,
MMAP_CHECK,
In order to pass both kernel_read_file_id and ima_hooks values, we would need to specify a struct containing a union.
struct caller_id {
union {
enum ima_hooks func_id;
enum kernel_read_file_id read_id;
};
};Option 3: incorportate the ima_hooks enumeration into kernel_read_file_id, perhaps changing the enumeration name.
For now, duplicate the new READING_KEXEC_IMAGE/INITRAMFS in ima_hooks.
--- Documentation/ABI/testing/ima_policy | 2 +- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_main.c | 19 ++++++++++++++++--- security/integrity/ima/ima_policy.c | 13 ++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
@@ -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
security/integrity/ima/ima.h +2 -0
@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , +INITRAMFS_CHECK , MAX_CHECK };
security/integrity/ima/ima_main.c +16 -3
@@ -355,7 +355,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id read_id) { -enum ima_hooks func =FILE_CHECK ; +enum ima_hooks func;if (!file && read_id ==READING_FIRMWARE ) { if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
@@ -373,10 +373,23 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } -if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ; +case READING_KEXEC_IMAGE : + func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }return process_measurement (file, buf, size,MAY_READ , func,0 ); }
security/integrity/ima/ima_policy.c +12 -1
@@ -612,6 +612,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =MMAP_CHECK ; else if (strcmp(args[0].from, "BPRM_CHECK") == 0) entry->func = BPRM_CHECK; + else if (strcmp(args[0].from, "KEXEC_CHECK") == 0) + entry->func = KEXEC_CHECK; + else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) + entry->func = INITRAMFS_CHECK; else result = -EINVAL ;if (!result)
@@ -855,7 +859,8 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, - func_module, func_firmware, func_post + func_module, func_firmware, func_post, + func_kexec, func_initramfs };static char *func_tokens[] = {
@@ -929,6 +934,12 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case POST_SETATTR :seq_printf (m,pt (Opt_func),ft (func_post));break ; +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" , func);seq_printf (m,pt (Opt_func), tbuf);
Signature · 1 line
2.1.0
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 36 lines · Mimi Zohar
Add IMA policy support for measuring/appraising the kexec image and initramfs.
Moving the enumeration to the vfs layer simplified the patches, allowing the IMA changes, for the most part, to be separated from the other changes. Unfortunately, passing either a
kernel_read_file_idor aima_hooksenumeration within IMA is messy.Option 1: duplicate
kernel_read_fileenumeration inima_hooksenum kernel_read_file_id { ... READING_KEXEC_IMAGE, READING_KEXEC_INITRAMFS, READING_MAX_IDenum ima_hooks { ... KEXEC_CHECK INITRAMFS_CHECKOption 2: define
ima_hooksas extension ofkernel_read_file
eg: enumima_hooks{
FILE_CHECK=READING_MAX_ID,
MMAP_CHECK,In order to pass both
kernel_read_file_idandima_hooksvalues, we would need to specify a struct containing a union.struct caller_id { union { enum ima_hooks func_id; enum kernel_read_file_id read_id; }; };
Quoted 6 lines
Option 3: incorportate the
ima_hooksenumeration intokernel_read_file_id, perhaps changing the enumeration name.For now, duplicate the new READING_KEXEC_IMAGE/INITRAMFS in
ima_hooks.Signed-off-by Mimi Zohar <[email protected]>
Option 3 seems the right way to go, but for now:
Quoted 117 lines
--- Documentation/ABI/testing/ima_policy | 2 +- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_main.c | 19 ++++++++++++++++--- security/integrity/ima/ima_policy.c | 13 ++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-)Documentation/ABI/testing/ima_policy +1
-1@@ -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 valuesecurity/integrity/ima/ima.h +2
-0@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , +INITRAMFS_CHECK , MAX_CHECK };security/integrity/ima/ima_main.c +16
-3@@ -355,7 +355,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id read_id) { -enum ima_hooks func =FILE_CHECK ; +enum ima_hooks func;if (!file && read_id ==READING_FIRMWARE ) { if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&@@ -373,10 +373,23 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } -if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ; +case READING_KEXEC_IMAGE : + func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }return process_measurement (file, buf, size,MAY_READ , func,0 ); }security/integrity/ima/ima_policy.c +12
-1@@ -612,6 +612,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =MMAP_CHECK ; else if (strcmp(args[0].from, "BPRM_CHECK") == 0) entry->func = BPRM_CHECK; + else if (strcmp(args[0].from, "KEXEC_CHECK") == 0) + entry->func = KEXEC_CHECK; + else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) + entry->func = INITRAMFS_CHECK; else result = -EINVAL ;if (!result)@@ -855,7 +859,8 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, - func_module, func_firmware, func_post + func_module, func_firmware, func_post, + func_kexec, func_initramfs };static char *func_tokens[] = {@@ -929,6 +934,12 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case POST_SETATTR :seq_printf (m,pt (Opt_func),ft (func_post));break ; +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" , func);seq_printf (m,pt (Opt_func), tbuf);Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 10 lines · Petko Manolov
Quoted 6 lines · Mimi Zohar
Option 3: incorportate the
ima_hooksenumeration intokernel_read_file_id, perhaps changing the enumeration name.For now, duplicate the new READING_KEXEC_IMAGE/INITRAMFS in
ima_hooks.Signed-off-by Mimi Zohar <[email protected]>Option 3 seems the right way to go, but for now:
I'm just not sure. The kernel_read_file_id enumeration is in <linux/fs.h>.
Quoted 1 line
Acked-by Petko Manolov <[email protected]>
Thank you.
Mimi
Dmitry Kasatkin
maintainerRe: [PATCH v3 19/22] ima: support for kexec image and initramfs
In reply to Mimi ZoharQuoted 102 lines · Mimi Zohar
Add IMA policy support for measuring/appraising the kexec image and initramfs.
Moving the enumeration to the vfs layer simplified the patches, allowing the IMA changes, for the most part, to be separated from the other changes. Unfortunately, passing either a
kernel_read_file_idor aima_hooksenumeration within IMA is messy.Option 1: duplicate
kernel_read_fileenumeration inima_hooksenum kernel_read_file_id { ... READING_KEXEC_IMAGE, READING_KEXEC_INITRAMFS, READING_MAX_IDenum ima_hooks { ... KEXEC_CHECK INITRAMFS_CHECKOption 2: define
ima_hooksas extension ofkernel_read_file
eg: enumima_hooks{
FILE_CHECK=READING_MAX_ID,
MMAP_CHECK,In order to pass both
kernel_read_file_idandima_hooksvalues, we would need to specify a struct containing a union.struct caller_id { union { enum ima_hooks func_id; enum kernel_read_file_id read_id; }; };Option 3: incorportate the
ima_hooksenumeration intokernel_read_file_id, perhaps changing the enumeration name.For now, duplicate the new READING_KEXEC_IMAGE/INITRAMFS in
ima_hooks.Signed-off-by Mimi Zohar <[email protected]>--- Documentation/ABI/testing/ima_policy | 2 +- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_main.c | 19 ++++++++++++++++--- security/integrity/ima/ima_policy.c | 13 ++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-)Documentation/ABI/testing/ima_policy +1
-1@@ -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 valuesecurity/integrity/ima/ima.h +2
-0@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , +INITRAMFS_CHECK , MAX_CHECK };security/integrity/ima/ima_main.c +7
-3@@ -355,7 +355,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id read_id) { -enum ima_hooks func =FILE_CHECK ; +enum ima_hooks func;if (!file && read_id ==READING_FIRMWARE ) { if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&@@ -373,10 +373,23 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } -if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ; +case READING_KEXEC_IMAGE :
security/integrity/ima/ima_main.c int ima_post_read_file(struct file *file, void *buf, loff_t size, · line 393
Quoted 10 lines
security/integrity/ima/ima_main.c +9
-0+ func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }
I would define a separate function like "int ima_read_id_to_func(id)" which search over the map
Something like...
struct
{
int id;
int func;
} map[] = { { .id = READING_FIRMWARE, .fun = FIRMWARE_CHECK },
...
{ -1, 0 }
};
Dmitry
Quoted 43 lines
security/integrity/ima/ima_main.c +0
-0return process_measurement (file, buf, size,MAY_READ , func,0 ); }security/integrity/ima/ima_policy.c +12
-2@@ -612,6 +612,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =MMAP_CHECK ; else if (strcmp(args[0].from, "BPRM_CHECK") == 0) entry->func = BPRM_CHECK; + else if (strcmp(args[0].from, "KEXEC_CHECK") == 0) + entry->func = KEXEC_CHECK; + else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) + entry->func = INITRAMFS_CHECK; else result = -EINVAL ;if (!result)@@ -855,7 +859,8 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, - func_module, func_firmware, func_post + func_module, func_firmware, func_post, + func_kexec, func_initramfs };static char *func_tokens[] = {@@ -929,6 +934,12 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case POST_SETATTR :seq_printf (m,pt (Opt_func),ft (func_post));break ; +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" , func);seq_printf (m,pt (Opt_func), tbuf); --2.1.0
Signature · 2 lines
Thanks, Dmitry
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 120 lines · Dmitry Kasatkin
Quoted 102 lines · Mimi Zohar
Add IMA policy support for measuring/appraising the kexec image and initramfs.
Moving the enumeration to the vfs layer simplified the patches, allowing the IMA changes, for the most part, to be separated from the other changes. Unfortunately, passing either a
kernel_read_file_idor aima_hooksenumeration within IMA is messy.Option 1: duplicate
kernel_read_fileenumeration inima_hooksenum kernel_read_file_id { ... READING_KEXEC_IMAGE, READING_KEXEC_INITRAMFS, READING_MAX_IDenum ima_hooks { ... KEXEC_CHECK INITRAMFS_CHECKOption 2: define
ima_hooksas extension ofkernel_read_file
eg: enumima_hooks{
FILE_CHECK=READING_MAX_ID,
MMAP_CHECK,In order to pass both
kernel_read_file_idandima_hooksvalues, we would need to specify a struct containing a union.struct caller_id { union { enum ima_hooks func_id; enum kernel_read_file_id read_id; }; };Option 3: incorportate the
ima_hooksenumeration intokernel_read_file_id, perhaps changing the enumeration name.For now, duplicate the new READING_KEXEC_IMAGE/INITRAMFS in
ima_hooks.Signed-off-by Mimi Zohar <[email protected]>--- Documentation/ABI/testing/ima_policy | 2 +- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_main.c | 19 ++++++++++++++++--- security/integrity/ima/ima_policy.c | 13 ++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-)Documentation/ABI/testing/ima_policy +1
-1@@ -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 valuesecurity/integrity/ima/ima.h +2
-0@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , +INITRAMFS_CHECK , MAX_CHECK };security/integrity/ima/ima_main.c +7
-3@@ -355,7 +355,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)int ima_post_read_file (struct file *file,void *buf,loff_t size,enum kernel_read_file_id read_id) { -enum ima_hooks func =FILE_CHECK ; +enum ima_hooks func;if (!file && read_id ==READING_FIRMWARE ) { if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&@@ -373,10 +373,23 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,return 0 ; } -if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ; +case READING_KEXEC_IMAGE :security/integrity/ima/ima_main.c int ima_post_read_file(struct file *file, void *buf, loff_t size, · line 393
Quoted 10 lines
security/integrity/ima/ima_main.c +9
-0+ func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }I would define a separate function like "int
ima_read_id_to_func(id)" which search over the mapSomething like...
struct
Quoted 9 lines
{ int id; int func; } map[] = {{ .id =
READING_FIRMWARE, .fun =FIRMWARE_CHECK},
...
{ -1, 0 }
};
So we stay with the duplication (option 1), but clean it up. That works for me.
Mimi
Quoted 46 lines
Quoted 43 lines
security/integrity/ima/ima_main.c +0
-0return process_measurement (file, buf, size,MAY_READ , func,0 ); }security/integrity/ima/ima_policy.c +12
-2@@ -612,6 +612,10 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =MMAP_CHECK ; else if (strcmp(args[0].from, "BPRM_CHECK") == 0) entry->func = BPRM_CHECK; + else if (strcmp(args[0].from, "KEXEC_CHECK") == 0) + entry->func = KEXEC_CHECK; + else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) + entry->func = INITRAMFS_CHECK; else result = -EINVAL ;if (!result)@@ -855,7 +859,8 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, - func_module, func_firmware, func_post + func_module, func_firmware, func_post, + func_kexec, func_initramfs };static char *func_tokens[] = {@@ -929,6 +934,12 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case POST_SETATTR :seq_printf (m,pt (Opt_func),ft (func_post));break ; +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" , func);seq_printf (m,pt (Opt_func), tbuf); --2.1.0
A message that is not in the archive (sent off-list or not yet received): CACE9dm8OJ1cgbKszUG-pCiEMVarUFLLWi_jewVV-JEMGAJsA-g@mail.gmail.com
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 44 lines · Dmitry Kasatkin
Quoted 3 lines · Mimi Zohar
Quoted 1 line · Dmitry Kasatkin
On Wed, Feb 3, 2016 at 9:06 PM, Mimi Zohar <[email protected]>
Quoted 38 lines · wrote:
Quoted 37 lines
Quoted 11 lines
security/integrity/ima/ima_main.c +5
-2-if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ;security/integrity/ima/ima_main.c int ima_post_read_file(struct file *file, void *buf, loff_t size, · line 392
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+case READING_KEXEC_IMAGE : + func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }I would define a separate function like "int
ima_read_id_to_func(id)" which search over the mapSomething like...
struct
{ int id; int func; } map[] = {{ .id =
READING_FIRMWARE, .fun =FIRMWARE_CHECK},
...
{ -1, 0 }
};
Quoted 7 lines
Quoted 3 lines
So we stay with the duplication (option 1), but clean it up. That works for me.
Actually it may be simpler.
Just define int idmap[MAX_ID] and assign to every id corresponding func. It will be quick and simple.
Unlike the ima_read_id_to_func() above or the original switch/case statement, this method assumes the kernel_read_file_id enumeration stays in sync with ima_hooks. In terms of the ima_read_id_to_func() function, it would iterate over the map[] to find the corresponding .id, whereas the current switch/case is a direct lookup.
Perhaps we should defer making a change for now.
Mimi
Dmitry Kasatkin
maintainerRe: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 49 lines · Mimi Zohar
Quoted 48 lines · Dmitry Kasatkin
Quoted 3 lines · Mimi Zohar
Quoted 1 line · Dmitry Kasatkin
On Wed, Feb 3, 2016 at 9:06 PM, Mimi Zohar <[email protected]>
Quoted 41 lines · wrote:
Quoted 28 lines
Quoted 11 lines
security/integrity/ima/ima_main.c +5
-2-if (read_id ==READING_FIRMWARE ) +switch (read_id) { +case READING_FIRMWARE : func =FIRMWARE_CHECK ; -else if (read_id ==READING_MODULE ) +break ; +case READING_MODULE : func =MODULE_CHECK ; +break ;security/integrity/ima/ima_main.c int ima_post_read_file(struct file *file, void *buf, loff_t size, · line 392
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+case READING_KEXEC_IMAGE : + func =KEXEC_CHECK ; +break ; +case READING_KEXEC_INITRAMFS : + func =INITRAMFS_CHECK ; +break ; +default : + func =FILE_CHECK ; +break ; + }I would define a separate function like "int
ima_read_id_to_func(id)" which search over the mapSomething like...
struct
Quoted 9 lines
{ int id; int func; } map[] = {{ .id =
READING_FIRMWARE, .fun =FIRMWARE_CHECK},
...
{ -1, 0 }
};So we stay with the duplication (option 1), but clean it up. That works for me.
Quoted 7 lines
Quoted 3 lines
Actually it may be simpler.
Just define int idmap[MAX_ID] and assign to every id corresponding func. It will be quick and simple.Unlike the
ima_read_id_to_func()above or the original switch/case statement, this method assumes thekernel_read_file_idenumeration stays in sync withima_hooks.
Actually not necessary. You can use array initialization by index, then you do not need to worry about sync...
static int idmap[] = {
[READING_FIRMWARE] = FIRMWARE_CHECK,
[READING_MODULE] = MODULE_CHECK,
...
};
Quoted 4 lines
In terms of the
ima_read_id_to_func()
function, it would iterate over the map[] to find the corresponding .id, whereas the current switch/case is a direct lookup.
switch is also iteration.
Dmitry
Actually
Quoted 4 lines
Perhaps we should defer making a change for now.
Mimi
Signature · 2 lines
Thanks, Dmitry
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 7 lines · Dmitry Kasatkin
On Thu, Feb 11, 2016 at 4:08 AM, Mimi Zohar <[email protected]> wrote:
static int idmap[] = {
[READING_FIRMWARE] =FIRMWARE_CHECK,
[READING_MODULE] =MODULE_CHECK,
...
};
That works nicely, even with non-sequential ordering or missing entries. The switch case be replaced with a single line:
func = read_idmap[read_id] == 0 ? FILE_CHECK : read_idmap[read_id];
Thank you!
Mimi
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
In reply to Mimi ZoharHi, Mimi
security/integrity/ima/ima.h enum ima_hooks { · line 151
Quoted 10 lines
security/integrity/ima/ima.h +2
-0@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , + INITRAMFS_CHECK,
KEXEC_CHECK is actually kexec kernel image check, INITRAMFS_CHECK is for kexec initramfs check, they are both for checking kexec loaded files.
Do you mind a longer id like KEXEC_KERNEL_CHECK and KEXEC_INITRAMFS_CHECK?
security/integrity/ima/ima.h enum ima_hooks { · line 154
Quoted 3 lines
security/integrity/ima/ima.h +0
-0MAX_CHECK };
Thanks
Dave
Re: [PATCH v3 19/22] ima: support for kexec image and initramfs
Quoted 16 lines · Dave Young
Hi, Mimi
security/integrity/ima/ima.h enum ima_hooks { · line 151
Quoted 10 lines
security/integrity/ima/ima.h +2
-0@@ -147,6 +147,8 @@ enum ima_hooks {POST_SETATTR ,MODULE_CHECK ,FIRMWARE_CHECK , +KEXEC_CHECK , + INITRAMFS_CHECK,
KEXEC_CHECKis actually kexec kernel image check,INITRAMFS_CHECKis for kexec initramfs check, they are both for checking kexec loaded files.
Quoted 1 line
Do you mind a longer id like
KEXEC_KERNEL_CHECKandKEXEC_INITRAMFS_CHECK?
These are the IMA policy tokens. Currently the IMA policy would include these KEXEC rules, where "appraise_type=imasig" indicates requiring a signature as opposed to a hash.
measure func=KEXEC_CHECK
appraise func=KEXEC_CHECK appraise_type=imasig #
measure func=INITRAMFS_CHECK
appraise func=INITRAMFS_CHECK appraise_type=imasig
I'll change these IMA policy identifiers to the longer names you suggested.
thanks,
Mimi
Quoted 6 lines
security/integrity/ima/ima.h enum ima_hooks { · line 154
Quoted 3 lines
security/integrity/ima/ima.h +0
-0MAX_CHECK };Thanks
Dave
PATCH v3 20/22 ima: load policy using path
In reply to Mimi ZoharFrom: 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
Changelog v3:
- moved kernel_read_file_from_path() to a separate patch
v2:
- after re-ordering the patches, replace calling integrity_kernel_read() to read the file with kernel_read_file_from_path() (Mimi)
- Patch description re-written by Luis R. Rodriguez
--- include/linux/fs.h | 1 + security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-)
include/linux/fs.h +1 -0
@@ -2531,6 +2531,7 @@ enum kernel_read_file_id {READING_MODULE ,READING_KEXEC_IMAGE ,READING_KEXEC_INITRAMFS , +READING_POLICY , READING_MAX_ID };
security/integrity/ima/ima_fs.c +41 -2
62 lines not on this page · raw message
Signature · 1 line
2.1.0
Re: [PATCH v3 20/22] ima: load policy using path
Quoted 96 lines · 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/policyChangelog v3:
- movedkernel_read_file_from_path()to a separate patch
v2:
- after re-ordering the patches, replace callingintegrity_kernel_read()to read the file withkernel_read_file_from_path()(Mimi)
- Patch description re-written by Luis R. RodriguezSigned-off-by Dmitry Kasatkin <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- include/linux/fs.h | 1 + security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-)include/linux/fs.h +1
-0@@ -2531,6 +2531,7 @@ enum kernel_read_file_id {READING_MODULE ,READING_KEXEC_IMAGE ,READING_KEXEC_INITRAMFS , +READING_POLICY , READING_MAX_ID };security/integrity/ima/ima_fs.c +36
-0@@ -22,6 +22,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/parser.h> +#include <linux/vmalloc.h> #include "ima.h" @@ -258,6 +259,41 @@ static const struct file_operations ima_ascii_measurements_ops = { .release = seq_release, }; +static ssize_t ima_read_policy (char *path) +{ +void *data; +char *datap; +loff_t size; +int rc, pathlen =strlen (path); + +char *p; + +/* remove \n */ + datap = path; +strsep (&datap,"\n" ); + + rc =kernel_read_file_from_path (path, &data, &size,0 ,READING_POLICY ); +if (rc <0 ) +return rc; + + datap = data; +while (size >0 && (p =strsep (&datap,"\n" ))) { +pr_debug ("rule: %s\n" , p); + rc =ima_parse_add_rule (p); +if (rc <0 ) +break ; + size -= rc; + } + +vfree (data); +if (rc <0 ) +return rc; +else if (size) +return -EINVAL ; +else +return pathlen; +} +static ssize_t ima_write_policy (struct file *file,const char __user *buf,size_t datalen,loff_t *ppos) {
security/integrity/ima/ima_fs.c static ssize_t ima_write_policy(struct file *file, const char __user *buf, · line 326
Quoted 7 lines
security/integrity/ima/ima_fs.c +1
-2@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex); +if (data[0 ] =='/' )
It seems that if we feed relative path to ima_policy the update will fail...
Quoted 14 lines
security/integrity/ima/ima_fs.c +4
-0+ result =ima_read_policy (data); + else + result =ima_parse_add_rule (data); +mutex_unlock (&ima_write_mutex); out_free:kfree (data); out:Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH v3 20/22] ima: load policy using path
________________________________________
From: Petko Manolov [[email protected]]
Sent: Sunday, February 07, 2016 9:59 PM
To: Mimi Zohar
Cc: [email protected]; Luis R. Rodriguez; [email protected]; [email protected]; [email protected]; David Howells; David Woodhouse; Kees Cook; Dmitry Torokhov; Dmitry Kasatkin; Eric Biederman; Rusty Russell; Dmitry Kasatkin; Dmitry Kasatkin
Subject: Re: [PATCH v3 20/22] ima: load policy using path
Quoted 104 lines · 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/policyChangelog v3:
- movedkernel_read_file_from_path()to a separate patch
v2:
- after re-ordering the patches, replace callingintegrity_kernel_read()to read the file withkernel_read_file_from_path()(Mimi)
- Patch description re-written by Luis R. RodriguezSigned-off-by Dmitry Kasatkin <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- include/linux/fs.h | 1 + security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-)include/linux/fs.h +1
-0@@ -2531,6 +2531,7 @@ enum kernel_read_file_id {READING_MODULE ,READING_KEXEC_IMAGE ,READING_KEXEC_INITRAMFS , +READING_POLICY , READING_MAX_ID };security/integrity/ima/ima_fs.c +37
-2@@ -22,6 +22,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/parser.h> +#include <linux/vmalloc.h> #include "ima.h" @@ -258,6 +259,41 @@ static const struct file_operations ima_ascii_measurements_ops = { .release = seq_release, }; +static ssize_t ima_read_policy (char *path) +{ +void *data; +char *datap; +loff_t size; +int rc, pathlen =strlen (path); + +char *p; + +/* remove \n */ + datap = path; +strsep (&datap,"\n" ); + + rc =kernel_read_file_from_path (path, &data, &size,0 ,READING_POLICY ); +if (rc <0 ) +return rc; + + datap = data; +while (size >0 && (p =strsep (&datap,"\n" ))) { +pr_debug ("rule: %s\n" , p); + rc =ima_parse_add_rule (p); +if (rc <0 ) +break ; + size -= rc; + } + +vfree (data); +if (rc <0 ) +return rc; +else if (size) +return -EINVAL ; +else +return pathlen; +} +static ssize_t ima_write_policy (struct file *file,const char __user *buf,size_t datalen,loff_t *ppos) {@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex); +if (data[0 ] =='/' )
Quoted 1 line
It seems that if we feed relative path to
ima_policythe update will fail...
Yes, i think it is always a good idea to pass absolute path.
Dmitry
Quoted 14 lines
security/integrity/ima/ima_fs.c +4
-1+ result =ima_read_policy (data); + else + result =ima_parse_add_rule (data); +mutex_unlock (&ima_write_mutex); out_free:kfree (data); out: --2.1.0
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v3 20/22] ima: load policy using path
Quoted 111 lines · Dmitry Kasatkin
________________________________________
From: Petko Manolov [[email protected]]
Sent: Sunday, February 07, 2016 9:59 PM
To: Mimi Zohar
Cc: [email protected]; Luis R. Rodriguez; [email protected]; [email protected]; [email protected]; David Howells; David Woodhouse; Kees Cook; Dmitry Torokhov; Dmitry Kasatkin; Eric Biederman; Rusty Russell; Dmitry Kasatkin; Dmitry Kasatkin
Subject: Re: [PATCH v3 20/22] ima: load policy using pathQuoted 102 lines · 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/policyChangelog v3:
- movedkernel_read_file_from_path()to a separate patch
v2:
- after re-ordering the patches, replace callingintegrity_kernel_read()to read the file withkernel_read_file_from_path()(Mimi)
- Patch description re-written by Luis R. RodriguezSigned-off-by Dmitry Kasatkin <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- include/linux/fs.h | 1 + security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-)include/linux/fs.h +1
-0@@ -2531,6 +2531,7 @@ enum kernel_read_file_id {READING_MODULE ,READING_KEXEC_IMAGE ,READING_KEXEC_INITRAMFS , +READING_POLICY , READING_MAX_ID };security/integrity/ima/ima_fs.c +36
-2@@ -22,6 +22,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/parser.h> +#include <linux/vmalloc.h> #include "ima.h" @@ -258,6 +259,41 @@ static const struct file_operations ima_ascii_measurements_ops = { .release = seq_release, }; +static ssize_t ima_read_policy (char *path) +{ +void *data; +char *datap; +loff_t size; +int rc, pathlen =strlen (path); + +char *p; + +/* remove \n */ + datap = path; +strsep (&datap,"\n" ); + + rc =kernel_read_file_from_path (path, &data, &size,0 ,READING_POLICY ); +if (rc <0 ) +return rc; + + datap = data; +while (size >0 && (p =strsep (&datap,"\n" ))) { +pr_debug ("rule: %s\n" , p); + rc =ima_parse_add_rule (p); +if (rc <0 ) +break ; + size -= rc; + } + +vfree (data); +if (rc <0 ) +return rc; +else if (size) +return -EINVAL ; +else +return pathlen; +} +static ssize_t ima_write_policy (struct file *file,const char __user *buf,size_t datalen,loff_t *ppos) {@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex);
Quoted 5 lines
Quoted 3 lines
security/integrity/ima/ima_fs.c +1
-0+if (data[0 ] =='/' )It seems that if we feed relative path to
ima_policythe update will fail...Yes, i think it is always a good idea to pass absolute path.
What if we at least emit a warning so people know what's wrong?
Petko
RE: [PATCH v3 20/22] ima: load policy using path
________________________________________
From: Petko Manolov [[email protected]]
Sent: Monday, February 08, 2016 12:35 PM
To: Dmitry Kasatkin
Cc: Mimi Zohar; [email protected]; Luis R. Rodriguez; [email protected]; [email protected]; [email protected]; David Howells; David Woodhouse; Kees Cook; Dmitry Torokhov; Dmitry Kasatkin; Eric Biederman; Rusty Russell; Dmitry Kasatkin
Subject: Re: [PATCH v3 20/22] ima: load policy using path
Quoted 111 lines · Dmitry Kasatkin
________________________________________
From: Petko Manolov [[email protected]]
Sent: Sunday, February 07, 2016 9:59 PM
To: Mimi Zohar
Cc: [email protected]; Luis R. Rodriguez; [email protected]; [email protected]; [email protected]; David Howells; David Woodhouse; Kees Cook; Dmitry Torokhov; Dmitry Kasatkin; Eric Biederman; Rusty Russell; Dmitry Kasatkin; Dmitry Kasatkin
Subject: Re: [PATCH v3 20/22] ima: load policy using pathQuoted 102 lines · 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/policyChangelog v3:
- movedkernel_read_file_from_path()to a separate patch
v2:
- after re-ordering the patches, replace callingintegrity_kernel_read()to read the file withkernel_read_file_from_path()(Mimi)
- Patch description re-written by Luis R. RodriguezSigned-off-by Dmitry Kasatkin <[email protected]>Signed-off-by Mimi Zohar <[email protected]>--- include/linux/fs.h | 1 + security/integrity/ima/ima_fs.c | 43 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-)include/linux/fs.h +1
-0@@ -2531,6 +2531,7 @@ enum kernel_read_file_id {READING_MODULE ,READING_KEXEC_IMAGE ,READING_KEXEC_INITRAMFS , +READING_POLICY , READING_MAX_ID };security/integrity/ima/ima_fs.c +36
-2@@ -22,6 +22,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/parser.h> +#include <linux/vmalloc.h> #include "ima.h" @@ -258,6 +259,41 @@ static const struct file_operations ima_ascii_measurements_ops = { .release = seq_release, }; +static ssize_t ima_read_policy (char *path) +{ +void *data; +char *datap; +loff_t size; +int rc, pathlen =strlen (path); + +char *p; + +/* remove \n */ + datap = path; +strsep (&datap,"\n" ); + + rc =kernel_read_file_from_path (path, &data, &size,0 ,READING_POLICY ); +if (rc <0 ) +return rc; + + datap = data; +while (size >0 && (p =strsep (&datap,"\n" ))) { +pr_debug ("rule: %s\n" , p); + rc =ima_parse_add_rule (p); +if (rc <0 ) +break ; + size -= rc; + } + +vfree (data); +if (rc <0 ) +return rc; +else if (size) +return -EINVAL ; +else +return pathlen; +} +static ssize_t ima_write_policy (struct file *file,const char __user *buf,size_t datalen,loff_t *ppos) {@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex);
Quoted 5 lines
Quoted 3 lines
security/integrity/ima/ima_fs.c +1
-0+if (data[0 ] =='/' )It seems that if we feed relative path to
ima_policythe update will fail...Yes, i think it is always a good idea to pass absolute path.
What if we at least emit a warning so people know what's wrong?
Petko
DK: May be a good idea to print that loading policy by path or not.
Re: [PATCH v3 20/22] ima: load policy using path
Quoted 7 lines · Dmitry Kasatkin
Quoted 7 lines
Quoted 7 lines
security/integrity/ima/ima_fs.c +0
-2@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex);
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
(quoted diff) +1
-0+ if (data[0] == '/')It seems that if we feed relative path to
ima_policythe update will fail...Yes, i think it is always a good idea to pass absolute path.
What if we at least emit a warning so people know what's wrong?
The next patch "ima: measure and appraise the IMA policy itself" adds the following. Is a failure message enough?
+ 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);
Quoted 4 lines
Petko
DK: May be a good idea to print that loading policy by path or not.
Are we including the pathname? Are you suggesting a log or audit message?
Mimi
Re: [PATCH v3 20/22] ima: load policy using path
Quoted 11 lines · Mimi Zohar
Quoted 9 lines · Dmitry Kasatkin
Quoted 9 lines
Quoted 8 lines
security/integrity/ima/ima_fs.c +1
-2@@ -286,9 +322,12 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, result =mutex_lock_interruptible (&ima_write_mutex);if (result <0 ) goto out_free; - result =ima_parse_add_rule (data); -mutex_unlock (&ima_write_mutex); +if (data[0 ] =='/' )
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
It seems that if we feed relative path to
ima_policythe update will fail...Yes, i think it is always a good idea to pass absolute path.
What if we at least emit a warning so people know what's wrong?
The next patch "ima: measure and appraise the IMA policy itself" adds the following. Is a failure message enough?
That would be the wrong message. The above code does not handle relative paths so any attempt to load the policy by "./ima_policy_file" or "../../ima_policy_file" will fail.
Isn't there a kernel function that checks if given string is a path-name?
Quoted 12 lines
(quoted diff) +4
-0+ 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 policyrequired",
(quoted diff) +4
-0+ 1, 0); + if (ima_appraise & IMA_APPRAISE_ENFORCE) + result = -EACCES; + } else result = ima_parse_add_rule(data);Quoted 2 lines
Quoted 5 lines
Quoted 3 lines
Petko
DK: May be a good idea to print that loading policy by path or not.
Are we including the pathname? Are you suggesting a log or audit message?
I guess log is more appropriate.
PATCH v3 21/22 ima: measure and appraise the IMA policy itself
In reply to Mimi ZoharAdd support for measuring and appraising the IMA policy itself.
--- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_fs.c | 9 ++++++++- security/integrity/ima/ima_main.c | 3 +++ security/integrity/ima/ima_policy.c | 10 +++++++++- 4 files changed, 22 insertions(+), 2 deletions(-)
security/integrity/ima/ima.h +2 -0
@@ -149,6 +149,7 @@ enum ima_hooks {FIRMWARE_CHECK ,KEXEC_CHECK ,INITRAMFS_CHECK , +POLICY_CHECK , MAX_CHECK };
@@ -191,6 +192,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(enum ima_hooks func,
security/integrity/ima/ima_fs.c +8 -1
@@ -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_main.c +3 -0
@@ -386,6 +386,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,case READING_KEXEC_INITRAMFS : func =INITRAMFS_CHECK ;break ; +case READING_POLICY : + func =POLICY_CHECK ; +break ;default : func =FILE_CHECK ;break ;
security/integrity/ima/ima_policy.c +9 -1
@@ -114,6 +114,7 @@ static struct ima_rule_entry default_measurement_rules[] = { .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, + {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, }; static struct ima_rule_entry default_appraise_rules[] = {
@@ -616,6 +617,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =KEXEC_CHECK ; else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) entry->func = INITRAMFS_CHECK; + else if (strcmp(args[0].from, "POLICY_CHECK") == 0) + entry->func = POLICY_CHECK; else result = -EINVAL ;if (!result)
@@ -774,6 +777,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) temp_ima_appraise |=IMA_APPRAISE_MODULES ; else if (entry->func == FIRMWARE_CHECK) temp_ima_appraise |= IMA_APPRAISE_FIRMWARE; + else if (entry->func == POLICY_CHECK) + temp_ima_appraise |= IMA_APPRAISE_POLICY;audit_log_format (ab,"res=%d" , !result);audit_log_end (ab);return result;
@@ -860,7 +865,7 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, func_module, func_firmware, func_post, - func_kexec, func_initramfs + func_kexec, func_initramfs, func_policy };static char *func_tokens[] = {
@@ -940,6 +945,9 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case INITRAMFS_CHECK :seq_printf (m,pt (Opt_func),ft (func_initramfs));break ; +case POLICY_CHECK : +seq_printf (m,pt (Opt_func),ft (func_policy)); +break ;default :snprintf (tbuf,sizeof (tbuf),"%d" , func);seq_printf (m,pt (Opt_func), tbuf);
Signature · 1 line
2.1.0
Re: [PATCH v3 21/22] ima: measure and appraise the IMA policy itself
Quoted 3 lines · Mimi Zohar
Add support for measuring and appraising the IMA policy itself.
Signed-off-by Mimi Zohar <[email protected]>
Quoted 117 lines
--- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_fs.c | 9 ++++++++- security/integrity/ima/ima_main.c | 3 +++ security/integrity/ima/ima_policy.c | 10 +++++++++- 4 files changed, 22 insertions(+), 2 deletions(-)security/integrity/ima/ima.h +2
-0@@ -149,6 +149,7 @@ enum ima_hooks {FIRMWARE_CHECK ,KEXEC_CHECK ,INITRAMFS_CHECK , +POLICY_CHECK , MAX_CHECK };@@ -191,6 +192,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(enum ima_hooks func,security/integrity/ima/ima_fs.c +8
-1@@ -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_main.c +3
-0@@ -386,6 +386,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,case READING_KEXEC_INITRAMFS : func =INITRAMFS_CHECK ;break ; +case READING_POLICY : + func =POLICY_CHECK ; +break ;default : func =FILE_CHECK ;break ;security/integrity/ima/ima_policy.c +9
-1@@ -114,6 +114,7 @@ static struct ima_rule_entry default_measurement_rules[] = { .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, + {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, }; static struct ima_rule_entry default_appraise_rules[] = {@@ -616,6 +617,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =KEXEC_CHECK ; else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) entry->func = INITRAMFS_CHECK; + else if (strcmp(args[0].from, "POLICY_CHECK") == 0) + entry->func = POLICY_CHECK; else result = -EINVAL ;if (!result)@@ -774,6 +777,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) temp_ima_appraise |=IMA_APPRAISE_MODULES ; else if (entry->func == FIRMWARE_CHECK) temp_ima_appraise |= IMA_APPRAISE_FIRMWARE; + else if (entry->func == POLICY_CHECK) + temp_ima_appraise |= IMA_APPRAISE_POLICY;audit_log_format (ab,"res=%d" , !result);audit_log_end (ab);return result;@@ -860,7 +865,7 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, func_module, func_firmware, func_post, - func_kexec, func_initramfs + func_kexec, func_initramfs, func_policy };static char *func_tokens[] = {@@ -940,6 +945,9 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case INITRAMFS_CHECK :seq_printf (m,pt (Opt_func),ft (func_initramfs));break ; +case POLICY_CHECK : +seq_printf (m,pt (Opt_func),ft (func_policy)); +break ;default :snprintf (tbuf,sizeof (tbuf),"%d" , func);seq_printf (m,pt (Opt_func), tbuf);Signature · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v3 21/22] ima: measure and appraise the IMA policy itself
In reply to Mimi ZoharQuoted 3 lines · Mimi Zohar
Add support for measuring and appraising the IMA policy itself.
Signed-off-by Mimi Zohar <[email protected]>
But from Documentation/CodingStyle
if (condition)
do_this();
else
do_that();This does not apply if only one branch of a conditional statement is a single statement; in the latter case use braces in both branches:
if (condition) {
do_this();
do_that();
} else {
otherwise();
}You have similar issue in other patches as well...
Dmitry
Quoted 113 lines
--- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_fs.c | 9 ++++++++- security/integrity/ima/ima_main.c | 3 +++ security/integrity/ima/ima_policy.c | 10 +++++++++- 4 files changed, 22 insertions(+), 2 deletions(-)security/integrity/ima/ima.h +2
-0@@ -149,6 +149,7 @@ enum ima_hooks {FIRMWARE_CHECK ,KEXEC_CHECK ,INITRAMFS_CHECK , +POLICY_CHECK , MAX_CHECK };@@ -191,6 +192,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(enum ima_hooks func,security/integrity/ima/ima_fs.c +8
-1@@ -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_main.c +3
-0@@ -386,6 +386,9 @@ int ima_post_read_file(struct file *file, void *buf, loff_t size,case READING_KEXEC_INITRAMFS : func =INITRAMFS_CHECK ;break ; +case READING_POLICY : + func =POLICY_CHECK ; +break ;default : func =FILE_CHECK ;break ;security/integrity/ima/ima_policy.c +9
-2@@ -114,6 +114,7 @@ static struct ima_rule_entry default_measurement_rules[] = { .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID}, {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC}, {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC}, + {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC}, }; static struct ima_rule_entry default_appraise_rules[] = {@@ -616,6 +617,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->func =KEXEC_CHECK ; else if (strcmp(args[0].from, "INITRAMFS_CHECK") == 0) entry->func = INITRAMFS_CHECK; + else if (strcmp(args[0].from, "POLICY_CHECK") == 0) + entry->func = POLICY_CHECK; else result = -EINVAL ;if (!result)@@ -774,6 +777,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) temp_ima_appraise |=IMA_APPRAISE_MODULES ; else if (entry->func == FIRMWARE_CHECK) temp_ima_appraise |= IMA_APPRAISE_FIRMWARE; + else if (entry->func == POLICY_CHECK) + temp_ima_appraise |= IMA_APPRAISE_POLICY;audit_log_format (ab,"res=%d" , !result);audit_log_end (ab);return result;@@ -860,7 +865,7 @@ static char *mask_tokens[] = {enum { func_file =0 , func_mmap, func_bprm, func_module, func_firmware, func_post, - func_kexec, func_initramfs + func_kexec, func_initramfs, func_policy };static char *func_tokens[] = {@@ -940,6 +945,9 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)case INITRAMFS_CHECK :seq_printf (m,pt (Opt_func),ft (func_initramfs));break ; +case POLICY_CHECK : +seq_printf (m,pt (Opt_func),ft (func_policy)); +break ;default :snprintf (tbuf,sizeof (tbuf),"%d" , func);seq_printf (m,pt (Opt_func), tbuf); --2.1.0
Signature · 2 lines
Thanks, Dmitry
Re: [PATCH v3 21/22] ima: measure and appraise the IMA policy itself
Quoted 25 lines · Dmitry Kasatkin
Quoted 3 lines · Mimi Zohar
Add support for measuring and appraising the IMA policy itself.
Signed-off-by Mimi Zohar <[email protected]>Acked-by Dmitry Kasatkin <[email protected]>But from
Documentation/CodingStyleif (condition)
do_this(); else do_that();This does not apply if only one branch of a conditional statement is a single statement; in the latter case use braces in both branches:
if (condition) { do_this(); do_that(); } else { otherwise(); }
Quoted 3 lines
You have similar issue in other patches as well...
Dmitry
Ok, I'll find and fix them. Thank you for the review!
Mimi
PATCH v3 22/22 ima: require signed IMA policy
In reply to Mimi ZoharRequire the IMA policy to be signed when additional rules can be added.
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
@@ -129,6 +129,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, .func = 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
@@ -412,9 +416,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].func ==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
Re: [PATCH v3 22/22] ima: require signed IMA policy
Quoted 7 lines · Mimi Zohar
Require the IMA policy to be signed when additional rules can be added.
v1:
- initialize the policy flag
- includeIMA_APPRAISE_POLICYin the policy flagSigned-off-by Mimi Zohar <[email protected]>
Quoted 39 lines
--- security/integrity/ima/ima_policy.c | 7 +++++++ 1 file changed, 7 insertions(+)security/integrity/ima/ima_policy.c +7
-0@@ -129,6 +129,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, .func = 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@@ -412,9 +416,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].func ==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 · 6 lines
2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v3 22/22] ima: require signed IMA policy
In reply to Mimi ZoharQuoted 7 lines · Mimi Zohar
Require the IMA policy to be signed when additional rules can be added.
v1:
- initialize the policy flag
- includeIMA_APPRAISE_POLICYin the policy flagSigned-off-by Mimi Zohar <[email protected]>
Quoted 35 lines
--- security/integrity/ima/ima_policy.c | 7 +++++++ 1 file changed, 7 insertions(+)security/integrity/ima/ima_policy.c +7
-1@@ -129,6 +129,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, .func = 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@@ -412,9 +416,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].func ==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. */ --2.1.0
Signature · 2 lines
Thanks, Dmitry
Quoted 14 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.
Quoted 8 lines
This patch set attempts to resolve 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. In addition, as none of the upstreamed LSMs define either a
kernel_module_from_fileor akernel_fw_from_filehook, this patch set removes these hooks and the associated functions. Theima_module_check()andima_fw_from_file()functions are renamed and called from the pre and postkernel_read_filesecurity functions respectively.
I'm very happy about the pre and post hooks; this solves the primary problem I'd had when comparing the firmware and module hooks. Thanks!
Once this series is in -next, I'll resend my rebased "loadpin" LSM.
-Kees
Quoted 76 lines
Changelog:
- Rebased on top of some of the "firmware_class: extensible firmware API" patches posted by Luis.
- Removed thekernel_module_from_fileandkernel_fw_from_filesecurity hooks and functions.
- Defined "kernel_read_file_id" enumeration, independently of "ima_hooks".
- Split patches for ease of review.The latest version of these patches can be found in the next-kernel-read-v3 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-loaderMimi
David Howells (1):
firmware: fold successful fw read earlyDmitry Kasatkin (3):
ima: separate 'security.ima' reading functionality from collect
ima: provide buffer hash calculation function
ima: load policy using pathLuis R. Rodriguez (2):
firmware: simplify dev_*() print messages for generic helpers
firmware: move completing fw into a helperMimi Zohar (16):
ima: refactorima_policy_show()to display "ima_hooks" rules
ima: use "ima_hooks" enum as function argument
vfs: define a generic function to read a file from the kernel
vfs: definekernel_read_file_idenumeration
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
vfs: definekernel_read_file_from_path
firmware: replace call tofw_read_file_contents()with kernel version
security: definekernel_read_filehook
vfs: definekernel_copy_file_from_fd()
module: replacecopy_module_from_fdwith kernel version
ima: remove firmware and module specific cached status info
kexec: replace call tocopy_file_from_fd()with kernel version
ima: support for kexec image and initramfs
ima: measure and appraise the IMA policy itself
ima: require signed IMA policyDocumentation/ABI/testing/ima_policy | 2 +- drivers/base/firmware_class.c | 76 ++++++------------- fs/exec.c | 95 +++++++++++++++++++++++ include/linux/fs.h | 15 ++++ include/linux/ima.h | 10 ++- include/linux/lsm_hooks.h | 35 ++++----- include/linux/security.h | 16 ++-- kernel/kexec_file.c | 73 +++--------------- kernel/module.c | 68 ++--------------- security/integrity/iint.c | 4 +- security/integrity/ima/ima.h | 49 ++++++++---- security/integrity/ima/ima_api.c | 25 +++---- security/integrity/ima/ima_appraise.c | 69 ++++++++--------- 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 | 104 +++++++++++++++++++------- security/integrity/ima/ima_policy.c | 107 +++++++++++++++++--------- security/integrity/ima/ima_template.c | 2 - security/integrity/ima/ima_template_lib.c | 1 - security/integrity/integrity.h | 15 ++-- security/security.c | 32 ++++---- 22 files changed, 605 insertions(+), 365 deletions(-)--
2.1.0
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Quoted 24 lines · Kees Cook
Quoted 14 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.
Quoted 8 lines
This patch set attempts to resolve 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. In addition, as none of the upstreamed LSMs define either a
kernel_module_from_fileor akernel_fw_from_filehook, this patch set removes these hooks and the associated functions. Theima_module_check()andima_fw_from_file()functions are renamed and called from the pre and postkernel_read_filesecurity functions respectively.
Quoted 2 lines
I'm very happy about the pre and post hooks; this solves the primary problem I'd had when comparing the firmware and module hooks. Thanks!
Thank you for reviewing the patches!
Quoted 1 line
Once this series is in -next, I'll resend my rebased "loadpin" LSM.
I was looking for this reference, when writing the patch description for modules, but couldn't remember it. Commit 2e72d51 "security: introduce kernel_module_from_file hook" patch description references Chrome OS.
Thanks!
Mimi