Skip to content

Commit e1adea9

Browse files
hdmdaviespcmoore
authored andcommitted
calipso: Allow request sockets to be relabelled by the lsm.
Request sockets need to have a label that takes into account the incoming connection as well as their parent's label. This is used for the outgoing SYN-ACK and for their child full-socket. Signed-off-by: Huw Davies <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 56ac42b commit e1adea9

File tree

6 files changed

+163
-8
lines changed

6 files changed

+163
-8
lines changed

include/net/netlabel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ struct netlbl_lsm_secattr {
229229
* @sock_getattr: retrieve the socket's attr
230230
* @sock_setattr: set the socket's attr
231231
* @sock_delattr: remove the socket's attr
232+
* @req_setattr: set the req socket's attr
233+
* @req_delattr: remove the req socket's attr
232234
*
233235
* Description:
234236
* This structure is filled out by the CALIPSO engine and passed
@@ -252,6 +254,10 @@ struct netlbl_calipso_ops {
252254
const struct calipso_doi *doi_def,
253255
const struct netlbl_lsm_secattr *secattr);
254256
void (*sock_delattr)(struct sock *sk);
257+
int (*req_setattr)(struct request_sock *req,
258+
const struct calipso_doi *doi_def,
259+
const struct netlbl_lsm_secattr *secattr);
260+
void (*req_delattr)(struct request_sock *req);
255261
};
256262

