Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4994818ybi; Tue, 30 Jul 2019 11:49:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqxwBVBK5M02TFSCjzBkMPMi3I54rNpHOh6znkSjm6YoXisaNHld9oj92LzpBLfNAhPZeAiP X-Received: by 2002:a65:534c:: with SMTP id w12mr110433506pgr.51.1564512578880; Tue, 30 Jul 2019 11:49:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564512578; cv=none; d=google.com; s=arc-20160816; b=tMJ8zXmuA2hvVB1TkmpcX5mEfQONHrP7KFStXMJJi/UjHpn33ZbA0GSgy7Ma+3LmlT W2mirphECo/sqbAY4t331RlRtSNPNJNiMzDUXlN4FRqqPVZ9JEtWKv27l2xKnhCddb+I 1YRiI50343JoIbzkXrjCEPqPZoscKhXUtj5vN023K9qGYFz2VfH76m/cG+ewHx976faI hposcpMMfiCVntpOa88Cswc3SCiaOVqAB4jdN9sEBx6oD1R5fsAKeE2zEGcL38YzCnAl r1et2adYOiW056E5frvFBHI/yKtvCmPLGI/HxD9EP+NUTyjuYEnkIZTCFa4Up+qbG3aF w2Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=0pXH41oDr5n5A5Nqy3r8yf+zNgEME8ywg2NvLGu1usI=; b=YEiUm75bverCYhZMPF9qtk7rMgDrxxMwbVEtSzLn1f6b0EzrySgUVO6HGvhZBDoH2s 2Upi3qfESvOg8OwmFi5u9CMkrttWoZjIJN+9i9ezYyKi2pbIeI1VRDxWl/fg4osECHHm zgos/x80TWCdtvwcyOBUzvk6OQUDl8Eb9vIGd7T3oyU26vcDAwOjQFIPYX8LtTIrQRTF 7QgzHJhn/i3pvfra5vgr85rSXxekwVsYOQyq1ExCdK8kSh9fxXe67MlgKLaGFeTJHIPN eDN5F75y7aCEX/OdZSiawGoijyghppTPEUZyYU+HZ6owOpQri5G4/DbWybEPRk6c3rCd uwdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tNsT5Uyt; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h5si873880pgs.322.2019.07.30.11.49.23; Tue, 30 Jul 2019 11:49:38 -0700 (PDT) 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=pass header.i=@google.com header.s=20161025 header.b=tNsT5Uyt; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388247AbfG3Srl (ORCPT + 99 others); Tue, 30 Jul 2019 14:47:41 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:40679 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728574AbfG3Srl (ORCPT ); Tue, 30 Jul 2019 14:47:41 -0400 Received: by mail-pg1-f202.google.com with SMTP id m19so26676878pgv.7 for ; Tue, 30 Jul 2019 11:47:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=0pXH41oDr5n5A5Nqy3r8yf+zNgEME8ywg2NvLGu1usI=; b=tNsT5UytdZLcCZd0C7EgSOYaigaGXMUScGxN0gDTyqpWT45H44j3LfHzphHUobVkL4 RcU4adigiMxAZXh10qbjMVLNUNASoXCRecfBxQjos3FUv/gFdXjcXhXEW7yiQpQLqsfr vxHaX0032jt+/tJfcIUYbo/dXfj6tap4ospXabSre+S9+fKsoIZbo2Tm3lQd8gFD8hyu HHX/3zfc7cqkDrDXl8MY9V92WDfj5h7cqWs2N9DAQbGd5LXyANGDL6jfMea7rwIx5fcu FGz0vLPdVrtVHJfyAMNwQRq47+Bf5WOLQ1ifCHhnus6oqnmKwUdwaN2klkSY1cIBnpSX De1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=0pXH41oDr5n5A5Nqy3r8yf+zNgEME8ywg2NvLGu1usI=; b=tCD7cFDndN+qFvwsKFoDVKMqL3kL3aZi6dnPIk6imlM80BQcp4hFEerbYRQBKcN+Mi Ct/FOWhMsna6gbqNxXKEDS4n3u6N+u8fSo+gzWkifNRPiQFhD8XCEAdsWVX5WHzvTLcf vzMvlTKnkhI721WaOsXN9IkOFoUslIEROBV4SH9xj/6TV3Ma9iuSgh07V6VqGFSa6mHo hFEYzATQxHgHhhJR7vQWprIDVVMYPa54ECUxlLpcnWXXiUUcjleH2gTmwfsWWh090q/9 YFD8xYoDBqegGjhJuhABuw6od2E24civZoJh5NBVpnxLQYpnxOONzOorkt2oYWWr8nXO ZTUw== X-Gm-Message-State: APjAAAUIwXyE+RgPjNR0ORs5xy/foeN+ahamY0hkWJh2j6R3g3KJGywW vPmqm/d5JhG0nVx0GfRI/fR5tZDEFCwgvUciljZDiw== X-Received: by 2002:a63:d941:: with SMTP id e1mr76211750pgj.75.1564512459798; Tue, 30 Jul 2019 11:47:39 -0700 (PDT) Date: Tue, 30 Jul 2019 11:47:34 -0700 In-Reply-To: <20190724222354.7cbd6c6e@oasis.local.home> Message-Id: <20190730184734.202386-1-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190724222354.7cbd6c6e@oasis.local.home> X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH] tracefs: Restrict tracefs when the kernel is locked down From: Matthew Garrett To: Steven Rostedt Cc: jmorris@namei.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Matthew Garrett , Matthew Garrett Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Tracefs may release more information about the kernel than desirable, so restrict it when the kernel is locked down in confidentiality mode by preventing open(). Signed-off-by: Matthew Garrett Cc: Steven Rostedt --- Added the iput() fs/tracefs/inode.c | 40 +++++++++++++++++++++++++++++++++++- include/linux/security.h | 1 + security/lockdown/lockdown.c | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index eeeae0475da9..fb4d1d89ce53 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -20,6 +20,7 @@ #include #include #include +#include #define TRACEFS_DEFAULT_MODE 0700 @@ -27,6 +28,23 @@ static struct vfsmount *tracefs_mount; static int tracefs_mount_count; static bool tracefs_registered; +static int default_open_file(struct inode *inode, struct file *filp) +{ + struct dentry *dentry = filp->f_path.dentry; + struct file_operations *real_fops; + int ret; + + if (!dentry) + return -EINVAL; + + ret = security_locked_down(LOCKDOWN_TRACEFS); + if (ret) + return ret; + + real_fops = dentry->d_fsdata; + return real_fops->open(inode, filp); +} + static ssize_t default_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) { @@ -221,6 +239,12 @@ static int tracefs_apply_options(struct super_block *sb) return 0; } +static void tracefs_destroy_inode(struct inode *inode) +{ + if (S_ISREG(inode->i_mode)) + kfree(inode->i_fop); +} + static int tracefs_remount(struct super_block *sb, int *flags, char *data) { int err; @@ -256,6 +280,7 @@ static int tracefs_show_options(struct seq_file *m, struct dentry *root) static const struct super_operations tracefs_super_operations = { .statfs = simple_statfs, + .destroy_inode = tracefs_destroy_inode, .remount_fs = tracefs_remount, .show_options = tracefs_show_options, }; @@ -387,6 +412,7 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops) { + struct file_operations *proxy_fops; struct dentry *dentry; struct inode *inode; @@ -402,8 +428,20 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode, if (unlikely(!inode)) return failed_creating(dentry); + proxy_fops = kzalloc(sizeof(struct file_operations), GFP_KERNEL); + if (unlikely(!proxy_fops)) { + iput(inode); + return failed_creating(dentry); + } + + if (!fops) + fops = &tracefs_file_operations; + + dentry->d_fsdata = (void *)fops; + memcpy(proxy_fops, fops, sizeof(*proxy_fops)); + proxy_fops->open = default_open_file; inode->i_mode = mode; - inode->i_fop = fops ? fops : &tracefs_file_operations; + inode->i_fop = proxy_fops; inode->i_private = data; d_instantiate(dentry, inode); fsnotify_create(dentry->d_parent->d_inode, dentry); diff --git a/include/linux/security.h b/include/linux/security.h index d92323b44a3f..807dc0d24982 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -121,6 +121,7 @@ enum lockdown_reason { LOCKDOWN_KPROBES, LOCKDOWN_BPF_READ, LOCKDOWN_PERF, + LOCKDOWN_TRACEFS, LOCKDOWN_CONFIDENTIALITY_MAX, }; diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c index 88064ce1c844..173191562047 100644 --- a/security/lockdown/lockdown.c +++ b/security/lockdown/lockdown.c @@ -36,6 +36,7 @@ static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = { [LOCKDOWN_KPROBES] = "use of kprobes", [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM", [LOCKDOWN_PERF] = "unsafe use of perf", + [LOCKDOWN_TRACEFS] = "use of tracefs", [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", }; -- 2.22.0.770.g0f2c4a37fd-goog