Skip to content

Commit 831830b

Browse files
Al ViroLinus Torvalds
authored andcommitted
restrict reading from /proc/<pid>/maps to those who share ->mm or can ptrace pid
Contents of /proc/*/maps is sensitive and may become sensitive after open() (e.g. if target originally shares our ->mm and later does exec on suid-root binary). Check at read() (actually, ->start() of iterator) time that mm_struct we'd grabbed and locked is - still the ->mm of target - equal to reader's ->mm or the target is ptracable by reader. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Acked-by: Rik van Riel <riel@redhat.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ac40532 commit 831830b

6 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vf
202202
(task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
203203
security_ptrace(current,task) == 0))
204204

205+
struct mm_struct *mm_for_maps(struct task_struct *task)
206+
{
207+
struct mm_struct *mm = get_task_mm(task);
208+
if (!mm)
209+
return NULL;
210+
down_read(&mm->mmap_sem);
211+
task_lock(task);
212+
if (task->mm != mm)
213+
goto out;
214+
if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215+
goto out;
216+
task_unlock(task);
217+
return mm;
218+
out:
219+
task_unlock(task);
220+
up_read(&mm->mmap_sem);
221+
mmput(mm);
222+
return NULL;
223+
}
224+
205225
static int proc_pid_cmdline(struct task_struct *task, char * buffer)
206226
{
207227
int res = 0;

‎fs/proc/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct vmalloc_info {
2727
unsigned long largest_chunk;
2828
};
2929

30+
extern struct mm_struct *mm_for_maps(struct task_struct *);
31+
3032
#ifdef CONFIG_MMU
3133
#define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
3234
extern void get_vmalloc_info(struct vmalloc_info *vmi);

‎fs/proc/task_mmu.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,11 @@ static void *m_start(struct seq_file *m, loff_t *pos)
397397
if (!priv->task)
398398
return NULL;
399399

400-
mm = get_task_mm(priv->task);
400+
mm = mm_for_maps(priv->task);
401401
if (!mm)
402402
return NULL;
403403

404404
priv->tail_vma = tail_vma = get_gate_vma(priv->task);
405-
down_read(&mm->mmap_sem);
406405

407406
/* Start with last addr hint */
408407
if (last_addr && (vma = find_vma(mm, last_addr))) {

‎fs/proc/task_nommu.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,13 @@ static void *m_start(struct seq_file *m, loff_t *pos)
165165
if (!priv->task)
166166
return NULL;
167167

168-
mm = get_task_mm(priv->task);
168+
mm = mm_for_maps(priv->task);
169169
if (!mm) {
170170
put_task_struct(priv->task);
171171
priv->task = NULL;
172172
return NULL;
173173
}
174174

175-
down_read(&mm->mmap_sem);
176-
177175
/* start from the Nth VMA */
178176
for (vml = mm->context.vmlist; vml; vml = vml->next)
179177
if (n-- == 0)

‎include/linux/ptrace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern void __ptrace_link(struct task_struct *child,
9797
extern void __ptrace_unlink(struct task_struct *child);
9898
extern void ptrace_untrace(struct task_struct *child);
9999
extern int ptrace_may_attach(struct task_struct *task);
100+
extern int __ptrace_may_attach(struct task_struct *task);
100101

101102
static inline void ptrace_link(struct task_struct *child,
102103
struct task_struct *new_parent)

‎kernel/ptrace.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int ptrace_check_attach(struct task_struct *child, int kill)
120120
return ret;
121121
}
122122

123-
static int may_attach(struct task_struct *task)
123+
int __ptrace_may_attach(struct task_struct *task)
124124
{
125125
/* May we inspect the given task?
126126
* This check is used both for attaching with ptrace
@@ -154,7 +154,7 @@ int ptrace_may_attach(struct task_struct *task)
154154
{
155155
int err;
156156
task_lock(task);
157-
err = may_attach(task);
157+
err = __ptrace_may_attach(task);
158158
task_unlock(task);
159159
return !err;
160160
}

0 commit comments

Comments
 (0)