cipso_v4_get_actual_opt_len() walks a compacted IP option buffer with no validation of the option it is standing on: it reads data[iter + 1] without checking that the length byte is inside the buffer, it accepts a zero length byte (spinning forever), and it never clamps the walked offset to len, so the returned length can exceed the 40-byte MAX_IPOPTLEN bound that every caller assumes. After cipso_v4_delopt() memmove-compacts a socket's options, the bytes past the compacted region are stale and attacker-controlled via setsockopt(IP_OPTIONS). An unprivileged user (CAP_NET_RAW in a user namespace, CIPSO labeling configured) can trigger the walk with connect(AF_UNSPEC), which reaches cipso_v4_sock_delattr() through the SELinux NetLabel hook. The poisoned optlen is later used in do_ip_getsockopt(), where it causes * an out-of-bounds read of the adjacent heap object copied back to userspace (kernel pointer disclosure, KASLR defeat), and * a stack buffer overflow when optlen exceeds the 56-byte scratch buffer (return-address overwrite; exploitable for privilege escalation on kernels built without stack protector). The same helper is also called from cipso_v4_skbuff_delattr() when a label is removed from output or forwarded packets; the option bytes there have already been validated by ip_options_compile(), so the new checks are defensive for that caller. Fix the defects by making the walk fail with -EINVAL as soon as an option header is truncated or malformed (length byte missing, zero, or one) or an option would run past len. cipso_v4_delopt() then drops the socket's entire option set (reusing the existing branch that frees the ip_options_rcu when CIPSO was the only option), and cipso_v4_skbuff_delattr() leaves a malformed packet unchanged instead of rewriting its header based on untrustworthy bytes. Well-formed option streams are unaffected: a socket whose options survive the compaction intact keeps them; only malformed streams now lose all options instead of carrying a partially parsed set. Fixes: 014ab19a69c3 ("selinux: Set socket NetLabel based on connection endpoint") Signed-off-by: Lucas Dupré--Rabinovici --- net/ipv4/cipso_ipv4.c | 22 +++++++++++++++-------- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c --- a/net/ipv4/cipso_ipv4.c +++ b/net/ipv4/cipso_ipv4.c @@ -1824,7 +1824,11 @@ } else if (data[iter] == IPOPT_NOP) { iter++; } else { + if (iter + 1 >= len || data[iter + 1] < 2) + return -EINVAL; iter += data[iter + 1]; + if (iter > len) + return -EINVAL; optlen = iter; } } @@ -2028,6 +2032,13 @@ optlen_new = cipso_v4_get_actual_opt_len(opt->opt.__data, opt->opt.optlen); + if (optlen_new < 0) { + /* malformed option stream, drop all the options */ + *opt_ptr = NULL; + hdr_delta = opt->opt.optlen; + kfree_rcu(opt, rcu); + return hdr_delta; + } hdr_delta = opt->opt.optlen; opt->opt.optlen = (optlen_new + 3) & ~3; hdr_delta -= opt->opt.optlen; @@ -2249,7 +2260,7 @@ int cipso_v4_skbuff_delattr(struct sk_buff *skb) { int ret_val, cipso_len, hdr_len_actual, new_hdr_len_actual, new_hdr_len, - hdr_len_delta; + hdr_len_delta, optlen; struct iphdr *iph; struct ip_options *opt = &IPCB(skb)->opt; unsigned char *cipso_ptr; @@ -2266,9 +2277,12 @@ cipso_ptr = (unsigned char *)iph + opt->cipso; cipso_len = cipso_ptr[1]; - hdr_len_actual = sizeof(struct iphdr) + - cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1), - opt->optlen); + optlen = cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1), + opt->optlen); + /* malformed option stream, leave the packet unchanged */ + if (optlen < 0) + return -EINVAL; + hdr_len_actual = sizeof(struct iphdr) + optlen; new_hdr_len_actual = hdr_len_actual - cipso_len; new_hdr_len = (new_hdr_len_actual + 3) & ~3; hdr_len_delta = (iph->ihl << 2) - new_hdr_len;