diff mbox series

ima: Replace two seq_printf() calls by seq_puts() in ima_show_template_data_ascii()

Message ID e96eac40-0745-80b5-6aab-f872e6415031@web.de (mailing list archive)
State New, archived
Headers show
Series ima: Replace two seq_printf() calls by seq_puts() in ima_show_template_data_ascii() | expand

Commit Message

Markus Elfring July 2, 2019, 7 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 2 Jul 2019 20:52:21 +0200

Two strings which did not contain a data format specification should be put
into a sequence. Thus use the corresponding function “seq_puts”.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 security/integrity/ima/ima_template_lib.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--
2.22.0

Comments

David Laight July 3, 2019, 9:16 a.m. UTC | #1
From:  Markus Elfring
> Sent: 02 July 2019 20:01
> 
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 2 Jul 2019 20:52:21 +0200
> 
> Two strings which did not contain a data format specification should be put
> into a sequence. Thus use the corresponding function “seq_puts”.
> 
> This issue was detected by using the Coccinelle software.

The two calls are almost certainly absolutely equivalent.
So this is probably just a minor performance improvement in a code
path where it really doesn't matter.

> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  security/integrity/ima/ima_template_lib.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
> index 9fe0ef7f91e2..05636e9b19b1 100644
> --- a/security/integrity/ima/ima_template_lib.c
> +++ b/security/integrity/ima/ima_template_lib.c
> @@ -74,7 +74,7 @@ static void ima_show_template_data_ascii(struct seq_file *m,
>  	case DATA_FMT_DIGEST_WITH_ALGO:
>  		buf_ptr = strnchr(field_data->data, buflen, ':');
>  		if (buf_ptr != field_data->data)
> -			seq_printf(m, "%s", field_data->data);
> +			seq_puts(m, field_data->data);
> 
>  		/* skip ':' and '\0' */
>  		buf_ptr += 2;

That code looks highly suspect!
It uses a bounded scan then assumes a '\0' terminated string.
It then adds 2 to a potentially NULL pointer.

About typical for 'security' code :-)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Markus Elfring July 3, 2019, 9:33 a.m. UTC | #2
> So this is probably just a minor performance improvement in a code
> path where it really doesn't matter.

I imagine that another small software adjustment can help a bit
to get nicer run time characteristics also at this place.


>> +++ b/security/integrity/ima/ima_template_lib.c
>> @@ -74,7 +74,7 @@ static void ima_show_template_data_ascii(struct seq_file *m,
>>  	case DATA_FMT_DIGEST_WITH_ALGO:
>>  		buf_ptr = strnchr(field_data->data, buflen, ':');
>>  		if (buf_ptr != field_data->data)
>> -			seq_printf(m, "%s", field_data->data);
>> +			seq_puts(m, field_data->data);
>>
>>  		/* skip ':' and '\0' */
>>  		buf_ptr += 2;
>
> That code looks highly suspect!

Would you like to change this implementation detail any more?

Regards,
Markus
Mimi Zohar July 3, 2019, 11:14 a.m. UTC | #3
On Wed, 2019-07-03 at 09:16 +0000, David Laight wrote:

> > diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
> > index 9fe0ef7f91e2..05636e9b19b1 100644
> > --- a/security/integrity/ima/ima_template_lib.c
> > +++ b/security/integrity/ima/ima_template_lib.c
> > @@ -74,7 +74,7 @@ static void ima_show_template_data_ascii(struct seq_file *m,
> >  	case DATA_FMT_DIGEST_WITH_ALGO:
> >  		buf_ptr = strnchr(field_data->data, buflen, ':');
> >  		if (buf_ptr != field_data->data)
> > -			seq_printf(m, "%s", field_data->data);
> > +			seq_puts(m, field_data->data);
> > 
> >  		/* skip ':' and '\0' */
> >  		buf_ptr += 2;
> 
> That code looks highly suspect!
> It uses a bounded scan then assumes a '\0' terminated string.
> It then adds 2 to a potentially NULL pointer.

The code here is used for displaying the IMA measurement list, that
the kernel itself created.  Protecting the in kernel memory from
attack is a different problem.  Refer to Igor Stoppa's write once
memory pools.

Mimi
diff mbox series

Patch

diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c
index 9fe0ef7f91e2..05636e9b19b1 100644
--- a/security/integrity/ima/ima_template_lib.c
+++ b/security/integrity/ima/ima_template_lib.c
@@ -74,7 +74,7 @@  static void ima_show_template_data_ascii(struct seq_file *m,
 	case DATA_FMT_DIGEST_WITH_ALGO:
 		buf_ptr = strnchr(field_data->data, buflen, ':');
 		if (buf_ptr != field_data->data)
-			seq_printf(m, "%s", field_data->data);
+			seq_puts(m, field_data->data);

 		/* skip ':' and '\0' */
 		buf_ptr += 2;
@@ -87,7 +87,7 @@  static void ima_show_template_data_ascii(struct seq_file *m,
 		ima_print_digest(m, buf_ptr, buflen);
 		break;
 	case DATA_FMT_STRING:
-		seq_printf(m, "%s", buf_ptr);
+		seq_puts(m, buf_ptr);
 		break;
 	default:
 		break;