diff mbox series

[bpf,1/2] bpf: Do not copy spin lock field from user in bpf_selem_alloc

Message ID 20221114134720.1057939-2-xukuohai@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Bug fix and test case for special map value field | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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: 2 this patch: 2
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-17 fail Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-22 fail Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for test_maps on s390x with gcc

Commit Message

Xu Kuohai Nov. 14, 2022, 1:47 p.m. UTC
bpf_selem_alloc function is used by inode_storage, sk_storage and
task_storage maps to set map value, for these map types, there may
be a spin lock in the map value, so if we use memcpy to copy the whole
map value from user, the spin lock field may be initialized incorrectly.

Since the spin lock field is zeroed by kzalloc, call copy_map_value
instead of memcpy to skip copying the spin lock field to fix it.

Fixes: 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
 kernel/bpf/bpf_local_storage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Alexei Starovoitov Nov. 16, 2022, 5:27 a.m. UTC | #1
On Mon, Nov 14, 2022 at 5:31 AM Xu Kuohai <xukuohai@huawei.com> wrote:
>
> bpf_selem_alloc function is used by inode_storage, sk_storage and
> task_storage maps to set map value, for these map types, there may
> be a spin lock in the map value, so if we use memcpy to copy the whole
> map value from user, the spin lock field may be initialized incorrectly.
>
> Since the spin lock field is zeroed by kzalloc, call copy_map_value
> instead of memcpy to skip copying the spin lock field to fix it.
>
> Fixes: 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")

The tag is wrong. When local storage was introduced it was not
possible to use spin_locks there.
Pls resubmit.
Xu Kuohai Nov. 16, 2022, 8:07 a.m. UTC | #2
On 11/16/2022 1:27 PM, Alexei Starovoitov wrote:
> On Mon, Nov 14, 2022 at 5:31 AM Xu Kuohai <xukuohai@huawei.com> wrote:
>>
>> bpf_selem_alloc function is used by inode_storage, sk_storage and
>> task_storage maps to set map value, for these map types, there may
>> be a spin lock in the map value, so if we use memcpy to copy the whole
>> map value from user, the spin lock field may be initialized incorrectly.
>>
>> Since the spin lock field is zeroed by kzalloc, call copy_map_value
>> instead of memcpy to skip copying the spin lock field to fix it.
>>
>> Fixes: 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
> 
> The tag is wrong. When local storage was introduced it was not
> possible to use spin_locks there.
> Pls resubmit.
> .

No, spin_lock was introduced by d83525ca62cf ("bpf: introduce bpf_spin_lock"),
before 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage").

To confirm this, I built a kernel image on comit 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
and run test case posted in patch 2, a softlockup was triggered. Then I picked
this patch and tried again, nothing failed.
Xu Kuohai Nov. 21, 2022, 11:30 a.m. UTC | #3
On 11/16/2022 4:07 PM, Xu Kuohai wrote:
> On 11/16/2022 1:27 PM, Alexei Starovoitov wrote:
>> On Mon, Nov 14, 2022 at 5:31 AM Xu Kuohai <xukuohai@huawei.com> wrote:
>>>
>>> bpf_selem_alloc function is used by inode_storage, sk_storage and
>>> task_storage maps to set map value, for these map types, there may
>>> be a spin lock in the map value, so if we use memcpy to copy the whole
>>> map value from user, the spin lock field may be initialized incorrectly.
>>>
>>> Since the spin lock field is zeroed by kzalloc, call copy_map_value
>>> instead of memcpy to skip copying the spin lock field to fix it.
>>>
>>> Fixes: 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
>>
>> The tag is wrong. When local storage was introduced it was not
>> possible to use spin_locks there.
>> Pls resubmit.
>> .
> 
> No, spin_lock was introduced by d83525ca62cf ("bpf: introduce bpf_spin_lock"),
> before 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage").
> 
> To confirm this, I built a kernel image on comit 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
> and run test case posted in patch 2, a softlockup was triggered. Then I picked
> this patch and tried again, nothing failed.

Hello, am I right? Or could you please give the correct fix-tag? Thanks.
Alexei Starovoitov Nov. 21, 2022, 7:50 p.m. UTC | #4
On Mon, Nov 21, 2022 at 3:30 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>
> On 11/16/2022 4:07 PM, Xu Kuohai wrote:
> > On 11/16/2022 1:27 PM, Alexei Starovoitov wrote:
> >> On Mon, Nov 14, 2022 at 5:31 AM Xu Kuohai <xukuohai@huawei.com> wrote:
> >>>
> >>> bpf_selem_alloc function is used by inode_storage, sk_storage and
> >>> task_storage maps to set map value, for these map types, there may
> >>> be a spin lock in the map value, so if we use memcpy to copy the whole
> >>> map value from user, the spin lock field may be initialized incorrectly.
> >>>
> >>> Since the spin lock field is zeroed by kzalloc, call copy_map_value
> >>> instead of memcpy to skip copying the spin lock field to fix it.
> >>>
> >>> Fixes: 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
> >>
> >> The tag is wrong. When local storage was introduced it was not
> >> possible to use spin_locks there.
> >> Pls resubmit.
> >> .
> >
> > No, spin_lock was introduced by d83525ca62cf ("bpf: introduce bpf_spin_lock"),
> > before 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage").
> >
> > To confirm this, I built a kernel image on comit 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")
> > and run test case posted in patch 2, a softlockup was triggered. Then I picked
> > this patch and tried again, nothing failed.
>
> Hello, am I right? Or could you please give the correct fix-tag? Thanks.

I see. I was under the impression that bpf_spin_lock was enabled
in the local storage later.
Ok. Applied as-is.
diff mbox series

Patch

diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 802fc15b0d73..f27fa5ba7d72 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -74,7 +74,7 @@  bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
 				gfp_flags | __GFP_NOWARN);
 	if (selem) {
 		if (value)
-			memcpy(SDATA(selem)->data, value, smap->map.value_size);
+			copy_map_value(&smap->map, SDATA(selem)->data, value);
 		return selem;
 	}