proc: rename struct proc_fs_info to proc_fs_opts
Subsystems (5)
PATCH v9 1/8 proc: rename struct proc_fs_info to proc_fs_opts
In reply to a message not in the archive--- fs/proc_namespace.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/proc_namespace.c +7 -7
@@ -37,23 +37,23 @@ static __poll_t mounts_poll(struct file *file, poll_table *wait)return res; } -struct proc_fs_info { +struct proc_fs_opts {int flag;const char *str; };static int show_sb_opts (struct seq_file *m,struct super_block *sb) { -static const struct proc_fs_info fs_info[] = { +static const struct proc_fs_opts fs_opts[] = { {SB_SYNCHRONOUS ,",sync" }, {SB_DIRSYNC ,",dirsync" }, {SB_MANDLOCK ,",mand" }, {SB_LAZYTIME ,",lazytime" }, {0 ,NULL } }; -const struct proc_fs_info *fs_infop; +const struct proc_fs_opts *fs_infop; -for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { +for (fs_infop = fs_opts; fs_infop->flag; fs_infop++) {if (sb->s_flags & fs_infop->flag)seq_puts (m, fs_infop->str); }
@@ -63,7 +63,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb)static void show_mnt_opts (struct seq_file *m,struct vfsmount *mnt) { -static const struct proc_fs_info mnt_info[] = { +static const struct proc_fs_opts mnt_opts[] = { {MNT_NOSUID ,",nosuid" }, {MNT_NODEV ,",nodev" }, {MNT_NOEXEC ,",noexec" },
@@ -72,9 +72,9 @@ static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt) { MNT_RELATIME, ",relatime" }, { 0, NULL } }; -const struct proc_fs_info *fs_infop; +const struct proc_fs_opts *fs_infop; -for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { +for (fs_infop = mnt_opts; fs_infop->flag; fs_infop++) {if (mnt->mnt_flags & fs_infop->flag)seq_puts (m, fs_infop->str); }
Signature · 1 line
2.25.1
PATCH v9 4/8 proc: instantiate only pids that we can ptrace on 'hidepid=4' mount option
In reply to a message not in the archiveIf "hidepid=4" mount option is set then do not instantiate pids that we can not ptrace. "hidepid=4" means that procfs should only contain pids that the caller can ptrace.
--- fs/proc/base.c | 15 +++++++++++++++ fs/proc/root.c | 13 ++++++++++--- include/linux/proc_fs.h | 1 + 3 files changed, 26 insertions(+), 3 deletions(-)
fs/proc/base.c +15 -0
@@ -701,6 +701,14 @@ static bool has_pid_permissions(struct proc_fs_info *fs_info, struct task_struct *task, int hide_pid_min) { +/* +* If 'hidpid' mount option is set force a ptrace check, +* we indicate that we are using a filesystem syscall +* by passing PTRACE_MODE_READ_FSCREDS +*/ +if (fs_info->hide_pid ==HIDEPID_NOT_PTRACEABLE ) +return ptrace_may_access (task,PTRACE_MODE_READ_FSCREDS ); +if (fs_info->hide_pid < hide_pid_min)return true;if (in_group_p (fs_info->pid_gid))
@@ -3319,7 +3327,14 @@ struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)if (!task) goto out; +/* Limit procfs to only ptraceable tasks */ +if (fs_info->hide_pid ==HIDEPID_NOT_PTRACEABLE ) { +if (!has_pid_permissions (fs_info, task,HIDEPID_NO_ACCESS )) + goto out_put_task; + } + result =proc_pid_instantiate (dentry, task,NULL ); +out_put_task:put_task_struct (task); out:return result;
fs/proc/root.c +10 -3
@@ -47,6 +47,14 @@ static const struct fs_parameter_spec proc_fs_parameters[] = { {} }; +static inline int valid_hidepid (unsigned int value) +{ +return (value ==HIDEPID_OFF || + value ==HIDEPID_NO_ACCESS || + value ==HIDEPID_INVISIBLE || + value ==HIDEPID_NOT_PTRACEABLE ); +} +static int proc_parse_param (struct fs_context *fc,struct fs_parameter *param) {struct proc_fs_context *ctx = fc->fs_private;
@@ -63,10 +71,9 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)break ;case Opt_hidepid: +if (!valid_hidepid (result.uint_32)) +return invalf (fc,"proc: unknown value of hidepid.\n" ); ctx->hidepid = result.uint_32; -if (ctx->hidepid <HIDEPID_OFF || - ctx->hidepid >HIDEPID_INVISIBLE ) -return invalfc (fc,"hidepid value must be between 0 and 2.\n" );break ;default :
include/linux/proc_fs.h +1 -0
@@ -32,6 +32,7 @@ enum {HIDEPID_OFF =0 ,HIDEPID_NO_ACCESS =1 ,HIDEPID_INVISIBLE =2 , + HIDEPID_NOT_PTRACEABLE = 4, /* Limit pids to only ptraceable pids */ }; struct proc_fs_info {
Signature · 1 line
2.25.1
PATCH v9 2/8 proc: allow to mount many instances of proc in one pid namespace
In reply to a message not in the archiveThis 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/<pids>/ 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/<pids>/ 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/<pid>/ 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 suggested by Eric W. Biederman.
--- fs/proc/base.c | 13 +++++++++---- fs/proc/inode.c | 4 ++-- fs/proc/root.c | 42 +++++++++++++++++++++++++---------------- fs/proc/self.c | 6 +++--- fs/proc/thread_self.c | 6 +++--- include/linux/proc_fs.h | 12 ++++++++++++ 6 files changed, 55 insertions(+), 28 deletions(-)
fs/proc/base.c +9 -4
@@ -3301,6 +3301,7 @@ struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags) {struct task_struct *task;unsigned tgid; +struct proc_fs_info *fs_info;struct pid_namespace *ns;struct dentry *result =ERR_PTR (-ENOENT );
@@ -3308,7 +3309,8 @@ struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)if (tgid == ~0U ) goto out; - ns = dentry->d_sb->s_fs_info; + fs_info =proc_sb_info (dentry->d_sb); + ns = fs_info->pid_ns;rcu_read_lock (); task =find_task_by_pid_ns (tgid, ns);if (task)
@@ -3372,6 +3374,7 @@ static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iteint proc_pid_readdir (struct file *file,struct dir_context *ctx) {struct tgid_iter iter; +struct proc_fs_info *fs_info =proc_sb_info (file_inode (file)->i_sb);struct pid_namespace *ns =proc_pid_ns (file_inode (file));loff_t pos = ctx->pos;
@@ -3379,13 +3382,13 @@ int proc_pid_readdir(struct file *file, struct dir_context *ctx)return 0 ;if (pos ==TGID_OFFSET -2 ) { -struct inode *inode =d_inode (ns->proc_self); +struct inode *inode =d_inode (fs_info->proc_self);if (!dir_emit (ctx,"self" ,4 , inode->i_ino,DT_LNK ))return 0 ; ctx->pos = pos = pos +1 ; }if (pos ==TGID_OFFSET -1 ) { -struct inode *inode =d_inode (ns->proc_thread_self); +struct inode *inode =d_inode (fs_info->proc_thread_self);if (!dir_emit (ctx,"thread-self" ,11 , inode->i_ino,DT_LNK ))return 0 ; ctx->pos = pos = pos +1 ;
@@ -3599,6 +3602,7 @@ static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentrystruct task_struct *task;struct task_struct *leader =get_proc_task (dir);unsigned tid; +struct proc_fs_info *fs_info;struct pid_namespace *ns;struct dentry *result =ERR_PTR (-ENOENT );
@@ -3609,7 +3613,8 @@ static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentryif (tid == ~0U ) goto out; - ns = dentry->d_sb->s_fs_info; + fs_info =proc_sb_info (dentry->d_sb); + ns = fs_info->pid_ns;rcu_read_lock (); task =find_task_by_pid_ns (tid, ns);if (task)
fs/proc/inode.c +2 -2
@@ -167,8 +167,8 @@ void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lockstatic int proc_show_options (struct seq_file *seq,struct dentry *root) { -struct super_block *sb = root->d_sb; -struct pid_namespace *pid = sb->s_fs_info; +struct proc_fs_info *fs_info =proc_sb_info (root->d_sb); +struct pid_namespace *pid = fs_info->pid_ns;if (!gid_eq (pid->pid_gid,GLOBAL_ROOT_GID ))seq_printf (seq,",gid=%u" ,from_kgid_munged (&init_user_ns, pid->pid_gid));
fs/proc/root.c +26 -16
100 lines not on this page · raw message
fs/proc/self.c +3 -3
@@ -36,10 +36,10 @@ static unsigned self_inum __ro_after_init;int proc_setup_self (struct super_block *s) {struct inode *root_inode =d_inode (s->s_root); -struct pid_namespace *ns =proc_pid_ns (root_inode); +struct proc_fs_info *fs_info =proc_sb_info (s);struct dentry *self;int ret = -ENOMEM ; - +inode_lock (root_inode); self =d_alloc_name (s->s_root,"self" );if (self) {
@@ -62,7 +62,7 @@ int proc_setup_self(struct super_block *s)if (ret)pr_err ("proc_fill_super: can't allocate /proc/self\n" );else - ns->proc_self = self; + fs_info->proc_self = self;return ret; }
fs/proc/thread_self.c +3 -3
@@ -36,7 +36,7 @@ static unsigned thread_self_inum __ro_after_init;int proc_setup_thread_self (struct super_block *s) {struct inode *root_inode =d_inode (s->s_root); -struct pid_namespace *ns =proc_pid_ns (root_inode); +struct proc_fs_info *fs_info =proc_sb_info (s);struct dentry *thread_self;int ret = -ENOMEM ;
@@ -60,9 +60,9 @@ int proc_setup_thread_self(struct super_block *s)inode_unlock (root_inode);if (ret) -pr_err ("proc_fill_super: can't allocate /proc/thread_self\n" ); +pr_err ("proc_fill_super: can't allocate /proc/thread-self\n" );else - ns->proc_thread_self = thread_self; + fs_info->proc_thread_self = thread_self;return ret; }
include/linux/proc_fs.h +12 -0
@@ -27,6 +27,17 @@ struct proc_ops {unsigned long (*proc_get_unmapped_area)(struct file *,unsigned long ,unsigned long ,unsigned long ,unsigned long ); }; +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 */ +}; + +static inline struct proc_fs_info *proc_sb_info (struct super_block *sb) +{ +return sb->s_fs_info; +} +#ifdef CONFIG_PROC_FS typedef int (*proc_write_t )(struct file *,char *,size_t );
@@ -161,6 +172,7 @@ int open_related_ns(struct ns_common *ns,/* get the associated pid namespace for a file in procfs */ static inline struct pid_namespace *proc_pid_ns (const struct inode *inode) { +return proc_sb_info (inode->i_sb)->pid_ns;return inode->i_sb->s_fs_info; }
Signature · 1 line
2.25.1
PATCH v9 8/8 proc: use human-readable values for hidehid
In reply to a message not in the archiveThe hidepid parameter values are becoming more and more and it becomes difficult to remember what each new magic number means.
--- Documentation/filesystems/proc.txt | 52 +++++++++++++++--------------- fs/proc/inode.c | 13 +++++++- fs/proc/root.c | 36 +++++++++++++++++++-- 3 files changed, 71 insertions(+), 30 deletions(-)
Documentation/filesystems/proc.txt +26 -26
@@ -2025,28 +2025,28 @@ The following mount options are supported: gid= Set the group authorized to learn processes information. subset= Show only the specified subset of procfs. -hidepid=0 means classic mode - everybody may access all /proc/<pid>/ directories -(default). - -hidepid=1 means users may not access any /proc/<pid>/ directories but their -own. Sensitive files like cmdline, sched*, status are now protected against -other users. This makes it impossible to learn whether any user runs -specific program (given the program doesn't reveal itself by its behaviour). -As an additional bonus, as /proc/<pid>/cmdline is unaccessible for other users, -poorly written programs passing sensitive information via program arguments are -now protected against local eavesdroppers. - -hidepid=2 means hidepid=1 plus all /proc/<pid>/ will be fully invisible to other -users. It doesn't mean that it hides a fact whether a process with a specific -pid value exists (it can be learned by other means, e.g. by "kill -0 $PID"), -but it hides process' uid and gid, which may be learned by stat()'ing -/proc/<pid>/ otherwise. It greatly complicates an intruder's task of gathering -information about running processes, whether some daemon runs with elevated -privileges, whether other user runs some sensitive program, whether other users -run any program at all, etc. - -hidepid=4 means that procfs should only contain /proc/<pid>/ directories -that the caller can ptrace. +hidepid=off or hidepid=0 means classic mode - everybody may access all +/proc/<pid>/ directories (default). + +hidepid=noaccess or hidepid=1 means users may not access any /proc/<pid>/ +directories but their own. Sensitive files like cmdline, sched*, status are now +protected against other users. This makes it impossible to learn whether any +user runs specific program (given the program doesn't reveal itself by its +behaviour). As an additional bonus, as /proc/<pid>/cmdline is unaccessible for +other users, poorly written programs passing sensitive information via program +arguments are now protected against local eavesdroppers. + +hidepid=invisible or hidepid=2 means hidepid=noaccess plus all /proc/<pid>/ will +be fully invisible to other users. It doesn't mean that it hides a fact whether +a process with a specific pid value exists (it can be learned by other means, +e.g. by "kill -0 $PID"), but it hides process' uid and gid, which may be learned +by stat()'ing /proc/<pid>/ otherwise. It greatly complicates an intruder's task +of gathering information about running processes, whether some daemon runs with +elevated privileges, whether other user runs some sensitive program, whether +other users run any program at all, etc. + +hidepid=ptraceable or hidepid=4 means that procfs should only contain +/proc/<pid>/ directories that the caller can ptrace. gid= defines a group authorized to learn processes information otherwise prohibited by hidepid=. If you use some daemon like identd which needs to learn
@@ -2093,8 +2093,8 @@ creates a new procfs instance. Mount options affect own procfs instance. It means that it became possible to have several procfs instances displaying tasks with different filtering options in one pid namespace. -# mount -o hidepid=2 -t proc proc /proc -# mount -o hidepid=1 -t proc proc /tmp/proc +# mount -o hidepid=invisible -t proc proc /proc +# mount -o hidepid=noaccess -t proc proc /tmp/proc # grep ^proc /proc/mounts -proc /proc proc rw,relatime,hidepid=2 0 0 -proc /tmp/proc proc rw,relatime,hidepid=1 0 0 +proc /proc proc rw,relatime,hidepid=invisible 0 0 +proc /tmp/proc proc rw,relatime,hidepid=noaccess 0 0
fs/proc/inode.c +12 -1
@@ -165,6 +165,17 @@ void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lockdeactivate_super (old_sb); } +static inline const char *hidepid2str (int v) +{ +switch (v) { +case HIDEPID_OFF :return "off" ; +case HIDEPID_NO_ACCESS :return "noaccess" ; +case HIDEPID_INVISIBLE :return "invisible" ; +case HIDEPID_NOT_PTRACEABLE :return "ptraceable" ; + } +BUG (); +} +static int proc_show_options (struct seq_file *seq,struct dentry *root) {struct proc_fs_info *fs_info =proc_sb_info (root->d_sb);
@@ -172,7 +183,7 @@ static int proc_show_options(struct seq_file *seq, struct dentry *root)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); +seq_printf (seq,",hidepid=%s" ,hidepid2str (fs_info->hide_pid));if (fs_info->pidonly !=PROC_PIDONLY_OFF )seq_printf (seq,",subset=pidfs" );
fs/proc/root.c +33 -3
@@ -45,7 +45,7 @@ enum proc_param {static const struct fs_parameter_spec proc_fs_parameters[] = {fsparam_u32 ("gid" , Opt_gid), -fsparam_u32 ("hidepid" , Opt_hidepid), +fsparam_string ("hidepid" , Opt_hidepid),fsparam_string ("subset" , Opt_subset), {} };
@@ -58,6 +58,35 @@ static inline int valid_hidepid(unsigned int value) value == HIDEPID_NOT_PTRACEABLE); } +static int proc_parse_hidepid_param (struct fs_context *fc,struct fs_parameter *param) +{ +struct proc_fs_context *ctx = fc->fs_private; +struct fs_parameter_spec hidepid_u32_spec =fsparam_u32 ("hidepid" , Opt_hidepid); +struct fs_parse_result result; +int base = (unsigned long )hidepid_u32_spec.data; + +if (param->type != fs_value_is_string) +return invalf (fc,"proc: unexpected type of hidepid value\n" ); + +if (!kstrtouint (param->string, base, &result.uint_32)) { + ctx->hidepid = result.uint_32; +return 0 ; + } + +if (!strcmp (param->string,"off" )) + ctx->hidepid =HIDEPID_OFF ; +else if (!strcmp (param->string,"noaccess" )) + ctx->hidepid =HIDEPID_NO_ACCESS ; +else if (!strcmp (param->string,"invisible" )) + ctx->hidepid =HIDEPID_INVISIBLE ; +else if (!strcmp (param->string,"ptraceable" )) + ctx->hidepid =HIDEPID_NOT_PTRACEABLE ; +else +return invalf (fc,"proc: unknown value of hidepid - %s\n" , param->string); + +return 0 ; +} +static int proc_parse_subset_param (struct fs_context *fc,char *value) {struct proc_fs_context *ctx = fc->fs_private;
@@ -97,9 +126,10 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)break ;case Opt_hidepid: -if (!valid_hidepid (result.uint_32)) +if (proc_parse_hidepid_param (fc, param)) +return -EINVAL ; +if (!valid_hidepid (ctx->hidepid))return invalf (fc,"proc: unknown value of hidepid.\n" ); - ctx->hidepid = result.uint_32;break ;case Opt_subset:
Signature · 1 line
2.25.1
PATCH v9 5/8 proc: add option to mount only a pids subset
In reply to a message not in the archiveThis allows to hide all files and directories in the procfs that are not related to tasks.
--- 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(+)
fs/proc/generic.c +9 -0
@@ -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)); }
fs/proc/inode.c +6 -0
@@ -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=pidfs" );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
fs/proc/root.c +33 -0
@@ -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,"pidfs" )) { + 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)
include/linux/proc_fs.h +7 -0
@@ -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)
Signature · 1 line
2.25.1
PATCH v9 6/8 docs: proc: add documentation for "hidepid=4" and "subset=pidfs" options and new mount behavior
In reply to a message not in the archive--- Documentation/filesystems/proc.txt | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
Documentation/filesystems/proc.txt +53 -0
@@ -50,6 +50,8 @@ Table of Contents 4 Configuring procfs 4.1 Mount options + 5 Filesystem behavior + ------------------------------------------------------------------------------ Preface ------------------------------------------------------------------------------
@@ -2021,6 +2023,7 @@ The following mount options are supported: hidepid= Set /proc/<pid>/ access mode. gid= Set the group authorized to learn processes information. + subset= Show only the specified subset of procfs. hidepid=0 means classic mode - everybody may access all /proc/<pid>/ directories (default).
@@ -2042,6 +2045,56 @@ information about running processes, whether some daemon runs with elevated privileges, whether other user runs some sensitive program, whether other users run any program at all, etc. +hidepid=4 means that procfs should only contain /proc/<pid>/ directories +that the caller can ptrace. + gid= defines a group authorized to learn processes information otherwise prohibited by hidepid=. If you use some daemon like identd which needs to learn information about processes information, just add identd to this group. + +subset=pidfs hides all top level files and directories in the procfs that +are not related to tasks. + +------------------------------------------------------------------------------ +5 Filesystem behavior +------------------------------------------------------------------------------ + +Originally, before the advent of pid namepsace, procfs was a global file +system. It means that there was only one procfs instance in the system. + +When pid namespace was added, a separate procfs instance was mounted in +each pid namespace. So, procfs mount options are global among all +mountpoints within the same namespace. + +# grep ^proc /proc/mounts +proc /proc proc rw,relatime,hidepid=2 0 0 + +# strace -e mount mount -o hidepid=1 -t proc proc /tmp/proc +mount("proc", "/tmp/proc", "proc", 0, "hidepid=1") = 0 ++++ exited with 0 +++ + +# grep ^proc /proc/mounts +proc /proc proc rw,relatime,hidepid=2 0 0 +proc /tmp/proc proc rw,relatime,hidepid=2 0 0 + +and only after remounting procfs mount options will change at all +mountpoints. + +# mount -o remount,hidepid=1 -t proc proc /tmp/proc + +# grep ^proc /proc/mounts +proc /proc proc rw,relatime,hidepid=1 0 0 +proc /tmp/proc proc rw,relatime,hidepid=1 0 0 + +This behavior is different from the behavior of other filesystems. + +The new procfs behavior is more like other filesystems. Each procfs mount +creates a new procfs instance. Mount options affect own procfs instance. +It means that it became possible to have several procfs instances +displaying tasks with different filtering options in one pid namespace. + +# mount -o hidepid=2 -t proc proc /proc +# mount -o hidepid=1 -t proc proc /tmp/proc +# grep ^proc /proc/mounts +proc /proc proc rw,relatime,hidepid=2 0 0 +proc /tmp/proc proc rw,relatime,hidepid=1 0 0
Signature · 1 line
2.25.1
PATCH v9 7/8 proc: move hidepid values to uapi as they are user interface to mount
In reply to a message not in the archive--- include/linux/proc_fs.h | 9 +-------- include/uapi/linux/proc_fs.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 include/uapi/linux/proc_fs.h
include/linux/proc_fs.h +1 -8
@@ -7,6 +7,7 @@#include <linux/types.h> #include <linux/fs.h> +#include <uapi/linux/proc_fs.h> struct proc_dir_entry ;struct seq_file ;
@@ -27,14 +28,6 @@ struct proc_ops {unsigned long (*proc_get_unmapped_area)(struct file *,unsigned long ,unsigned long ,unsigned long ,unsigned long ); }; -/* definitions for hide_pid field */ -enum { -HIDEPID_OFF =0 , -HIDEPID_NO_ACCESS =1 , -HIDEPID_INVISIBLE =2 , -HIDEPID_NOT_PTRACEABLE =4 ,/* Limit pids to only ptraceable pids */ -}; -/* definitions for proc mount option pidonly */ enum {PROC_PIDONLY_OFF =0 ,
include/uapi/linux/proc_fs.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_PROC_FS_H +#define _UAPI_PROC_FS_H + +/* definitions for hide_pid field */ +enum { +HIDEPID_OFF =0 , +HIDEPID_NO_ACCESS =1 , +HIDEPID_INVISIBLE =2 , +HIDEPID_NOT_PTRACEABLE =4 , +}; + +#endif
Signature · 1 line
2.25.1
PATCH v9 3/8 proc: move hide_pid, pid_gid from pid_namespace to proc_fs_info
In reply to a message not in the archiveMove hide_pid and pid_gid parameters inside procfs fs_info struct instead of making them per pid namespace. Since we have a multiple procfs instances per pid namespace we need to make sure that all proc-specific parameters are also per-superblock.
--- fs/proc/base.c | 18 +++++++++--------- fs/proc/inode.c | 9 ++++----- fs/proc/root.c | 4 ++-- include/linux/pid_namespace.h | 8 -------- include/linux/proc_fs.h | 9 +++++++++ 5 files changed, 24 insertions(+), 24 deletions(-)
fs/proc/base.c +9 -9
@@ -697,13 +697,13 @@ int proc_setattr(struct dentry *dentry, struct iattr *attr)* May current process learn task's sched/cmdline info (for hide_pid_min=1) * or euid/egid (for hide_pid_min=2)? */ -static bool has_pid_permissions (struct pid_namespace *pid, +static bool has_pid_permissions (struct proc_fs_info *fs_info,struct task_struct *task,int hide_pid_min) { -if (pid->hide_pid < hide_pid_min) +if (fs_info->hide_pid < hide_pid_min)return true; -if (in_group_p (pid->pid_gid)) +if (in_group_p (fs_info->pid_gid))return true;return ptrace_may_access (task,PTRACE_MODE_READ_FSCREDS ); }
@@ -711,18 +711,18 @@ static bool has_pid_permissions(struct pid_namespace *pid,static int proc_pid_permission (struct inode *inode,int mask) { -struct pid_namespace *pid =proc_pid_ns (inode); +struct proc_fs_info *fs_info =proc_sb_info (inode->i_sb);struct task_struct *task;bool has_perms; task =get_proc_task (inode);if (!task)return -ESRCH ; - has_perms =has_pid_permissions (pid, task,HIDEPID_NO_ACCESS ); + has_perms =has_pid_permissions (fs_info, task,HIDEPID_NO_ACCESS );put_task_struct (task);if (!has_perms) { -if (pid->hide_pid ==HIDEPID_INVISIBLE ) { +if (fs_info->hide_pid ==HIDEPID_INVISIBLE ) {/* * Let's make getdents(), stat(), and open() * consistent with each other. If a process
@@ -1897,7 +1897,7 @@ int pid_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int query_flags) { struct inode *inode = d_inode(path->dentry); -struct pid_namespace *pid =proc_pid_ns (inode); +struct proc_fs_info *fs_info =proc_sb_info (inode->i_sb);struct task_struct *task;generic_fillattr (inode, stat);
@@ -1907,7 +1907,7 @@ int pid_getattr(const struct path *path, struct kstat *stat,rcu_read_lock (); task =pid_task (proc_pid (inode),PIDTYPE_PID );if (task) { -if (!has_pid_permissions (pid, task,HIDEPID_INVISIBLE )) { +if (!has_pid_permissions (fs_info, task,HIDEPID_INVISIBLE )) {rcu_read_unlock ();/* * This doesn't prevent learning whether PID exists,
@@ -3402,7 +3402,7 @@ int proc_pid_readdir(struct file *file, struct dir_context *ctx)unsigned int len;cond_resched (); -if (!has_pid_permissions (ns, iter.task,HIDEPID_INVISIBLE )) +if (!has_pid_permissions (fs_info, iter.task,HIDEPID_INVISIBLE ))continue ; len =snprintf (name,sizeof (name),"%u" , iter.tgid);
fs/proc/inode.c +4 -5
@@ -168,12 +168,11 @@ void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lockstatic int proc_show_options (struct seq_file *seq,struct dentry *root) {struct proc_fs_info *fs_info =proc_sb_info (root->d_sb); -struct pid_namespace *pid = fs_info->pid_ns; -if (!gid_eq (pid->pid_gid,GLOBAL_ROOT_GID )) -seq_printf (seq,",gid=%u" ,from_kgid_munged (&init_user_ns, pid->pid_gid)); -if (pid->hide_pid !=HIDEPID_OFF ) -seq_printf (seq,",hidepid=%u" , pid->hide_pid); +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);return 0 ; }
fs/proc/root.c +2 -2
@@ -85,9 +85,9 @@ static void proc_apply_options(struct super_block *s,struct proc_fs_context *ctx = fc->fs_private;if (ctx->mask & (1 << Opt_gid)) - pid_ns->pid_gid =make_kgid (user_ns, ctx->gid); + ctx->fs_info->pid_gid =make_kgid (user_ns, ctx->gid);if (ctx->mask & (1 << Opt_hidepid)) - pid_ns->hide_pid = ctx->hidepid; + ctx->fs_info->hide_pid = ctx->hidepid; }static int proc_fill_super (struct super_block *s,struct fs_context *fc)
include/linux/pid_namespace.h +0 -8
@@ -17,12 +17,6 @@struct fs_pin ; -enum {/* definitions for pid_namespace's hide_pid field */ -HIDEPID_OFF =0 , -HIDEPID_NO_ACCESS =1 , -HIDEPID_INVISIBLE =2 , -}; -struct pid_namespace {struct kref kref;struct idr idr;
@@ -41,8 +35,6 @@ struct pid_namespace {#endif struct user_namespace *user_ns;struct ucounts *ucounts; -kgid_t pid_gid; -int hide_pid;int reboot;/* group exit code if this pidns was rebooted */ struct ns_common ns; } __randomize_layout;
include/linux/proc_fs.h +9 -0
@@ -27,10 +27,19 @@ struct proc_ops {unsigned long (*proc_get_unmapped_area)(struct file *,unsigned long ,unsigned long ,unsigned long ,unsigned long ); }; +/* definitions for hide_pid field */ +enum { +HIDEPID_OFF =0 , +HIDEPID_NO_ACCESS =1 , +HIDEPID_INVISIBLE =2 , +}; +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; };static inline struct proc_fs_info *proc_sb_info (struct super_block *sb)
Signature · 1 line
2.25.1