Skip to contentSkip to conversation

sysctl: control functionality of /proc/pid/mem

PATCH Superseded Kees Cook 3 participants 4 messages Jan 21, 2012 – Jan 23, 2012

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 sysctl: control functionality of /proc/pid/mem

Add the "proc_pid_mem" sysctl to control whether or not /proc/pid/mem is allowed to work: 0: disabled, 1: read only, 2: read/write.

---
 Documentation/sysctl/kernel.txt |   14 ++++++++++++++
 fs/proc/base.c                  |   14 +++++++++++++-
 kernel/sysctl.c                 |   14 ++++++++++++++
 3 files changed, 41 insertions(+), 1 deletions(-)
Documentation/sysctl/kernel.txt +14 -0
index 8c20fbd..6d52dba 100644
@@ -56,6 +56,7 @@ show up in /proc/sys/kernel:
 - printk_delay
 - printk_ratelimit
 - printk_ratelimit_burst
+- proc_pid_mem
 - randomize_va_space
 - real-root-dev               ==> Documentation/initrd.txt
 - reboot-cmd                  [ SPARC only ]
@@ -477,6 +478,19 @@ send before ratelimiting kicks in.
 
 ==============================================================
 
+proc_pid_mem:
+
+This option can be used to select the level of access given to potential
+ptracers when using the per-process "mem" file in /proc/pid/mem.
+
+0 - Disable entirely.
+
+1 - Allow potential ptracers read access to process memory, but not writes.
+
+2 - Allow potential ptracers read and write access to process memory.
+
+==============================================================
+
 randomize_va_space:
 
 This option can be used to select the type of process address
fs/proc/base.c +13 -1
index 9cde9ed..53133c7 100644
@@ -109,6 +109,8 @@ struct pid_entry {
 	union proc_op op;
 };
 
+int sysctl_proc_pid_mem = 2;
+
 #define NOD(NAME, MODE, IOP, FOP, OP) {			\
 	.name = (NAME),					\
 	.len  = sizeof(NAME) - 1,			\
