Received: by 10.223.176.46 with SMTP id f43csp3103285wra; Mon, 22 Jan 2018 08:27:41 -0800 (PST) X-Google-Smtp-Source: AH8x226uL9OpYOPah9Jay04b3j9A5Y888lRIF4peFIVMb3HJ5FLOA0+gNXb8dfngjHvm8yr+6cR4 X-Received: by 10.107.17.95 with SMTP id z92mr9165436ioi.30.1516638460735; Mon, 22 Jan 2018 08:27:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516638460; cv=none; d=google.com; s=arc-20160816; b=Y5wAK0RPcdqjfn8SXLLh13tJACqqlrq7H+KCmsQ1sZRJ7vSTBaRAcZU0F+N232DHaw oWb9D7tPcQv9BY+wuNNF2P8jWFK0fkS6aaXacfrclDyggHMcOu9fHYAl1pkrP+5/cbzA EpOxmmU11jKYbSj4R6s+Vn17GjcGPANUxHq9+4dsNjchacRQZyoLgvhIeuVuXIIFBr05 Nk0Jy3lDhMM5jJJE4LXPxFCGftoEYeTAgVqx+7jzm0H3UBpYyd/I777cBwHLrUhBRnOk lM6adb+gWgOyLNp+IaGi1TwflbU0iVMM6GLF5JQhrxg8PW6/9zUpunhDHFLcZDwYuwEf wGyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=5O/1YDQ8Q2ZnFXPb6w/CruJiVtk4CbAm3uT8P852LDw=; b=diO7E+nvzULYUcVuOxTOXPjAZozUa6SY2GK2jRQ8MjqCzcyilIAkcT/jLGsfQ98HDf TKP3LmJfP7l8B26FivAGSd58Nk4VHNn8OG2+wDZh6Ov11Ypgc7w4Hlp5wd2BTpqED2Xg 14sSUMwG6dsugHaqsEDruhY56NMSHPJ4bdMqLbUdq+9cW6+y4G6qBpUE7jNaRqoRBbx0 KE1TY5VKNAaGDUy3iGnCczcRhDSEQ48mrW6Om7LzAU26jfbLNKxXj9NFkQg2FAZ1Lybn ERniKUdRcPswyNtzCTKVz/Xxux4/527C2NqN1KQaAH+Oo95tkbTC3LYQVLxCkf60tjj1 oDbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=W6EhZACI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d68si6165653itd.148.2018.01.22.08.27.27; Mon, 22 Jan 2018 08:27:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=W6EhZACI; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751216AbeAVQZo (ORCPT + 99 others); Mon, 22 Jan 2018 11:25:44 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36445 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751086AbeAVQZk (ORCPT ); Mon, 22 Jan 2018 11:25:40 -0500 Received: by mail-wm0-f65.google.com with SMTP id f3so18004951wmc.1; Mon, 22 Jan 2018 08:25:39 -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=5O/1YDQ8Q2ZnFXPb6w/CruJiVtk4CbAm3uT8P852LDw=; b=W6EhZACIYbnAqZKwgQjzwp3/FOL9kgJXQCo8xpZX7yXenA4M49HLBtyAWsQ+QLE6oX 6FsIhkQ9oGyiZsrhPMoIYXoBr7OkyPTFbyOS83jFZrloY6tMPp6U+SfdthhqOwvqBmOV Y64kH9HARrtGx1EpIq1lpKjT6zdoTjSxwyzYdVBTRIKJZGIw6wFIyX8yXHSbOc2ei4iE S/Yrq/h9rQ8MvyFec41wC0mQ45JNn8Fh4yNdoPHNZY7GXXpn6Lx7ILs2ULN9LluJiXoh rjLV+s7wzu+JBevWNOP4uIp+zB0FjlwsVrCwx8VqPC67ceGSm2yCNSolq7m/bDJqMp8K RUlg== 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=5O/1YDQ8Q2ZnFXPb6w/CruJiVtk4CbAm3uT8P852LDw=; b=S8acu4tL9CqzSD1N6ltJhTOAlG3YGTcpCpCMOb/g5beYC3l/G6wBVzaGM1140Nay80 prMfUvSD+3UTspI1ZHRuip9oYOU8Kpd+ufKpsHXjvFPm+1hCAPukqKrx0FCY3cU32R4G zKGom4l3UP3069SNxPqrT5pFaqjq/tjIqXxwXKLVCX5eyO1XRFZdYABb/IhJFFv0Bztf 7FfEUUpoNEGfqMVaw6OxH45G5ak8yb/GqxStm06QIf74XkJKCUDa/E0q+XNklYW4PRq1 HqB3q5PWC9zuU8f6B8nSjIUmE17X+6r3nmyjd7W1ujC8rwpHixzQJAUBcJNNeVCMmW/S 7RqA== X-Gm-Message-State: AKwxytd8szONd1hSjQObB0ktrp9NKdEdLtlnKtI7EXaJgFnZ1B2AH0fO 04to5GLjLhBN7teibZC6i0E= X-Received: by 10.80.134.132 with SMTP id r4mr14594239eda.250.1516638338511; Mon, 22 Jan 2018 08:25:38 -0800 (PST) Received: from localhost.localdomain ([178.19.216.175]) by smtp.gmail.com with ESMTPSA id g21sm11920844edg.88.2018.01.22.08.25.37 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 Jan 2018 08:25:37 -0800 (PST) From: Alban Crequy X-Google-Original-From: Alban Crequy To: alban@kinvolk.io Cc: dongsu@kinvolk.io, iago@kinvolk.io, linux-kernel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, miklos@szeredi.hu, viro@zeniv.linux.org.uk, zohar@linux.vnet.ibm.com, dmitry.kasatkin@gmail.com, james.l.morris@oracle.com, serge@hallyn.com, seth.forshee@canonical.com, hch@infradead.org Subject: [RFC PATCH v3 2/2] ima: force re-appraisal on filesystems with FS_IMA_NO_CACHE Date: Mon, 22 Jan 2018 17:24:52 +0100 Message-Id: <20180122162452.8756-3-alban@kinvolk.io> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180122162452.8756-1-alban@kinvolk.io> References: <20180122162452.8756-1-alban@kinvolk.io> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alban Crequy This patch forces files to be re-measured, re-appraised and re-audited on file systems with the feature flag FS_IMA_NO_CACHE. In that way, cached integrity results won't be used. How to test this: The test I did was using a patched version of the memfs FUSE driver [1][2] and two very simple "hello-world" programs [4] (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-v3" [3]. Step by step test procedure: 1. Mount the memfs FUSE using [2]: rm -f /tmp/memfs-switch* ; memfs -L DEBUG /mnt/memfs 2. Copy prog1 and prog2 using [4] 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 [...] ima-ng sha1:ac14c9268cd2[...] /mnt/memfs/prog1 10 [...] ima-ng sha1:799cb5d1e06d[...] /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://github.com/bbengfort/memfs [2] https://github.com/kinvolk/memfs/commits/alban/switch-files [3] https://github.com/kinvolk/linux/commits/alban/fuse-flag-ima-nocache-v3 [4] https://github.com/kinvolk/fuse-userns-patches/commit/cf1f5750cab0 Cc: linux-kernel@vger.kernel.org Cc: linux-integrity@vger.kernel.org Cc: linux-security-module@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Cc: Miklos Szeredi Cc: Alexander Viro Cc: Mimi Zohar Cc: Dmitry Kasatkin Cc: James Morris Cc: "Serge E. Hallyn" Cc: Seth Forshee Cc: Christoph Hellwig Tested-by: Dongsu Park Signed-off-by: Alban Crequy --- security/integrity/ima/ima_main.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index 6d78cb26784d..8870a7bbe9b9 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" @@ -228,9 +229,28 @@ static int process_measurement(struct file *file, char *buf, loff_t size, IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK | IMA_ACTION_FLAGS); - if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags)) - /* reset all flags if ima_inode_setxattr was called */ + /* + * Reset the measure, appraise and audit cached flags either if: + * - ima_inode_setxattr was called, 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 (inode->i_sb->s_type->fs_flags & FS_IMA_NO_CACHE) { + if (action & IMA_MEASURE) { + iint->measured_pcrs = 0; + iint->flags &= + ~(IMA_COLLECTED | IMA_MEASURE | IMA_MEASURED); + } + if (action & IMA_APPRAISE) + iint->flags &= + ~(IMA_COLLECTED | IMA_APPRAISE | IMA_APPRAISED | + IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK); + if (action & IMA_AUDIT) + iint->flags &= + ~(IMA_COLLECTED | IMA_AUDIT | IMA_AUDITED); + } /* Determine if already appraised/measured based on bitmask * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, -- 2.13.6