aboutsummaryrefslogtreecommitdiffstats
path: root/security/selinux/hooks.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/selinux/hooks.c')
-rw-r--r--security/selinux/hooks.c296
1 files changed, 210 insertions, 86 deletions
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 69649783c266..3e3fde7c1d2b 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -292,6 +292,7 @@ static void sk_free_security(struct sock *sk)
292 struct sk_security_struct *ssec = sk->sk_security; 292 struct sk_security_struct *ssec = sk->sk_security;
293 293
294 sk->sk_security = NULL; 294 sk->sk_security = NULL;
295 selinux_netlbl_sk_security_free(ssec);
295 kfree(ssec); 296 kfree(ssec);
296} 297}
297 298
@@ -325,7 +326,7 @@ enum {
325 Opt_rootcontext = 4, 326 Opt_rootcontext = 4,
326}; 327};
327 328
328static match_table_t tokens = { 329static const match_table_t tokens = {
329 {Opt_context, CONTEXT_STR "%s"}, 330 {Opt_context, CONTEXT_STR "%s"},
330 {Opt_fscontext, FSCONTEXT_STR "%s"}, 331 {Opt_fscontext, FSCONTEXT_STR "%s"},
331 {Opt_defcontext, DEFCONTEXT_STR "%s"}, 332 {Opt_defcontext, DEFCONTEXT_STR "%s"},
@@ -958,7 +959,8 @@ out_err:
958 return rc; 959 return rc;
959} 960}
960 961
961void selinux_write_opts(struct seq_file *m, struct security_mnt_opts *opts) 962static void selinux_write_opts(struct seq_file *m,
963 struct security_mnt_opts *opts)
962{ 964{
963 int i; 965 int i;
964 char *prefix; 966 char *prefix;
@@ -1291,7 +1293,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
1291 /* Default to the fs superblock SID. */ 1293 /* Default to the fs superblock SID. */
1292 isec->sid = sbsec->sid; 1294 isec->sid = sbsec->sid;
1293 1295
1294 if (sbsec->proc) { 1296 if (sbsec->proc && !S_ISLNK(inode->i_mode)) {
1295 struct proc_inode *proci = PROC_I(inode); 1297 struct proc_inode *proci = PROC_I(inode);
1296 if (proci->pde) { 1298 if (proci->pde) {
1297 isec->sclass = inode_mode_to_security_class(inode->i_mode); 1299 isec->sclass = inode_mode_to_security_class(inode->i_mode);
@@ -2121,7 +2123,6 @@ static inline void flush_unauthorized_files(struct files_struct *files)
2121 long j = -1; 2123 long j = -1;
2122 int drop_tty = 0; 2124 int drop_tty = 0;
2123 2125
2124 mutex_lock(&tty_mutex);
2125 tty = get_current_tty(); 2126 tty = get_current_tty();
2126 if (tty) { 2127 if (tty) {
2127 file_list_lock(); 2128 file_list_lock();
@@ -2139,8 +2140,8 @@ static inline void flush_unauthorized_files(struct files_struct *files)
2139 } 2140 }
2140 } 2141 }
2141 file_list_unlock(); 2142 file_list_unlock();
2143 tty_kref_put(tty);
2142 } 2144 }
2143 mutex_unlock(&tty_mutex);
2144 /* Reset controlling tty. */ 2145 /* Reset controlling tty. */
2145 if (drop_tty) 2146 if (drop_tty)
2146 no_tty(); 2147 no_tty();
@@ -3543,38 +3544,44 @@ out:
3543#endif /* IPV6 */ 3544#endif /* IPV6 */
3544 3545
3545static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 3546static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
3546 char **addrp, int src, u8 *proto) 3547 char **_addrp, int src, u8 *proto)
3547{ 3548{
3548 int ret = 0; 3549 char *addrp;
3550 int ret;
3549 3551
3550 switch (ad->u.net.family) { 3552 switch (ad->u.net.family) {
3551 case PF_INET: 3553 case PF_INET:
3552 ret = selinux_parse_skb_ipv4(skb, ad, proto); 3554 ret = selinux_parse_skb_ipv4(skb, ad, proto);
3553 if (ret || !addrp) 3555 if (ret)
3554 break; 3556 goto parse_error;
3555 *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 3557 addrp = (char *)(src ? &ad->u.net.v4info.saddr :
3556 &ad->u.net.v4info.daddr); 3558 &ad->u.net.v4info.daddr);
3557 break; 3559 goto okay;
3558 3560
3559#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 3561#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3560 case PF_INET6: 3562 case PF_INET6:
3561 ret = selinux_parse_skb_ipv6(skb, ad, proto); 3563 ret = selinux_parse_skb_ipv6(skb, ad, proto);
3562 if (ret || !addrp) 3564 if (ret)
3563 break; 3565 goto parse_error;
3564 *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 3566 addrp = (char *)(src ? &ad->u.net.v6info.saddr :
3565 &ad->u.net.v6info.daddr); 3567 &ad->u.net.v6info.daddr);
3566 break; 3568 goto okay;
3567#endif /* IPV6 */ 3569#endif /* IPV6 */
3568 default: 3570 default:
3569 break; 3571 addrp = NULL;
3572 goto okay;
3570 } 3573 }
3571 3574
3572 if (unlikely(ret)) 3575parse_error:
3573 printk(KERN_WARNING 3576 printk(KERN_WARNING
3574 "SELinux: failure in selinux_parse_skb()," 3577 "SELinux: failure in selinux_parse_skb(),"
3575 " unable to parse packet\n"); 3578 " unable to parse packet\n");
3576
3577 return ret; 3579 return ret;
3580
3581okay:
3582 if (_addrp)
3583 *_addrp = addrp;
3584 return 0;
3578} 3585}
3579 3586
3580/** 3587/**
@@ -3789,6 +3796,7 @@ out:
3789 3796
3790static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 3797static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3791{ 3798{
3799 struct sock *sk = sock->sk;
3792 struct inode_security_struct *isec; 3800 struct inode_security_struct *isec;
3793 int err; 3801 int err;
3794 3802
@@ -3802,7 +3810,6 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
3802 isec = SOCK_INODE(sock)->i_security; 3810 isec = SOCK_INODE(sock)->i_security;
3803 if (isec->sclass == SECCLASS_TCP_SOCKET || 3811 if (isec->sclass == SECCLASS_TCP_SOCKET ||
3804 isec->sclass == SECCLASS_DCCP_SOCKET) { 3812 isec->sclass == SECCLASS_DCCP_SOCKET) {
3805 struct sock *sk = sock->sk;
3806 struct avc_audit_data ad; 3813 struct avc_audit_data ad;
3807 struct sockaddr_in *addr4 = NULL; 3814 struct sockaddr_in *addr4 = NULL;
3808 struct sockaddr_in6 *addr6 = NULL; 3815 struct sockaddr_in6 *addr6 = NULL;
@@ -3836,6 +3843,8 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
3836 goto out; 3843 goto out;
3837 } 3844 }
3838 3845
3846 err = selinux_netlbl_socket_connect(sk, address);
3847
3839out: 3848out:
3840 return err; 3849 return err;
3841} 3850}
@@ -4065,20 +4074,28 @@ static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
4065} 4074}
4066 4075
4067static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4076static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4068 struct avc_audit_data *ad, 4077 u16 family)
4069 u16 family, char *addrp)
4070{ 4078{
4071 int err; 4079 int err;
4072 struct sk_security_struct *sksec = sk->sk_security; 4080 struct sk_security_struct *sksec = sk->sk_security;
4073 u32 peer_sid; 4081 u32 peer_sid;
4074 u32 sk_sid = sksec->sid; 4082 u32 sk_sid = sksec->sid;
4083 struct avc_audit_data ad;
4084 char *addrp;
4085
4086 AVC_AUDIT_DATA_INIT(&ad, NET);
4087 ad.u.net.netif = skb->iif;
4088 ad.u.net.family = family;
4089 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4090 if (err)
4091 return err;
4075 4092
4076 if (selinux_compat_net) 4093 if (selinux_compat_net)
4077 err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad, 4094 err = selinux_sock_rcv_skb_iptables_compat(sk, skb, &ad,
4078 family, addrp); 4095 family, addrp);
4079 else 4096 else
4080 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4097 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4081 PACKET__RECV, ad); 4098 PACKET__RECV, &ad);
4082 if (err) 4099 if (err)
4083 return err; 4100 return err;
4084 4101
@@ -4087,12 +4104,14 @@ static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4087 if (err) 4104 if (err)
4088 return err; 4105 return err;
4089 err = avc_has_perm(sk_sid, peer_sid, 4106 err = avc_has_perm(sk_sid, peer_sid,
4090 SECCLASS_PEER, PEER__RECV, ad); 4107 SECCLASS_PEER, PEER__RECV, &ad);
4108 if (err)
4109 selinux_netlbl_err(skb, err, 0);
4091 } else { 4110 } else {
4092 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad); 4111 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4093 if (err) 4112 if (err)
4094 return err; 4113 return err;
4095 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad); 4114 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4096 } 4115 }
4097 4116
4098 return err; 4117 return err;
@@ -4106,6 +4125,8 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4106 u32 sk_sid = sksec->sid; 4125 u32 sk_sid = sksec->sid;
4107 struct avc_audit_data ad; 4126 struct avc_audit_data ad;
4108 char *addrp; 4127 char *addrp;
4128 u8 secmark_active;
4129 u8 peerlbl_active;
4109 4130
4110 if (family != PF_INET && family != PF_INET6) 4131 if (family != PF_INET && family != PF_INET6)
4111 return 0; 4132 return 0;
@@ -4114,6 +4135,18 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4114 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4135 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4115 family = PF_INET; 4136 family = PF_INET;
4116 4137
4138 /* If any sort of compatibility mode is enabled then handoff processing
4139 * to the selinux_sock_rcv_skb_compat() function to deal with the
4140 * special handling. We do this in an attempt to keep this function
4141 * as fast and as clean as possible. */
4142 if (selinux_compat_net || !selinux_policycap_netpeer)
4143 return selinux_sock_rcv_skb_compat(sk, skb, family);
4144
4145 secmark_active = selinux_secmark_enabled();
4146 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4147 if (!secmark_active && !peerlbl_active)
4148 return 0;
4149
4117 AVC_AUDIT_DATA_INIT(&ad, NET); 4150 AVC_AUDIT_DATA_INIT(&ad, NET);
4118 ad.u.net.netif = skb->iif; 4151 ad.u.net.netif = skb->iif;
4119 ad.u.net.family = family; 4152 ad.u.net.family = family;
@@ -4121,15 +4154,7 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4121 if (err) 4154 if (err)
4122 return err; 4155 return err;
4123 4156
4124 /* If any sort of compatibility mode is enabled then handoff processing 4157 if (peerlbl_active) {
4125 * to the selinux_sock_rcv_skb_compat() function to deal with the
4126 * special handling. We do this in an attempt to keep this function
4127 * as fast and as clean as possible. */
4128 if (selinux_compat_net || !selinux_policycap_netpeer)
4129 return selinux_sock_rcv_skb_compat(sk, skb, &ad,
4130 family, addrp);
4131
4132 if (netlbl_enabled() || selinux_xfrm_enabled()) {
4133 u32 peer_sid; 4158 u32 peer_sid;
4134 4159
4135 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4160 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
@@ -4137,13 +4162,17 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4137 return err; 4162 return err;
4138 err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4163 err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family,
4139 peer_sid, &ad); 4164 peer_sid, &ad);
4140 if (err) 4165 if (err) {
4166 selinux_netlbl_err(skb, err, 0);
4141 return err; 4167 return err;
4168 }
4142 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4169 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4143 PEER__RECV, &ad); 4170 PEER__RECV, &ad);
4171 if (err)
4172 selinux_netlbl_err(skb, err, 0);
4144 } 4173 }
4145 4174
4146 if (selinux_secmark_enabled()) { 4175 if (secmark_active) {
4147 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4176 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4148 PACKET__RECV, &ad); 4177 PACKET__RECV, &ad);
4149 if (err) 4178 if (err)
@@ -4202,10 +4231,12 @@ static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *
4202 u32 peer_secid = SECSID_NULL; 4231 u32 peer_secid = SECSID_NULL;
4203 u16 family; 4232 u16 family;
4204 4233
4205 if (sock) 4234 if (skb && skb->protocol == htons(ETH_P_IP))
4235 family = PF_INET;
4236 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4237 family = PF_INET6;
4238 else if (sock)
4206 family = sock->sk->sk_family; 4239 family = sock->sk->sk_family;
4207 else if (skb && skb->sk)
4208 family = skb->sk->sk_family;
4209 else 4240 else
4210 goto out; 4241 goto out;
4211 4242
@@ -4263,8 +4294,6 @@ static void selinux_sock_graft(struct sock *sk, struct socket *parent)
4263 sk->sk_family == PF_UNIX) 4294 sk->sk_family == PF_UNIX)
4264 isec->sid = sksec->sid; 4295 isec->sid = sksec->sid;
4265 sksec->sclass = isec->sclass; 4296 sksec->sclass = isec->sclass;
4266
4267 selinux_netlbl_sock_graft(sk, parent);
4268} 4297}
4269 4298
4270static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 4299static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
@@ -4272,10 +4301,15 @@ static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4272{ 4301{
4273 struct sk_security_struct *sksec = sk->sk_security; 4302 struct sk_security_struct *sksec = sk->sk_security;
4274 int err; 4303 int err;
4304 u16 family = sk->sk_family;
4275 u32 newsid; 4305 u32 newsid;
4276 u32 peersid; 4306 u32 peersid;
4277 4307
4278 err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid); 4308 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4309 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4310 family = PF_INET;
4311
4312 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4279 if (err) 4313 if (err)
4280 return err; 4314 return err;
4281 if (peersid == SECSID_NULL) { 4315 if (peersid == SECSID_NULL) {
@@ -4310,12 +4344,18 @@ static void selinux_inet_csk_clone(struct sock *newsk,
4310 selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 4344 selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family);
4311} 4345}
4312 4346
4313static void selinux_inet_conn_established(struct sock *sk, 4347static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
4314 struct sk_buff *skb)
4315{ 4348{
4349 u16 family = sk->sk_family;
4316 struct sk_security_struct *sksec = sk->sk_security; 4350 struct sk_security_struct *sksec = sk->sk_security;
4317 4351
4318 selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid); 4352 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4353 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4354 family = PF_INET;
4355
4356 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
4357
4358 selinux_netlbl_inet_conn_established(sk, family);
4319} 4359}
4320 4360
4321static void selinux_req_classify_flow(const struct request_sock *req, 4361static void selinux_req_classify_flow(const struct request_sock *req,
@@ -4365,39 +4405,54 @@ out:
4365static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4405static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4366 u16 family) 4406 u16 family)
4367{ 4407{
4408 int err;
4368 char *addrp; 4409 char *addrp;
4369 u32 peer_sid; 4410 u32 peer_sid;
4370 struct avc_audit_data ad; 4411 struct avc_audit_data ad;
4371 u8 secmark_active; 4412 u8 secmark_active;
4413 u8 netlbl_active;
4372 u8 peerlbl_active; 4414 u8 peerlbl_active;
4373 4415
4374 if (!selinux_policycap_netpeer) 4416 if (!selinux_policycap_netpeer)
4375 return NF_ACCEPT; 4417 return NF_ACCEPT;
4376 4418
4377 secmark_active = selinux_secmark_enabled(); 4419 secmark_active = selinux_secmark_enabled();
4378 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4420 netlbl_active = netlbl_enabled();
4421 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
4379 if (!secmark_active && !peerlbl_active) 4422 if (!secmark_active && !peerlbl_active)
4380 return NF_ACCEPT; 4423 return NF_ACCEPT;
4381 4424
4425 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4426 return NF_DROP;
4427
4382 AVC_AUDIT_DATA_INIT(&ad, NET); 4428 AVC_AUDIT_DATA_INIT(&ad, NET);
4383 ad.u.net.netif = ifindex; 4429 ad.u.net.netif = ifindex;
4384 ad.u.net.family = family; 4430 ad.u.net.family = family;
4385 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4431 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4386 return NF_DROP; 4432 return NF_DROP;
4387 4433
4388 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4434 if (peerlbl_active) {
4389 return NF_DROP; 4435 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4390 4436 peer_sid, &ad);
4391 if (peerlbl_active) 4437 if (err) {
4392 if (selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4438 selinux_netlbl_err(skb, err, 1);
4393 peer_sid, &ad) != 0)
4394 return NF_DROP; 4439 return NF_DROP;
4440 }
4441 }
4395 4442
4396 if (secmark_active) 4443 if (secmark_active)
4397 if (avc_has_perm(peer_sid, skb->secmark, 4444 if (avc_has_perm(peer_sid, skb->secmark,
4398 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4445 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4399 return NF_DROP; 4446 return NF_DROP;
4400 4447
4448 if (netlbl_active)
4449 /* we do this in the FORWARD path and not the POST_ROUTING
4450 * path because we want to make sure we apply the necessary
4451 * labeling before IPsec is applied so we can leverage AH
4452 * protection */
4453 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4454 return NF_DROP;
4455
4401 return NF_ACCEPT; 4456 return NF_ACCEPT;
4402} 4457}
4403 4458
@@ -4421,6 +4476,37 @@ static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4421} 4476}
4422#endif /* IPV6 */ 4477#endif /* IPV6 */
4423 4478
4479static unsigned int selinux_ip_output(struct sk_buff *skb,
4480 u16 family)
4481{
4482 u32 sid;
4483
4484 if (!netlbl_enabled())
4485 return NF_ACCEPT;
4486
4487 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4488 * because we want to make sure we apply the necessary labeling
4489 * before IPsec is applied so we can leverage AH protection */
4490 if (skb->sk) {
4491 struct sk_security_struct *sksec = skb->sk->sk_security;
4492 sid = sksec->sid;
4493 } else
4494 sid = SECINITSID_KERNEL;
4495 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4496 return NF_DROP;
4497
4498 return NF_ACCEPT;
4499}
4500
4501static unsigned int selinux_ipv4_output(unsigned int hooknum,
4502 struct sk_buff *skb,
4503 const struct net_device *in,
4504 const struct net_device *out,
4505 int (*okfn)(struct sk_buff *))
4506{
4507 return selinux_ip_output(skb, PF_INET);
4508}
4509
4424static int selinux_ip_postroute_iptables_compat(struct sock *sk, 4510static int selinux_ip_postroute_iptables_compat(struct sock *sk,
4425 int ifindex, 4511 int ifindex,
4426 struct avc_audit_data *ad, 4512 struct avc_audit_data *ad,
@@ -4488,30 +4574,36 @@ static int selinux_ip_postroute_iptables_compat(struct sock *sk,
4488 4574
4489static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4575static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4490 int ifindex, 4576 int ifindex,
4491 struct avc_audit_data *ad, 4577 u16 family)
4492 u16 family,
4493 char *addrp,
4494 u8 proto)
4495{ 4578{
4496 struct sock *sk = skb->sk; 4579 struct sock *sk = skb->sk;
4497 struct sk_security_struct *sksec; 4580 struct sk_security_struct *sksec;
4581 struct avc_audit_data ad;
4582 char *addrp;
4583 u8 proto;
4498 4584
4499 if (sk == NULL) 4585 if (sk == NULL)
4500 return NF_ACCEPT; 4586 return NF_ACCEPT;
4501 sksec = sk->sk_security; 4587 sksec = sk->sk_security;
4502 4588
4589 AVC_AUDIT_DATA_INIT(&ad, NET);
4590 ad.u.net.netif = ifindex;
4591 ad.u.net.family = family;
4592 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4593 return NF_DROP;
4594
4503 if (selinux_compat_net) { 4595 if (selinux_compat_net) {
4504 if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex, 4596 if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex,
4505 ad, family, addrp)) 4597 &ad, family, addrp))
4506 return NF_DROP; 4598 return NF_DROP;
4507 } else { 4599 } else {
4508 if (avc_has_perm(sksec->sid, skb->secmark, 4600 if (avc_has_perm(sksec->sid, skb->secmark,
4509 SECCLASS_PACKET, PACKET__SEND, ad)) 4601 SECCLASS_PACKET, PACKET__SEND, &ad))
4510 return NF_DROP; 4602 return NF_DROP;
4511 } 4603 }
4512 4604
4513 if (selinux_policycap_netpeer) 4605 if (selinux_policycap_netpeer)
4514 if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto)) 4606 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4515 return NF_DROP; 4607 return NF_DROP;
4516 4608
4517 return NF_ACCEPT; 4609 return NF_ACCEPT;
@@ -4525,23 +4617,15 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4525 struct sock *sk; 4617 struct sock *sk;
4526 struct avc_audit_data ad; 4618 struct avc_audit_data ad;
4527 char *addrp; 4619 char *addrp;
4528 u8 proto;
4529 u8 secmark_active; 4620 u8 secmark_active;
4530 u8 peerlbl_active; 4621 u8 peerlbl_active;
4531 4622
4532 AVC_AUDIT_DATA_INIT(&ad, NET);
4533 ad.u.net.netif = ifindex;
4534 ad.u.net.family = family;
4535 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4536 return NF_DROP;
4537
4538 /* If any sort of compatibility mode is enabled then handoff processing 4623 /* If any sort of compatibility mode is enabled then handoff processing
4539 * to the selinux_ip_postroute_compat() function to deal with the 4624 * to the selinux_ip_postroute_compat() function to deal with the
4540 * special handling. We do this in an attempt to keep this function 4625 * special handling. We do this in an attempt to keep this function
4541 * as fast and as clean as possible. */ 4626 * as fast and as clean as possible. */
4542 if (selinux_compat_net || !selinux_policycap_netpeer) 4627 if (selinux_compat_net || !selinux_policycap_netpeer)
4543 return selinux_ip_postroute_compat(skb, ifindex, &ad, 4628 return selinux_ip_postroute_compat(skb, ifindex, family);
4544 family, addrp, proto);
4545 4629
4546 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4630 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4547 * packet transformation so allow the packet to pass without any checks 4631 * packet transformation so allow the packet to pass without any checks
@@ -4557,21 +4641,45 @@ static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4557 if (!secmark_active && !peerlbl_active) 4641 if (!secmark_active && !peerlbl_active)
4558 return NF_ACCEPT; 4642 return NF_ACCEPT;
4559 4643
4560 /* if the packet is locally generated (skb->sk != NULL) then use the 4644 /* if the packet is being forwarded then get the peer label from the
4561 * socket's label as the peer label, otherwise the packet is being 4645 * packet itself; otherwise check to see if it is from a local
4562 * forwarded through this system and we need to fetch the peer label 4646 * application or the kernel, if from an application get the peer label
4563 * directly from the packet */ 4647 * from the sending socket, otherwise use the kernel's sid */
4564 sk = skb->sk; 4648 sk = skb->sk;
4565 if (sk) { 4649 if (sk == NULL) {
4650 switch (family) {
4651 case PF_INET:
4652 if (IPCB(skb)->flags & IPSKB_FORWARDED)
4653 secmark_perm = PACKET__FORWARD_OUT;
4654 else
4655 secmark_perm = PACKET__SEND;
4656 break;
4657 case PF_INET6:
4658 if (IP6CB(skb)->flags & IP6SKB_FORWARDED)
4659 secmark_perm = PACKET__FORWARD_OUT;
4660 else
4661 secmark_perm = PACKET__SEND;
4662 break;
4663 default:
4664 return NF_DROP;
4665 }
4666 if (secmark_perm == PACKET__FORWARD_OUT) {
4667 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4668 return NF_DROP;
4669 } else
4670 peer_sid = SECINITSID_KERNEL;
4671 } else {
4566 struct sk_security_struct *sksec = sk->sk_security; 4672 struct sk_security_struct *sksec = sk->sk_security;
4567 peer_sid = sksec->sid; 4673 peer_sid = sksec->sid;
4568 secmark_perm = PACKET__SEND; 4674 secmark_perm = PACKET__SEND;
4569 } else {
4570 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4571 return NF_DROP;
4572 secmark_perm = PACKET__FORWARD_OUT;
4573 } 4675 }
4574 4676
4677 AVC_AUDIT_DATA_INIT(&ad, NET);
4678 ad.u.net.netif = ifindex;
4679 ad.u.net.family = family;
4680 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
4681 return NF_DROP;
4682
4575 if (secmark_active) 4683 if (secmark_active)
4576 if (avc_has_perm(peer_sid, skb->secmark, 4684 if (avc_has_perm(peer_sid, skb->secmark,
4577 SECCLASS_PACKET, secmark_perm, &ad)) 4685 SECCLASS_PACKET, secmark_perm, &ad))
@@ -5214,8 +5322,12 @@ static int selinux_setprocattr(struct task_struct *p,
5214 5322
5215 if (sid == 0) 5323 if (sid == 0)
5216 return -EINVAL; 5324 return -EINVAL;
5217 5325 /*
5218 /* Only allow single threaded processes to change context */ 5326 * SELinux allows to change context in the following case only.
5327 * - Single threaded processes.
5328 * - Multi threaded processes intend to change its context into
5329 * more restricted domain (defined by TYPEBOUNDS statement).
5330 */
5219 if (atomic_read(&p->mm->mm_users) != 1) { 5331 if (atomic_read(&p->mm->mm_users) != 1) {
5220 struct task_struct *g, *t; 5332 struct task_struct *g, *t;
5221 struct mm_struct *mm = p->mm; 5333 struct mm_struct *mm = p->mm;
@@ -5223,11 +5335,16 @@ static int selinux_setprocattr(struct task_struct *p,
5223 do_each_thread(g, t) { 5335 do_each_thread(g, t) {
5224 if (t->mm == mm && t != p) { 5336 if (t->mm == mm && t != p) {
5225 read_unlock(&tasklist_lock); 5337 read_unlock(&tasklist_lock);
5226 return -EPERM; 5338 error = security_bounded_transition(tsec->sid, sid);
5339 if (!error)
5340 goto boundary_ok;
5341
5342 return error;
5227 } 5343 }
5228 } while_each_thread(g, t); 5344 } while_each_thread(g, t);
5229 read_unlock(&tasklist_lock); 5345 read_unlock(&tasklist_lock);
5230 } 5346 }
5347boundary_ok:
5231 5348
5232 /* Check permissions for the transition. */ 5349 /* Check permissions for the transition. */
5233 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 5350 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
@@ -5636,6 +5753,13 @@ static struct nf_hook_ops selinux_ipv4_ops[] = {
5636 .pf = PF_INET, 5753 .pf = PF_INET,
5637 .hooknum = NF_INET_FORWARD, 5754 .hooknum = NF_INET_FORWARD,
5638 .priority = NF_IP_PRI_SELINUX_FIRST, 5755 .priority = NF_IP_PRI_SELINUX_FIRST,
5756 },
5757 {
5758 .hook = selinux_ipv4_output,
5759 .owner = THIS_MODULE,
5760 .pf = PF_INET,
5761 .hooknum = NF_INET_LOCAL_OUT,
5762 .priority = NF_IP_PRI_SELINUX_FIRST,
5639 } 5763 }
5640}; 5764};
5641 5765