Unverified Commit ebadb285 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!12715 MPTCP Upstream part 14

Merge Pull Request from: @geliangtang 
 
mptcp: check the protocol with DEBUG_NET

Recently, Paolo fixed a bug where a TCP-specific helper was used with an
MPTCP socket [1]. The bug was not detected by fuzzer or static analysis.

Following this, it has been suggested to add a check, only in debug
mode. This is what this series is doing.

The series has been split to be upstreamed: a preparation patch for
MPTCP, the modification for TCP, then for MPTCP. It is not clear if it
would be OK to add that upstream. If not, we can squash these three
patches in "DO-NOT-MERGE: mptcp: improve code coverage for CI" commit we
have in our export tree.

Note that the MPTCP Token kUnit test needs to be adapted for this new
check. This is what is done in patch 1/3. 
 
Link:https://gitee.com/openeuler/kernel/pulls/12715

 

Reviewed-by: default avatarYue Haibing <yuehaibing@huawei.com>
Signed-off-by: default avatarZhang Peng <zhangpeng362@huawei.com>
parents 90d2f3a5 733f531d
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -351,7 +351,23 @@ static inline void msk_owned_by_me(const struct mptcp_sock *msk)
	sock_owned_by_me((const struct sock *)msk);
}

#ifdef CONFIG_DEBUG_NET
/* MPTCP-specific: we might (indirectly) call this helper with the wrong sk */
#undef tcp_sk
#define tcp_sk(ptr) ({								\
	typeof(ptr) _ptr = (ptr);						\
	WARN_ON(_ptr->sk_protocol != IPPROTO_TCP);				\
	container_of_const(_ptr, struct tcp_sock, inet_conn.icsk_inet.sk);	\
})
#define mptcp_sk(ptr) ({						\
	typeof(ptr) _ptr = (ptr);					\
	WARN_ON(_ptr->sk_protocol != IPPROTO_MPTCP);			\
	container_of_const(_ptr, struct mptcp_sock, sk.icsk_inet.sk);	\
})

#else /* !CONFIG_DEBUG_NET */
#define mptcp_sk(ptr) container_of_const(ptr, struct mptcp_sock, sk.icsk_inet.sk)
#endif

/* the msk socket don't use the backlog, also account for the bulk
 * free memory
+6 −1
Original line number Diff line number Diff line
@@ -52,14 +52,19 @@ static struct mptcp_subflow_context *build_ctx(struct kunit *test)
static struct mptcp_sock *build_msk(struct kunit *test)
{
	struct mptcp_sock *msk;
	struct sock *sk;

	msk = kunit_kzalloc(test, sizeof(struct mptcp_sock), GFP_USER);
	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, msk);
	refcount_set(&((struct sock *)msk)->sk_refcnt, 1);
	sock_net_set((struct sock *)msk, &init_net);

	sk = (struct sock *)msk;

	/* be sure the token helpers can dereference sk->sk_prot */
	((struct sock *)msk)->sk_prot = &tcp_prot;
	sk->sk_prot = &tcp_prot;
	sk->sk_protocol = IPPROTO_MPTCP;

	return msk;
}