Code: static struct ip_options *tcp_v4_save_options(struct sock *sk,
					      struct sk_buff *skb)
 {
	struct ip_options *opt = &(IPCB(skb)->opt);
	struct ip_options *dopt = NULL;
 
 	if (opt && opt->optlen) {
		int opt_size = optlength(opt);
 		dopt = kmalloc(opt_size, GFP_ATOMIC);
 		if (dopt) {
			if (ip_options_echo(dopt, skb)) {
 				kfree(dopt);
 				dopt = NULL;
 			}
		}
	}
	return dopt;
}

Commit Message: inet: add RCU protection to inet->opt

We lack proper synchronization to manipulate inet->opt ip_options

Problem is ip_make_skb() calls ip_setup_cork() and
ip_setup_cork() possibly makes a copy of ipc->opt (struct ip_options),
without any protection against another thread manipulating inet->opt.

Another thread can change inet->opt pointer and free old one under us.

Use RCU to protect inet->opt (changed to inet->inet_opt).

Instead of handling atomic refcounts, just copy ip_options when
necessary, to avoid cache line dirtying.

We cant insert an rcu_head in struct ip_options since its included in
skb->cb[], so this patch is large because I had to introduce a new
ip_options_rcu structure.

Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
CWE ID: CWE-362
static struct ip_options *tcp_v4_save_options(struct sock *sk,
static struct ip_options_rcu *tcp_v4_save_options(struct sock *sk,
						  struct sk_buff *skb)
 {
	const struct ip_options *opt = &(IPCB(skb)->opt);
	struct ip_options_rcu *dopt = NULL;
 
 	if (opt && opt->optlen) {
		int opt_size = sizeof(*dopt) + opt->optlen;

 		dopt = kmalloc(opt_size, GFP_ATOMIC);
 		if (dopt) {
			if (ip_options_echo(&dopt->opt, skb)) {
 				kfree(dopt);
 				dopt = NULL;
 			}
		}
	}
	return dopt;
}
