Skip to content

Commit 41e8149

Browse files
10ne1brauner
authored andcommitted
proc: add config & param to block forcing mem writes
This adds a Kconfig option and boot param to allow removing the FOLL_FORCE flag from /proc/pid/mem write calls because it can be abused. The traditional forcing behavior is kept as default because it can break GDB and some other use cases. Previously we tried a more sophisticated approach allowing distributions to fine-tune /proc/pid/mem behavior, however that got NAK-ed by Linus [1], who prefers this simpler approach with semantics also easier to understand for users. Link: https://lore.kernel.org/lkml/CAHk-=wiGWLChxYmUA5HrT5aopZrB7_2VTa0NLZcxORgkUe5tEQ@mail.gmail.com/ [1] Cc: Doug Anderson <[email protected]> Cc: Jeff Xu <[email protected]> Cc: Jann Horn <[email protected]> Cc: Kees Cook <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Christian Brauner <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Adrian Ratiu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 8400291 commit 41e8149

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,16 @@
47884788
printk.time= Show timing data prefixed to each printk message line
47894789
Format: <bool> (1/Y/y=enable, 0/N/n=disable)
47904790

4791+
proc_mem.force_override= [KNL]
4792+
Format: {always | ptrace | never}
4793+
Traditionally /proc/pid/mem allows memory permissions to be
4794+
overridden without restrictions. This option may be set to
4795+
restrict that. Can be one of:
4796+
- 'always': traditional behavior always allows mem overrides.
4797+
- 'ptrace': only allow mem overrides for active ptracers.
4798+
- 'never': never allow mem overrides.
4799+
If not specified, default is the CONFIG_PROC_MEM_* choice.
4800+
47914801
processor.max_cstate= [HW,ACPI]
47924802
Limit processor to maximum C-state
47934803
max_cstate=9 overrides any DMI blacklist limit.

fs/proc/base.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include <linux/elf.h>
8686
#include <linux/pid_namespace.h>
8787
#include <linux/user_namespace.h>
88+
#include <linux/fs_parser.h>
8889
#include <linux/fs_struct.h>
8990
#include <linux/slab.h>
9091
#include <linux/sched/autogroup.h>
@@ -117,6 +118,40 @@
117118
static u8 nlink_tid __ro_after_init;
118119
static u8 nlink_tgid __ro_after_init;
119120

121+
enum proc_mem_force {
122+
PROC_MEM_FORCE_ALWAYS,
123+
PROC_MEM_FORCE_PTRACE,
124+
PROC_MEM_FORCE_NEVER
125+
};
126+
127+
static enum proc_mem_force proc_mem_force_override __ro_after_init =
128+
IS_ENABLED(CONFIG_PROC_MEM_NO_FORCE) ? PROC_MEM_FORCE_NEVER :
129+
IS_ENABLED(CONFIG_PROC_MEM_FORCE_PTRACE) ? PROC_MEM_FORCE_PTRACE :
130+
PROC_MEM_FORCE_ALWAYS;
131+
132+
static const struct constant_table proc_mem_force_table[] __initconst = {
133+
{ "always", PROC_MEM_FORCE_ALWAYS },
134+
{ "ptrace", PROC_MEM_FORCE_PTRACE },
135+
{ "never", PROC_MEM_FORCE_NEVER },
136+
{ }
137+
};
138+
139+
static int __init early_proc_mem_force_override(char *buf)
140+
{
141+
if (!buf)
142+
return -EINVAL;
143+
144+
/*
145+
* lookup_constant() defaults to proc_mem_force_override to preseve
146+
* the initial Kconfig choice in case an invalid param gets passed.
147+
*/
148+
proc_mem_force_override = lookup_constant(proc_mem_force_table,
149+
buf, proc_mem_force_override);
150+
151+
return 0;
152+
}
153+
early_param("proc_mem.force_override", early_proc_mem_force_override);
154+
120155
struct pid_entry {
121156
const char *name;
122157
unsigned int len;
@@ -835,6 +870,28 @@ static int mem_open(struct inode *inode, struct file *file)
835870
return ret;
836871
}
837872

873+
static bool proc_mem_foll_force(struct file *file, struct mm_struct *mm)
874+
{
875+
struct task_struct *task;
876+
bool ptrace_active = false;
877+
878+
switch (proc_mem_force_override) {
879+
case PROC_MEM_FORCE_NEVER:
880+
return false;
881+
case PROC_MEM_FORCE_PTRACE:
882+
task = get_proc_task(file_inode(file));
883+
if (task) {
884+
ptrace_active = READ_ONCE(task->ptrace) &&
885+
READ_ONCE(task->mm) == mm &&
886+
READ_ONCE(task->parent) == current;
887+
put_task_struct(task);
888+
}
889+
return ptrace_active;
890+
default:
891+
return true;
892+
}
893+
}
894+
838895
static ssize_t mem_rw(struct file *file, char __user *buf,
839896
size_t count, loff_t *ppos, int write)
840897
{
@@ -855,7 +912,9 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
855912
if (!mmget_not_zero(mm))
856913
goto free;
857914

858-
flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
915+
flags = write ? FOLL_WRITE : 0;
916+
if (proc_mem_foll_force(file, mm))
917+
flags |= FOLL_FORCE;
859918

860919
while (count > 0) {
861920
size_t this_len = min_t(size_t, count, PAGE_SIZE);

security/Kconfig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ config SECURITY_DMESG_RESTRICT
1919

2020
If you are unsure how to answer this question, answer N.
2121

22+
choice
23+
prompt "Allow /proc/pid/mem access override"
24+
default PROC_MEM_ALWAYS_FORCE
25+
help
26+
Traditionally /proc/pid/mem allows users to override memory
27+
permissions for users like ptrace, assuming they have ptrace
28+
capability.
29+
30+
This allows people to limit that - either never override, or
31+
require actual active ptrace attachment.
32+
33+
Defaults to the traditional behavior (for now)
34+
35+
config PROC_MEM_ALWAYS_FORCE
36+
bool "Traditional /proc/pid/mem behavior"
37+
help
38+
This allows /proc/pid/mem accesses to override memory mapping
39+
permissions if you have ptrace access rights.
40+
41+
config PROC_MEM_FORCE_PTRACE
42+
bool "Require active ptrace() use for access override"
43+
help
44+
This allows /proc/pid/mem accesses to override memory mapping
45+
permissions for active ptracers like gdb.
46+
47+
config PROC_MEM_NO_FORCE
48+
bool "Never"
49+
help
50+
Never override memory mapping permissions
51+
52+
endchoice
53+
2254
config SECURITY
2355
bool "Enable different security models"
2456
depends on SYSFS

0 commit comments

Comments
 (0)