From patchwork Tue May 16 12:53:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 9729161 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0802D602DB for ; Tue, 16 May 2017 12:59:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EBDB028A16 for ; Tue, 16 May 2017 12:59:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE81028A1C; Tue, 16 May 2017 12:59:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7DFF528A16 for ; Tue, 16 May 2017 12:59:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753144AbdEPM6r (ORCPT ); Tue, 16 May 2017 08:58:47 -0400 Received: from lhrrgout.huawei.com ([194.213.3.17]:26613 "EHLO lhrrgout.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753086AbdEPM6p (ORCPT ); Tue, 16 May 2017 08:58:45 -0400 Received: from 172.18.7.190 (EHLO lhreml703-cah.china.huawei.com) ([172.18.7.190]) by lhrrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DGS44176; Tue, 16 May 2017 12:58:38 +0000 (GMT) Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.66.1) by smtpsuk.huawei.com (10.201.108.44) with Microsoft SMTP Server (TLS) id 14.3.301.0; Tue, 16 May 2017 13:57:17 +0100 From: Roberto Sassu To: CC: , , Roberto Sassu Subject: [PATCH 6/7] ima: add securityfs interface to restore a measurements list Date: Tue, 16 May 2017 14:53:46 +0200 Message-ID: <20170516125347.10574-7-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170516125347.10574-1-roberto.sassu@huawei.com> References: <20170516125347.10574-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.66.1] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.591AF77E.009E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 356c6357c084b72f05049fc19efa0a59 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Through the new interface restore_kexec_list, it will be possible to restore a measurements list, previously read from binary_kexec_runtime_measurements. The patch reuses the policy functions to create a buffer, read the measurements and call ima_restore_measurement_list(). Signed-off-by: Roberto Sassu --- security/integrity/ima/ima_fs.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c index a93f941..6e3f93f 100644 --- a/security/integrity/ima/ima_fs.c +++ b/security/integrity/ima/ima_fs.c @@ -77,6 +77,7 @@ static const struct file_operations ima_measurements_count_ops = { }; static struct dentry *binary_kexec_runtime_measurements; +static struct dentry *restore_kexec_list; /* returns pointer to hlist_node */ static void *ima_measurements_start(struct seq_file *m, loff_t *pos) @@ -297,7 +298,7 @@ static const struct file_operations ima_ascii_measurements_ops = { .release = seq_release, }; -static ssize_t ima_read_policy(char *path) +static ssize_t ima_read_policy(char *path, bool khdr) { void *data; char *datap; @@ -317,6 +318,13 @@ static ssize_t ima_read_policy(char *path) } datap = data; + + if (khdr) { + rc = ima_restore_measurement_list(size, data); + size = 0; + goto out; + } + while (size > 0 && (p = strsep(&datap, "\n"))) { pr_debug("rule: %s\n", p); rc = ima_parse_add_rule(p); @@ -325,6 +333,7 @@ static ssize_t ima_read_policy(char *path) size -= rc; } +out: vfree(data); if (rc < 0) return rc; @@ -337,6 +346,7 @@ static ssize_t ima_read_policy(char *path) static ssize_t ima_write_policy(struct file *file, const char __user *buf, size_t datalen, loff_t *ppos) { + bool khdr = file->f_path.dentry == restore_kexec_list; char *data; ssize_t result; @@ -363,8 +373,13 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, if (result < 0) goto out_free; + if (khdr && data[0] != '/') { + mutex_unlock(&ima_write_mutex); + goto out_free; + } + if (data[0] == '/') { - result = ima_read_policy(data); + result = ima_read_policy(data, khdr); } else if (ima_appraise & IMA_APPRAISE_POLICY) { pr_err("IMA: signed policy file (specified as an absolute pathname) required\n"); integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL, @@ -393,7 +408,7 @@ static struct dentry *violations; static struct dentry *ima_policy; enum ima_fs_flags { - IMA_FS_BUSY, + IMA_FS_BUSY, IMA_RESTORE_LIST_BUSY, }; static unsigned long ima_fs_flags; @@ -412,6 +427,9 @@ static const struct seq_operations ima_policy_seqops = { */ static int ima_open_policy(struct inode *inode, struct file *filp) { + bool khdr = filp->f_path.dentry == restore_kexec_list; + unsigned long bit = khdr ? IMA_RESTORE_LIST_BUSY : IMA_FS_BUSY; + if (!(filp->f_flags & O_WRONLY)) { #ifndef CONFIG_IMA_READ_POLICY return -EACCES; @@ -423,7 +441,7 @@ static int ima_open_policy(struct inode *inode, struct file *filp) return seq_open(filp, &ima_policy_seqops); #endif } - if (test_and_set_bit(IMA_FS_BUSY, &ima_fs_flags)) + if (test_and_set_bit(bit, &ima_fs_flags)) return -EBUSY; return 0; } @@ -439,6 +457,11 @@ static int ima_release_policy(struct inode *inode, struct file *file) { const char *cause = valid_policy ? "completed" : "failed"; + if (file->f_path.dentry == restore_kexec_list) { + clear_bit(IMA_RESTORE_LIST_BUSY, &ima_fs_flags); + return 0; + } + if ((file->f_flags & O_ACCMODE) == O_RDONLY) return seq_release(inode, file); @@ -522,9 +545,16 @@ int __init ima_fs_init(void) &ima_measurements_ops); if (IS_ERR(binary_kexec_runtime_measurements)) goto out; + + restore_kexec_list = securityfs_create_file("restore_kexec_list", + S_IWUSR, ima_dir, NULL, + &ima_measure_policy_ops); + if (IS_ERR(restore_kexec_list)) + goto out; #endif return 0; out: + securityfs_remove(restore_kexec_list); securityfs_remove(binary_kexec_runtime_measurements); securityfs_remove(violations); securityfs_remove(runtime_measurements_count);