Skip to content

Commit 006ebb4

Browse files
stephensmalleyxjamesmorris
authored andcommitted
Security: split proc ptrace checking into read vs. attach
Enable security modules to distinguish reading of process state via proc from full ptrace access by renaming ptrace_may_attach to ptrace_may_access and adding a mode argument indicating whether only read access or full attach access is requested. This allows security modules to permit access to reading process state without granting full ptrace access. The base DAC/capability checking remains unchanged. Read access to /proc/pid/mem continues to apply a full ptrace attach check since check_mem_permission() already requires the current task to already be ptracing the target. The other ptrace checks within proc for elements like environ, maps, and fds are changed to pass the read mode instead of attach. In the SELinux case, we model such reading of process state as a reading of a proc file labeled with the target process' label. This enables SELinux policy to permit such reading of process state without permitting control or manipulation of the target process, as there are a number of cases where programs probe for such information via proc but do not need to be able to control the target (e.g. procps, lsof, PolicyKit, ConsoleKit). At present we have to choose between allowing full ptrace in policy (more permissive than required/desired) or breaking functionality (or in some cases just silencing the denials via dontaudit rules but this can hide genuine attacks). This version of the patch incorporates comments from Casey Schaufler (change/replace existing ptrace_may_attach interface, pass access mode), and Chris Wright (provide greater consistency in the checking). Note that like their predecessors __ptrace_may_attach and ptrace_may_attach, the __ptrace_may_access and ptrace_may_access interfaces use different return value conventions from each other (0 or -errno vs. 1 or 0). I retained this difference to avoid any changes to the caller logic but made the difference clearer by changing the latter interface to return a bool rather than an int and by adding a comment about it to ptrace.h for any future callers. Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Acked-by: Chris Wright <chrisw@sous-sol.org> Signed-off-by: James Morris <jmorris@namei.org>
1 parent feb2a5b commit 006ebb4

11 files changed

Lines changed: 55 additions & 30 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static int check_mem_permission(struct task_struct *task)
233233
*/
234234
if (task->parent == current && (task->ptrace & PT_PTRACED) &&
235235
task_is_stopped_or_traced(task) &&
236-
ptrace_may_attach(task))
236+
ptrace_may_access(task, PTRACE_MODE_ATTACH))
237237
return 0;
238238

