All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing
@ 2023-07-23  7:54 Lin Ma
  2023-07-24 22:15 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Lin Ma @ 2023-07-23  7:54 UTC (permalink / raw)
  To: davem, edumazet, kuba, pabeni, ast, martin.lau, yhs, andrii,
	void, houtao1, laoar.shao, inwardvessel, kuniyu, netdev,
	linux-kernel, bpf
  Cc: Lin Ma

The nla_for_each_nested parsing in function bpf_sk_storage_diag_alloc
does not check the length of the nested attribute. This can lead to an
out-of-attribute read and allow a malformed nlattr (e.g., length 0) to
be viewed as a 4 byte integer.

This patch adds additional check before the execution of nla_get_u32 to
make sure the attribute has enough length.

Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
Signed-off-by: Lin Ma <linma@zju.edu.cn>
---
 net/core/bpf_sk_storage.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index d4172534dfa8..6f1afbb394a6 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -511,6 +511,11 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 		if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
 			continue;
 
+		if (nla_len(nla) < sizeof(map_fd)) {
+			err = -EINVAL;
+			goto err_free;
+		}
+
 		map_fd = nla_get_u32(nla);
 		map = bpf_map_get(map_fd);
 		if (IS_ERR(map)) {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing
  2023-07-23  7:54 [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing Lin Ma
@ 2023-07-24 22:15 ` Jakub Kicinski
  2023-07-25  0:12   ` Lin Ma
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2023-07-24 22:15 UTC (permalink / raw)
  To: Lin Ma
  Cc: davem, edumazet, pabeni, ast, martin.lau, yhs, andrii, void,
	houtao1, laoar.shao, inwardvessel, kuniyu, netdev, linux-kernel,
	bpf

On Sun, 23 Jul 2023 15:54:52 +0800 Lin Ma wrote:
> diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
> index d4172534dfa8..6f1afbb394a6 100644
> --- a/net/core/bpf_sk_storage.c
> +++ b/net/core/bpf_sk_storage.c
> @@ -511,6 +511,11 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
>  		if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
>  			continue;
>  
> +		if (nla_len(nla) < sizeof(map_fd)) {
> +			err = -EINVAL;
> +			goto err_free;
> +		}

You can move this check earlier, when the attributes are getting
counted. That way we can avoid the alloc/free on error.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing
  2023-07-24 22:15 ` Jakub Kicinski
@ 2023-07-25  0:12   ` Lin Ma
  0 siblings, 0 replies; 3+ messages in thread
From: Lin Ma @ 2023-07-25  0:12 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, edumazet, pabeni, ast, martin.lau, yhs, andrii, void,
	houtao1, laoar.shao, inwardvessel, kuniyu, netdev, linux-kernel,
	bpf

Hi Jakub,

> 
> You can move this check earlier, when the attributes are getting
> counted. That way we can avoid the alloc/free on error.

Good point, will fix that and prepare another patch

Thanks
Lin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-25  0:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-23  7:54 [PATCH v1] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing Lin Ma
2023-07-24 22:15 ` Jakub Kicinski
2023-07-25  0:12   ` Lin Ma

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.