diff mbox series

[v3,2/3] ima: Return int in the functions to measure a buffer

Message ID 20210705090922.3321178-3-roberto.sassu@huawei.com (mailing list archive)
State New, archived
Headers show
Series ima: Provide more info about buffer measurement | expand

Commit Message

Roberto Sassu July 5, 2021, 9:09 a.m. UTC
ima_measure_critical_data() and process_buffer_measurement() currently
don't return a result. A caller wouldn't be able to know whether those
functions were executed successfully.

This patch modifies the return type from void to int, and returns 0 if the
buffer has been successfully measured, a negative value otherwise.

Also, this patch does not modify the behavior of existing callers by
processing the returned value. For those, the return value is ignored.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 include/linux/ima.h               | 15 +++++++-----
 security/integrity/ima/ima.h      | 10 ++++----
 security/integrity/ima/ima_main.c | 40 ++++++++++++++++++-------------
 3 files changed, 37 insertions(+), 28 deletions(-)

Comments

Lakshmi Ramasubramanian July 6, 2021, 7:21 p.m. UTC | #1
On 7/5/2021 2:09 AM, Roberto Sassu wrote:
> ima_measure_critical_data() and process_buffer_measurement() currently
> don't return a result. A caller wouldn't be able to know whether those
> functions were executed successfully.
> 
> This patch modifies the return type from void to int, and returns 0 if the
> buffer has been successfully measured, a negative value otherwise.
> 
> Also, this patch does not modify the behavior of existing callers by
> processing the returned value. For those, the return value is ignored.
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> ---
>   include/linux/ima.h               | 15 +++++++-----
>   security/integrity/ima/ima.h      | 10 ++++----
>   security/integrity/ima/ima_main.c | 40 ++++++++++++++++++-------------
>   3 files changed, 37 insertions(+), 28 deletions(-)

Reviewed-by: Lakshmi Ramasubramanian <nramas@linux.microsoft.com>

  -lakshmi