257263
/*

net/ipv6/calipso.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,90 @@ static void calipso_sock_delattr(struct sock *sk)
889889
txopt_put(txopts);
890890
}
891891

892+
/* request sock functions.
893+
*/
894+
895+
/**
896+
* calipso_req_setattr - Add a CALIPSO option to a connection request socket
897+
* @req: the connection request socket
898+
* @doi_def: the CALIPSO DOI to use
899+
* @secattr: the specific security attributes of the socket
900+
*
901+
* Description:
902+
* Set the CALIPSO option on the given socket using the DOI definition and
903+
* security attributes passed to the function. Returns zero on success and
904+
* negative values on failure.
905+
*
906+
*/
907+
static int calipso_req_setattr(struct request_sock *req,
908+
const struct calipso_doi *doi_def,
909+
const struct netlbl_lsm_secattr *secattr)
910+
{
911+
struct ipv6_txoptions *txopts;
912+
struct inet_request_sock *req_inet = inet_rsk(req);
913+
struct ipv6_opt_hdr *old, *new;
914+
struct sock *sk = sk_to_full_sk(req_to_sk(req));
915+
916+
if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
917+
old = req_inet->ipv6_opt->hopopt;
918+
else
919+
old = NULL;
920+
921+
new = calipso_opt_insert(old, doi_def, secattr);
922+
if (IS_ERR(new))
923+
return PTR_ERR(new);
924+
925+
txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
926+
new, new ? ipv6_optlen(new) : 0);
927+
928+
kfree(new);
929+
930+
if (IS_ERR(txopts))
931+
return PTR_ERR(txopts);
932+
933+
txopts = xchg(&req_inet->ipv6_opt, txopts);
934+
if (txopts) {
935+
atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
936+
txopt_put(txopts);
937+
}
938+
939+
return 0;
940+
}
941+
942+
/**
943+
* calipso_req_delattr - Delete the CALIPSO option from a request socket
944+
* @reg: the request socket
945+
*
946+
* Description:
947+
* Removes the CALIPSO option from a request socket, if present.
948+
*
949+
*/
950+
static void calipso_req_delattr(struct request_sock *req)
951+
{
952+
struct inet_request_sock *req_inet = inet_rsk(req);
953+
struct ipv6_opt_hdr *new;
954+
struct ipv6_txoptions *txopts;
955+
struct sock *sk = sk_to_full_sk(req_to_sk(req));
956+
957+
if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
958+
return;
959+
960+
if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
961+
return; /* Nothing to do */
962+
963+
txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
964+
new, new ? ipv6_optlen(new) : 0);
965+
966+
if (!IS_ERR(txopts)) {
967+
txopts = xchg(&req_inet->ipv6_opt, txopts);
968+
if (txopts) {
969+
atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
970+
txopt_put(txopts);
971+
}
972+
}
973+
kfree(new);
974+
}
975+
892976
static const struct netlbl_calipso_ops ops = {
893977
.doi_add = calipso_doi_add,
894978
.doi_free = calipso_doi_free,
@@ -899,6 +983,8 @@ static const struct netlbl_calipso_ops ops = {
899983
.sock_getattr = calipso_sock_getattr,
900984
.sock_setattr = calipso_sock_setattr,
901985
.sock_delattr = calipso_sock_delattr,
986+
.req_setattr = calipso_req_setattr,
987+
.req_delattr = calipso_req_delattr,
902988
};
903989

904990
/**

net/netlabel/netlabel_calipso.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,43 @@ void calipso_sock_delattr(struct sock *sk)
578578
if (ops)
579579
ops->sock_delattr(sk);
580580
}
581+
582+
/**
583+
* calipso_req_setattr - Add a CALIPSO option to a connection request socket
584+
* @req: the connection request socket
585+
* @doi_def: the CALIPSO DOI to use
586+
* @secattr: the specific security attributes of the socket
587+
*
588+
* Description:
589+
* Set the CALIPSO option on the given socket using the DOI definition and
590+
* security attributes passed to the function. Returns zero on success and
591+
* negative values on failure.
592+
*
593+
*/
594+
int calipso_req_setattr(struct request_sock *req,
595+
const struct calipso_doi *doi_def,
596+
const struct netlbl_lsm_secattr *secattr)
597+
{
598+
int ret_val = -ENOMSG;
599+
const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
600+
601+
if (ops)
602+
ret_val = ops->req_setattr(req, doi_def, secattr);
603+
return ret_val;
604+
}
605+
606+
/**
607+
* calipso_req_delattr - Delete the CALIPSO option from a request socket
608+
* @reg: the request socket
609+
*
610+
* Description:
611+
* Removes the CALIPSO option from a request socket, if present.
612+
*
613+
*/
614+
void calipso_req_delattr(struct request_sock *req)
615+
{
616+
const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
617+
618+
if (ops)
619+
ops->req_delattr(req);
620+
}

net/netlabel/netlabel_calipso.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,9 @@ int calipso_sock_setattr(struct sock *sk,
133133
const struct calipso_doi *doi_def,
134134
const struct netlbl_lsm_secattr *secattr);
135135
void calipso_sock_delattr(struct sock *sk);
136+
int calipso_req_setattr(struct request_sock *req,
137+
const struct calipso_doi *doi_def,
138+
const struct netlbl_lsm_secattr *secattr);
139+
void calipso_req_delattr(struct request_sock *req);
136140

137141
#endif

net/netlabel/netlabel_kapi.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,12 +1053,13 @@ int netlbl_req_setattr(struct request_sock *req,
10531053
{
10541054
int ret_val;
10551055
struct netlbl_dommap_def *entry;
1056+
struct inet_request_sock *ireq = inet_rsk(req);
10561057

10571058
rcu_read_lock();
10581059
switch (req->rsk_ops->family) {
10591060
case AF_INET:
10601061
entry = netlbl_domhsh_getentry_af4(secattr->domain,
1061-
inet_rsk(req)->ir_rmt_addr);
1062+
ireq->ir_rmt_addr);
10621063
if (entry == NULL) {
10631064
ret_val = -ENOENT;
10641065
goto req_setattr_return;
@@ -1069,9 +1070,7 @@ int netlbl_req_setattr(struct request_sock *req,
10691070
entry->cipso, secattr);
10701071
break;
10711072
case NETLBL_NLTYPE_UNLABELED:
1072-
/* just delete the protocols we support for right now
1073-
* but we could remove other protocols if needed */
1074-
cipso_v4_req_delattr(req);
1073+
netlbl_req_delattr(req);
10751074
ret_val = 0;
10761075
break;
10771076
default:
@@ -1080,9 +1079,24 @@ int netlbl_req_setattr(struct request_sock *req,
10801079
break;
10811080
#if IS_ENABLED(CONFIG_IPV6)
10821081
case AF_INET6:
1083-
/* since we don't support any IPv6 labeling protocols right
1084-
* now we can optimize everything away until we do */
1085-
ret_val = 0;
1082+
entry = netlbl_domhsh_getentry_af6(secattr->domain,
1083+
&ireq->ir_v6_rmt_addr);
1084+
if (entry == NULL) {
1085+
ret_val = -ENOENT;
1086+
goto req_setattr_return;
1087+
}
1088+
switch (entry->type) {
1089+
case NETLBL_NLTYPE_CALIPSO:
1090+
ret_val = calipso_req_setattr(req,
1091+
entry->calipso, secattr);
1092+
break;
1093+
case NETLBL_NLTYPE_UNLABELED:
1094+
netlbl_req_delattr(req);
1095+
ret_val = 0;
1096+
break;
1097+
default:
1098+
ret_val = -ENOENT;
1099+
}
10861100
break;
10871101
#endif /* IPv6 */
10881102
default:
@@ -1108,6 +1122,11 @@ void netlbl_req_delattr(struct request_sock *req)
11081122
case AF_INET:
11091123
cipso_v4_req_delattr(req);
11101124
break;
1125+
#if IS_ENABLED(CONFIG_IPV6)
1126+
case AF_INET6:
1127+
calipso_req_delattr(req);
1128+
break;
1129+
#endif /* IPv6 */
11111130
}
11121131
}
11131132

security/selinux/netlabel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
284284
int rc;
285285
struct netlbl_lsm_secattr secattr;
286286

287-
if (family != PF_INET)
287+
if (family != PF_INET && family != PF_INET6)
288288
return 0;
289289

290290
netlbl_secattr_init(&secattr);

0 commit comments

Comments
 (0)