239239
/*
@@ -251,7 +251,8 @@ struct mm_struct *mm_for_maps(struct task_struct *task)
251251
task_lock(task);
252252
if (task->mm != mm)
253253
goto out;
254-
if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
254+
if (task->mm != current->mm &&
255+
__ptrace_may_access(task, PTRACE_MODE_READ) < 0)
255256
goto out;
256257
task_unlock(task);
257258
return mm;
@@ -518,7 +519,7 @@ static int proc_fd_access_allowed(struct inode *inode)
518519
*/
519520
task = get_proc_task(inode);
520521
if (task) {
521-
allowed = ptrace_may_attach(task);
522+
allowed = ptrace_may_access(task, PTRACE_MODE_READ);
522523
put_task_struct(task);
523524
}
524525
return allowed;
@@ -904,7 +905,7 @@ static ssize_t environ_read(struct file *file, char __user *buf,
904905
if (!task)
905906
goto out_no_task;
906907

907-
if (!ptrace_may_attach(task))
908+
if (!ptrace_may_access(task, PTRACE_MODE_READ))
908909
goto out;
909910

910911
ret = -ENOMEM;

‎fs/proc/task_mmu.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int show_map(struct seq_file *m, void *v)
210210
dev_t dev = 0;
211211
int len;
212212

213-
if (maps_protect && !ptrace_may_attach(task))
213+
if (maps_protect && !ptrace_may_access(task, PTRACE_MODE_READ))
214214
return -EACCES;
215215

216216
if (file) {
@@ -646,7 +646,7 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
646646
goto out;
647647

648648
ret = -EACCES;
649-
if (!ptrace_may_attach(task))
649+
if (!ptrace_may_access(task, PTRACE_MODE_READ))
650650
goto out_task;
651651

652652
ret = -EINVAL;
@@ -747,7 +747,7 @@ static int show_numa_map_checked(struct seq_file *m, void *v)
747747
struct proc_maps_private *priv = m->private;
748748
struct task_struct *task = priv->task;
749749

750-
if (maps_protect && !ptrace_may_attach(task))
750+
if (maps_protect && !ptrace_may_access(task, PTRACE_MODE_READ))
751751
return -EACCES;
752752

753753
return show_numa_map(m, v);

‎fs/proc/task_nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static int show_map(struct seq_file *m, void *_vml)
113113
struct proc_maps_private *priv = m->private;
114114
struct task_struct *task = priv->task;
115115

116-
if (maps_protect && !ptrace_may_attach(task))
116+
if (maps_protect && !ptrace_may_access(task, PTRACE_MODE_READ))
117117
return -EACCES;
118118

119119
return nommu_vma_show(m, vml->vma);

‎include/linux/ptrace.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ extern void __ptrace_link(struct task_struct *child,
9595
struct task_struct *new_parent);
9696
extern void __ptrace_unlink(struct task_struct *child);
9797
extern void ptrace_untrace(struct task_struct *child);
98-
extern int ptrace_may_attach(struct task_struct *task);
99-
extern int __ptrace_may_attach(struct task_struct *task);
98+
#define PTRACE_MODE_READ 1
99+
#define PTRACE_MODE_ATTACH 2
100+
/* Returns 0 on success, -errno on denial. */
101+
extern int __ptrace_may_access(struct task_struct *task, unsigned int mode);
102+
/* Returns true on success, false on denial. */
103+
extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
100104

101105
static inline int ptrace_reparented(struct task_struct *child)
102106
{

‎include/linux/security.h‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct audit_krule;
4646
*/
4747
extern int cap_capable(struct task_struct *tsk, int cap);
4848
extern int cap_settime(struct timespec *ts, struct timezone *tz);
49-
extern int cap_ptrace(struct task_struct *parent, struct task_struct *child);
49+
extern int cap_ptrace(struct task_struct *parent, struct task_struct *child,
50+
unsigned int mode);
5051
extern int cap_capget(struct task_struct *target, kernel_cap_t *effective, kernel_cap_t *inheritable, kernel_cap_t *permitted);
5152
extern int cap_capset_check(struct task_struct *target, kernel_cap_t *effective, kernel_cap_t *inheritable, kernel_cap_t *permitted);
5253
extern void cap_capset_set(struct task_struct *target, kernel_cap_t *effective, kernel_cap_t *inheritable, kernel_cap_t *permitted);
@@ -1170,6 +1171,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
11701171
* attributes would be changed by the execve.
11711172
* @parent contains the task_struct structure for parent process.
11721173
* @child contains the task_struct structure for child process.
1174+
* @mode contains the PTRACE_MODE flags indicating the form of access.
11731175
* Return 0 if permission is granted.
11741176
* @capget:
11751177
* Get the @effective, @inheritable, and @permitted capability sets for
@@ -1295,7 +1297,8 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
12951297
struct security_operations {
12961298
char name[SECURITY_NAME_MAX + 1];
12971299

1298-
int (*ptrace) (struct task_struct *parent, struct task_struct *child);
1300+
int (*ptrace) (struct task_struct *parent, struct task_struct *child,
1301+
unsigned int mode);
12991302
int (*capget) (struct task_struct *target,
13001303
kernel_cap_t *effective,
13011304
kernel_cap_t *inheritable, kernel_cap_t *permitted);
@@ -1573,7 +1576,8 @@ extern struct dentry *securityfs_create_dir(const char *name, struct dentry *par
15731576
extern void securityfs_remove(struct dentry *dentry);
15741577

15751578
/* Security operations */
1576-
int security_ptrace(struct task_struct *parent, struct task_struct *child);
1579+
int security_ptrace(struct task_struct *parent, struct task_struct *child,
1580+
unsigned int mode);
15771581
int security_capget(struct task_struct *target,
15781582
kernel_cap_t *effective,
15791583
kernel_cap_t *inheritable,
@@ -1755,9 +1759,11 @@ static inline int security_init(void)
17551759
return 0;
17561760
}
17571761

1758-
static inline int security_ptrace(struct task_struct *parent, struct task_struct *child)
1762+
static inline int security_ptrace(struct task_struct *parent,
1763+
struct task_struct *child,
1764+
unsigned int mode)
17591765
{
1760-
return cap_ptrace(parent, child);
1766+
return cap_ptrace(parent, child, mode);
17611767
}
17621768

17631769
static inline int security_capget(struct task_struct *target,

‎kernel/ptrace.c‎

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

124-
int __ptrace_may_attach(struct task_struct *task)
124+
int __ptrace_may_access(struct task_struct *task, unsigned int mode)
125125
{
126126
/* May we inspect the given task?
127127
* This check is used both for attaching with ptrace
@@ -148,16 +148,16 @@ int __ptrace_may_attach(struct task_struct *task)
148148
if (!dumpable && !capable(CAP_SYS_PTRACE))
149149
return -EPERM;
150150

151-
return security_ptrace(current, task);
151+
return security_ptrace(current, task, mode);
152152
}
153153

154-
int ptrace_may_attach(struct task_struct *task)
154+
bool ptrace_may_access(struct task_struct *task, unsigned int mode)
155155
{
156156
int err;
157157
task_lock(task);
158-
err = __ptrace_may_attach(task);
158+
err = __ptrace_may_access(task, mode);
159159
task_unlock(task);
160-
return !err;
160+
return (!err ? true : false);
161161
}
162162

163163
int ptrace_attach(struct task_struct *task)
@@ -195,7 +195,7 @@ int ptrace_attach(struct task_struct *task)
195195
/* the same process cannot be attached many times */
196196
if (task->ptrace & PT_PTRACED)
197197
goto bad;
198-
retval = __ptrace_may_attach(task);
198+
retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
199199
if (retval)
200200
goto bad;
201201

@@ -494,7 +494,8 @@ int ptrace_traceme(void)
494494
*/
495495
task_lock(current);
496496
if (!(current->ptrace & PT_PTRACED)) {
497-
ret = security_ptrace(current->parent, current);
497+
ret = security_ptrace(current->parent, current,
498+
PTRACE_MODE_ATTACH);
498499
/*
499500
* Set the ptrace bit in the process ptrace flags.
500501
*/

‎security/commoncap.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ int cap_settime(struct timespec *ts, struct timezone *tz)
6363
return 0;
6464
}
6565

66-
int cap_ptrace (struct task_struct *parent, struct task_struct *child)
66+
int cap_ptrace (struct task_struct *parent, struct task_struct *child,
67+
unsigned int mode)
6768
{
6869
/* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
6970
if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&

‎security/dummy.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#include <linux/prctl.h>
3131
#include <linux/securebits.h>
3232

33-
static int dummy_ptrace (struct task_struct *parent, struct task_struct *child)
33+
static int dummy_ptrace (struct task_struct *parent, struct task_struct *child,
34+
unsigned int mode)
3435
{
3536
return 0;
3637
}

‎security/security.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ int mod_reg_security(const char *name, struct security_operations *ops)
161161

162162
/* Security operations */
163163

164-
int security_ptrace(struct task_struct *parent, struct task_struct *child)
164+
int security_ptrace(struct task_struct *parent, struct task_struct *child,
165+
unsigned int mode)
165166
{
166-
return security_ops->ptrace(parent, child);
167+
return security_ops->ptrace(parent, child, mode);
167168
}
168169

169170
int security_capget(struct task_struct *target,

‎security/selinux/hooks.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,14 +1686,23 @@ static inline u32 file_to_av(struct file *file)
16861686

16871687
/* Hook functions begin here. */
16881688

1689-
static int selinux_ptrace(struct task_struct *parent, struct task_struct *child)
1689+
static int selinux_ptrace(struct task_struct *parent,
1690+
struct task_struct *child,
1691+
unsigned int mode)
16901692
{
16911693
int rc;
16921694

1693-
rc = secondary_ops->ptrace(parent, child);
1695+
rc = secondary_ops->ptrace(parent, child, mode);
16941696
if (rc)
16951697
return rc;
16961698

1699+
if (mode == PTRACE_MODE_READ) {
1700+
struct task_security_struct *tsec = parent->security;
1701+
struct task_security_struct *csec = child->security;
1702+
return avc_has_perm(tsec->sid, csec->sid,
1703+
SECCLASS_FILE, FILE__READ, NULL);
1704+
}
1705+
16971706
return task_has_perm(parent, child, PROCESS__PTRACE);
16981707
}
16991708

0 commit comments

Comments
 (0)