@@ -35,7 +35,7 @@ int ima_appraise = IMA_APPRAISE_ENFORCE;
3535int ima_appraise ;
3636#endif
3737
38- int ima_hash_algo = HASH_ALGO_SHA1 ;
38+ int __ro_after_init ima_hash_algo = HASH_ALGO_SHA1 ;
3939static int hash_setup_done ;
4040
4141static struct notifier_block ima_lsm_policy_notifier = {
@@ -76,6 +76,11 @@ static int __init hash_setup(char *str)
7676}
7777__setup ("ima_hash=" , hash_setup );
7878
79+ enum hash_algo ima_get_current_hash_algo (void )
80+ {
81+ return ima_hash_algo ;
82+ }
83+
7984/* Prevent mmap'ing a file execute that is already mmap'ed write */
8085static int mmap_violation_check (enum ima_hooks func , struct file * file ,
8186 char * * pathbuf , const char * * pathname ,
@@ -822,7 +827,7 @@ int ima_post_load_data(char *buf, loff_t size,
822827 return 0 ;
823828}
824829
825- /*
830+ /**
826831 * process_buffer_measurement - Measure the buffer or the buffer data hash
827832 * @mnt_userns: user namespace of the mount the inode was found from
828833 * @inode: inode associated with the object being measured (NULL for KEY_CHECK)
@@ -833,14 +838,20 @@ int ima_post_load_data(char *buf, loff_t size,
833838 * @pcr: pcr to extend the measurement
834839 * @func_data: func specific data, may be NULL
835840 * @buf_hash: measure buffer data hash
841+ * @digest: buffer digest will be written to
842+ * @digest_len: buffer length
836843 *
837844 * Based on policy, either the buffer data or buffer data hash is measured
845+ *
846+ * Return: 0 if the buffer has been successfully measured, 1 if the digest
847+ * has been written to the passed location but not added to a measurement entry,
848+ * a negative value otherwise.
838849 */
839- void process_buffer_measurement (struct user_namespace * mnt_userns ,
840- struct inode * inode , const void * buf , int size ,
841- const char * eventname , enum ima_hooks func ,
842- int pcr , const char * func_data ,
843- bool buf_hash )
850+ int process_buffer_measurement (struct user_namespace * mnt_userns ,
851+ struct inode * inode , const void * buf , int size ,
852+ const char * eventname , enum ima_hooks func ,
853+ int pcr , const char * func_data ,
854+ bool buf_hash , u8 * digest , size_t digest_len )
844855{
845856 int ret = 0 ;
846857 const char * audit_cause = "ENOMEM" ;
@@ -861,8 +872,11 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
861872 int action = 0 ;
862873 u32 secid ;
863874
864- if (!ima_policy_flag )
865- return ;
875+ if (digest && digest_len < digest_hash_len )
876+ return - EINVAL ;
877+
878+ if (!ima_policy_flag && !digest )
879+ return - ENOENT ;
866880
867881 template = ima_template_desc_buf ();
868882 if (!template ) {
@@ -883,8 +897,8 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
883897 action = ima_get_action (mnt_userns , inode , current_cred (),
884898 secid , 0 , func , & pcr , & template ,
885899 func_data );
886- if (!(action & IMA_MEASURE ))
887- return ;
900+ if (!(action & IMA_MEASURE ) && ! digest )
901+ return - ENOENT ;
888902 }
889903
890904 if (!pcr )
@@ -914,6 +928,12 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
914928 event_data .buf_len = digest_hash_len ;
915929 }
916930
931+ if (digest )
932+ memcpy (digest , iint .ima_hash -> digest , digest_hash_len );
933+
934+ if (!ima_policy_flag || (func && !(action & IMA_MEASURE )))
935+ return 1 ;
936+
917937 ret = ima_alloc_init_template (& event_data , & entry , template );
918938 if (ret < 0 ) {
919939 audit_cause = "alloc_entry" ;
@@ -932,7 +952,7 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
932952 func_measure_str (func ),
933953 audit_cause , ret , 0 , ret );
934954
935- return ;
955+ return ret ;
936956}
937957
938958/**
@@ -956,7 +976,7 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
956976
957977 process_buffer_measurement (file_mnt_user_ns (f .file ), file_inode (f .file ),
958978 buf , size , "kexec-cmdline" , KEXEC_CMDLINE , 0 ,
959- NULL , false);
979+ NULL , false, NULL , 0 );
960980 fdput (f );
961981}
962982
@@ -967,23 +987,30 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
967987 * @buf: pointer to buffer data
968988 * @buf_len: length of buffer data (in bytes)
969989 * @hash: measure buffer data hash
990+ * @digest: buffer digest will be written to
991+ * @digest_len: buffer length
970992 *
971993 * Measure data critical to the integrity of the kernel into the IMA log
972994 * and extend the pcr. Examples of critical data could be various data
973995 * structures, policies, and states stored in kernel memory that can
974996 * impact the integrity of the system.
997+ *
998+ * Return: 0 if the buffer has been successfully measured, 1 if the digest
999+ * has been written to the passed location but not added to a measurement entry,
1000+ * a negative value otherwise.
9751001 */
976- void ima_measure_critical_data (const char * event_label ,
977- const char * event_name ,
978- const void * buf , size_t buf_len ,
979- bool hash )
1002+ int ima_measure_critical_data (const char * event_label ,
1003+ const char * event_name ,
1004+ const void * buf , size_t buf_len ,
1005+ bool hash , u8 * digest , size_t digest_len )
9801006{
9811007 if (!event_name || !event_label || !buf || !buf_len )
982- return ;
1008+ return - ENOPARAM ;
9831009
984- process_buffer_measurement (& init_user_ns , NULL , buf , buf_len , event_name ,
985- CRITICAL_DATA , 0 , event_label ,
986- hash );
1010+ return process_buffer_measurement (& init_user_ns , NULL , buf , buf_len ,
1011+ event_name , CRITICAL_DATA , 0 ,
1012+ event_label , hash , digest ,
1013+ digest_len );
9871014}
9881015
9891016static int __init init_ima (void )
0 commit comments