mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mimi Zohar <zohar@linux.vnet.ibm.com>
To: linux-security-module@vger.kernel.org
Cc: Dmitry Kasatkin <d.kasatkin@samsung.com>,
	linux-kernel@vger.kernel.org, James Morris <jmorris@namei.org>,
	David Howells <dhowells@redhat.com>,
	Mimi Zohar <zohar@linux.vnet.ibm.com>
Subject: [PATCH v2 23/23] ima: provide hash algo info in the xattr
Date: Mon, 21 Oct 2013 18:43:08 -0400	[thread overview]
Message-ID: <1382395388-8108-24-git-send-email-zohar@linux.vnet.ibm.com> (raw)
In-Reply-To: <1382395388-8108-1-git-send-email-zohar@linux.vnet.ibm.com>

From: Dmitry Kasatkin <d.kasatkin@samsung.com>

All files labeled with 'security.ima' hashes, are hashed using the
same hash algorithm.  Changing from one hash algorithm to another,
requires relabeling the filesystem.  This patch defines a new xattr
type, which includes the hash algorithm, permitting different files
to be hashed with different algorithms.

Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
---
 security/integrity/ima/ima_appraise.c | 61 +++++++++++++++++++++++++++--------
 security/integrity/integrity.h        | 13 +++++++-
 2 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index 116630c..734e946 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -15,6 +15,7 @@
 #include <linux/magic.h>
 #include <linux/ima.h>
 #include <linux/evm.h>
+#include <crypto/hash_info.h>
 
 #include "ima.h"
 
@@ -45,10 +46,22 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
 static int ima_fix_xattr(struct dentry *dentry,
 			 struct integrity_iint_cache *iint)
 {
-	iint->ima_hash->type = IMA_XATTR_DIGEST;
-	return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
-				     &iint->ima_hash->type,
-				     1 + iint->ima_hash->length, 0);
+	int rc, offset;
+	u8 algo = iint->ima_hash->algo;
+
+	if (algo <= HASH_ALGO_SHA1) {
+		offset = 1;
+		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
+	} else {
+		offset = 0;
+		iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
+		iint->ima_hash->xattr.ng.algo = algo;
+	}
+	rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
+				   &iint->ima_hash->xattr.data[offset],
+				   (sizeof(iint->ima_hash->xattr) - offset) +
+				   iint->ima_hash->length, 0);
+	return rc;
 }
 
 /* Return specific func appraised cached result */
