Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] remove duplicated code from proc and ptrace

Extract common code used by ptrace_attach() and may_ptrace_attach()
into a separate function.

Signed-off-by: Miklos Szeredi <miklos@szeredi.hu>
Cc: <viro@parcelfarce.linux.theplanet.co.uk>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Miklos Szeredi and committed by
Linus Torvalds
ab8d11be 5e21ccb1

+33 -44
+4 -31
fs/proc/base.c
··· 346 346 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 347 347 security_ptrace(current,task) == 0)) 348 348 349 - static int may_ptrace_attach(struct task_struct *task) 350 - { 351 - int retval = 0; 352 - 353 - task_lock(task); 354 - 355 - if (!task->mm) 356 - goto out; 357 - if (((current->uid != task->euid) || 358 - (current->uid != task->suid) || 359 - (current->uid != task->uid) || 360 - (current->gid != task->egid) || 361 - (current->gid != task->sgid) || 362 - (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) 363 - goto out; 364 - rmb(); 365 - if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE)) 366 - goto out; 367 - if (security_ptrace(current, task)) 368 - goto out; 369 - 370 - retval = 1; 371 - out: 372 - task_unlock(task); 373 - return retval; 374 - } 375 - 376 349 static int proc_pid_environ(struct task_struct *task, char * buffer) 377 350 { 378 351 int res = 0; ··· 355 382 if (len > PAGE_SIZE) 356 383 len = PAGE_SIZE; 357 384 res = access_process_vm(task, mm->env_start, buffer, len, 0); 358 - if (!may_ptrace_attach(task)) 385 + if (!ptrace_may_attach(task)) 359 386 res = -ESRCH; 360 387 mmput(mm); 361 388 } ··· 658 685 int ret = -ESRCH; 659 686 struct mm_struct *mm; 660 687 661 - if (!MAY_PTRACE(task) || !may_ptrace_attach(task)) 688 + if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 662 689 goto out; 663 690 664 691 ret = -ENOMEM; ··· 684 711 685 712 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 686 713 retval = access_process_vm(task, src, page, this_len, 0); 687 - if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) { 714 + if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 688 715 if (!ret) 689 716 ret = -EIO; 690 717 break; ··· 722 749 struct task_struct *task = proc_task(file->f_dentry->d_inode); 723 750 unsigned long dst = *ppos; 724 751 725 - if (!MAY_PTRACE(task) || !may_ptrace_attach(task)) 752 + if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 726 753 return -ESRCH; 727 754 728 755 page = (char *)__get_free_page(GFP_USER);
+1
include/linux/ptrace.h
··· 90 90 struct task_struct *new_parent); 91 91 extern void __ptrace_unlink(struct task_struct *child); 92 92 extern void ptrace_untrace(struct task_struct *child); 93 + extern int ptrace_may_attach(struct task_struct *task); 93 94 94 95 static inline void ptrace_link(struct task_struct *child, 95 96 struct task_struct *new_parent)
+28 -13
kernel/ptrace.c
··· 118 118 return ret; 119 119 } 120 120 121 + static int may_attach(struct task_struct *task) 122 + { 123 + if (!task->mm) 124 + return -EPERM; 125 + if (((current->uid != task->euid) || 126 + (current->uid != task->suid) || 127 + (current->uid != task->uid) || 128 + (current->gid != task->egid) || 129 + (current->gid != task->sgid) || 130 + (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) 131 + return -EPERM; 132 + smp_rmb(); 133 + if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE)) 134 + return -EPERM; 135 + 136 + return security_ptrace(current, task); 137 + } 138 + 139 + int ptrace_may_attach(struct task_struct *task) 140 + { 141 + int err; 142 + task_lock(task); 143 + err = may_attach(task); 144 + task_unlock(task); 145 + return !err; 146 + } 147 + 121 148 int ptrace_attach(struct task_struct *task) 122 149 { 123 150 int retval; ··· 154 127 goto bad; 155 128 if (task == current) 156 129 goto bad; 157 - if (!task->mm) 158 - goto bad; 159 - if(((current->uid != task->euid) || 160 - (current->uid != task->suid) || 161 - (current->uid != task->uid) || 162 - (current->gid != task->egid) || 163 - (current->gid != task->sgid) || 164 - (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) 165 - goto bad; 166 - smp_rmb(); 167 - if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE)) 168 - goto bad; 169 130 /* the same process cannot be attached many times */ 170 131 if (task->ptrace & PT_PTRACED) 171 132 goto bad; 172 - retval = security_ptrace(current, task); 133 + retval = may_attach(task); 173 134 if (retval) 174 135 goto bad; 175 136