All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mimi Zohar <zohar@linux.ibm.com>
To: Lakshmi Ramasubramanian <nramas@linux.microsoft.com>,
	linux-integrity@vger.kernel.org
Cc: sashal@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1] IMA: pre-allocate buffer to hold keyrings string
Date: Thu, 16 Jan 2020 20:17:06 -0500	[thread overview]
Message-ID: <1579223826.5049.13.camel@linux.ibm.com> (raw)
In-Reply-To: <20200116234623.2959-1-nramas@linux.microsoft.com>

Hi Lakshmi,

Trimming the Cc list. This patch is limited to IMA. 

On Thu, 2020-01-16 at 15:46 -0800, Lakshmi Ramasubramanian wrote:

>  
> @@ -949,6 +949,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
>  	bool uid_token;
>  	struct ima_template_desc *template_desc;
>  	int result = 0;
> +	size_t keyrings_len;
>  
>  	ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
>  				       AUDIT_INTEGRITY_POLICY_RULE);
> @@ -1114,14 +1115,47 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
>  		case Opt_keyrings:
>  			ima_log_string(ab, "keyrings", args[0].from);
>  
> +			keyrings_len = strlen(args[0].from) + 1;
> +
>  			if ((entry->keyrings) ||
>  			    (entry->action != MEASURE) ||
> -			    (entry->func != KEY_CHECK)) {
> +			    (entry->func != KEY_CHECK) ||
> +			    (keyrings_len < 2)) {
>  				result = -EINVAL;
>  				break;
>  			}
> +
> +			if (ima_keyrings) {
> +				if (keyrings_len > ima_keyrings_len) {
> +					char *tmpbuf;
> +
> +					tmpbuf = krealloc(ima_keyrings,
> +							  keyrings_len,
> +							  GFP_KERNEL);
> +					if (!tmpbuf) {
> +						result = -ENOMEM;
> +						break;
> +					}
> +
> +					ima_keyrings = tmpbuf;
> +					ima_keyrings_len = keyrings_len;
> +				}
> +			} else {
> +				ima_keyrings = kzalloc(keyrings_len,
> +						       GFP_KERNEL);
> +				if (!ima_keyrings) {
> +					result = -ENOMEM;
> +					break;
> +				}
> +
> +				ima_keyrings_len = keyrings_len;
> +			}

The first time this code is executed ima_keyrings_len is 0.  So "if
(ima_keyrings_len < keyring_len)" will be true.  There's no reason to
differentiate between the first time or subsequent times this code is
executed.  In both cases, krealloc() can be used. 

Mimi


> +
>  			entry->keyrings = kstrdup(args[0].from, GFP_KERNEL);
>  			if (!entry->keyrings) {
> +				kfree(ima_keyrings);
> +				ima_keyrings = NULL;
> +				ima_keyrings_len = 0;
>  				result = -ENOMEM;
>  				break;
>  			}


      reply	other threads:[~2020-01-17  1:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-16 23:46 [PATCH v1] IMA: pre-allocate buffer to hold keyrings string Lakshmi Ramasubramanian
2020-01-16 23:46 ` Lakshmi Ramasubramanian
2020-01-17  1:17 ` Mimi Zohar [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1579223826.5049.13.camel@linux.ibm.com \
    --to=zohar@linux.ibm.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nramas@linux.microsoft.com \
    --cc=sashal@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.