Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp84218ybi; Thu, 20 Jun 2019 18:22:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqylh0+6vSCNYoe7XYRL0o18O24aGKP9V4pTIuWU5Q+3igDCNLiBKZAqyeix+fnzXmIAykg+ X-Received: by 2002:a17:902:bb95:: with SMTP id m21mr129965813pls.154.1561080124265; Thu, 20 Jun 2019 18:22:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561080124; cv=none; d=google.com; s=arc-20160816; b=Rnm1z8F3tVctf/5lvzkOq29WDhCZu9SSxRdOnMTG8/7gxTm8eXnmablspe2W0mkHxE S58Yipd3Nukh66YM0ZUdC5ROeSZodtsFz/a3QFQXYERl/jFcVbVdovetSYkX2qRweTnj MR5PcXjoXwhg1OBCBSy2UC/ZhecBVSM7zHIE+V5oG3OuyGzFb+jj38/WKWDx67U8Br5o ziAfpKmuuHAeFHCwDV/WrmITpj0vpE07pmiUyNLGDaAiafzVBtIr3+F3v8Eyj1AK3JBu xa0Khad9gB+diiOh/bPpqFDNLWoOU6Azmd7sX0/2Lrd4L5KtT7Tg8L8DyeVuPAxi1JCM tbQg== 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=9IKEhDS9kxNf+hGzBv54zcBuApEcoDpMeguZDjGavUE=; b=MV3QPMS2QXFIgrT+RX79Eza/M7WrazFxhIimCEfF3n7fh6HvN0RK3krUwLnC8nqld/ cldw3iA6RJ4MnJYntAAKoW64fMbAFVnxDq4a3p9jfdvvCdGIi+Jtha0vmOYDMU8LScvg 7JGP14mHF+zY28o9zhj2MMSmO3s/Mlz+irNKG0vIgTab+806RZLh63p/Fze/c0Ds1aQJ /BTBXlAo9rHx/uOCLzOTjQTb+yp9HHey4WN9aqBzHXbn0glWi5w1/sxXp810/HNMX2Qm FpZuiV/f1pTH5NbKOUKjKCKzinfaPnAoh+zPqNeial1teQF22elmTIPLv1cwRWsGCMWi KKFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=c68jQJgi; 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 k4si975522pgq.293.2019.06.20.18.21.48; Thu, 20 Jun 2019 18:22:04 -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=c68jQJgi; 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 S1726955AbfFUBVQ (ORCPT + 99 others); Thu, 20 Jun 2019 21:21:16 -0400 Received: from mail-pf1-f201.google.com ([209.85.210.201]:45518 "EHLO mail-pf1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726906AbfFUBVB (ORCPT ); Thu, 20 Jun 2019 21:21:01 -0400 Received: by mail-pf1-f201.google.com with SMTP id i27so3246912pfk.12 for ; Thu, 20 Jun 2019 18:21:01 -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=9IKEhDS9kxNf+hGzBv54zcBuApEcoDpMeguZDjGavUE=; b=c68jQJgieWay1PoGnvINsjlX2fEmfo+CsuOc1McNqW1CrQPHHwDdMjaDY3EF97grMG YmbsornuvMNiMDs6Js/wPfcH6zVDjPG/y8tDkH8+uxhhHAPysNwQu1PeIYHo9Z32+fkH ttitmJz2t+NQWK11NmEVIUQIILNfMgkllTUR31S/Haiqi1oBfCXgitGeUR9i2yZkU3Rx 4eChJiBsHPWtMa8hhYPFx7cpIXA9S3HBizrBUXvfAbnz/eCDu6c8QNdH9lPtVMsgHoHz 12WeuGMcmNswHHXm1JSXozii/S5x0Rt0/g4iktYOaujIvPQX2HbGkBgwaNZCFVegNplC 82IA== 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=9IKEhDS9kxNf+hGzBv54zcBuApEcoDpMeguZDjGavUE=; b=jZ4gBQ4c59gF4O+WHIONZv9PBi1E6uIkkeJ07cuLPfAxZ3tRo6L9ytGxXXa1ceG+J1 gbuC7TvenXt9C8nyD0BsrB7Ir/M0StiDhOAO8fFhRorx4lRPJmttlAqOLlqMVQX2cYwK lWBdI3rW/VbKPxjbiCezRLRmFi8rcd1YKXqwBz4zhvVZqZdRDSrBVmout5LiV48rIT26 XEJp8lYb30XXDuDonWEUZtnScVZEDpiH7jg93xa9zM049tDsAKCaz1L68MMwWtQQdkZU G/ilz63RLI9+spsJpS1no3VFvBaMCsAXT8cU4DQUsfIZbKnK9JVCTZMw5fsg2RqgS8DH Yg1w== X-Gm-Message-State: APjAAAUdKFXFYXRmqT8Zk6RJrlcF5ayjEGPASCnzjPHnbGj19Lx4HuuS /YTQrTyH9yVloKtbOuYKnB/l5oc7orHd4nbB774Zyg== X-Received: by 2002:a63:374a:: with SMTP id g10mr15221482pgn.31.1561080060541; Thu, 20 Jun 2019 18:21:00 -0700 (PDT) Date: Thu, 20 Jun 2019 18:19:40 -0700 In-Reply-To: <20190621011941.186255-1-matthewgarrett@google.com> Message-Id: <20190621011941.186255-30-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190621011941.186255-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH V33 29/30] tracefs: Restrict tracefs when the kernel is locked down From: Matthew Garrett To: jmorris@namei.org Cc: linux-security@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Matthew Garrett , Matthew Garrett , Steven Rostedt 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 --- fs/tracefs/inode.c | 41 +++++++++++++++++++++++++++++++++++- include/linux/security.h | 1 + security/lockdown/lockdown.c | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 7098c49f3693..f6c04fa8e415 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -24,6 +24,7 @@ #include #include #include +#include #define TRACEFS_DEFAULT_MODE 0700 @@ -31,6 +32,21 @@ 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; + + if (!dentry) + return -EINVAL; + + if (security_is_locked_down(LOCKDOWN_TRACEFS)) + return -EPERM; + + 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) { @@ -50,6 +66,13 @@ static const struct file_operations tracefs_file_operations = { .llseek = noop_llseek, }; +static const struct file_operations tracefs_proxy_file_operations = { + .read = default_read_file, + .write = default_write_file, + .open = default_open_file, + .llseek = noop_llseek, +}; + static struct tracefs_dir_ops { int (*mkdir)(const char *name); int (*rmdir)(const char *name); @@ -225,6 +248,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; @@ -260,6 +289,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, }; @@ -393,6 +423,7 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode, { struct dentry *dentry; struct inode *inode; + struct file_operations *proxy_fops; if (!(mode & S_IFMT)) mode |= S_IFREG; @@ -406,8 +437,16 @@ 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 (!proxy_fops) + return failed_creating(dentry); + + dentry->d_fsdata = fops ? (void *)fops : + (void *)&tracefs_file_operations; + memcpy(proxy_fops, dentry->d_fsdata, sizeof(struct file_operations)); + 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 2563a9e3b415..040e7fc33397 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -100,6 +100,7 @@ enum lockdown_reason { LOCKDOWN_KPROBES, LOCKDOWN_BPF, LOCKDOWN_PERF, + LOCKDOWN_TRACEFS, LOCKDOWN_CONFIDENTIALITY_MAX, }; diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c index a6f7b0770e78..7dc601f06cd3 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] = "use of bpf", [LOCKDOWN_PERF] = "unsafe use of perf", + [LOCKDOWN_TRACEFS] = "use of tracefs", [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", }; -- 2.22.0.410.gd8fdbe21b5-goog