Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1606006ybb; Thu, 9 Apr 2020 05:39:58 -0700 (PDT) X-Google-Smtp-Source: APiQypKQV8DCcaSpL/Uk0/DR0ipeU3u6Gt5N0ZfBVx+MRhku12PQRhgHVULz3Y0ehotWUYaOxHsd X-Received: by 2002:ac8:32cd:: with SMTP id a13mr1678623qtb.360.1586435998238; Thu, 09 Apr 2020 05:39:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586435998; cv=none; d=google.com; s=arc-20160816; b=ffJKyVZ/Kh+GRN8Ci3J4+Ns3i9IMi6aNQR2M7pVYo4opR2VaM8nDT0UfdnI5ZTKHRD CBxoGlnU0yqmLXakxUeQ3ZjO06ktQBndDKBe4X3/u/5wgcD0afBI1HxMgDzIGhaSIv6+ LtGP+sE6zMI3/VmWsFPiTVgyDMXROFqPKEgjyE9FI1S+iURQDaTaQ9IWVgCoEA6e6p9G IcpXosdA7yjuwS6YobjxERIEMySD1oQnfIB5B8xjFJ7URG6xeRHyWc0HOLZVUZwc1dom 9oPHgtgBpMe/n69UPOky4wMUq2NKrW/q5Xod4f4gTeWrfnWX7HRFUzIxP+B0hxHjIDHp m1zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=4EfVvf/zV5YKTa6Il7GXZ5U7IofC5nCg9JCAT1nD7Q8=; b=XWbBr8B8QmqKyyxNyJ5TuiQZw3xKw4jwXR+wkas3FagQhSXqtiw9vbPL6u6yqVOFen KTlUXY+3xUDLgkrH2v9NQnf4pF9QFMzhngwMYFQRY+HfZeegjb0O6sQohpvvd9ZDZDrR qoFuYjYLZTq1C5scLw9UPkhUm6/itEoEw0sI3bMysBhYdTa6o9uzcBRPmGkUC3Apff0Y stX9Y7+vAsMIw3vgRKg/3feT8vsgT4itquSaVTmESj7Ak5WnCbftbruAJZU8tW7C1s8e Jg1ogj9M/sQav+qVjQb92K3U4F8tN0gX6Sd2uMRBg22/m/sjoxDzR7xc8GCqa7pyPe2u hn8g== ARC-Authentication-Results: i=1; mx.google.com; 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=QUARANTINE 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 a26si591601qto.322.2020.04.09.05.39.41; Thu, 09 Apr 2020 05:39:58 -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; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726723AbgDIMiv (ORCPT + 99 others); Thu, 9 Apr 2020 08:38:51 -0400 Received: from raptor.unsafe.ru ([5.9.43.93]:58600 "EHLO raptor.unsafe.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725987AbgDIMis (ORCPT ); Thu, 9 Apr 2020 08:38:48 -0400 Received: from comp-core-i7-2640m-0182e6.redhat.com (ip-89-102-33-211.net.upcbroadband.cz [89.102.33.211]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by raptor.unsafe.ru (Postfix) with ESMTPSA id 57875209D9; Thu, 9 Apr 2020 12:38:44 +0000 (UTC) From: Alexey Gladkov To: LKML Cc: Kernel Hardening , Linux API , Linux FS Devel , Linux Security Module , Akinobu Mita , Alexander Viro , Alexey Dobriyan , Alexey Gladkov , Andrew Morton , Andy Lutomirski , Daniel Micay , Djalal Harouni , "Dmitry V . Levin" , "Eric W . Biederman" , Greg Kroah-Hartman , Ingo Molnar , "J . Bruce Fields" , Jeff Layton , Jonathan Corbet , Kees Cook , Linus Torvalds , Oleg Nesterov , David Howells Subject: [PATCH RESEND v11 5/8] proc: add option to mount only a pids subset Date: Thu, 9 Apr 2020 14:37:49 +0200 Message-Id: <20200409123752.1070597-6-gladkov.alexey@gmail.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200409123752.1070597-1-gladkov.alexey@gmail.com> References: <20200409123752.1070597-1-gladkov.alexey@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.1 (raptor.unsafe.ru [5.9.43.93]); Thu, 09 Apr 2020 12:38:44 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This allows to hide all files and directories in the procfs that are not related to tasks. Signed-off-by: Alexey Gladkov Reviewed-by: Alexey Dobriyan Reviewed-by: Kees Cook --- fs/proc/generic.c | 9 +++++++++ fs/proc/inode.c | 6 ++++++ fs/proc/root.c | 33 +++++++++++++++++++++++++++++++++ include/linux/proc_fs.h | 7 +++++++ 4 files changed, 55 insertions(+) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 3faed94e4b65..ee5b6482009c 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -269,6 +269,11 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) { + struct proc_fs_info *fs_info = proc_sb_info(dir->i_sb); + + if (fs_info->pidonly == PROC_PIDONLY_ON) + return ERR_PTR(-ENOENT); + return proc_lookup_de(dir, dentry, PDE(dir)); } @@ -325,6 +330,10 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx, int proc_readdir(struct file *file, struct dir_context *ctx) { struct inode *inode = file_inode(file); + struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb); + + if (fs_info->pidonly == PROC_PIDONLY_ON) + return 1; return proc_readdir_de(file, ctx, PDE(inode)); } diff --git a/fs/proc/inode.c b/fs/proc/inode.c index 91fe4896fa85..e6577ce6027b 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -173,6 +173,8 @@ static int proc_show_options(struct seq_file *seq, struct dentry *root) seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, fs_info->pid_gid)); if (fs_info->hide_pid != HIDEPID_OFF) seq_printf(seq, ",hidepid=%u", fs_info->hide_pid); + if (fs_info->pidonly != PROC_PIDONLY_OFF) + seq_printf(seq, ",subset=pid"); return 0; } @@ -393,12 +395,16 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr, static int proc_reg_open(struct inode *inode, struct file *file) { + struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb); struct proc_dir_entry *pde = PDE(inode); int rv = 0; typeof_member(struct proc_ops, proc_open) open; typeof_member(struct proc_ops, proc_release) release; struct pde_opener *pdeo; + if (fs_info->pidonly == PROC_PIDONLY_ON) + return -ENOENT; + /* * Ensure that * 1) PDE's ->release hook will be called no matter what diff --git a/fs/proc/root.c b/fs/proc/root.c index 62eae22403d2..dbcd96f07c7a 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -34,16 +34,19 @@ struct proc_fs_context { unsigned int mask; int hidepid; int gid; + int pidonly; }; enum proc_param { Opt_gid, Opt_hidepid, + Opt_subset, }; static const struct fs_parameter_spec proc_fs_parameters[] = { fsparam_u32("gid", Opt_gid), fsparam_u32("hidepid", Opt_hidepid), + fsparam_string("subset", Opt_subset), {} }; @@ -55,6 +58,29 @@ static inline int valid_hidepid(unsigned int value) value == HIDEPID_NOT_PTRACEABLE); } +static int proc_parse_subset_param(struct fs_context *fc, char *value) +{ + struct proc_fs_context *ctx = fc->fs_private; + + while (value) { + char *ptr = strchr(value, ','); + + if (ptr != NULL) + *ptr++ = '\0'; + + if (*value != '\0') { + if (!strcmp(value, "pid")) { + ctx->pidonly = PROC_PIDONLY_ON; + } else { + return invalf(fc, "proc: unsupported subset option - %s\n", value); + } + } + value = ptr; + } + + return 0; +} + static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) { struct proc_fs_context *ctx = fc->fs_private; @@ -76,6 +102,11 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) ctx->hidepid = result.uint_32; break; + case Opt_subset: + if (proc_parse_subset_param(fc, param->string) < 0) + return -EINVAL; + break; + default: return -EINVAL; } @@ -95,6 +126,8 @@ static void proc_apply_options(struct super_block *s, ctx->fs_info->pid_gid = make_kgid(user_ns, ctx->gid); if (ctx->mask & (1 << Opt_hidepid)) ctx->fs_info->hide_pid = ctx->hidepid; + if (ctx->mask & (1 << Opt_subset)) + ctx->fs_info->pidonly = ctx->pidonly; } static int proc_fill_super(struct super_block *s, struct fs_context *fc) diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 21d19353fdc7..afd38cae2339 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -35,12 +35,19 @@ enum { HIDEPID_NOT_PTRACEABLE = 4, /* Limit pids to only ptraceable pids */ }; +/* definitions for proc mount option pidonly */ +enum { + PROC_PIDONLY_OFF = 0, + PROC_PIDONLY_ON = 1, +}; + struct proc_fs_info { struct pid_namespace *pid_ns; struct dentry *proc_self; /* For /proc/self */ struct dentry *proc_thread_self; /* For /proc/thread-self */ kgid_t pid_gid; int hide_pid; + int pidonly; }; static inline struct proc_fs_info *proc_sb_info(struct super_block *sb) -- 2.25.2