Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3825934ybv; Mon, 10 Feb 2020 07:06:53 -0800 (PST) X-Google-Smtp-Source: APXvYqySYf7g+SfVhDZ5GOP/i9HNn3TcyV74GiMiBUTzzRKvcHaRteAyrVigiqUA5yDbgWRwGWCU X-Received: by 2002:a54:4816:: with SMTP id j22mr1083965oij.179.1581347213130; Mon, 10 Feb 2020 07:06:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581347213; cv=none; d=google.com; s=arc-20160816; b=PkOUFQ5YT8g3WPdlLgfC64T4S+BmKpeGQhrhbNodpY2rh7Tt7fvNWueBWAd4Zji6Fa 0vzdA9rxbwqyINmZm6HnW/BFwu2FQ4kg9Ysfot1+fXDc1iM0MsavbhjoYWSPiLLQCvkq 3SjQXLgLeR+XJYfUd9FaJ1xgwNxTMCOMTemskJafqnuLLsS6KDXxxVM5SESpVMKjl75N EAGm5WKzuuHt7emG+r/bHGkBTadaQzPq35meTP44ssPY6/YUAXQRR/o86m2OfAN2z+v5 damnA2Zbd+WtvM34Ic8QOwxbjx5U9OHcXESZDJ8HLYMqT4eyDV1JRwOxn9gRbzeC+qEt yCdg== 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=TMJU8ShN8oUcrfQ7N5SMgwy3z6XZ1AT1OToIUvkk7yU=; b=pWS8w1okJ2QFt2Eaxa8U7aFHkkSMNgNXipyJxv62cvj69znKYV+u0jfnY6s1mLQv2b vX1cu2LOCNxFz9CQkT322VbHRvfWTYplTaoFFrleQitnqNz8rUino25887jeEnZJY+Ya kjka+w7rSkVXPswC5z6nK1ZzZeek0k/G2hYaENLuk68kjtqp/X5IqttkfSZGV/0Bohg6 W54ODeBNpF/h4Vkuz2ELVShlIMARW8ewyJ9v3pkI153ZGKTEhkgL8LVQVYZRpmrrLoTR FRDI96qFbWrtmE5GdCMK6x7RtelcmR8Hl4EyIPRgUsAAwAQDQz/um5e3SBxsQl+Q9VCQ sD1w== 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 s11si280926oic.57.2020.02.10.07.06.40; Mon, 10 Feb 2020 07:06:53 -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; 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 S1729254AbgBJPGP (ORCPT + 99 others); Mon, 10 Feb 2020 10:06:15 -0500 Received: from monster.unsafe.ru ([5.9.28.80]:54062 "EHLO mail.unsafe.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729207AbgBJPGP (ORCPT ); Mon, 10 Feb 2020 10:06:15 -0500 Received: from comp-core-i7-2640m-0182e6.redhat.com (nat-pool-brq-t.redhat.com [213.175.37.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.unsafe.ru (Postfix) with ESMTPSA id EDF07C61AB0; Mon, 10 Feb 2020 15:06:11 +0000 (UTC) From: Alexey Gladkov To: LKML , Kernel Hardening , Linux API , Linux FS Devel , Linux Security Module Cc: 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 , Solar Designer Subject: [PATCH v8 06/11] proc: support mounting procfs instances inside same pid namespace Date: Mon, 10 Feb 2020 16:05:14 +0100 Message-Id: <20200210150519.538333-7-gladkov.alexey@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200210150519.538333-1-gladkov.alexey@gmail.com> References: <20200210150519.538333-1-gladkov.alexey@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch allows to have multiple procfs instances inside the same pid namespace. The aim here is lightweight sandboxes, and to allow that we have to modernize procfs internals. 1) The main aim of this work is to have on embedded systems one supervisor for apps. Right now we have some lightweight sandbox support, however if we create pid namespacess we have to manages all the processes inside too, where our goal is to be able to run a bunch of apps each one inside its own mount namespace without being able to notice each other. We only want to use mount namespaces, and we want procfs to behave more like a real mount point. 2) Linux Security Modules have multiple ptrace paths inside some subsystems, however inside procfs, the implementation does not guarantee that the ptrace() check which triggers the security_ptrace_check() hook will always run. We have the 'hidepid' mount option that can be used to force the ptrace_may_access() check inside has_pid_permissions() to run. The problem is that 'hidepid' is per pid namespace and not attached to the mount point, any remount or modification of 'hidepid' will propagate to all other procfs mounts. This also does not allow to support Yama LSM easily in desktop and user sessions. Yama ptrace scope which restricts ptrace and some other syscalls to be allowed only on inferiors, can be updated to have a per-task context, where the context will be inherited during fork(), clone() and preserved across execve(). If we support multiple private procfs instances, then we may force the ptrace_may_access() on /proc// to always run inside that new procfs instances. This will allow to specifiy on user sessions if we should populate procfs with pids that the user can ptrace or not. By using Yama ptrace scope, some restricted users will only be able to see inferiors inside /proc, they won't even be able to see their other processes. Some software like Chromium, Firefox's crash handler, Wine and others are already using Yama to restrict which processes can be ptracable. With this change this will give the possibility to restrict /proc// but more importantly this will give desktop users a generic and usuable way to specifiy which users should see all processes and which users can not. Side notes: * This covers the lack of seccomp where it is not able to parse arguments, it is easy to install a seccomp filter on direct syscalls that operate on pids, however /proc// is a Linux ABI using filesystem syscalls. With this change LSMs should be able to analyze open/read/write/close... In the new patchset version I removed the 'newinstance' option as Eric W. Biederman suggested. Cc: Kees Cook Cc: "Eric W. Biederman" Signed-off-by: Alexey Gladkov --- fs/proc/root.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/fs/proc/root.c b/fs/proc/root.c index efd76c004e86..5d5cba4c899b 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -82,7 +82,7 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) return 0; } -static void proc_apply_options(struct super_block *s, +static void proc_apply_options(struct proc_fs_info *fs_info, struct fs_context *fc, struct pid_namespace *pid_ns, struct user_namespace *user_ns) @@ -90,15 +90,17 @@ static void proc_apply_options(struct super_block *s, struct proc_fs_context *ctx = fc->fs_private; if (pid_ns->proc_mnt) { - struct proc_fs_info *fs_info = proc_sb_info(pid_ns->proc_mnt->mnt_sb); - proc_fs_set_pid_gid(ctx->fs_info, proc_fs_pid_gid(fs_info)); - proc_fs_set_hide_pid(ctx->fs_info, proc_fs_hide_pid(fs_info)); + struct proc_fs_info *pidns_fs_info = proc_sb_info(pid_ns->proc_mnt->mnt_sb); + + proc_fs_set_pid_gid(fs_info, proc_fs_pid_gid(pidns_fs_info)); + proc_fs_set_hide_pid(fs_info, proc_fs_hide_pid(pidns_fs_info)); } if (ctx->mask & (1 << Opt_gid)) - proc_fs_set_pid_gid(ctx->fs_info, make_kgid(user_ns, ctx->gid)); + proc_fs_set_pid_gid(fs_info, make_kgid(user_ns, ctx->gid)); + if (ctx->mask & (1 << Opt_hidepid)) - proc_fs_set_hide_pid(ctx->fs_info, ctx->hidepid); + proc_fs_set_hide_pid(fs_info, ctx->hidepid); } static int proc_fill_super(struct super_block *s, struct fs_context *fc) @@ -108,7 +110,7 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc) struct inode *root_inode; int ret; - proc_apply_options(s, fc, pid_ns, current_user_ns()); + proc_apply_options(ctx->fs_info, fc, pid_ns, current_user_ns()); /* User space would break if executables or devices appear on proc */ s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV; @@ -118,6 +120,7 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc) s->s_magic = PROC_SUPER_MAGIC; s->s_op = &proc_sops; s->s_time_gran = 1; + s->s_fs_info = ctx->fs_info; /* * procfs isn't actually a stacking filesystem; however, there is @@ -157,15 +160,13 @@ static int proc_reconfigure(struct fs_context *fc) sync_filesystem(sb); - proc_apply_options(sb, fc, pid, current_user_ns()); + proc_apply_options(fs_info, fc, pid, current_user_ns()); return 0; } static int proc_get_tree(struct fs_context *fc) { - struct proc_fs_context *ctx = fc->fs_private; - - return get_tree_keyed(fc, proc_fill_super, ctx->fs_info); + return get_tree_nodev(fc, proc_fill_super); } static void proc_fs_context_free(struct fs_context *fc) @@ -186,25 +187,19 @@ static const struct fs_context_operations proc_fs_context_ops = { static int proc_init_fs_context(struct fs_context *fc) { struct proc_fs_context *ctx; - struct pid_namespace *pid_ns; ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL); if (!ctx) return -ENOMEM; - pid_ns = get_pid_ns(task_active_pid_ns(current)); - - if (!pid_ns->proc_mnt) { - ctx->fs_info = kzalloc(sizeof(struct proc_fs_info), GFP_KERNEL); - if (!ctx->fs_info) { - kfree(ctx); - return -ENOMEM; - } - ctx->fs_info->pid_ns = pid_ns; - } else { - ctx->fs_info = proc_sb_info(pid_ns->proc_mnt->mnt_sb); + ctx->fs_info = kzalloc(sizeof(struct proc_fs_info), GFP_KERNEL); + if (!ctx->fs_info) { + kfree(ctx); + return -ENOMEM; } + ctx->fs_info->pid_ns = get_pid_ns(task_active_pid_ns(current)); + put_user_ns(fc->user_ns); fc->user_ns = get_user_ns(ctx->fs_info->pid_ns->user_ns); fc->fs_private = ctx; -- 2.24.1