From patchwork Tue Jan 16 10:41:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alban Crequy X-Patchwork-Id: 10166443 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 A7773601E7 for ; Tue, 16 Jan 2018 10:42:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9485928459 for ; Tue, 16 Jan 2018 10:42:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8692028468; Tue, 16 Jan 2018 10:42:11 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham 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 C1EF728459 for ; Tue, 16 Jan 2018 10:42:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751195AbeAPKmK (ORCPT ); Tue, 16 Jan 2018 05:42:10 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:46102 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750892AbeAPKmJ (ORCPT ); Tue, 16 Jan 2018 05:42:09 -0500 Received: by mail-wm0-f66.google.com with SMTP id 143so7300366wma.5; Tue, 16 Jan 2018 02:42:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=PcjZXq50+RUvXkCEq9JboO/ECyPFt4ni+42ZIfcHUZ0=; b=h0m7tZRZXFqiF7bCEIp6f+3KmCXhVACCfOajHU5/YER5us76yMHgBSCe+2/wMw9tI7 Vc0xwUQ8Hzc+iQSIPbpL6WSGt7vZXcP/n/4gstSBqSu1xb0FY3yddHp9RGvwWvNTnncy lQPlni7fjh3CKpUgo5htVdDX5W960c1KXFbyn8A68vhtFfCE27AhZBnBstm+V2KowyG6 /ZFc46baXnfb2QUTe+Vx50hRv5W/05KwFWyX4czuOZm8Nzr66o9ZTmtNX6MdxVWa7suf CxVNcF+8fdksbUQHarPAQjOGz4e1L/z8Wq7Cg0BVRJw+IjOU+c2ACaG5kWUvMFc+sGbv VDsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=PcjZXq50+RUvXkCEq9JboO/ECyPFt4ni+42ZIfcHUZ0=; b=IXhKqSBDGRiIMJgVFa3JV0JX/W0tKduo9xobqR87WFm8rctAvAt4F4i8aBEuuDSCDc YNELdMEJ7NNSOD/eCxbQIMCfI0Ebtu/nqz21hDS1ihaD+urgWsg0tOtIIVzCLXdNHX7l Ze7g8QG91u202D809JhHc1T2xbssNhfEON/C45odRRNCUR0p35FxLPnUejcIiJvYlLT6 KOSfmTSfD/Jzh9ngzyQLlEa834PYYD6lh7eZpVwS+Jfz+NYENX1tcaVc6GsIbI8wZiLa uqCFmJFXNoI25+21tFiIRFaSefOGnd5q3Jx8GkmvNeyLPAloH1/ziF3zXfNJUO4iTtK0 T9wA== X-Gm-Message-State: AKGB3mJheN3cJzW6A7FIb2v4tXww4BwlqLCnKF79F990pDQXU+efRLn5 zymKL/7QGboGTBrLdPgnMuc= X-Google-Smtp-Source: ACJfBouusvPm5hPfHuG76XI639j+hu7ptLkdEJW7ZfLLBECUQZwY62p/l+23gR+VwIF9NgDIxy7e8A== X-Received: by 10.80.167.161 with SMTP id i30mr52594515edc.26.1516099327450; Tue, 16 Jan 2018 02:42:07 -0800 (PST) Received: from localhost.localdomain ([178.19.216.175]) by smtp.gmail.com with ESMTPSA id d3sm1259234edc.15.2018.01.16.02.42.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Jan 2018 02:42:06 -0800 (PST) From: Alban Crequy X-Google-Original-From: Alban Crequy To: Alban Crequy Cc: iago@kinvolk.io, dongsu@kinvolk.io, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Miklos Szeredi , Mimi Zohar , Seth Forshee , Christoph Hellwig Subject: [PATCH] ima,fuse: introduce new fs flag FS_NO_IMA_CACHE Date: Tue, 16 Jan 2018 11:41:21 +0100 Message-Id: <20180116104121.4231-1-alban@kinvolk.io> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180115144804.GA28856@infradead.org> References: <20180115144804.GA28856@infradead.org> Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alban Crequy This patch forces files to be re-measured, re-appraised and re-audited on file systems with the feature flag FS_NO_IMA_CACHE. In that way, cached integrity results won't be used. For now, only FUSE filesystems use this flag. This is because the userspace FUSE process can change the underlying files at any times. This patch is based on the patch "ima: define a new policy option named force" by Mimi. [1] How to test this: ==== The test I did was using a patched version of the memfs FUSE driver [2][3] and two very simple "hello-world" programs [5] (prog1 prints "hello world: 1" and prog2 prints "hello world: 2"). I copy prog1 and prog2 in the fuse-memfs mount point, execute them and check the sha1 hash in "/sys/kernel/security/ima/ascii_runtime_measurements". My patch on the memfs FUSE driver added a backdoor command to serve prog1 when the kernel asks for prog2 or vice-versa. In this way, I can exec prog1 and get it to print "hello world: 2" without ever replacing the file via the VFS, so the kernel is not aware of the change. The test was done using the branch "alban/fuse-flag-ima-nocache" [4]. Step by step test procedure: 1. Mount the memfs FUSE using [3]: rm -f /tmp/memfs-switch* ; memfs -L DEBUG /mnt/memfs 2. Copy prog1 and prog2 using [5] cp prog1 /mnt/memfs/prog1 cp prog2 /mnt/memfs/prog2 3. Lookup the files and let the FUSE driver to keep the handles open: dd if=/mnt/memfs/prog1 bs=1 | (read -n 1 x ; sleep 3600 ) & dd if=/mnt/memfs/prog2 bs=1 | (read -n 1 x ; sleep 3600 ) & 4. Check the 2 programs work correctly: $ /mnt/memfs/prog1 hello world: 1 $ /mnt/memfs/prog2 hello world: 2 5. Check the measurements for prog1 and prog2: $ sudo cat /sys/kernel/security/ima/ascii_runtime_measurements|grep /mnt/memfs/prog 10 7ac5aed52061cb09120e977c6d04ee5c7b11c371 ima-ng sha1:ac14c9268cd2811f7a5adea17b27d84f50e1122c /mnt/memfs/prog1 10 9acc17a9a32aec4a676b8f6558e17a3d6c9a78e6 ima-ng sha1:799cb5d1e06d5c37ae7a76ba25ecd1bd01476383 /mnt/memfs/prog2 6. Use the backdoor command in my patched memfs to redirect file operations on file handle 3 to file handle 2: rm -f /tmp/memfs-switch* ; touch /tmp/memfs-switch-3-2 7. Check how the FUSE driver serves different content for the files: $ /mnt/memfs/prog1 hello world: 2 $ /mnt/memfs/prog2 hello world: 2 8. Check the measurements: sudo cat /sys/kernel/security/ima/ascii_runtime_measurements|grep /mnt/memfs/prog Without the patch, there are no new measurements, despite the FUSE driver having served different executables. With the patch, I can see additional measurements for prog1 and prog2 with the hashes reversed when the FUSE driver served the alternative content. ==== [1] https://www.spinics.net/lists/linux-integrity/msg00948.html [2] https://github.com/bbengfort/memfs [3] https://github.com/kinvolk/memfs/commits/alban/switch-files [4] https://github.com/kinvolk/linux/commits/alban/fuse-flag-ima-nocache [5] https://github.com/kinvolk/fuse-userns-patches/commit/cf1f5750cab0 Cc: linux-integrity@vger.kernel.org Cc: linux-security-module@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Miklos Szeredi Cc: Mimi Zohar Cc: Seth Forshee Cc: Christoph Hellwig Tested-by: Dongsu Park Signed-off-by: Alban Crequy --- fs/fuse/inode.c | 2 +- include/linux/fs.h | 1 + security/integrity/ima/ima_main.c | 11 +++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index 8c98edee3628..b511e6469b0a 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -1212,7 +1212,7 @@ static void fuse_kill_sb_anon(struct super_block *sb) static struct file_system_type fuse_fs_type = { .owner = THIS_MODULE, .name = "fuse", - .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT, + .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT | FS_NO_IMA_CACHE, .mount = fuse_mount, .kill_sb = fuse_kill_sb_anon, }; diff --git a/include/linux/fs.h b/include/linux/fs.h index fce19c491970..88da6908a2b2 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2075,6 +2075,7 @@ struct file_system_type { #define FS_BINARY_MOUNTDATA 2 #define FS_HAS_SUBTYPE 4 #define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */ +#define FS_NO_IMA_CACHE 16 /* Force IMA to re-measure, re-appraise, re-audit files */ #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */ struct dentry *(*mount) (struct file_system_type *, int, const char *, void *); diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index 88af481502f7..e6e45ab15dfc 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "ima.h" @@ -229,13 +230,15 @@ static int process_measurement(struct file *file, char *buf, loff_t size, IMA_ACTION_FLAGS); /* - * Reset the measure, appraise and audit cached flags either if - * ima_inode_setxattr was called or based on policy, forcing - * the file to be re-evaluated. + * Reset the measure, appraise and audit cached flags either if: + * - ima_inode_setxattr was called, or + * - based on policy ("force"), or + * - based on filesystem feature flag + * forcing the file to be re-evaluated. */ if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags)) { iint->flags &= ~IMA_DONE_MASK; - } else if (action & IMA_FORCE) { + } else if (action & IMA_FORCE || inode->i_sb->s_type->fs_flags & FS_NO_IMA_CACHE) { if (action & IMA_MEASURE) { iint->measured_pcrs = 0; iint->flags &=