mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2025-08-22 00:42:01 +02:00

As reported by Byeonguk, the bad_words test in verifier_bits_iter.c
occasionally fails on s390 host. Quoting Ilya's explanation:
s390 kernel runs in a completely separate address space, there is no
user/kernel split at TASK_SIZE. The same address may be valid in both
the kernel and the user address spaces, there is no way to tell by
looking at it. The config option related to this property is
ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE.
Also, unfortunately, 0 is a valid address in the s390 kernel address
space.
Fix the issue by using -4095 as the bad address for bits iterator, as
suggested by Ilya. Verify that bpf_iter_bits_new() returns -EINVAL for
NULL address and -EFAULT for bad address.
Fixes: ebafc1e535
("selftests/bpf: Add three test cases for bits_iter")
Reported-by: Byeonguk Jeong <jungbu2855@gmail.com>
Closes: https://lore.kernel.org/bpf/ZycSXwjH4UTvx-Cn@ub22/
Signed-off-by: Hou Tao <houtao1@huawei.com>
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20241105043057.3371482-1-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
233 lines
4.2 KiB
C
233 lines
4.2 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/* Copyright (c) 2024 Yafang Shao <laoar.shao@gmail.com> */
|
|
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
#include "bpf_misc.h"
|
|
#include "task_kfunc_common.h"
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign,
|
|
u32 nr_bits) __ksym __weak;
|
|
int *bpf_iter_bits_next(struct bpf_iter_bits *it) __ksym __weak;
|
|
void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym __weak;
|
|
|
|
u64 bits_array[511] = {};
|
|
|
|
SEC("iter.s/cgroup")
|
|
__description("bits iter without destroy")
|
|
__failure __msg("Unreleased reference")
|
|
int BPF_PROG(no_destroy, struct bpf_iter_meta *meta, struct cgroup *cgrp)
|
|
{
|
|
struct bpf_iter_bits it;
|
|
u64 data = 1;
|
|
|
|
bpf_iter_bits_new(&it, &data, 1);
|
|
bpf_iter_bits_next(&it);
|
|
return 0;
|
|
}
|
|
|
|
SEC("iter/cgroup")
|
|
__description("uninitialized iter in ->next()")
|
|
__failure __msg("expected an initialized iter_bits as arg #1")
|
|
int BPF_PROG(next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp)
|
|
{
|
|
struct bpf_iter_bits *it = NULL;
|
|
|
|
bpf_iter_bits_next(it);
|
|
return 0;
|
|
}
|
|
|
|
SEC("iter/cgroup")
|
|
__description("uninitialized iter in ->destroy()")
|
|
__failure __msg("expected an initialized iter_bits as arg #1")
|
|
int BPF_PROG(destroy_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp)
|
|
{
|
|
struct bpf_iter_bits it = {};
|
|
|
|
bpf_iter_bits_destroy(&it);
|
|
return 0;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("null pointer")
|
|
__success __retval(0)
|
|
int null_pointer(void)
|
|
{
|
|
struct bpf_iter_bits iter;
|
|
int err, nr = 0;
|
|
int *bit;
|
|
|
|
err = bpf_iter_bits_new(&iter, NULL, 1);
|
|
bpf_iter_bits_destroy(&iter);
|
|
if (err != -EINVAL)
|
|
return 1;
|
|
|
|
bpf_for_each(bits, bit, NULL, 1)
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("bits copy")
|
|
__success __retval(10)
|
|
int bits_copy(void)
|
|
{
|
|
u64 data = 0xf7310UL; /* 4 + 3 + 2 + 1 + 0*/
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
bpf_for_each(bits, bit, &data, 1)
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("bits memalloc")
|
|
__success __retval(64)
|
|
int bits_memalloc(void)
|
|
{
|
|
u64 data[2];
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
__builtin_memset(&data, 0xf0, sizeof(data)); /* 4 * 16 */
|
|
bpf_for_each(bits, bit, &data[0], ARRAY_SIZE(data))
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("bit index")
|
|
__success __retval(8)
|
|
int bit_index(void)
|
|
{
|
|
u64 data = 0x100;
|
|
int bit_idx = 0;
|
|
int *bit;
|
|
|
|
bpf_for_each(bits, bit, &data, 1) {
|
|
if (*bit == 0)
|
|
continue;
|
|
bit_idx = *bit;
|
|
}
|
|
return bit_idx;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("bits too big")
|
|
__success __retval(0)
|
|
int bits_too_big(void)
|
|
{
|
|
u64 data[4];
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
__builtin_memset(&data, 0xff, sizeof(data));
|
|
bpf_for_each(bits, bit, &data[0], 512) /* Be greater than 511 */
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("fewer words")
|
|
__success __retval(1)
|
|
int fewer_words(void)
|
|
{
|
|
u64 data[2] = {0x1, 0xff};
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
bpf_for_each(bits, bit, &data[0], 1)
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("zero words")
|
|
__success __retval(0)
|
|
int zero_words(void)
|
|
{
|
|
u64 data[2] = {0x1, 0xff};
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
bpf_for_each(bits, bit, &data[0], 0)
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("huge words")
|
|
__success __retval(0)
|
|
int huge_words(void)
|
|
{
|
|
u64 data[8] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1};
|
|
int nr = 0;
|
|
int *bit;
|
|
|
|
bpf_for_each(bits, bit, &data[0], 67108865)
|
|
nr++;
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("max words")
|
|
__success __retval(4)
|
|
int max_words(void)
|
|
{
|
|
volatile int nr = 0;
|
|
int *bit;
|
|
|
|
bits_array[0] = (1ULL << 63) | 1U;
|
|
bits_array[510] = (1ULL << 33) | (1ULL << 32);
|
|
|
|
bpf_for_each(bits, bit, bits_array, 511) {
|
|
if (nr == 0 && *bit != 0)
|
|
break;
|
|
if (nr == 2 && *bit != 32672)
|
|
break;
|
|
nr++;
|
|
}
|
|
return nr;
|
|
}
|
|
|
|
SEC("syscall")
|
|
__description("bad words")
|
|
__success __retval(0)
|
|
int bad_words(void)
|
|
{
|
|
void *bad_addr = (void *)-4095;
|
|
struct bpf_iter_bits iter;
|
|
volatile int nr;
|
|
int *bit;
|
|
int err;
|
|
|
|
err = bpf_iter_bits_new(&iter, bad_addr, 1);
|
|
bpf_iter_bits_destroy(&iter);
|
|
if (err != -EFAULT)
|
|
return 1;
|
|
|
|
nr = 0;
|
|
bpf_for_each(bits, bit, bad_addr, 1)
|
|
nr++;
|
|
if (nr != 0)
|
|
return 2;
|
|
|
|
err = bpf_iter_bits_new(&iter, bad_addr, 4);
|
|
bpf_iter_bits_destroy(&iter);
|
|
if (err != -EFAULT)
|
|
return 3;
|
|
|
|
nr = 0;
|
|
bpf_for_each(bits, bit, bad_addr, 4)
|
|
nr++;
|
|
if (nr != 0)
|
|
return 4;
|
|
|
|
return 0;
|
|
}
|