Skip to content

Commit 389fb80

Browse files
pcmooreJames Morris
authored andcommitted
netlabel: Label incoming TCP connections correctly in SELinux
The current NetLabel/SELinux behavior for incoming TCP connections works but only through a series of happy coincidences that rely on the limited nature of standard CIPSO (only able to convey MLS attributes) and the write equality imposed by the SELinux MLS constraints. The problem is that network sockets created as the result of an incoming TCP connection were not on-the-wire labeled based on the security attributes of the parent socket but rather based on the wire label of the remote peer. The issue had to do with how IP options were managed as part of the network stack and where the LSM hooks were in relation to the code which set the IP options on these newly created child sockets. While NetLabel/SELinux did correctly set the socket's on-the-wire label it was promptly cleared by the network stack and reset based on the IP options of the remote peer. This patch, in conjunction with a prior patch that adjusted the LSM hook locations, works to set the correct on-the-wire label format for new incoming connections through the security_inet_conn_request() hook. Besides the correct behavior there are many advantages to this change, the most significant is that all of the NetLabel socket labeling code in SELinux now lives in hooks which can return error codes to the core stack which allows us to finally get ride of the selinux_netlbl_inode_permission() logic which greatly simplfies the NetLabel/SELinux glue code. In the process of developing this patch I also ran into a small handful of AF_INET6 cleanliness issues that have been fixed which should make the code safer and easier to extend in the future. Signed-off-by: Paul Moore <paul.moore@hp.com> Acked-by: Casey Schaufler <casey@schaufler-ca.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 284904a commit 389fb80

File tree

8 files changed

+360
-220
lines changed

8 files changed

+360
-220
lines changed

include/net/cipso_ipv4.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <linux/net.h>
4141
#include <linux/skbuff.h>
4242
#include <net/netlabel.h>
43+
#include <net/request_sock.h>
4344
#include <asm/atomic.h>
4445

4546
/* known doi values */
@@ -215,6 +216,10 @@ int cipso_v4_sock_setattr(struct sock *sk,
215216
const struct netlbl_lsm_secattr *secattr);
216217
void cipso_v4_sock_delattr(struct sock *sk);
217218
int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
219+
int cipso_v4_req_setattr(struct request_sock *req,
220+
const struct cipso_v4_doi *doi_def,
221+
const struct netlbl_lsm_secattr *secattr);
222+
void cipso_v4_req_delattr(struct request_sock *req);
218223
int cipso_v4_skbuff_setattr(struct sk_buff *skb,
219224
const struct cipso_v4_doi *doi_def,
220225
const struct netlbl_lsm_secattr *secattr);
@@ -247,6 +252,18 @@ static inline int cipso_v4_sock_getattr(struct sock *sk,
247252
return -ENOSYS;
248253
}
249254

255+
static inline int cipso_v4_req_setattr(struct request_sock *req,
256+
const struct cipso_v4_doi *doi_def,
257+
const struct netlbl_lsm_secattr *secattr)
258+
{
259+
return -ENOSYS;
260+
}
261+
262+
static inline void cipso_v4_req_delattr(struct request_sock *req)
263+
{
264+
return;
265+
}
266+
250267
static inline int cipso_v4_skbuff_setattr(struct sk_buff *skb,
251268
const struct cipso_v4_doi *doi_def,
252269
const struct netlbl_lsm_secattr *secattr)

include/net/netlabel.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/in.h>
3737
#include <linux/in6.h>
3838
#include <net/netlink.h>
39+
#include <net/request_sock.h>
3940
#include <asm/atomic.h>
4041

