lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Tue, 19 Oct 2021 18:22:24 -0700
From:   Martin KaFai Lau <kafai@...com>
To:     Hou Tao <houtao1@...wei.com>
CC:     Alexei Starovoitov <ast@...nel.org>, Yonghong Song <yhs@...com>,
        Daniel Borkmann <daniel@...earbox.net>,
        Andrii Nakryiko <andrii@...nel.org>, <netdev@...r.kernel.org>,
        <bpf@...r.kernel.org>
Subject: Re: [PATCH bpf-next v2 3/5] bpf: add dummy BPF STRUCT_OPS for test
 purpose

On Sat, Oct 16, 2021 at 08:48:04PM +0800, Hou Tao wrote:
> +static struct bpf_dummy_ops_test_args *
> +dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
> +{
> +	__u32 size_in;
> +	struct bpf_dummy_ops_test_args *args;
> +	void __user *ctx_in;
> +	void __user *u_state;
> +
> +	if (!nr || nr > MAX_BPF_FUNC_ARGS)
> +		return ERR_PTR(-EINVAL);
> +
> +	size_in = kattr->test.ctx_size_in;
> +	if (size_in != sizeof(u64) * nr)
> +		return ERR_PTR(-EINVAL);
> +
> +	args = kzalloc(sizeof(*args), GFP_KERNEL);
> +	if (!args)
> +		return ERR_PTR(-ENOMEM);
> +
> +	ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
> +	if (copy_from_user(args->args, ctx_in, size_in))
args is leaked.

> +		return ERR_PTR(-EFAULT);
> +
> +	u_state = u64_to_user_ptr(args->args[0]);
> +	if (!u_state)
> +		return args;
> +
> +	if (copy_from_user(&args->state, u_state, sizeof(args->state))) {
> +		kfree(args);
> +		return ERR_PTR(-EFAULT);
> +	}
> +
> +	return args;
> +}

[ ... ]

> +int bpf_dummy_struct_ops_test_run(struct bpf_prog *prog,
> +				  const union bpf_attr *kattr,
> +				  union bpf_attr __user *uattr)
> +{
> +	const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;
> +	const struct btf_type *func_proto = prog->aux->attach_func_proto;
> +	struct bpf_dummy_ops_test_args *args = NULL;
> +	struct bpf_tramp_progs *tprogs = NULL;
args = NULL and tprogs = NULL are not needed.

> +	void *image = NULL;
> +	unsigned int op_idx;
> +	int err;
> +	int prog_ret;
> +
> +	args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto));
> +	if (IS_ERR(args))
> +		return PTR_ERR(args);
> +
> +	tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
> +	if (!tprogs) {
> +		err = -ENOMEM;
> +		goto out;
> +	}
> +
> +	image = bpf_jit_alloc_exec(PAGE_SIZE);
> +	if (!image) {
> +		err = -ENOMEM;
> +		goto out;
> +	}
> +	set_vm_flush_reset_perms(image);
> +
> +	op_idx = prog->expected_attach_type;
> +	err = bpf_struct_ops_prepare_trampoline(tprogs, prog,
> +						&st_ops->func_models[op_idx],
> +						image, image + PAGE_SIZE);
> +	if (err < 0)
> +		goto out;
> +
> +	set_memory_ro((long)image, 1);
> +	set_memory_x((long)image, 1);
> +	prog_ret = dummy_ops_call_op(image, args);
> +
> +	err = dummy_ops_copy_args(args);
> +	if (err)
> +		goto out;
> +	if (put_user(prog_ret, &uattr->test.retval))
> +		err = -EFAULT;
> +out:
> +	kfree(args);
> +	bpf_jit_free_exec(image);
> +	kfree(tprogs);
> +	return err;
> +}
> +
> +static int bpf_dummy_init(struct btf *btf)
> +{
> +	s32 type_id;
> +
> +	type_id = btf_find_by_name_kind(btf, "bpf_dummy_ops_state",
> +					BTF_KIND_STRUCT);
> +	if (type_id < 0)
> +		return -EINVAL;
> +
> +	dummy_ops_state = btf_type_by_id(btf, type_id);
Probably just do btf_find_by_name_kind("bpf_dummy_ops_state")
in bpf_dummy_ops_btf_struct_access() during each test.   There is
no need to optimize and cache it only for the testing purpose.

> +
> +	return 0;
> +}
> +
> +static bool bpf_dummy_ops_is_valid_access(int off, int size,
> +					  enum bpf_access_type type,
> +					  const struct bpf_prog *prog,
> +					  struct bpf_insn_access_aux *info)
> +{
> +	return bpf_check_btf_func_ctx_access(off, size, type, prog, info);
> +}
> +
> +static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log,
> +					   const struct btf *btf,
> +					   const struct btf_type *t, int off,
> +					   int size, enum bpf_access_type atype,
> +					   u32 *next_btf_id)
> +{
> +	int err;
> +
> +	if (atype != BPF_READ && t != dummy_ops_state) {
I think this can be further simplified.  The only struct that
the bpf_prog can access is bpf_dummy_ops_state, so the
"atype != BPF_READ" test can be removed.  The log message
then needs to be adjusted.

Others lgtm.

> +		bpf_log(log, "only write to bpf_dummy_ops_state is supported\n");
> +		return -EACCES;
> +	}
> +
> +	err = btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
> +	if (err < 0)
> +		return err;
> +
> +	return atype == BPF_READ ? err : NOT_INIT;
> +}

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