> 
> diff --git a/include/linux/ima.h b/include/linux/ima.h
> index 81e830d01ced..60492263aa64 100644
> --- a/include/linux/ima.h
> +++ b/include/linux/ima.h
> @@ -35,10 +35,10 @@ extern void ima_post_path_mknod(struct user_namespace *mnt_userns,
>   extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
>   extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size);
>   extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size);
> -extern void ima_measure_critical_data(const char *event_label,
> -				      const char *event_name,
> -				      const void *buf, size_t buf_len,
> -				      bool hash);
> +extern int ima_measure_critical_data(const char *event_label,
> +				     const char *event_name,
> +				     const void *buf, size_t buf_len,
> +				     bool hash);
>   
>   #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
>   extern void ima_appraise_parse_cmdline(void);
> @@ -144,10 +144,13 @@ static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size
>   
>   static inline void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) {}
>   
> -static inline void ima_measure_critical_data(const char *event_label,
> +static inline int ima_measure_critical_data(const char *event_label,
>   					     const char *event_name,
>   					     const void *buf, size_t buf_len,
> -					     bool hash) {}
> +					     bool hash)
> +{
> +	return -ENOENT;
> +}
>   
>   #endif /* CONFIG_IMA */
>   
> diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
> index f0e448ed1f9f..03db221324c3 100644
> --- a/security/integrity/ima/ima.h
> +++ b/security/integrity/ima/ima.h
> @@ -264,11 +264,11 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
>   			   struct evm_ima_xattr_data *xattr_value,
>   			   int xattr_len, const struct modsig *modsig, int pcr,
>   			   struct ima_template_desc *template_desc);
> -void process_buffer_measurement(struct user_namespace *mnt_userns,
> -				struct inode *inode, const void *buf, int size,
> -				const char *eventname, enum ima_hooks func,
> -				int pcr, const char *func_data,
> -				bool buf_hash);
> +int process_buffer_measurement(struct user_namespace *mnt_userns,
> +			       struct inode *inode, const void *buf, int size,
> +			       const char *eventname, enum ima_hooks func,
> +			       int pcr, const char *func_data,
> +			       bool buf_hash);
>   void ima_audit_measurement(struct integrity_iint_cache *iint,
>   			   const unsigned char *filename);
>   int ima_alloc_init_template(struct ima_event_data *event_data,
> diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> index 8ef1fa357e0c..b512c06d8ee1 100644
> --- a/security/integrity/ima/ima_main.c
> +++ b/security/integrity/ima/ima_main.c
> @@ -827,7 +827,7 @@ int ima_post_load_data(char *buf, loff_t size,
>   	return 0;
>   }
>   
> -/*
> +/**
>    * process_buffer_measurement - Measure the buffer or the buffer data hash
>    * @mnt_userns:	user namespace of the mount the inode was found from
>    * @inode: inode associated with the object being measured (NULL for KEY_CHECK)
> @@ -840,12 +840,15 @@ int ima_post_load_data(char *buf, loff_t size,
>    * @buf_hash: measure buffer data hash
>    *
>    * Based on policy, either the buffer data or buffer data hash is measured
> + *
> + * Return: 0 if the buffer has been successfully measured, a negative value
> + * otherwise.
>    */
> -void process_buffer_measurement(struct user_namespace *mnt_userns,
> -				struct inode *inode, const void *buf, int size,
> -				const char *eventname, enum ima_hooks func,
> -				int pcr, const char *func_data,
> -				bool buf_hash)
> +int process_buffer_measurement(struct user_namespace *mnt_userns,
> +			       struct inode *inode, const void *buf, int size,
> +			       const char *eventname, enum ima_hooks func,
> +			       int pcr, const char *func_data,
> +			       bool buf_hash)
>   {
>   	int ret = 0;
>   	const char *audit_cause = "ENOMEM";
> @@ -867,7 +870,7 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
>   	u32 secid;
>   
>   	if (!ima_policy_flag)
> -		return;
> +		return -ENOENT;
>   
>   	template = ima_template_desc_buf();
>   	if (!template) {
> @@ -889,7 +892,7 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
>   					secid, 0, func, &pcr, &template,
>   					func_data);
>   		if (!(action & IMA_MEASURE))
> -			return;
> +			return -ENOENT;
>   	}
>   
>   	if (!pcr)
> @@ -937,7 +940,7 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
>   					func_measure_str(func),
>   					audit_cause, ret, 0, ret);
>   
> -	return;
> +	return ret;
>   }
>   
>   /**
> @@ -977,18 +980,21 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
>    * and extend the pcr.  Examples of critical data could be various data
>    * structures, policies, and states stored in kernel memory that can
>    * impact the integrity of the system.
> + *
> + * Return: 0 if the buffer has been successfully measured, a negative value
> + * otherwise.
>    */
> -void ima_measure_critical_data(const char *event_label,
> -			       const char *event_name,
> -			       const void *buf, size_t buf_len,
> -			       bool hash)
> +int ima_measure_critical_data(const char *event_label,
> +			      const char *event_name,
> +			      const void *buf, size_t buf_len,
> +			      bool hash)
>   {
>   	if (!event_name || !event_label || !buf || !buf_len)
> -		return;
> +		return -ENOPARAM;
>   
> -	process_buffer_measurement(&init_user_ns, NULL, buf, buf_len, event_name,
> -				   CRITICAL_DATA, 0, event_label,
> -				   hash);
> +	return process_buffer_measurement(&init_user_ns, NULL, buf, buf_len,
> +					  event_name, CRITICAL_DATA, 0,
> +					  event_label, hash);
>   }
>   
>   static int __init init_ima(void)
>
Mimi Zohar July 19, 2021, 8:28 p.m. UTC | #2
Hi Roberto,

On Mon, 2021-07-05 at 11:09 +0200, Roberto Sassu wrote:
> ima_measure_critical_data() and process_buffer_measurement() currently
> don't return a result. A caller wouldn't be able to know whether those
> functions were executed successfully.

Missing is an explanation as to why these functions aren't currently
returning a result.   The LSM/IMA hooks only return a negative result
for failure to appraise a file's integrity, not measure a file.  Only
failure to appraise a file's integrity results in preventing the file
from being read/executed/mmaped.  Other failures are only audited.