4142
struct cipso_v4_doi;
@@ -406,13 +407,16 @@ int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
406407
*/
407408
int netlbl_enabled(void);
408409
int netlbl_sock_setattr(struct sock *sk,
410+
u16 family,
409411
const struct netlbl_lsm_secattr *secattr);
410412
void netlbl_sock_delattr(struct sock *sk);
411413
int netlbl_sock_getattr(struct sock *sk,
412414
struct netlbl_lsm_secattr *secattr);
413415
int netlbl_conn_setattr(struct sock *sk,
414416
struct sockaddr *addr,
415417
const struct netlbl_lsm_secattr *secattr);
418+
int netlbl_req_setattr(struct request_sock *req,
419+
const struct netlbl_lsm_secattr *secattr);
416420
int netlbl_skbuff_setattr(struct sk_buff *skb,
417421
u16 family,
418422
const struct netlbl_lsm_secattr *secattr);
@@ -519,7 +523,8 @@ static inline int netlbl_enabled(void)
519523
return 0;
520524
}
521525
static inline int netlbl_sock_setattr(struct sock *sk,
522-
const struct netlbl_lsm_secattr *secattr)
526+
u16 family,
527+
const struct netlbl_lsm_secattr *secattr)
523528
{
524529
return -ENOSYS;
525530
}
@@ -537,6 +542,11 @@ static inline int netlbl_conn_setattr(struct sock *sk,
537542
{
538543
return -ENOSYS;
539544
}
545+
static inline int netlbl_req_setattr(struct request_sock *req,
546+
const struct netlbl_lsm_secattr *secattr)
547+
{
548+
return -ENOSYS;
549+
}
540550
static inline int netlbl_skbuff_setattr(struct sk_buff *skb,
541551
u16 family,
542552
const struct netlbl_lsm_secattr *secattr)

net/ipv4/cipso_ipv4.c

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,23 +1942,85 @@ int cipso_v4_sock_setattr(struct sock *sk,
19421942
}
19431943

19441944
/**
1945-
* cipso_v4_sock_delattr - Delete the CIPSO option from a socket
1946-
* @sk: the socket
1945+
* cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1946+
* @req: the connection request socket
1947+
* @doi_def: the CIPSO DOI to use
1948+
* @secattr: the specific security attributes of the socket
19471949
*
19481950
* Description:
1949-
* Removes the CIPSO option from a socket, if present.
1951+
* Set the CIPSO option on the given socket using the DOI definition and
1952+
* security attributes passed to the function. Returns zero on success and
1953+
* negative values on failure.
19501954
*
19511955
*/
1952-
void cipso_v4_sock_delattr(struct sock *sk)
1956+
int cipso_v4_req_setattr(struct request_sock *req,
1957+
const struct cipso_v4_doi *doi_def,
1958+
const struct netlbl_lsm_secattr *secattr)
19531959
{
1954-
u8 hdr_delta;
1955-
struct ip_options *opt;
1956-
struct inet_sock *sk_inet;
1960+
int ret_val = -EPERM;
1961+
unsigned char *buf = NULL;
1962+
u32 buf_len;
1963+
u32 opt_len;
1964+
struct ip_options *opt = NULL;
1965+
struct inet_request_sock *req_inet;
19571966

1958-
sk_inet = inet_sk(sk);
1959-
opt = sk_inet->opt;
1960-
if (opt == NULL || opt->cipso == 0)
1961-
return;
1967+
/* We allocate the maximum CIPSO option size here so we are probably
1968+
* being a little wasteful, but it makes our life _much_ easier later
1969+
* on and after all we are only talking about 40 bytes. */
1970+
buf_len = CIPSO_V4_OPT_LEN_MAX;
1971+
buf = kmalloc(buf_len, GFP_ATOMIC);
1972+
if (buf == NULL) {
1973+
ret_val = -ENOMEM;
1974+
goto req_setattr_failure;
1975+
}
1976+
1977+
ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1978+
if (ret_val < 0)
1979+
goto req_setattr_failure;
1980+
buf_len = ret_val;
1981+
1982+
/* We can't use ip_options_get() directly because it makes a call to
1983+
* ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1984+
* we won't always have CAP_NET_RAW even though we _always_ want to
1985+
* set the IPOPT_CIPSO option. */
1986+
opt_len = (buf_len + 3) & ~3;
1987+
opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1988+
if (opt == NULL) {
1989+
ret_val = -ENOMEM;
1990+
goto req_setattr_failure;
1991+
}
1992+
memcpy(opt->__data, buf, buf_len);
1993+
opt->optlen = opt_len;
1994+
opt->cipso = sizeof(struct iphdr);
1995+
kfree(buf);
1996+
buf = NULL;
1997+
1998+
req_inet = inet_rsk(req);
1999+
opt = xchg(&req_inet->opt, opt);
2000+
kfree(opt);
2001+
2002+
return 0;
2003+
2004+
req_setattr_failure:
2005+
kfree(buf);
2006+
kfree(opt);
2007+
return ret_val;
2008+
}
2009+
2010+
/**
2011+
* cipso_v4_delopt - Delete the CIPSO option from a set of IP options
2012+
* @opt_ptr: IP option pointer
2013+
*
2014+
* Description:
2015+
* Deletes the CIPSO IP option from a set of IP options and makes the necessary
2016+
* adjustments to the IP option structure. Returns zero on success, negative
2017+
* values on failure.
2018+
*
2019+
*/
2020+
int cipso_v4_delopt(struct ip_options **opt_ptr)
2021+
{
2022+
int hdr_delta = 0;
2023+
struct ip_options *opt = *opt_ptr;
19622024

19632025
if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
19642026
u8 cipso_len;
@@ -2003,18 +2065,62 @@ void cipso_v4_sock_delattr(struct sock *sk)
20032065
} else {
20042066
/* only the cipso option was present on the socket so we can
20052067
* remove the entire option struct */
2006-
sk_inet->opt = NULL;
2068+
*opt_ptr = NULL;
20072069
hdr_delta = opt->optlen;
20082070
kfree(opt);
20092071
}
20102072

2073+
return hdr_delta;
2074+
}
2075+
2076+
/**
2077+
* cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2078+
* @sk: the socket
2079+
*
2080+
* Description:
2081+
* Removes the CIPSO option from a socket, if present.
2082+
*
2083+
*/
2084+
void cipso_v4_sock_delattr(struct sock *sk)
2085+
{
2086+
int hdr_delta;
2087+
struct ip_options *opt;
2088+
struct inet_sock *sk_inet;
2089+
2090+
sk_inet = inet_sk(sk);
2091+
opt = sk_inet->opt;
2092+
if (opt == NULL || opt->cipso == 0)
2093+
return;
2094+
2095+
hdr_delta = cipso_v4_delopt(&sk_inet->opt);
20112096
if (sk_inet->is_icsk && hdr_delta > 0) {
20122097
struct inet_connection_sock *sk_conn = inet_csk(sk);
20132098
sk_conn->icsk_ext_hdr_len -= hdr_delta;
20142099
sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
20152100
}
20162101
}
20172102

2103+
/**
2104+
* cipso_v4_req_delattr - Delete the CIPSO option from a request socket
2105+
* @reg: the request socket
2106+
*
2107+
* Description:
2108+
* Removes the CIPSO option from a request socket, if present.
2109+
*
2110+
*/
2111+
void cipso_v4_req_delattr(struct request_sock *req)
2112+
{
2113+
struct ip_options *opt;
2114+
struct inet_request_sock *req_inet;
2115+
2116+
req_inet = inet_rsk(req);
2117+
opt = req_inet->opt;
2118+
if (opt == NULL || opt->cipso == 0)
2119+
return;
2120+
2121+
cipso_v4_delopt(&req_inet->opt);
2122+
}
2123+
20182124
/**
20192125
* cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
20202126
* @cipso: the CIPSO v4 option

0 commit comments

Comments
 (0)