Skip to content

Commit 014ab19

Browse files
committed
selinux: Set socket NetLabel based on connection endpoint
Previous work enabled the use of address based NetLabel selectors, which while highly useful, brought the potential for additional per-packet overhead when used. This patch attempts to solve that by applying NetLabel socket labels when sockets are connect()'d. This should alleviate the per-packet NetLabel labeling for all connected sockets (yes, it even works for connected DGRAM sockets). Signed-off-by: Paul Moore <paul.moore@hp.com> Reviewed-by: James Morris <jmorris@namei.org>
1 parent 948bf85 commit 014ab19

8 files changed

Lines changed: 311 additions & 37 deletions

File tree

‎include/net/cipso_ipv4.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway);
207207
int cipso_v4_sock_setattr(struct sock *sk,
208208
const struct cipso_v4_doi *doi_def,
209209
const struct netlbl_lsm_secattr *secattr);
210+
void cipso_v4_sock_delattr(struct sock *sk);
210211
int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
211212
int cipso_v4_skbuff_setattr(struct sk_buff *skb,
212213
const struct cipso_v4_doi *doi_def,
@@ -230,6 +231,10 @@ static inline int cipso_v4_sock_setattr(struct sock *sk,
230231
return -ENOSYS;
231232
}
232233

234+
static inline void cipso_v4_sock_delattr(struct sock *sk)
235+
{
236+
}
237+
233238
static inline int cipso_v4_sock_getattr(struct sock *sk,
234239
struct netlbl_lsm_secattr *secattr)
235240
{

‎include/net/netlabel.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,12 @@ int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
380380
int netlbl_enabled(void);
381381
int netlbl_sock_setattr(struct sock *sk,
382382
const struct netlbl_lsm_secattr *secattr);
383+
void netlbl_sock_delattr(struct sock *sk);
383384
int netlbl_sock_getattr(struct sock *sk,
384385
struct netlbl_lsm_secattr *secattr);
386+
int netlbl_conn_setattr(struct sock *sk,
387+
struct sockaddr *addr,
388+
const struct netlbl_lsm_secattr *secattr);
385389
int netlbl_skbuff_setattr(struct sk_buff *skb,
386390
u16 family,
387391
const struct netlbl_lsm_secattr *secattr);
@@ -449,11 +453,20 @@ static inline int netlbl_sock_setattr(struct sock *sk,
449453
{
450454
return -ENOSYS;
451455
}
456+
static inline void netlbl_sock_delattr(struct sock *sk)
457+
{
458+
}
452459
static inline int netlbl_sock_getattr(struct sock *sk,
453460
struct netlbl_lsm_secattr *secattr)
454461
{
455462
return -ENOSYS;
456463
}
464+
static inline int netlbl_conn_setattr(struct sock *sk,
465+
struct sockaddr *addr,
466+
const struct netlbl_lsm_secattr *secattr)
467+
{
468+
return -ENOSYS;
469+
}
457470
static inline int netlbl_skbuff_setattr(struct sk_buff *skb,
458471
u16 family,
459472
const struct netlbl_lsm_secattr *secattr)

‎net/ipv4/cipso_ipv4.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,80 @@ int cipso_v4_sock_setattr(struct sock *sk,
18091809
return ret_val;
18101810
}
18111811

1812+
/**
1813+
* cipso_v4_sock_delattr - Delete the CIPSO option from a socket
1814+
* @sk: the socket
1815+
*
1816+
* Description:
1817+
* Removes the CIPSO option from a socket, if present.
1818+
*
1819+
*/
1820+
void cipso_v4_sock_delattr(struct sock *sk)
1821+
{
1822+
u8 hdr_delta;
1823+
struct ip_options *opt;
1824+
struct inet_sock *sk_inet;
1825+
1826+
sk_inet = inet_sk(sk);
1827+
opt = sk_inet->opt;
1828+
if (opt == NULL || opt->cipso == 0)
1829+
return;
1830+
1831+
if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
1832+
u8 cipso_len;
1833+
u8 cipso_off;
1834+
unsigned char *cipso_ptr;
1835+
int iter;
1836+
int optlen_new;
1837+
1838+
cipso_off = opt->cipso - sizeof(struct iphdr);
1839+
cipso_ptr = &opt->__data[cipso_off];
1840+
cipso_len = cipso_ptr[1];
1841+
1842+
if (opt->srr > opt->cipso)
1843+
opt->srr -= cipso_len;
1844+
if (opt->rr > opt->cipso)
1845+
opt->rr -= cipso_len;
1846+
if (opt->ts > opt->cipso)
1847+
opt->ts -= cipso_len;
1848+
if (opt->router_alert > opt->cipso)
1849+
opt->router_alert -= cipso_len;
1850+
opt->cipso = 0;
1851+
1852+
memmove(cipso_ptr, cipso_ptr + cipso_len,
1853+
opt->optlen - cipso_off - cipso_len);
1854+
1855+
/* determining the new total option length is tricky because of
1856+
* the padding necessary, the only thing i can think to do at
1857+
* this point is walk the options one-by-one, skipping the
1858+
* padding at the end to determine the actual option size and
1859+
* from there we can determine the new total option length */
1860+
iter = 0;
1861+
optlen_new = 0;
1862+
while (iter < opt->optlen)
1863+
if (opt->__data[iter] != IPOPT_NOP) {
1864+
iter += opt->__data[iter + 1];
1865+
optlen_new = iter;
1866+
} else
1867+
iter++;
1868+
hdr_delta = opt->optlen;
1869+
opt->optlen = (optlen_new + 3) & ~3;
1870+
hdr_delta -= opt->optlen;
1871+
} else {
1872+
/* only the cipso option was present on the socket so we can
1873+
* remove the entire option struct */
1874+
sk_inet->opt = NULL;
1875+
hdr_delta = opt->optlen;
1876+
kfree(opt);
1877+
}
1878+
1879+
if (sk_inet->is_icsk && hdr_delta > 0) {
1880+
struct inet_connection_sock *sk_conn = inet_csk(sk);
1881+
sk_conn->icsk_ext_hdr_len -= hdr_delta;
1882+
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1883+
}
1884+
}
1885+
18121886
/**
18131887
* cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
18141888
* @cipso: the CIPSO v4 option

‎net/netlabel/netlabel_kapi.c‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
/*
13-
* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
13+
* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
1414
*
1515
* This program is free software; you can redistribute it and/or modify
1616
* it under the terms of the GNU General Public License as published by
@@ -455,6 +455,20 @@ int netlbl_sock_setattr(struct sock *sk,
455455
return ret_val;
456456
}
457457

458+
/**
459+
* netlbl_sock_delattr - Delete all the NetLabel labels on a socket
460+
* @sk: the socket
461+
*
462+
* Description:
463+
* Remove all the NetLabel labeling from @sk. The caller is responsible for
464+
* ensuring that @sk is locked.
465+
*
466+
*/
467+
void netlbl_sock_delattr(struct sock *sk)
468+
{
469+
cipso_v4_sock_delattr(sk);
470+
}
471+
458472
/**
459473
* netlbl_sock_getattr - Determine the security attributes of a sock
460474
* @sk: the sock
@@ -472,6 +486,68 @@ int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
472486
return cipso_v4_sock_getattr(sk, secattr);
473487
}
474488

489+
/**
490+
* netlbl_conn_setattr - Label a connected socket using the correct protocol
491+
* @sk: the socket to label
492+
* @addr: the destination address
493+
* @secattr: the security attributes
494+
*
495+
* Description:
496+
* Attach the correct label to the given connected socket using the security
497+
* attributes specified in @secattr. The caller is responsible for ensuring
498+
* that @sk is locked. Returns zero on success, negative values on failure.
499+
*
500+
*/
501+
int netlbl_conn_setattr(struct sock *sk,
502+
struct sockaddr *addr,
503+
const struct netlbl_lsm_secattr *secattr)
504+
{
505+
int ret_val;
506+
struct sockaddr_in *addr4;
507+
struct netlbl_domaddr4_map *af4_entry;
508+
509+
rcu_read_lock();
510+
switch (addr->sa_family) {
511+
case AF_INET:
512+
addr4 = (struct sockaddr_in *)addr;
513+
af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
514+
addr4->sin_addr.s_addr);
515+
if (af4_entry == NULL) {
516+
ret_val = -ENOENT;
517+
goto conn_setattr_return;
518+
}
519+
switch (af4_entry->type) {
520+
case NETLBL_NLTYPE_CIPSOV4:
521+
ret_val = cipso_v4_sock_setattr(sk,
522+
af4_entry->type_def.cipsov4,
523+
secattr);
524+
break;
525+
case NETLBL_NLTYPE_UNLABELED:
526+
/* just delete the protocols we support for right now
527+
* but we could remove other protocols if needed */
528+
cipso_v4_sock_delattr(sk);
529+
ret_val = 0;
530+
break;
531+
default:
532+
ret_val = -ENOENT;
533+
}
534+
break;
535+
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
536+
case AF_INET6:
537+
/* since we don't support any IPv6 labeling protocols right
538+
* now we can optimize everything away until we do */
539+
ret_val = 0;
540+
break;
541+
#endif /* IPv6 */
542+
default:
543+
ret_val = 0;
544+
}
545+
546+
conn_setattr_return:
547+
rcu_read_unlock();
548+
return ret_val;
549+
}
550+
475551
/**
476552
* netlbl_skbuff_setattr - Label a packet using the correct protocol
477553
* @skb: the packet

‎security/selinux/hooks.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,7 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in
37943794

37953795
static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
37963796
{
3797+
struct sock *sk = sock->sk;
37973798
struct inode_security_struct *isec;
37983799
int err;
37993800

@@ -3807,7 +3808,6 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
38073808
isec = SOCK_INODE(sock)->i_security;
38083809
if (isec->sclass == SECCLASS_TCP_SOCKET ||
38093810
isec->sclass == SECCLASS_DCCP_SOCKET) {
3810-
struct sock *sk = sock->sk;
38113811
struct avc_audit_data ad;
38123812
struct sockaddr_in *addr4 = NULL;
38133813
struct sockaddr_in6 *addr6 = NULL;
@@ -3841,6 +3841,8 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
38413841
goto out;
38423842
}
38433843

3844+
err = selinux_netlbl_socket_connect(sk, address);
3845+
38443846
out:
38453847
return err;
38463848
}
@@ -4290,8 +4292,6 @@ static void selinux_sock_graft(struct sock *sk, struct socket *parent)
42904292
sk->sk_family == PF_UNIX)
42914293
isec->sid = sksec->sid;
42924294
sksec->sclass = isec->sclass;
4293-
4294-
selinux_netlbl_sock_graft(sk, parent);
42954295
}
42964296

42974297
static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
@@ -4342,8 +4342,7 @@ static void selinux_inet_csk_clone(struct sock *newsk,
43424342
selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family);
43434343
}
43444344

4345-
static void selinux_inet_conn_established(struct sock *sk,
4346-
struct sk_buff *skb)
4345+
static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
43474346
{
43484347
u16 family = sk->sk_family;
43494348
struct sk_security_struct *sksec = sk->sk_security;
@@ -4353,6 +4352,8 @@ static void selinux_inet_conn_established(struct sock *sk,
43534352
family = PF_INET;
43544353

43554354
selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
4355+
4356+
selinux_netlbl_inet_conn_established(sk, family);
43564357
}
43574358

43584359
static void selinux_req_classify_flow(const struct request_sock *req,

‎security/selinux/include/netlabel.h‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
5252
u16 family,
5353
u32 sid);
5454

55-
void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock);
55+
void selinux_netlbl_inet_conn_established(struct sock *sk, u16 family);
5656
int selinux_netlbl_socket_post_create(struct socket *sock);
5757
int selinux_netlbl_inode_permission(struct inode *inode, int mask);
5858
int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
@@ -62,6 +62,8 @@ int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
6262
int selinux_netlbl_socket_setsockopt(struct socket *sock,
6363
int level,
6464
int optname);
65+
int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr);
66+
6567
#else
6668
static inline void selinux_netlbl_cache_invalidate(void)
6769
{
@@ -98,8 +100,14 @@ static inline int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
98100
return 0;
99101
}
100102

101-
static inline void selinux_netlbl_sock_graft(struct sock *sk,
102-
struct socket *sock)
103+
static inline int selinux_netlbl_conn_setsid(struct sock *sk,
104+
struct sockaddr *addr)
105+
{
106+
return 0;
107+
}
108+
109+
static inline void selinux_netlbl_inet_conn_established(struct sock *sk,
110+
u16 family)
103111
{
104112
return;
105113
}
@@ -125,6 +133,11 @@ static inline int selinux_netlbl_socket_setsockopt(struct socket *sock,
125133
{
126134
return 0;
127135
}
136+
static inline int selinux_netlbl_socket_connect(struct sock *sk,
137+
struct sockaddr *addr)
138+
{
139+
return 0;
140+
}
128141
#endif /* CONFIG_NETLABEL */
129142

130143
#endif

‎security/selinux/include/objsec.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct sk_security_struct {
118118
NLBL_REQUIRE,
119119
NLBL_LABELED,
120120
NLBL_REQSKB,
121+
NLBL_CONNLABELED,
121122
} nlbl_state;
122123
#endif
123124
};

0 commit comments

Comments
 (0)