linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
To: zohar@linux.ibm.com, dmitry.kasatkin@gmail.com,
	jmorris@namei.org, serge@hallyn.com,
	zhangliguang@linux.alibaba.com
Cc: linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] ima: support to tune appraise mode in runtime
Date: Thu,  9 Apr 2020 11:39:07 +0800	[thread overview]
Message-ID: <20200409033907.102833-3-tianjia.zhang@linux.alibaba.com> (raw)
In-Reply-To: <20200409033907.102833-1-tianjia.zhang@linux.alibaba.com>

In order to tune appraise mode in runtime, writing a PKCS#7 signature
corresponding the signed content is required. The content should be off,
enforce, log or fix. Given a simple way to archive this:

$ echo -n off > mode
$ openssl smime -sign -nocerts -noattr -binary \
    -in mode -inkey <system_trusted_key> \
    -signer <cert> -outform der -out mode.p7s
$ sudo cat mode.p7s \
    > /sys/kernel/security/ima/appraise_mode

Note that the signing key must be a trust key located in
system trusted keyring. So even the root privilege cannot
simply disable the enforcement.

Signed-off-by: luanshi <zhangliguang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
---
 security/integrity/ima/ima_fs.c | 102 ++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index 65384f6ac0d9..c21ca145de0f 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -20,11 +20,15 @@
 #include <linux/rcupdate.h>
 #include <linux/parser.h>
 #include <linux/vmalloc.h>
+#include <linux/verification.h>
 
 #include "ima.h"
 
 static DEFINE_MUTEX(ima_write_mutex);
 
+/* maximum length of token allowed for signed appraise mode */
+#define APPRAISE_MAX_TOKEN_SIZE (512 * 1024)
+
 bool ima_canonical_fmt;
 static int __init default_canonical_fmt_setup(char *str)
 {
@@ -466,8 +470,106 @@ static ssize_t ima_appraise_mode_read(struct file *filp,
 	return simple_read_from_buffer(buf, count, ppos, mode, strlen(mode));
 }
 
+static int check_signature_info(char *buf, size_t count)
+{
+	u8 *p;
+
+	/*
+	 * In order to tune the appraise mode, a PKCS#7 signature is
+	 * supplied.
+	 *
+	 * Assuming ASN.1 encoding supplied, the minimal length would be
+	 * 4-byte header plus at least 256-byte payload.
+	 */
+	if (count < 260)
+		return -EINVAL;
+
+	p = (u8 *)buf;
+
+	/* The primitive type must be a sequence */
+	if (p[0] != 0x30 || p[1] != 0x82)
+		return -EINVAL;
+
+	/* Match up the length of the supplied buffer */
+	if (be16_to_cpup((__be16 *)(p + 2)) != count - 4)
+		return -EINVAL;
+
+	return 0;
+}
+
+/* Verify the supplied PKCS#7 signature. The signed content may be off,
+ * enforce, log, fix.
+ */
+static int repopulate_ima_appraise_mode(void *pkcs7, size_t pkcs7_len)
+{
+	static char *appraise_mode_strings[] = { "off", "enforce", "fix", "log" };
+	static int appraise_modes[] = {
+		0,
+		IMA_APPRAISE_ENFORCE,
+		IMA_APPRAISE_FIX,
+		IMA_APPRAISE_LOG,
+	};
+	int index, ret = -1;
+	const char *s;
+	int size = ARRAY_SIZE(appraise_mode_strings);
+
+	for (index = 0; index < size; index++) {
+		s = appraise_mode_strings[index];
+		ret = verify_pkcs7_signature(s, strlen(s), pkcs7, pkcs7_len,
+					    NULL, VERIFYING_UNSPECIFIED_SIGNATURE,
+					    NULL, NULL);
+		if (!ret)
+			break;
+	}
+
+	if (index == size)
+		goto out;
+
+	ima_appraise = appraise_modes[index];
+
+out:
+	return ret;
+}
+
+static ssize_t ima_appraise_mode_write(struct file *filp,
+					const char __user *ubuf,
+					size_t count, loff_t *ppos)
+{
+	char *buf;
+	ssize_t ret;
+
+	if (*ppos > 1)
+		return -EFBIG;
+
+	if (count > APPRAISE_MAX_TOKEN_SIZE)
+		return -EFBIG;
+
+	buf = kmalloc(count, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = simple_write_to_buffer(buf, count, ppos, ubuf, count);
+	if (ret <= 0)
+		goto out;
+
+	ret = check_signature_info(buf, count);
+	if (ret)
+		goto out;
+
+	ret = repopulate_ima_appraise_mode(buf, count);
+	if (ret)
+		goto out;
+
+	ret = count;
+
+out:
+	kfree(buf);
+	return ret;
+}
+
 static const struct file_operations ima_appraise_mode_ops = {
 	.read = ima_appraise_mode_read,
+	.write = ima_appraise_mode_write,
 	.llseek = generic_file_llseek,
 };
 
-- 
2.17.1


  parent reply	other threads:[~2020-04-09  3:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-09  3:39 [PATCH 0/2] support to read and tune appraise mode in runtime Tianjia Zhang
2020-04-09  3:39 ` [PATCH 1/2] ima: support to read appraise mode Tianjia Zhang
2020-04-09  3:39 ` Tianjia Zhang [this message]
2020-04-09  5:40   ` [PATCH 2/2] ima: support to tune appraise mode in runtime kbuild test robot
2020-04-13 21:55 ` [PATCH 0/2] support to read and " Mimi Zohar
2020-04-14  3:36   ` Tianjia Zhang
2020-04-14 13:41     ` Mimi Zohar
2020-04-15  2:49       ` Tianjia Zhang

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=20200409033907.102833-3-tianjia.zhang@linux.alibaba.com \
    --to=tianjia.zhang@linux.alibaba.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=jmorris@namei.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=zhangliguang@linux.alibaba.com \
    --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
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;
as well as URLs for NNTP newsgroup(s).