@@ -699,9 +701,13 @@ static const struct file_operations proc_single_file_operations = {
 
 static int mem_open(struct inode* inode, struct file* file)
 {
-	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+	struct task_struct *task;
 	struct mm_struct *mm;
 
+	if (sysctl_proc_pid_mem < 1)
+		return -EACCES;
+
+	task = get_proc_task(file->f_path.dentry->d_inode);
 	if (!task)
 		return -ESRCH;
 
@@ -726,6 +732,9 @@ static ssize_t mem_read(struct file * file, char __user * buf,
 	unsigned long src = *ppos;
 	struct mm_struct *mm = file->private_data;
 
+	if (sysctl_proc_pid_mem < 1)
+		return -EACCES;
+
 	if (!mm)
 		return 0;
 
@@ -770,6 +779,9 @@ static ssize_t mem_write(struct file * file, const char __user *buf,
 	unsigned long dst = *ppos;
 	struct mm_struct *mm = file->private_data;
 
+	if (sysctl_proc_pid_mem < 2)
+		return -EACCES;
+
 	if (!mm)
 		return 0;
 
kernel/sysctl.c +14 -0
index f487f25..dda911f 100644
@@ -109,6 +109,9 @@ extern int sysctl_nr_trim_pages;
 #ifdef CONFIG_BLOCK
 extern int blk_iopoll_enabled;
 #endif
+#ifdef CONFIG_PROC_FS
+extern int sysctl_proc_pid_mem;
+#endif
 
 /* Constants used for minimum and  maximum */
 #ifdef CONFIG_LOCKUP_DETECTOR
@@ -1004,6 +1007,17 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= proc_dointvec,
 	},
 #endif
+#ifdef CONFIG_PROC_FS
+	{
+		.procname	= "proc_pid_mem",
+		.data		= &sysctl_proc_pid_mem,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= &zero,
+		.extra2		= &two,
+	},
+#endif
 	{ }
 };
 
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 2 lines · Kees Cook

Add the "proc_pid_mem" sysctl to control whether or not /proc/pid/mem is allowed to work: 0: disabled, 1: read only, 2: read/write.

Maybe mention that the default is 2 (or did you mean to change that sooner or later?).

Quoted 6 lines
---
 Documentation/sysctl/kernel.txt |   14 ++++++++++++++
 fs/proc/base.c                  |   14 +++++++++++++-
 kernel/sysctl.c                 |   14 ++++++++++++++
 3 files changed, 41 insertions(+), 1 deletions(-)
Signature · 2 lines
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
#2 lore raw
In reply to Kees Cook
Quoted 105 lines · Kees Cook

Add the "proc_pid_mem" sysctl to control whether or not /proc/pid/mem is allowed to work: 0: disabled, 1: read only, 2: read/write.

---
 Documentation/sysctl/kernel.txt |   14 ++++++++++++++
 fs/proc/base.c                  |   14 +++++++++++++-
 kernel/sysctl.c                 |   14 ++++++++++++++
 3 files changed, 41 insertions(+), 1 deletions(-)
Documentation/sysctl/kernel.txt +14 -0
index 8c20fbd..6d52dba 100644
@@ -56,6 +56,7 @@ show up in /proc/sys/kernel:
 - printk_delay
 - printk_ratelimit
 - printk_ratelimit_burst
+- proc_pid_mem
 - randomize_va_space
 - real-root-dev               ==> Documentation/initrd.txt
 - reboot-cmd                  [ SPARC only ]
@@ -477,6 +478,19 @@ send before ratelimiting kicks in.
 
 ==============================================================
 
+proc_pid_mem:
+
+This option can be used to select the level of access given to potential
+ptracers when using the per-process "mem" file in /proc/pid/mem.
+
+0 - Disable entirely.
+
+1 - Allow potential ptracers read access to process memory, but not writes.
+
+2 - Allow potential ptracers read and write access to process memory.
+
+==============================================================
+
 randomize_va_space:
 
 This option can be used to select the type of process address
fs/proc/base.c +13 -1
index 9cde9ed..53133c7 100644
@@ -109,6 +109,8 @@ struct pid_entry {
 	union proc_op op;
 };
 
+int sysctl_proc_pid_mem = 2;
+
 #define NOD(NAME, MODE, IOP, FOP, OP) {			\
 	.name = (NAME),					\
 	.len  = sizeof(NAME) - 1,			\
@@ -699,9 +701,13 @@ static const struct file_operations proc_single_file_operations = {
 
 static int mem_open(struct inode* inode, struct file* file)
 {
-	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+	struct task_struct *task;
 	struct mm_struct *mm;
 
+	if (sysctl_proc_pid_mem < 1)
+		return -EACCES;
+
+	task = get_proc_task(file->f_path.dentry->d_inode);
 	if (!task)
 		return -ESRCH;
 
@@ -726,6 +732,9 @@ static ssize_t mem_read(struct file * file, char __user * buf,
 	unsigned long src = *ppos;
 	struct mm_struct *mm = file->private_data;
 
+	if (sysctl_proc_pid_mem < 1)
+		return -EACCES;
+
 	if (!mm)
 		return 0;
 
@@ -770,6 +779,9 @@ static ssize_t mem_write(struct file * file, const char __user *buf,
 	unsigned long dst = *ppos;
 	struct mm_struct *mm = file->private_data;
 
+	if (sysctl_proc_pid_mem < 2)
+		return -EACCES;
+
 	if (!mm)
 		return 0;
 
kernel/sysctl.c +3 -0
index f487f25..dda911f 100644
@@ -109,6 +109,9 @@ extern int sysctl_nr_trim_pages;
 #ifdef CONFIG_BLOCK
 extern int blk_iopoll_enabled;
 #endif
+#ifdef CONFIG_PROC_FS
+extern int sysctl_proc_pid_mem;
+#endif
 
 /* Constants used for minimum and  maximum */
 #ifdef CONFIG_LOCKUP_DETECTOR

kernel/sysctl.c static struct ctl_table kern_table[] = { · line 1010

Quoted 4 lines
kernel/sysctl.c +1 -0
@@ -1004,6 +1007,17 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= proc_dointvec,
 	},
 #endif
+#ifdef CONFIG_PROC_FS
    ^^^^^^^^^^^^^^^^^^^^^^

That ifdef is entertaining. CONFIG_SYSCTL depends on CONFIG_PROC_FS so which interesting case did you imagine this ifdef would be false? Did you test to ensure the code is not compiled in that interesting case?

Quoted 12 lines
kernel/sysctl.c +10 -0
+	{
+		.procname	= "proc_pid_mem",
+		.data		= &sysctl_proc_pid_mem,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= &zero,
+		.extra2		= &two,
+	},
+#endif
 	{ }
 };
author
#3 lore raw
Quoted 6 lines · Eric W. Biederman
Quoted 1 line

+#ifdef CONFIG_PROC_FS

   ^^^^^^^^^^^^^^^^^^^^^^

That ifdef is entertaining.  CONFIG_SYSCTL depends on CONFIG_PROC_FS so which interesting case did you imagine this ifdef would be false? Did you test to ensure the code is not compiled in that interesting case?

I noticed that when I added it, and decided to err on the side of more configs if some day /proc/sys got moved out of /proc. Anyway, I can easily remove it. v3 coming up...

-Kees

Signature · 2 lines
Kees Cook
ChromeOS Security