> 
> This patch modifies the return type from void to int, and returns 0 if the
> buffer has been successfully measured, a negative value otherwise.

Needed here is an explanation as to why ima_measure_critical_data() is
special.

> 
> Also, this patch does not modify the behavior of existing callers by
> processing the returned value. For those, the return value is ignored.

I agree that the existing behavior shouldn't change, but will this
result in the bots complaining?

thanks,

Mimi
Roberto Sassu July 20, 2021, 12:38 p.m. UTC | #3
> From: Mimi Zohar [mailto:zohar@linux.ibm.com]
> Sent: Monday, July 19, 2021 10:28 PM
> Hi Roberto,
> 
> On Mon, 2021-07-05 at 11:09 +0200, Roberto Sassu wrote:
> > ima_measure_critical_data() and process_buffer_measurement() currently
> > don't return a result. A caller wouldn't be able to know whether those
> > functions were executed successfully.
> 
> Missing is an explanation as to why these functions aren't currently
> returning a result.   The LSM/IMA hooks only return a negative result
> for failure to appraise a file's integrity, not measure a file.  Only
> failure to appraise a file's integrity results in preventing the file
> from being read/executed/mmaped.  Other failures are only audited.

Hi Mimi

ok, will add it.

> > This patch modifies the return type from void to int, and returns 0 if the
> > buffer has been successfully measured, a negative value otherwise.
> 
> Needed here is an explanation as to why ima_measure_critical_data() is
> special.

We don't want to unnecessarily calculate the digest twice.

> > Also, this patch does not modify the behavior of existing callers by
> > processing the returned value. For those, the return value is ignored.
> 
> I agree that the existing behavior shouldn't change, but will this
> result in the bots complaining?

If I remember correctly, I didn't get any error even with W=1.

Thanks

Roberto

HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063
Managing Director: Li Peng, Li Jian, Shi Yanli

> thanks,
> 
> Mimi
Mimi Zohar July 20, 2021, 1:01 p.m. UTC | #4
On Tue, 2021-07-20 at 12:38 +0000, Roberto Sassu wrote:
> > > This patch modifies the return type from void to int, and returns 0 if the
> > > buffer has been successfully measured, a negative value otherwise.
> > 
> > Needed here is an explanation as to why ima_measure_critical_data() is
> > special.
> 
> We don't want to unnecessarily calculate the digest twice.

That's what the "iint" cache is for.  .  This needs more a of an
explaintion as to why  ima_measure_critical_data() is special.

thanks,

Mimi
diff mbox series

Patch

diff --git a/include/linux/ima.h b/include/linux/ima.h
index 81e830d01ced..60492263aa64 100644
--- a/include/linux/ima.h
+++ b/include/linux/ima.h
@@ -35,10 +35,10 @@  extern void ima_post_path_mknod(struct user_namespace *mnt_userns,
 extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
 extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size);
 extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size);
-extern void ima_measure_critical_data(const char *event_label,
-				      const char *event_name,
-				      const void *buf, size_t buf_len,
-				      bool hash);
+extern int ima_measure_critical_data(const char *event_label,
+				     const char *event_name,
+				     const void *buf, size_t buf_len,
+				     bool hash);
 
 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
 extern void ima_appraise_parse_cmdline(void);
@@ -144,10 +144,13 @@  static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size
 
 static inline void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) {}
 
-static inline void ima_measure_critical_data(const char *event_label,
+static inline int ima_measure_critical_data(const char *event_label,
 					     const char *event_name,
 					     const void *buf, size_t buf_len,
-					     bool hash) {}
+					     bool hash)
+{
+	return -ENOENT;
+}
 
 #endif /* CONFIG_IMA */
 
diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
index f0e448ed1f9f..03db221324c3 100644
--- a/security/integrity/ima/ima.h
+++ b/security/integrity/ima/ima.h
@@ -264,11 +264,11 @@  void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
 			   struct evm_ima_xattr_data *xattr_value,
 			   int xattr_len, const struct modsig *modsig, int pcr,
 			   struct ima_template_desc *template_desc);