@@ -112,15 +125,31 @@ void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
 {
 	struct signature_v2_hdr *sig;
 
-	if (!xattr_value || xattr_len < 0 || xattr_len <= 1 + sizeof(*sig))
+	if (!xattr_value || xattr_len < 2)
 		return;
 
-	sig = (typeof(sig)) xattr_value->digest;
-
-	if (xattr_value->type != EVM_IMA_XATTR_DIGSIG || sig->version != 2)
-		return;
-
-	hash->algo = sig->hash_algo;
+	switch (xattr_value->type) {
+	case EVM_IMA_XATTR_DIGSIG:
+		sig = (typeof(sig))xattr_value;
+		if (sig->version != 2 || xattr_len <= sizeof(*sig))
+			return;
+		hash->algo = sig->hash_algo;
+		break;
+	case IMA_XATTR_DIGEST_NG:
+		hash->algo = xattr_value->digest[0];
+		break;
+	case IMA_XATTR_DIGEST:
+		/* this is for backward compatibility */
+		if (xattr_len == 21) {
+			unsigned int zero = 0;
+			if (!memcmp(&xattr_value->digest[16], &zero, 4))
+				hash->algo = HASH_ALGO_MD5;
+			else
+				hash->algo = HASH_ALGO_SHA1;
+		} else if (xattr_len == 17)
+			hash->algo = HASH_ALGO_MD5;
+		break;
+	}
 }
 
 int ima_read_xattr(struct dentry *dentry,
@@ -153,7 +182,7 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
 	enum integrity_status status = INTEGRITY_UNKNOWN;
 	const char *op = "appraise_data";
 	char *cause = "unknown";
-	int rc = xattr_len;
+	int rc = xattr_len, hash_start = 0;
 
 	if (!ima_appraise)
 		return 0;
@@ -180,17 +209,21 @@ int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
 		goto out;
 	}
 	switch (xattr_value->type) {
+	case IMA_XATTR_DIGEST_NG:
+		/* first byte contains algorithm id */
+		hash_start = 1;
 	case IMA_XATTR_DIGEST:
 		if (iint->flags & IMA_DIGSIG_REQUIRED) {
 			cause = "IMA signature required";
 			status = INTEGRITY_FAIL;
 			break;
 		}
-		if (xattr_len - 1 >= iint->ima_hash->length)
+		if (xattr_len - sizeof(xattr_value->type) - hash_start >=
+				iint->ima_hash->length)
 			/* xattr length may be longer. md5 hash in previous
 			   version occupied 20 bytes in xattr, instead of 16
 			 */
-			rc = memcmp(xattr_value->digest,
+			rc = memcmp(&xattr_value->digest[hash_start],
 				    iint->ima_hash->digest,
 				    iint->ima_hash->length);
 		else
diff --git a/security/integrity/integrity.h b/security/integrity/integrity.h
index 5429ca5..2fb5e53 100644
--- a/security/integrity/integrity.h
+++ b/security/integrity/integrity.h
@@ -54,6 +54,7 @@ enum evm_ima_xattr_type {
 	IMA_XATTR_DIGEST = 0x01,
 	EVM_XATTR_HMAC,
 	EVM_IMA_XATTR_DIGSIG,
+	IMA_XATTR_DIGEST_NG,
 };
 
 struct evm_ima_xattr_data {
@@ -66,7 +67,17 @@ struct evm_ima_xattr_data {
 struct ima_digest_data {
 	u8 algo;
 	u8 length;
-	u8 type;
+	union {
+		struct {
+			u8 unused;
+			u8 type;
+		} sha1;
+		struct {
+			u8 type;
+			u8 algo;
+		} ng;
+		u8 data[2];
+	} xattr;
 	u8 digest[0];
 } __packed;
 
-- 
1.8.1.4


      parent reply	other threads:[~2013-10-21 22:44 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-21 22:42 [PATCH v2 00/23] ima: larger digests and extensible template support Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 01/23] crypto: provide single place for hash algo information Mimi Zohar
2013-10-22  5:24   ` Herbert Xu
2013-10-22 11:29     ` Dmitry Kasatkin
2013-10-22 11:32       ` Herbert Xu
2013-10-22 11:57         ` Dmitry Kasatkin
2013-10-22 12:07           ` Herbert Xu
2013-10-22 12:50             ` Mimi Zohar
2013-10-22 12:53               ` Herbert Xu
2013-10-21 22:42 ` [PATCH v2 02/23] keys: change asymmetric keys to use common hash definitions Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 03/23] ima: provide support for arbitrary hash algorithms Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 04/23] ima: read and use signature hash algorithm Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 05/23] ima: pass full xattr with the signature Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 06/23] ima: use dynamically allocated hash storage Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 07/23] ima: differentiate between template hash and file data hash sizes Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 08/23] ima: provide dedicated hash algo allocation function Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 09/23] ima: support arbitrary hash algorithms in ima_calc_buffer_hash Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 10/23] ima: ima_calc_boot_agregate must use SHA1 Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 11/23] ima: pass the file descriptor to ima_add_violation() Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 12/23] ima: pass the filename argument up to ima_add_template_entry() Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 13/23] ima: define new function ima_alloc_init_template() to API Mimi Zohar
2013-10-21 22:42 ` [PATCH v2 14/23] ima: new templates management mechanism Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 15/23] ima: define template fields library and new helpers Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 16/23] ima: define new template ima-ng and template fields d-ng and n-ng Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 17/23] ima: switch to new template management mechanism Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 18/23] ima: add audit log support for larger hashes Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 19/23] ima: defer determining the appraisal hash algorithm for 'ima' template Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 20/23] ima: add Kconfig default measurement list template Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 21/23] ima: define kernel parameter 'ima_template=' to change configured default Mimi Zohar
2013-10-21 22:43 ` [PATCH v2 22/23] ima: enable support for larger default filedata hash algorithms Mimi Zohar
2013-10-21 22:43 ` 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=1382395388-8108-24-git-send-email-zohar@linux.vnet.ibm.com \
    --to=zohar@linux.vnet.ibm.com \
    --cc=d.kasatkin@samsung.com \
    --cc=dhowells@redhat.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®