diff mbox series

[v2] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing

Message ID 20230725023330.422856-1-linma@zju.edu.cn (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [v2] bpf: Add length check for SK_DIAG_BPF_STORAGE_REQ_MAP_FD parsing | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1347 this patch: 1347
netdev/cc_maintainers success CCed 11 of 11 maintainers
netdev/build_clang success Errors and warnings before: 1365 this patch: 1365
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1370 this patch: 1370
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix

Commit Message

Lin Ma July 25, 2023, 2:33 a.m. UTC
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 an additional check when the nlattr is getting counted.
This makes sure the latter nla_get_u32 can access the attributes with
the correct length.

Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Lin Ma <linma@zju.edu.cn>
---
V1 -> V2: moves the check to the counting loop as Jakub suggested,
          alters the commit message accordingly.

 net/core/bpf_sk_storage.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Leon Romanovsky July 25, 2023, 4:44 a.m. UTC | #1
On Tue, Jul 25, 2023 at 10:33:30AM +0800, Lin Ma wrote:
> 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 an additional check when the nlattr is getting counted.
> This makes sure the latter nla_get_u32 can access the attributes with
> the correct length.
> 
> Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Lin Ma <linma@zju.edu.cn>
> ---
> V1 -> V2: moves the check to the counting loop as Jakub suggested,
>           alters the commit message accordingly.
> 
>  net/core/bpf_sk_storage.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
> index d4172534dfa8..cca7594be92e 100644
> --- a/net/core/bpf_sk_storage.c
> +++ b/net/core/bpf_sk_storage.c
> @@ -496,8 +496,11 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
>  		return ERR_PTR(-EPERM);
>  
>  	nla_for_each_nested(nla, nla_stgs, rem) {
> -		if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
> +		if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD) {
> +			if (nla_len(nla) != sizeof(u32))

Jakub, it seems like Lin adds this check to all nla_for_each_nested() loops.
IMHO, the better change will be to change nla_for_each_nested() skip empty/not valid NLAs.

Thanks

> +				return ERR_PTR(-EINVAL);
>  			nr_maps++;
> +		}
>  	}
>  
>  	diag = kzalloc(struct_size(diag, maps, nr_maps), GFP_KERNEL);
> -- 
> 2.17.1
> 
>
Lin Ma July 25, 2023, 5:24 a.m. UTC | #2
Hello Leon,

> 
> Jakub, it seems like Lin adds this check to all nla_for_each_nested() loops.
> IMHO, the better change will be to change nla_for_each_nested() skip empty/not valid NLAs.
> 
> Thanks

I guess you just get these fixes misunderstood. I do not add the nla_len check
to  **all nla_for_each_nested** :(. I only add checks to those who do not access 
the attributes without verifying the length, which is buggy.

The others, either do a similar nla_len check already or just do nla_validate
somewhere else. That is to say, they **validate** the relevant attributes.

In short, nla_for_each_nested is just a loop macro that iterates the nlattrs,
like nla_for_each macro. It is weird for them to do nlattr validation as there
could have already been a call to nla_validate to ensure those attributes are
correct. That is, for those who do not, a simple nla_len check is the simplest
and most efficient choice.

Regards
Lin
Leon Romanovsky July 25, 2023, 5:54 a.m. UTC | #3
On Tue, Jul 25, 2023 at 01:24:38PM +0800, Lin Ma wrote:
> Hello Leon,
> 
> > 
> > Jakub, it seems like Lin adds this check to all nla_for_each_nested() loops.
> > IMHO, the better change will be to change nla_for_each_nested() skip empty/not valid NLAs.
> > 
> > Thanks
> 
> I guess you just get these fixes misunderstood. I do not add the nla_len check
> to  **all nla_for_each_nested** :(. I only add checks to those who do not access 
> the attributes without verifying the length, which is buggy.
> 
> The others, either do a similar nla_len check already or just do nla_validate
> somewhere else. That is to say, they **validate** the relevant attributes.
> 
> In short, nla_for_each_nested is just a loop macro that iterates the nlattrs,
> like nla_for_each macro. It is weird for them to do nlattr validation as there
> could have already been a call to nla_validate to ensure those attributes are
> correct. That is, for those who do not, a simple nla_len check is the simplest
> and most efficient choice.

My concern is related to maintainability in long run. Your check adds
another layer of cabal knowledge which will be copied/pasted in other
places.

Thanks

> 
> Regards
> Lin
Lin Ma July 25, 2023, 6:05 a.m. UTC | #4
Hello Leon,

> 
> My concern is related to maintainability in long run. Your check adds
> another layer of cabal knowledge which will be copied/pasted in other
> places.
> 
> Thanks
> 

Yeah, I guess you are right. I guess I should not just *fix* this issue
but also think of the maintainability. The very first idea pop into my
mind is to complete the necessary nla_policy hence the invalid nlattrs
could be rejected at the very first place.

Will spend more time on this.

Regards
Lin
Jakub Kicinski July 26, 2023, 3:11 a.m. UTC | #5
On Tue, 25 Jul 2023 10:33:30 +0800 Lin Ma wrote:
> 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 an additional check when the nlattr is getting counted.
> This makes sure the latter nla_get_u32 can access the attributes with
> the correct length.
> 
> Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Lin Ma <linma@zju.edu.cn>

Reviewed-by: Jakub Kicinski <kuba@kernel.org>

Those who parse manually must do checks manually. It is what it is.
Paolo Abeni July 27, 2023, 7:34 a.m. UTC | #6
On Tue, 2023-07-25 at 10:33 +0800, Lin Ma wrote:
> 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 an additional check when the nlattr is getting counted.
> This makes sure the latter nla_get_u32 can access the attributes with
> the correct length.
> 
> Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Lin Ma <linma@zju.edu.cn>

I guess this should go via the ebpf tree, right? Setting the delegate
accordingly. 

Please correct me if I'm wrong.

Thanks!

/P
Martin KaFai Lau July 28, 2023, 11:57 p.m. UTC | #7
On 7/27/23 12:34 AM, Paolo Abeni wrote:
> On Tue, 2023-07-25 at 10:33 +0800, Lin Ma wrote:
>> 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 an additional check when the nlattr is getting counted.
>> This makes sure the latter nla_get_u32 can access the attributes with
>> the correct length.
>>
>> Fixes: 1ed4d92458a9 ("bpf: INET_DIAG support in bpf_sk_storage")
>> Suggested-by: Jakub Kicinski <kuba@kernel.org>
>> Signed-off-by: Lin Ma <linma@zju.edu.cn>
> 
> I guess this should go via the ebpf tree, right? Setting the delegate
> accordingly.

Already applied to the bpf tree. Thanks.
pw-bot seems not doing auto-reply for the bpf tree.
diff mbox series

Patch

diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index d4172534dfa8..cca7594be92e 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -496,8 +496,11 @@  bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 		return ERR_PTR(-EPERM);
 
 	nla_for_each_nested(nla, nla_stgs, rem) {
-		if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD)
+		if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD) {
+			if (nla_len(nla) != sizeof(u32))
+				return ERR_PTR(-EINVAL);
 			nr_maps++;
+		}
 	}
 
 	diag = kzalloc(struct_size(diag, maps, nr_maps), GFP_KERNEL);