LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 1/1] ima: check control characters in policy file path
@ 2021-08-14  8:27 Tianxing Zhang
  2021-08-17 17:45 ` Mimi Zohar
  0 siblings, 1 reply; 2+ messages in thread
From: Tianxing Zhang @ 2021-08-14  8:27 UTC (permalink / raw)
  To: zohar
  Cc: linux-integrity, linux-security-module, linux-kernel, Tianxing Zhang

When a policy file path contains control characters like '\r' or '\b',
invalid error messages can be printed to overwrite system messages:

$ echo -e "/\rtest 12345678" > /sys/kernel/security/ima/policy

This patch rejects policy paths with control characters.

Signed-off-by: Tianxing Zhang <anakinzhang96@gmail.com>
---
 security/integrity/ima/ima_fs.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index 3d8e9d5db5aa..e6daa138de89 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -316,6 +316,7 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 {
 	char *data;
 	ssize_t result;
+	int i;
 
 	if (datalen >= PAGE_SIZE)
 		datalen = PAGE_SIZE - 1;
@@ -331,6 +332,14 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 		goto out;
 	}
 
+	for (i = 0; data[i] != '\n' && data[i] != '\0'; i++) {
+		if (iscntrl(data[i])) {
+			pr_err_once("file path with no control characters required\n");
+			result = -EINVAL;
+			goto out_free;
+		}
+	}
+
 	result = mutex_lock_interruptible(&ima_write_mutex);
 	if (result < 0)
 		goto out_free;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] ima: check control characters in policy file path
  2021-08-14  8:27 [PATCH 1/1] ima: check control characters in policy file path Tianxing Zhang
@ 2021-08-17 17:45 ` Mimi Zohar
  0 siblings, 0 replies; 2+ messages in thread
From: Mimi Zohar @ 2021-08-17 17:45 UTC (permalink / raw)
  To: Tianxing Zhang; +Cc: linux-integrity, linux-security-module, linux-kernel

On Sat, 2021-08-14 at 16:27 +0800, Tianxing Zhang wrote:
> When a policy file path contains control characters like '\r' or '\b',
> invalid error messages can be printed to overwrite system messages:
> 
> $ echo -e "/\rtest 12345678" > /sys/kernel/security/ima/policy
> 
> This patch rejects policy paths with control characters.
> 
> Signed-off-by: Tianxing Zhang <anakinzhang96@gmail.com>
> ---
>  security/integrity/ima/ima_fs.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
> index 3d8e9d5db5aa..e6daa138de89 100644
> --- a/security/integrity/ima/ima_fs.c
> +++ b/security/integrity/ima/ima_fs.c
> @@ -316,6 +316,7 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
>  {
>  	char *data;
>  	ssize_t result;
> +	int i;
>  
>  	if (datalen >= PAGE_SIZE)
>  		datalen = PAGE_SIZE - 1;
> @@ -331,6 +332,14 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
>  		goto out;
>  	}
>  
> +	for (i = 0; data[i] != '\n' && data[i] != '\0'; i++) {
> +		if (iscntrl(data[i])) {
> +			pr_err_once("file path with no control characters required\n");
> +			result = -EINVAL;
> +			goto out_free;
> +		}
> +	}
> +
>  	result = mutex_lock_interruptible(&ima_write_mutex);
>  	if (result < 0)
>  		goto out_free;

The IMA audit messages already display pathnames via
audit_log_untrustedstring().  Shouldn't any change be limited to the
ima_policy_read() code path?

thanks,

Mimi


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-17 17:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-14  8:27 [PATCH 1/1] ima: check control characters in policy file path Tianxing Zhang
2021-08-17 17:45 ` Mimi Zohar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).