Linux-Integrity Archive on lore.kernel.org
 help / color / Atom feed
From: Roberto Sassu <roberto.sassu@huawei.com>
To: Mimi Zohar <zohar@linux.ibm.com>,
	"jarkko.sakkinen@linux.intel.com"
	<jarkko.sakkinen@linux.intel.com>,
	"james.bottomley@hansenpartnership.com" 
	<james.bottomley@hansenpartnership.com>,
	"linux-integrity@vger.kernel.org"
	<linux-integrity@vger.kernel.org>
Cc: "linux-security-module@vger.kernel.org" 
	<linux-security-module@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Silviu Vlasceanu <Silviu.Vlasceanu@huawei.com>
Subject: RE: [PATCH 5/8] ima: allocate and initialize tfm for each PCR bank
Date: Fri, 31 Jan 2020 13:42:13 +0000
Message-ID: <17b00b78aa2249f19ba376c7613cfb38@huawei.com> (raw)
In-Reply-To: <1580473133.6104.48.camel@linux.ibm.com>

> -----Original Message-----
> From: linux-integrity-owner@vger.kernel.org [mailto:linux-integrity-
> owner@vger.kernel.org] On Behalf Of Mimi Zohar
> Sent: Friday, January 31, 2020 1:19 PM
> To: Roberto Sassu <roberto.sassu@huawei.com>;
> jarkko.sakkinen@linux.intel.com;
> james.bottomley@hansenpartnership.com; linux-integrity@vger.kernel.org
> Cc: linux-security-module@vger.kernel.org; linux-kernel@vger.kernel.org;
> Silviu Vlasceanu <Silviu.Vlasceanu@huawei.com>
> Subject: Re: [PATCH 5/8] ima: allocate and initialize tfm for each PCR bank
> 
> On Mon, 2020-01-27 at 18:04 +0100, Roberto Sassu wrote:
> > This patch creates a crypto_shash structure for each allocated PCR bank
> and
> > for SHA1 if a bank with that algorithm is not currently allocated.
> >
> > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> 
> <snip>
> 
> > +int __init ima_init_crypto(void)
> > +{
> > +	enum hash_algo algo;
> > +	long rc;
> > +	int i;
> > +
> > +	rc = ima_init_ima_crypto();
> > +	if (rc)
> > +		return rc;
> > +
> > +	ima_algo_array = kmalloc_array(ima_tpm_chip-
> >nr_allocated_banks + 1,
> > +				       sizeof(*ima_algo_array), GFP_KERNEL);
> 
> Perhaps instead of hard coding "nr_allocated_banks + 1", create a
> variable for storing the number of "extra" banks.  Walk the banks
> setting ima_sha1_idx and, later, in a subsequent patch set
> ima_hash_algo_idx.

I could store the indexes in an array and add ARRAY_SIZE of that array.

> > +	if (!ima_algo_array) {
> > +		rc = -ENOMEM;
> > +		goto out;
> > +	}
> > +
> > +	for (i = 0; i < ima_tpm_chip->nr_allocated_banks + 1; i++) {
> > +		ima_algo_array[i].tfm = NULL;
> > +		ima_algo_array[i].algo = HASH_ALGO__LAST;
> > +	}
> 
> ima_init_crypto() is executed once on initialization.  I'm not sure if
> it makes a difference, but if you're really concerned about an
> additional loop, the initialization, here, could be limited to the
> "extra" banks.  The other banks could be initialized at the beginning
> of the next loop.

I set algo to HASH_ALGO__LAST to mark the ima_algo_array entries as
uninitialized. ima_alloc_tfm() uses ima_algo_array in the next loop.
Replacing kmalloc_array() with kcalloc() would cause algo to be set to
HASH_ALGO_MD4.

I could check tfm first, which ensures that also algo was initialized.

Roberto

> > +	ima_sha1_idx = -1;
> > +
> > +	for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
> > +		algo = ima_tpm_chip->allocated_banks[i].crypto_id;
> > +		ima_algo_array[i].algo = algo;
> > +
> > +		/* unknown TPM algorithm */
> > +		if (algo == HASH_ALGO__LAST)
> > +			continue;
> > +
> > +		if (algo == HASH_ALGO_SHA1)
> > +			ima_sha1_idx = i;
> > +
> > +		if (algo == ima_hash_algo) {
> > +			ima_algo_array[i].tfm = ima_shash_tfm;
> > +			continue;
> > +		}
> > +
> > +		ima_algo_array[i].tfm = ima_alloc_tfm(algo);
> > +		if (IS_ERR(ima_algo_array[i].tfm)) {
> > +			if (algo == HASH_ALGO_SHA1) {
> > +				rc = PTR_ERR(ima_algo_array[i].tfm);
> > +				ima_algo_array[i].tfm = NULL;
> > +				goto out_array;
> > +			}
> > +
> > +			ima_algo_array[i].tfm = NULL;
> > +		}
> > +	}
> > +
> > +	if (ima_sha1_idx < 0) {
> > +		ima_algo_array[i].tfm = ima_alloc_tfm(HASH_ALGO_SHA1);
> > +		if (IS_ERR(ima_algo_array[i].tfm)) {
> > +			rc = PTR_ERR(ima_algo_array[i].tfm);
> > +			goto out_array;
> > +		}
> > +
> > +		ima_sha1_idx = i;
> > +		ima_algo_array[i].algo = HASH_ALGO_SHA1;
> > +	}
> > +
> > +	return 0;
> > +out_array:
> > +	for (i = 0; i < ima_tpm_chip->nr_allocated_banks + 1; i++) {
> > +		if (!ima_algo_array[i].tfm ||
> > +		    ima_algo_array[i].tfm == ima_shash_tfm)
> > +			continue;
> > +
> > +		crypto_free_shash(ima_algo_array[i].tfm);
> > +	}
> > +out:
> > +	crypto_free_shash(ima_shash_tfm);
> > +	return rc;
> > +}


  reply index

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-27 17:04 [PATCH 0/8] ima: support stronger algorithms for attestation Roberto Sassu
2020-01-27 17:04 ` [PATCH 1/8] tpm: initialize crypto_id of allocated_banks to HASH_ALGO__LAST Roberto Sassu
2020-01-29  8:39   ` Petr Vorel
2020-01-30  8:47   ` Jarkko Sakkinen
2020-01-30 16:11     ` Roberto Sassu
2020-01-31 13:33       ` Mimi Zohar
2020-02-01 17:10         ` Jarkko Sakkinen
2020-01-27 17:04 ` [PATCH 2/8] ima: evaluate error in init_ima() Roberto Sassu
2020-01-31 13:33   ` Mimi Zohar
2020-01-27 17:04 ` [PATCH 3/8] ima: store template digest directly in ima_template_entry Roberto Sassu
2020-01-27 17:04 ` [PATCH 4/8] ima: switch to dynamically allocated buffer for template digests Roberto Sassu
2020-01-27 17:04 ` [PATCH 5/8] ima: allocate and initialize tfm for each PCR bank Roberto Sassu
2020-01-31 12:18   ` Mimi Zohar
2020-01-31 13:42     ` Roberto Sassu [this message]
2020-01-31 13:58       ` Mimi Zohar
2020-01-27 17:04 ` [PATCH 6/8] ima: calculate and extend PCR with digests in ima_template_entry Roberto Sassu
2020-01-27 17:29   ` Roberto Sassu
2020-01-27 17:04 ` [PATCH 7/8] ima: use ima_hash_algo for collision detection in the measurement list Roberto Sassu
2020-01-30 22:26   ` Mimi Zohar
2020-01-31 14:02     ` Roberto Sassu
2020-01-31 14:22       ` Mimi Zohar
2020-01-31 14:41         ` Roberto Sassu
2020-01-31 14:50           ` Mimi Zohar
2020-01-27 17:04 ` [PATCH 8/8] ima: switch to ima_hash_algo for boot aggregate Roberto Sassu
2020-01-31 15:21   ` Roberto Sassu
2020-01-30 22:26 ` [PATCH 0/8] ima: support stronger algorithms for attestation Mimi Zohar

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=17b00b78aa2249f19ba376c7613cfb38@huawei.com \
    --to=roberto.sassu@huawei.com \
    --cc=Silviu.Vlasceanu@huawei.com \
    --cc=james.bottomley@hansenpartnership.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=zohar@linux.ibm.com \
    /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

Linux-Integrity Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-integrity/0 linux-integrity/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-integrity linux-integrity/ https://lore.kernel.org/linux-integrity \
		linux-integrity@vger.kernel.org
	public-inbox-index linux-integrity

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-integrity


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git