Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp8724111ybl; Wed, 25 Dec 2019 05:04:19 -0800 (PST) X-Google-Smtp-Source: APXvYqzOShnAjr1OutYFVlodHMoEO5WewQYRKdNJCR3/LLF1RGIN+BKGLj8CIwo+pAGkrd7paRvb X-Received: by 2002:aca:48d6:: with SMTP id v205mr1423926oia.10.1577279059009; Wed, 25 Dec 2019 05:04:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577279059; cv=none; d=google.com; s=arc-20160816; b=KwOHvRJUyYggyrqc/56WcLy3gWGoiJfqVJ0eFUxoAwR9Y90bpxhi6u4LZlFdtGQANk Q6fnDCwGj0glWR083uocJvYgxqqqoImK535HaMr0V+L8X8neCH0D4slIMY4n4tcuoJZh BG0HCFMk47di4RDZDkpdUg1rX7geI2wKorV2h9OZY0xJnShlW8qewvG0pxZRxp89EP7C VwKOEPoxaWt1YmU0ebiI3krbK7jk2g7xkdEH+Mb3iB3sLoxdC2xF7qU00ORPksLi2Eo8 zzKYjgnxY6GdQK8wC202JOYPZpq52UcWy7x7KOd2wuVJP2abQGQpd5Qu6qu09vPZoME6 +X3w== 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=XVm6RJ01EU8MaUglxMY070cLtQflDcdxGDhE08gqhew=; b=Ud0ijNhI6yaYAvGgpR4sCwsyMEiX3de+H9Qi07YDBahtdvGn1F3J0fjPuBG9+O8uUN 6GBPOxln35ezVCy2a4smHZiPoAu1n3J+HJPnzVVQ/mokMlOUIXtNp7tZmcqXD6Fo2PIU 1F3wMEvl4SnXCgnvRRkiiaC0k/6DWeEpCr8l4q/RlA1k3Od/7MIGgyy4m5BF1690mjzw 6vHLUZxU8T3sZ89vTAVQM7sui06nvY5RrDjAkHeTzkkWwekdTu3OXLhvvVrxLDrVMAEt QQ1+pvIqjOui62QoX8RUHenQS0PD8Z7vcZhaYvvvW2pvR/bOWBY6WQvbDu34wrEpXylL NcXQ== 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 f3si13199101oia.264.2019.12.25.05.04.08; Wed, 25 Dec 2019 05:04:18 -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 S1727100AbfLYNDX (ORCPT + 99 others); Wed, 25 Dec 2019 08:03:23 -0500 Received: from monster.unsafe.ru ([5.9.28.80]:36182 "EHLO mail.unsafe.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726866AbfLYNDQ (ORCPT ); Wed, 25 Dec 2019 08:03:16 -0500 Received: from localhost.localdomain (ip-89-102-33-211.net.upcbroadband.cz [89.102.33.211]) (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 30432C61B12; Wed, 25 Dec 2019 12:53: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 , Stephen Rothwell Subject: [PATCH v6 05/10] proc: add helpers to set and get proc hidepid and gid mount options Date: Wed, 25 Dec 2019 13:51:46 +0100 Message-Id: <20191225125151.1950142-6-gladkov.alexey@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191225125151.1950142-1-gladkov.alexey@gmail.com> References: <20191225125151.1950142-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 is a cleaning patch to add helpers to set and get proc mount options instead of directly using them. This make it easy to track what's happening and easy to update in future. Cc: Kees Cook Cc: Andy Lutomirski Signed-off-by: Djalal Harouni Signed-off-by: Alexey Gladkov --- fs/proc/base.c | 6 +++--- fs/proc/inode.c | 11 +++++++---- fs/proc/root.c | 8 ++++---- include/linux/proc_fs.h | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index caca1929fee1..4ccb280a3e79 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -699,9 +699,9 @@ static bool has_pid_permissions(struct proc_fs_info *fs_info, struct task_struct *task, int hide_pid_min) { - if (fs_info->hide_pid < hide_pid_min) + if (proc_fs_hide_pid(fs_info) < hide_pid_min) return true; - if (in_group_p(fs_info->pid_gid)) + if (in_group_p(proc_fs_pid_gid(fs_info))) return true; return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); } @@ -720,7 +720,7 @@ static int proc_pid_permission(struct inode *inode, int mask) put_task_struct(task); if (!has_perms) { - if (fs_info->hide_pid == HIDEPID_INVISIBLE) { + if (proc_fs_hide_pid(fs_info) == HIDEPID_INVISIBLE) { /* * Let's make getdents(), stat(), and open() * consistent with each other. If a process diff --git a/fs/proc/inode.c b/fs/proc/inode.c index b90c233e5968..70b722fb8811 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -105,11 +105,14 @@ void __init proc_init_kmemcache(void) static int proc_show_options(struct seq_file *seq, struct dentry *root) { struct proc_fs_info *fs_info = proc_sb_info(root->d_sb); + int hidepid = proc_fs_hide_pid(fs_info); + kgid_t gid = proc_fs_pid_gid(fs_info); - if (!gid_eq(fs_info->pid_gid, GLOBAL_ROOT_GID)) - 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 (!gid_eq(gid, GLOBAL_ROOT_GID)) + seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, gid)); + + if (hidepid != HIDEPID_OFF) + seq_printf(seq, ",hidepid=%u", hidepid); return 0; } diff --git a/fs/proc/root.c b/fs/proc/root.c index 1ca47d446aa4..efd76c004e86 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -91,14 +91,14 @@ static void proc_apply_options(struct super_block *s, if (pid_ns->proc_mnt) { struct proc_fs_info *fs_info = proc_sb_info(pid_ns->proc_mnt->mnt_sb); - ctx->fs_info->pid_gid = fs_info->pid_gid; - ctx->fs_info->hide_pid = fs_info->hide_pid; + 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)); } if (ctx->mask & (1 << Opt_gid)) - ctx->fs_info->pid_gid = make_kgid(user_ns, ctx->gid); + proc_fs_set_pid_gid(ctx->fs_info, make_kgid(user_ns, ctx->gid)); if (ctx->mask & (1 << Opt_hidepid)) - ctx->fs_info->hide_pid = ctx->hidepid; + proc_fs_set_hide_pid(ctx->fs_info, ctx->hidepid); } 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 05ecf4e8923f..fd92bf38aa62 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -36,6 +36,26 @@ static inline struct proc_fs_info *proc_sb_info(struct super_block *sb) return sb->s_fs_info; } +static inline void proc_fs_set_hide_pid(struct proc_fs_info *fs_info, int hide_pid) +{ + fs_info->hide_pid = hide_pid; +} + +static inline void proc_fs_set_pid_gid(struct proc_fs_info *fs_info, kgid_t gid) +{ + fs_info->pid_gid = gid; +} + +static inline int proc_fs_hide_pid(struct proc_fs_info *fs_info) +{ + return fs_info->hide_pid; +} + +static inline kgid_t proc_fs_pid_gid(struct proc_fs_info *fs_info) +{ + return fs_info->pid_gid; +} + extern void proc_root_init(void); extern void proc_flush_task(struct task_struct *); @@ -111,6 +131,24 @@ static inline struct proc_fs_info *proc_sb_info(struct super_block *sb) return NULL; } +static inline void proc_fs_set_hide_pid(struct proc_fs_info *fs_info, int hide_pid) +{ +} + +static inline void proc_fs_set_pid_gid(struct proc_info_fs *fs_info, kgid_t gid) +{ +} + +static inline int proc_fs_hide_pid(struct proc_fs_info *fs_info) +{ + return 0; +} + +extern kgid_t proc_fs_pid_gid(struct proc_fs_info *fs_info) +{ + return GLOBAL_ROOT_GID; +} + static inline void proc_root_init(void) { } -- 2.24.1