-void process_buffer_measurement(struct user_namespace *mnt_userns,
-				struct inode *inode, const void *buf, int size,
-				const char *eventname, enum ima_hooks func,
-				int pcr, const char *func_data,
-				bool buf_hash);
+int process_buffer_measurement(struct user_namespace *mnt_userns,
+			       struct inode *inode, const void *buf, int size,
+			       const char *eventname, enum ima_hooks func,
+			       int pcr, const char *func_data,
+			       bool buf_hash);
 void ima_audit_measurement(struct integrity_iint_cache *iint,
 			   const unsigned char *filename);
 int ima_alloc_init_template(struct ima_event_data *event_data,
diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
index 8ef1fa357e0c..b512c06d8ee1 100644
--- a/security/integrity/ima/ima_main.c
+++ b/security/integrity/ima/ima_main.c
@@ -827,7 +827,7 @@  int ima_post_load_data(char *buf, loff_t size,
 	return 0;
 }
 
-/*
+/**
  * process_buffer_measurement - Measure the buffer or the buffer data hash
  * @mnt_userns:	user namespace of the mount the inode was found from
  * @inode: inode associated with the object being measured (NULL for KEY_CHECK)
@@ -840,12 +840,15 @@  int ima_post_load_data(char *buf, loff_t size,
  * @buf_hash: measure buffer data hash
  *
  * Based on policy, either the buffer data or buffer data hash is measured
+ *
+ * Return: 0 if the buffer has been successfully measured, a negative value
+ * otherwise.
  */
-void process_buffer_measurement(struct user_namespace *mnt_userns,
-				struct inode *inode, const void *buf, int size,
-				const char *eventname, enum ima_hooks func,
-				int pcr, const char *func_data,
-				bool buf_hash)
+int process_buffer_measurement(struct user_namespace *mnt_userns,
+			       struct inode *inode, const void *buf, int size,
+			       const char *eventname, enum ima_hooks func,
+			       int pcr, const char *func_data,
+			       bool buf_hash)
 {
 	int ret = 0;
 	const char *audit_cause = "ENOMEM";
@@ -867,7 +870,7 @@  void process_buffer_measurement(struct user_namespace *mnt_userns,
 	u32 secid;
 
 	if (!ima_policy_flag)
-		return;
+		return -ENOENT;
 
 	template = ima_template_desc_buf();
 	if (!template) {
@@ -889,7 +892,7 @@  void process_buffer_measurement(struct user_namespace *mnt_userns,
 					secid, 0, func, &pcr, &template,
 					func_data);
 		if (!(action & IMA_MEASURE))
-			return;
+			return -ENOENT;
 	}
 
 	if (!pcr)
@@ -937,7 +940,7 @@  void process_buffer_measurement(struct user_namespace *mnt_userns,
 					func_measure_str(func),
 					audit_cause, ret, 0, ret);
 
-	return;
+	return ret;
 }
 
 /**
@@ -977,18 +980,21 @@  void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
  * and extend the pcr.  Examples of critical data could be various data
  * structures, policies, and states stored in kernel memory that can
  * impact the integrity of the system.
+ *
+ * Return: 0 if the buffer has been successfully measured, a negative value
+ * otherwise.
  */
-void ima_measure_critical_data(const char *event_label,
-			       const char *event_name,
-			       const void *buf, size_t buf_len,
-			       bool hash)
+int ima_measure_critical_data(const char *event_label,
+			      const char *event_name,
+			      const void *buf, size_t buf_len,
+			      bool hash)
 {
 	if (!event_name || !event_label || !buf || !buf_len)
-		return;
+		return -ENOPARAM;
 
-	process_buffer_measurement(&init_user_ns, NULL, buf, buf_len, event_name,
-				   CRITICAL_DATA, 0, event_label,
-				   hash);
+	return process_buffer_measurement(&init_user_ns, NULL, buf, buf_len,
+					  event_name, CRITICAL_DATA, 0,
+					  event_label, hash);
 }
 
 static int __init init_ima(void)