aboutsummaryrefslogtreecommitdiffstats
path: root/security/selinux/hooks.c
diff options
context:
space:
mode:
authorStephen Smalley <sds@tycho.nsa.gov>2017-01-09 10:07:31 -0500
committerPaul Moore <paul@paul-moore.com>2017-01-09 10:07:31 -0500
commitbe0554c9bf9f7cc96f5205df8f8bd3573b74320e (patch)
treee7ab18363d24006d50a67d13d7b72d61efd40537 /security/selinux/hooks.c
parent01593d3299a1cfdb5e08acf95f63ec59dd674906 (diff)
selinux: clean up cred usage and simplify
SELinux was sometimes using the task "objective" credentials when it could/should use the "subjective" credentials. This was sometimes hidden by the fact that we were unnecessarily passing around pointers to the current task, making it appear as if the task could be something other than current, so eliminate all such passing of current. Inline various permission checking helper functions that can be reduced to a single avc_has_perm() call. Since the credentials infrastructure only allows a task to alter its own credentials, we can always assume that current must be the same as the target task in selinux_setprocattr after the check. We likely should move this check from selinux_setprocattr() to proc_pid_attr_write() and drop the task argument to the security hook altogether; it can only serve to confuse things. Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Paul Moore <paul@paul-moore.com>
Diffstat (limited to 'security/selinux/hooks.c')
-rw-r--r--security/selinux/hooks.c294
1 files changed, 117 insertions, 177 deletions
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index e32f4b5f23a5..262e108c36d4 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -210,16 +210,6 @@ static inline u32 task_sid(const struct task_struct *task)
210 return sid; 210 return sid;
211} 211}
212 212
213/*
214 * get the subjective security ID of the current task
215 */
216static inline u32 current_sid(void)
217{
218 const struct task_security_struct *tsec = current_security();
219
220 return tsec->sid;
221}
222
223/* Allocate and free functions for each kind of security blob. */ 213/* Allocate and free functions for each kind of security blob. */
224 214
225static int inode_alloc_security(struct inode *inode) 215static int inode_alloc_security(struct inode *inode)
@@ -1687,55 +1677,6 @@ static inline u32 signal_to_av(int sig)
1687 return perm; 1677 return perm;
1688} 1678}
1689 1679
1690/*
1691 * Check permission between a pair of credentials
1692 * fork check, ptrace check, etc.
1693 */
1694static int cred_has_perm(const struct cred *actor,
1695 const struct cred *target,
1696 u32 perms)
1697{
1698 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1699
1700 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1701}
1702
1703/*
1704 * Check permission between a pair of tasks, e.g. signal checks,
1705 * fork check, ptrace check, etc.
1706 * tsk1 is the actor and tsk2 is the target
1707 * - this uses the default subjective creds of tsk1
1708 */
1709static int task_has_perm(const struct task_struct *tsk1,
1710 const struct task_struct *tsk2,
1711 u32 perms)
1712{
1713 const struct task_security_struct *__tsec1, *__tsec2;
1714 u32 sid1, sid2;
1715
1716 rcu_read_lock();
1717 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1718 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1719 rcu_read_unlock();
1720 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
1721}
1722
1723/*
1724 * Check permission between current and another task, e.g. signal checks,
1725 * fork check, ptrace check, etc.
1726 * current is the actor and tsk2 is the target
1727 * - this uses current's subjective creds
1728 */
1729static int current_has_perm(const struct task_struct *tsk,
1730 u32 perms)
1731{
1732 u32 sid, tsid;
1733
1734 sid = current_sid();
1735 tsid = task_sid(tsk);
1736 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1737}
1738
1739#if CAP_LAST_CAP > 63 1680#if CAP_LAST_CAP > 63
1740#error Fix SELinux to handle capabilities > 63. 1681#error Fix SELinux to handle capabilities > 63.
1741#endif 1682#endif
@@ -1777,16 +1718,6 @@ static int cred_has_capability(const struct cred *cred,
1777 return rc; 1718 return rc;
1778} 1719}
1779 1720
1780/* Check whether a task is allowed to use a system operation. */
1781static int task_has_system(struct task_struct *tsk,
1782 u32 perms)
1783{
1784 u32 sid = task_sid(tsk);
1785
1786 return avc_has_perm(sid, SECINITSID_KERNEL,
1787 SECCLASS_SYSTEM, perms, NULL);
1788}
1789
1790/* Check whether a task has a particular permission to an inode. 1721/* Check whether a task has a particular permission to an inode.
1791 The 'adp' parameter is optional and allows other audit 1722 The 'adp' parameter is optional and allows other audit
1792 data to be passed (e.g. the dentry). */ 1723 data to be passed (e.g. the dentry). */
@@ -1958,15 +1889,6 @@ static int may_create(struct inode *dir,
1958 FILESYSTEM__ASSOCIATE, &ad); 1889 FILESYSTEM__ASSOCIATE, &ad);
1959} 1890}
1960 1891
1961/* Check whether a task can create a key. */
1962static int may_create_key(u32 ksid,
1963 struct task_struct *ctx)
1964{
1965 u32 sid = task_sid(ctx);
1966
1967 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
1968}
1969
1970#define MAY_LINK 0 1892#define MAY_LINK 0
1971#define MAY_UNLINK 1 1893#define MAY_UNLINK 1
1972#define MAY_RMDIR 2 1894#define MAY_RMDIR 2
@@ -2222,24 +2144,26 @@ static int selinux_binder_transfer_file(struct task_struct *from,
2222static int selinux_ptrace_access_check(struct task_struct *child, 2144static int selinux_ptrace_access_check(struct task_struct *child,
2223 unsigned int mode) 2145 unsigned int mode)
2224{ 2146{
2225 if (mode & PTRACE_MODE_READ) { 2147 u32 sid = current_sid();
2226 u32 sid = current_sid(); 2148 u32 csid = task_sid(child);
2227 u32 csid = task_sid(child); 2149
2150 if (mode & PTRACE_MODE_READ)
2228 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2151 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2229 }
2230 2152
2231 return current_has_perm(child, PROCESS__PTRACE); 2153 return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2232} 2154}
2233 2155
2234static int selinux_ptrace_traceme(struct task_struct *parent) 2156static int selinux_ptrace_traceme(struct task_struct *parent)
2235{ 2157{
2236 return task_has_perm(parent, current, PROCESS__PTRACE); 2158 return avc_has_perm(task_sid(parent), current_sid(), SECCLASS_PROCESS,
2159 PROCESS__PTRACE, NULL);
2237} 2160}
2238 2161
2239static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 2162static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2240 kernel_cap_t *inheritable, kernel_cap_t *permitted) 2163 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2241{ 2164{
2242 return current_has_perm(target, PROCESS__GETCAP); 2165 return avc_has_perm(current_sid(), task_sid(target), SECCLASS_PROCESS,
2166 PROCESS__GETCAP, NULL);
2243} 2167}
2244 2168
2245static int selinux_capset(struct cred *new, const struct cred *old, 2169static int selinux_capset(struct cred *new, const struct cred *old,
@@ -2247,7 +2171,8 @@ static int selinux_capset(struct cred *new, const struct cred *old,
2247 const kernel_cap_t *inheritable, 2171 const kernel_cap_t *inheritable,
2248 const kernel_cap_t *permitted) 2172 const kernel_cap_t *permitted)
2249{ 2173{
2250 return cred_has_perm(old, new, PROCESS__SETCAP); 2174 return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2175 PROCESS__SETCAP, NULL);
2251} 2176}
2252 2177
2253/* 2178/*
@@ -2303,29 +2228,22 @@ static int selinux_quota_on(struct dentry *dentry)
2303 2228
2304static int selinux_syslog(int type) 2229static int selinux_syslog(int type)
2305{ 2230{
2306 int rc;
2307
2308 switch (type) { 2231 switch (type) {
2309 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2232 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2310 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 2233 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2311 rc = task_has_system(current, SYSTEM__SYSLOG_READ); 2234 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2312 break; 2235 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2313 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2236 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2314 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2237 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2315 /* Set level of messages printed to console */ 2238 /* Set level of messages printed to console */
2316 case SYSLOG_ACTION_CONSOLE_LEVEL: 2239 case SYSLOG_ACTION_CONSOLE_LEVEL:
2317 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 2240 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2318 break; 2241 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2319 case SYSLOG_ACTION_CLOSE: /* Close log */ 2242 NULL);
2320 case SYSLOG_ACTION_OPEN: /* Open log */
2321 case SYSLOG_ACTION_READ: /* Read from log */
2322 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2323 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
2324 default:
2325 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2326 break;
2327 } 2243 }
2328 return rc; 2244 /* All other syslog types */
2245 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
2246 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2329} 2247}
2330 2248
2331/* 2249/*
@@ -2350,13 +2268,13 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2350 2268
2351/* binprm security operations */ 2269/* binprm security operations */
2352 2270
2353static u32 ptrace_parent_sid(struct task_struct *task) 2271static u32 ptrace_parent_sid(void)
2354{ 2272{
2355 u32 sid = 0; 2273 u32 sid = 0;
2356 struct task_struct *tracer; 2274 struct task_struct *tracer;
2357 2275
2358 rcu_read_lock(); 2276 rcu_read_lock();
2359 tracer = ptrace_parent(task); 2277 tracer = ptrace_parent(current);
2360 if (tracer) 2278 if (tracer)
2361 sid = task_sid(tracer); 2279 sid = task_sid(tracer);
2362 rcu_read_unlock(); 2280 rcu_read_unlock();
@@ -2485,7 +2403,7 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2485 * changes its SID has the appropriate permit */ 2403 * changes its SID has the appropriate permit */
2486 if (bprm->unsafe & 2404 if (bprm->unsafe &
2487 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 2405 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2488 u32 ptsid = ptrace_parent_sid(current); 2406 u32 ptsid = ptrace_parent_sid();
2489 if (ptsid != 0) { 2407 if (ptsid != 0) {
2490 rc = avc_has_perm(ptsid, new_tsec->sid, 2408 rc = avc_has_perm(ptsid, new_tsec->sid,
2491 SECCLASS_PROCESS, 2409 SECCLASS_PROCESS,
@@ -3582,6 +3500,7 @@ static int default_noexec;
3582static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 3500static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3583{ 3501{
3584 const struct cred *cred = current_cred(); 3502 const struct cred *cred = current_cred();
3503 u32 sid = cred_sid(cred);
3585 int rc = 0; 3504 int rc = 0;
3586 3505
3587 if (default_noexec && 3506 if (default_noexec &&
@@ -3592,7 +3511,8 @@ static int file_map_prot_check(struct file *file, unsigned long prot, int shared
3592 * private file mapping that will also be writable. 3511 * private file mapping that will also be writable.
3593 * This has an additional check. 3512 * This has an additional check.
3594 */ 3513 */
3595 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); 3514 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
3515 PROCESS__EXECMEM, NULL);
3596 if (rc) 3516 if (rc)
3597 goto error; 3517 goto error;
3598 } 3518 }
@@ -3643,6 +3563,7 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
3643 unsigned long prot) 3563 unsigned long prot)
3644{ 3564{
3645 const struct cred *cred = current_cred(); 3565 const struct cred *cred = current_cred();
3566 u32 sid = cred_sid(cred);
3646 3567
3647 if (selinux_checkreqprot) 3568 if (selinux_checkreqprot)
3648 prot = reqprot; 3569 prot = reqprot;
@@ -3652,12 +3573,14 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
3652 int rc = 0; 3573 int rc = 0;
3653 if (vma->vm_start >= vma->vm_mm->start_brk && 3574 if (vma->vm_start >= vma->vm_mm->start_brk &&
3654 vma->vm_end <= vma->vm_mm->brk) { 3575 vma->vm_end <= vma->vm_mm->brk) {
3655 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); 3576 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
3577 PROCESS__EXECHEAP, NULL);
3656 } else if (!vma->vm_file && 3578 } else if (!vma->vm_file &&
3657 ((vma->vm_start <= vma->vm_mm->start_stack && 3579 ((vma->vm_start <= vma->vm_mm->start_stack &&
3658 vma->vm_end >= vma->vm_mm->start_stack) || 3580 vma->vm_end >= vma->vm_mm->start_stack) ||
3659 vma_is_stack_for_current(vma))) { 3581 vma_is_stack_for_current(vma))) {
3660 rc = current_has_perm(current, PROCESS__EXECSTACK); 3582 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
3583 PROCESS__EXECSTACK, NULL);
3661 } else if (vma->vm_file && vma->anon_vma) { 3584 } else if (vma->vm_file && vma->anon_vma) {
3662 /* 3585 /*
3663 * We are making executable a file mapping that has 3586 * We are making executable a file mapping that has
@@ -3790,7 +3713,9 @@ static int selinux_file_open(struct file *file, const struct cred *cred)
3790 3713
3791static int selinux_task_create(unsigned long clone_flags) 3714static int selinux_task_create(unsigned long clone_flags)
3792{ 3715{
3793 return current_has_perm(current, PROCESS__FORK); 3716 u32 sid = current_sid();
3717
3718 return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3794} 3719}
3795 3720
3796/* 3721/*
@@ -3900,15 +3825,12 @@ static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3900 3825
3901static int selinux_kernel_module_request(char *kmod_name) 3826static int selinux_kernel_module_request(char *kmod_name)
3902{ 3827{
3903 u32 sid;
3904 struct common_audit_data ad; 3828 struct common_audit_data ad;
3905 3829
3906 sid = task_sid(current);
3907
3908 ad.type = LSM_AUDIT_DATA_KMOD; 3830 ad.type = LSM_AUDIT_DATA_KMOD;
3909 ad.u.kmod_name = kmod_name; 3831 ad.u.kmod_name = kmod_name;
3910 3832
3911 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, 3833 return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3912 SYSTEM__MODULE_REQUEST, &ad); 3834 SYSTEM__MODULE_REQUEST, &ad);
3913} 3835}
3914 3836
@@ -3960,17 +3882,20 @@ static int selinux_kernel_read_file(struct file *file,
3960 3882
3961static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 3883static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3962{ 3884{
3963 return current_has_perm(p, PROCESS__SETPGID); 3885 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3886 PROCESS__SETPGID, NULL);
3964} 3887}
3965 3888
3966static int selinux_task_getpgid(struct task_struct *p) 3889static int selinux_task_getpgid(struct task_struct *p)
3967{ 3890{
3968 return current_has_perm(p, PROCESS__GETPGID); 3891 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3892 PROCESS__GETPGID, NULL);
3969} 3893}
3970 3894
3971static int selinux_task_getsid(struct task_struct *p) 3895static int selinux_task_getsid(struct task_struct *p)
3972{ 3896{
3973 return current_has_perm(p, PROCESS__GETSESSION); 3897 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3898 PROCESS__GETSESSION, NULL);
3974} 3899}
3975 3900
3976static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3901static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
@@ -3980,17 +3905,20 @@ static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3980 3905
3981static int selinux_task_setnice(struct task_struct *p, int nice) 3906static int selinux_task_setnice(struct task_struct *p, int nice)
3982{ 3907{
3983 return current_has_perm(p, PROCESS__SETSCHED); 3908 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3909 PROCESS__SETSCHED, NULL);
3984} 3910}
3985 3911
3986static int selinux_task_setioprio(struct task_struct *p, int ioprio) 3912static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3987{ 3913{
3988 return current_has_perm(p, PROCESS__SETSCHED); 3914 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3915 PROCESS__SETSCHED, NULL);
3989} 3916}
3990 3917
3991static int selinux_task_getioprio(struct task_struct *p) 3918static int selinux_task_getioprio(struct task_struct *p)
3992{ 3919{
3993 return current_has_perm(p, PROCESS__GETSCHED); 3920 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3921 PROCESS__GETSCHED, NULL);
3994} 3922}
3995 3923
3996static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 3924static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
@@ -4003,47 +3931,48 @@ static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4003 later be used as a safe reset point for the soft limit 3931 later be used as a safe reset point for the soft limit
4004 upon context transitions. See selinux_bprm_committing_creds. */ 3932 upon context transitions. See selinux_bprm_committing_creds. */
4005 if (old_rlim->rlim_max != new_rlim->rlim_max) 3933 if (old_rlim->rlim_max != new_rlim->rlim_max)
4006 return current_has_perm(p, PROCESS__SETRLIMIT); 3934 return avc_has_perm(current_sid(), task_sid(p),
3935 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
4007 3936
4008 return 0; 3937 return 0;
4009} 3938}
4010 3939
4011static int selinux_task_setscheduler(struct task_struct *p) 3940static int selinux_task_setscheduler(struct task_struct *p)
4012{ 3941{
4013 return current_has_perm(p, PROCESS__SETSCHED); 3942 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3943 PROCESS__SETSCHED, NULL);
4014} 3944}
4015 3945
4016static int selinux_task_getscheduler(struct task_struct *p) 3946static int selinux_task_getscheduler(struct task_struct *p)
4017{ 3947{
4018 return current_has_perm(p, PROCESS__GETSCHED); 3948 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3949 PROCESS__GETSCHED, NULL);
4019} 3950}
4020 3951
4021static int selinux_task_movememory(struct task_struct *p) 3952static int selinux_task_movememory(struct task_struct *p)
4022{ 3953{
4023 return current_has_perm(p, PROCESS__SETSCHED); 3954 return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS,
3955 PROCESS__SETSCHED, NULL);
4024} 3956}
4025 3957
4026static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3958static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
4027 int sig, u32 secid) 3959 int sig, u32 secid)
4028{ 3960{
4029 u32 perm; 3961 u32 perm;
4030 int rc;
4031 3962
4032 if (!sig) 3963 if (!sig)
4033 perm = PROCESS__SIGNULL; /* null signal; existence test */ 3964 perm = PROCESS__SIGNULL; /* null signal; existence test */
4034 else 3965 else
4035 perm = signal_to_av(sig); 3966 perm = signal_to_av(sig);
4036 if (secid) 3967 if (!secid)
4037 rc = avc_has_perm(secid, task_sid(p), 3968 secid = current_sid();
4038 SECCLASS_PROCESS, perm, NULL); 3969 return avc_has_perm(secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
4039 else
4040 rc = current_has_perm(p, perm);
4041 return rc;
4042} 3970}
4043 3971
4044static int selinux_task_wait(struct task_struct *p) 3972static int selinux_task_wait(struct task_struct *p)
4045{ 3973{
4046 return task_has_perm(p, current, PROCESS__SIGCHLD); 3974 return avc_has_perm(task_sid(p), current_sid(), SECCLASS_PROCESS,
3975 PROCESS__SIGCHLD, NULL);
4047} 3976}
4048 3977
4049static void selinux_task_to_inode(struct task_struct *p, 3978static void selinux_task_to_inode(struct task_struct *p,
@@ -4333,12 +4262,11 @@ static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4333 socksid); 4262 socksid);
4334} 4263}
4335 4264
4336static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms) 4265static int sock_has_perm(struct sock *sk, u32 perms)
4337{ 4266{
4338 struct sk_security_struct *sksec = sk->sk_security; 4267 struct sk_security_struct *sksec = sk->sk_security;
4339 struct common_audit_data ad; 4268 struct common_audit_data ad;
4340 struct lsm_network_audit net = {0,}; 4269 struct lsm_network_audit net = {0,};
4341 u32 tsid = task_sid(task);
4342 4270
4343 if (sksec->sid == SECINITSID_KERNEL) 4271 if (sksec->sid == SECINITSID_KERNEL)
4344 return 0; 4272 return 0;
@@ -4347,7 +4275,8 @@ static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
4347 ad.u.net = &net; 4275 ad.u.net = &net;
4348 ad.u.net->sk = sk; 4276 ad.u.net->sk = sk;
4349 4277
4350 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad); 4278 return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms,
4279 &ad);
4351} 4280}
4352 4281
4353static int selinux_socket_create(int family, int type, 4282static int selinux_socket_create(int family, int type,
@@ -4409,7 +4338,7 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in
4409 u16 family; 4338 u16 family;
4410 int err; 4339 int err;
4411 4340
4412 err = sock_has_perm(current, sk, SOCKET__BIND); 4341 err = sock_has_perm(sk, SOCKET__BIND);
4413 if (err) 4342 if (err)
4414 goto out; 4343 goto out;
4415 4344
@@ -4508,7 +4437,7 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
4508 struct sk_security_struct *sksec = sk->sk_security; 4437 struct sk_security_struct *sksec = sk->sk_security;
4509 int err; 4438 int err;
4510 4439
4511 err = sock_has_perm(current, sk, SOCKET__CONNECT); 4440 err = sock_has_perm(sk, SOCKET__CONNECT);
4512 if (err) 4441 if (err)
4513 return err; 4442 return err;
4514 4443
@@ -4560,7 +4489,7 @@ out:
4560 4489
4561static int selinux_socket_listen(struct socket *sock, int backlog) 4490static int selinux_socket_listen(struct socket *sock, int backlog)
4562{ 4491{
4563 return sock_has_perm(current, sock->sk, SOCKET__LISTEN); 4492 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4564} 4493}
4565 4494
4566static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 4495static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
@@ -4571,7 +4500,7 @@ static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4571 u16 sclass; 4500 u16 sclass;
4572 u32 sid; 4501 u32 sid;
4573 4502
4574 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT); 4503 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4575 if (err) 4504 if (err)
4576 return err; 4505 return err;
4577 4506
@@ -4592,30 +4521,30 @@ static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4592static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 4521static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4593 int size) 4522 int size)
4594{ 4523{
4595 return sock_has_perm(current, sock->sk, SOCKET__WRITE); 4524 return sock_has_perm(sock->sk, SOCKET__WRITE);
4596} 4525}
4597 4526
4598static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 4527static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4599 int size, int flags) 4528 int size, int flags)
4600{ 4529{
4601 return sock_has_perm(current, sock->sk, SOCKET__READ); 4530 return sock_has_perm(sock->sk, SOCKET__READ);
4602} 4531}
4603 4532
4604static int selinux_socket_getsockname(struct socket *sock) 4533static int selinux_socket_getsockname(struct socket *sock)
4605{ 4534{
4606 return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 4535 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4607} 4536}
4608 4537
4609static int selinux_socket_getpeername(struct socket *sock) 4538static int selinux_socket_getpeername(struct socket *sock)
4610{ 4539{
4611 return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 4540 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4612} 4541}
4613 4542
4614static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 4543static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4615{ 4544{
4616 int err; 4545 int err;
4617 4546
4618 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT); 4547 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4619 if (err) 4548 if (err)
4620 return err; 4549 return err;
4621 4550
@@ -4625,12 +4554,12 @@ static int selinux_socket_setsockopt(struct socket *sock, int level, int optname
4625static int selinux_socket_getsockopt(struct socket *sock, int level, 4554static int selinux_socket_getsockopt(struct socket *sock, int level,
4626 int optname) 4555 int optname)
4627{ 4556{
4628 return sock_has_perm(current, sock->sk, SOCKET__GETOPT); 4557 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4629} 4558}
4630 4559
4631static int selinux_socket_shutdown(struct socket *sock, int how) 4560static int selinux_socket_shutdown(struct socket *sock, int how)
4632{ 4561{
4633 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN); 4562 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4634} 4563}
4635 4564
4636static int selinux_socket_unix_stream_connect(struct sock *sock, 4565static int selinux_socket_unix_stream_connect(struct sock *sock,
@@ -5118,7 +5047,7 @@ static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5118 goto out; 5047 goto out;
5119 } 5048 }
5120 5049
5121 err = sock_has_perm(current, sk, perm); 5050 err = sock_has_perm(sk, perm);
5122out: 5051out:
5123 return err; 5052 return err;
5124} 5053}
@@ -5449,20 +5378,17 @@ static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5449 return selinux_nlmsg_perm(sk, skb); 5378 return selinux_nlmsg_perm(sk, skb);
5450} 5379}
5451 5380
5452static int ipc_alloc_security(struct task_struct *task, 5381static int ipc_alloc_security(struct kern_ipc_perm *perm,
5453 struct kern_ipc_perm *perm,
5454 u16 sclass) 5382 u16 sclass)
5455{ 5383{
5456 struct ipc_security_struct *isec; 5384 struct ipc_security_struct *isec;
5457 u32 sid;
5458 5385
5459 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 5386 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5460 if (!isec) 5387 if (!isec)
5461 return -ENOMEM; 5388 return -ENOMEM;
5462 5389
5463 sid = task_sid(task);
5464 isec->sclass = sclass; 5390 isec->sclass = sclass;
5465 isec->sid = sid; 5391 isec->sid = current_sid();
5466 perm->security = isec; 5392 perm->security = isec;
5467 5393
5468 return 0; 5394 return 0;
@@ -5530,7 +5456,7 @@ static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5530 u32 sid = current_sid(); 5456 u32 sid = current_sid();
5531 int rc; 5457 int rc;
5532 5458
5533 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 5459 rc = ipc_alloc_security(&msq->q_perm, SECCLASS_MSGQ);
5534 if (rc) 5460 if (rc)
5535 return rc; 5461 return rc;
5536 5462
@@ -5577,7 +5503,8 @@ static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5577 case IPC_INFO: 5503 case IPC_INFO:
5578 case MSG_INFO: 5504 case MSG_INFO:
5579 /* No specific object, just general system-wide information. */ 5505 /* No specific object, just general system-wide information. */
5580 return task_has_system(current, SYSTEM__IPC_INFO); 5506 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
5507 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5581 case IPC_STAT: 5508 case IPC_STAT:
5582 case MSG_STAT: 5509 case MSG_STAT:
5583 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 5510 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
@@ -5671,7 +5598,7 @@ static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5671 u32 sid = current_sid(); 5598 u32 sid = current_sid();
5672 int rc; 5599 int rc;
5673 5600
5674 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 5601 rc = ipc_alloc_security(&shp->shm_perm, SECCLASS_SHM);
5675 if (rc) 5602 if (rc)
5676 return rc; 5603 return rc;
5677 5604
@@ -5719,7 +5646,8 @@ static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5719 case IPC_INFO: 5646 case IPC_INFO:
5720 case SHM_INFO: 5647 case SHM_INFO:
5721 /* No specific object, just general system-wide information. */ 5648 /* No specific object, just general system-wide information. */
5722 return task_has_system(current, SYSTEM__IPC_INFO); 5649 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
5650 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5723 case IPC_STAT: 5651 case IPC_STAT:
5724 case SHM_STAT: 5652 case SHM_STAT:
5725 perms = SHM__GETATTR | SHM__ASSOCIATE; 5653 perms = SHM__GETATTR | SHM__ASSOCIATE;
@@ -5763,7 +5691,7 @@ static int selinux_sem_alloc_security(struct sem_array *sma)
5763 u32 sid = current_sid(); 5691 u32 sid = current_sid();
5764 int rc; 5692 int rc;
5765 5693
5766 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 5694 rc = ipc_alloc_security(&sma->sem_perm, SECCLASS_SEM);
5767 if (rc) 5695 if (rc)
5768 return rc; 5696 return rc;
5769 5697
@@ -5811,7 +5739,8 @@ static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5811 case IPC_INFO: 5739 case IPC_INFO:
5812 case SEM_INFO: 5740 case SEM_INFO:
5813 /* No specific object, just general system-wide information. */ 5741 /* No specific object, just general system-wide information. */
5814 return task_has_system(current, SYSTEM__IPC_INFO); 5742 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
5743 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5815 case GETPID: 5744 case GETPID:
5816 case GETNCNT: 5745 case GETNCNT:
5817 case GETZCNT: 5746 case GETZCNT:
@@ -5892,15 +5821,16 @@ static int selinux_getprocattr(struct task_struct *p,
5892 int error; 5821 int error;
5893 unsigned len; 5822 unsigned len;
5894 5823
5824 rcu_read_lock();
5825 __tsec = __task_cred(p)->security;
5826
5895 if (current != p) { 5827 if (current != p) {
5896 error = current_has_perm(p, PROCESS__GETATTR); 5828 error = avc_has_perm(current_sid(), __tsec->sid,
5829 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
5897 if (error) 5830 if (error)
5898 return error; 5831 goto bad;
5899 } 5832 }
5900 5833
5901 rcu_read_lock();
5902 __tsec = __task_cred(p)->security;
5903
5904 if (!strcmp(name, "current")) 5834 if (!strcmp(name, "current"))
5905 sid = __tsec->sid; 5835 sid = __tsec->sid;
5906 else if (!strcmp(name, "prev")) 5836 else if (!strcmp(name, "prev"))
@@ -5913,8 +5843,10 @@ static int selinux_getprocattr(struct task_struct *p,
5913 sid = __tsec->keycreate_sid; 5843 sid = __tsec->keycreate_sid;
5914 else if (!strcmp(name, "sockcreate")) 5844 else if (!strcmp(name, "sockcreate"))
5915 sid = __tsec->sockcreate_sid; 5845 sid = __tsec->sockcreate_sid;
5916 else 5846 else {
5917 goto invalid; 5847 error = -EINVAL;
5848 goto bad;
5849 }
5918 rcu_read_unlock(); 5850 rcu_read_unlock();
5919 5851
5920 if (!sid) 5852 if (!sid)
@@ -5925,9 +5857,9 @@ static int selinux_getprocattr(struct task_struct *p,
5925 return error; 5857 return error;
5926 return len; 5858 return len;
5927 5859
5928invalid: 5860bad:
5929 rcu_read_unlock(); 5861 rcu_read_unlock();
5930 return -EINVAL; 5862 return error;
5931} 5863}
5932 5864
5933static int selinux_setprocattr(struct task_struct *p, 5865static int selinux_setprocattr(struct task_struct *p,
@@ -5935,31 +5867,38 @@ static int selinux_setprocattr(struct task_struct *p,
5935{ 5867{
5936 struct task_security_struct *tsec; 5868 struct task_security_struct *tsec;
5937 struct cred *new; 5869 struct cred *new;
5938 u32 sid = 0, ptsid; 5870 u32 mysid = current_sid(), sid = 0, ptsid;
5939 int error; 5871 int error;
5940 char *str = value; 5872 char *str = value;
5941 5873
5942 if (current != p) { 5874 if (current != p) {
5943 /* SELinux only allows a process to change its own 5875 /*
5944 security attributes. */ 5876 * A task may only alter its own credentials.
5877 * SELinux has always enforced this restriction,
5878 * and it is now mandated by the Linux credentials
5879 * infrastructure; see Documentation/security/credentials.txt.
5880 */
5945 return -EACCES; 5881 return -EACCES;
5946 } 5882 }
5947 5883
5948 /* 5884 /*
5949 * Basic control over ability to set these attributes at all. 5885 * Basic control over ability to set these attributes at all.
5950 * current == p, but we'll pass them separately in case the
5951 * above restriction is ever removed.
5952 */ 5886 */
5953 if (!strcmp(name, "exec")) 5887 if (!strcmp(name, "exec"))
5954 error = current_has_perm(p, PROCESS__SETEXEC); 5888 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
5889 PROCESS__SETEXEC, NULL);
5955 else if (!strcmp(name, "fscreate")) 5890 else if (!strcmp(name, "fscreate"))
5956 error = current_has_perm(p, PROCESS__SETFSCREATE); 5891 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
5892 PROCESS__SETFSCREATE, NULL);
5957 else if (!strcmp(name, "keycreate")) 5893 else if (!strcmp(name, "keycreate"))
5958 error = current_has_perm(p, PROCESS__SETKEYCREATE); 5894 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
5895 PROCESS__SETKEYCREATE, NULL);
5959 else if (!strcmp(name, "sockcreate")) 5896 else if (!strcmp(name, "sockcreate"))
5960 error = current_has_perm(p, PROCESS__SETSOCKCREATE); 5897 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
5898 PROCESS__SETSOCKCREATE, NULL);
5961 else if (!strcmp(name, "current")) 5899 else if (!strcmp(name, "current"))
5962 error = current_has_perm(p, PROCESS__SETCURRENT); 5900 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
5901 PROCESS__SETCURRENT, NULL);
5963 else 5902 else
5964 error = -EINVAL; 5903 error = -EINVAL;
5965 if (error) 5904 if (error)
@@ -6013,7 +5952,8 @@ static int selinux_setprocattr(struct task_struct *p,
6013 } else if (!strcmp(name, "fscreate")) { 5952 } else if (!strcmp(name, "fscreate")) {
6014 tsec->create_sid = sid; 5953 tsec->create_sid = sid;
6015 } else if (!strcmp(name, "keycreate")) { 5954 } else if (!strcmp(name, "keycreate")) {
6016 error = may_create_key(sid, p); 5955 error = avc_has_perm(mysid, sid, SECCLASS_KEY, KEY__CREATE,
5956 NULL);
6017 if (error) 5957 if (error)
6018 goto abort_change; 5958 goto abort_change;
6019 tsec->keycreate_sid = sid; 5959 tsec->keycreate_sid = sid;
@@ -6040,7 +5980,7 @@ static int selinux_setprocattr(struct task_struct *p,
6040 5980
6041 /* Check for ptracing, and update the task SID if ok. 5981 /* Check for ptracing, and update the task SID if ok.
6042 Otherwise, leave SID unchanged and fail. */ 5982 Otherwise, leave SID unchanged and fail. */
6043 ptsid = ptrace_parent_sid(p); 5983 ptsid = ptrace_parent_sid();
6044 if (ptsid != 0) { 5984 if (ptsid != 0) {
6045 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 5985 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
6046 PROCESS__PTRACE, NULL); 5986 PROCESS__PTRACE, NULL);