public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Song Liu <song@kernel.org>,
	bpf@vger.kernel.org, fsverity@lists.linux.dev
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@kernel.org, kernel-team@meta.com, ebiggers@kernel.org,
	tytso@mit.edu, roberto.sassu@huaweicloud.com, kpsingh@kernel.org,
	vadfed@meta.com
Subject: Re: [PATCH v12 bpf-next 2/9] bpf: Factor out helper check_reg_const_str()
Date: Mon, 6 Nov 2023 16:40:11 +0000	[thread overview]
Message-ID: <6091db94-f730-40e8-a9c4-1ee6a3c56d11@linux.dev> (raw)
In-Reply-To: <20231104001313.3538201-3-song@kernel.org>

On 04/11/2023 00:13, Song Liu wrote:
> ARG_PTR_TO_CONST_STR is used to specify constant string args for BPF
> helpers. The logic that verifies a reg is ARG_PTR_TO_CONST_STR is
> implemented in check_func_arg().
> 
> As we introduce kfuncs with constant string args, it is necessary to
> do the same check for kfuncs (in check_kfunc_args). Factor out the logic
> for ARG_PTR_TO_CONST_STR to a new check_reg_const_str() so that it can be
> reused.
> 
> check_func_arg() ensures check_reg_const_str() is only called with reg of
> type PTR_TO_MAP_VALUE. Add a redundent type check in check_reg_const_str()
> to avoid misuse in the future. Other than this redundent check, there is
> no change in behavior.
> 
> Signed-off-by: Song Liu <song@kernel.org>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
> ---
>   kernel/bpf/verifier.c | 85 +++++++++++++++++++++++++------------------
>   1 file changed, 49 insertions(+), 36 deletions(-)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2197385d91dc..618446006d5a 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -8718,6 +8718,54 @@ static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,
>   	return state->stack[spi].spilled_ptr.dynptr.type;
>   }
>   
> +static int check_reg_const_str(struct bpf_verifier_env *env,
> +			       struct bpf_reg_state *reg, u32 regno)
> +{
> +	struct bpf_map *map = reg->map_ptr;
> +	int err;
> +	int map_off;
> +	u64 map_addr;
> +	char *str_ptr;
> +
> +	if (reg->type != PTR_TO_MAP_VALUE)
> +		return -EINVAL;
> +
> +	if (!bpf_map_is_rdonly(map)) {
> +		verbose(env, "R%d does not point to a readonly map'\n", regno);
> +		return -EACCES;
> +	}
> +
> +	if (!tnum_is_const(reg->var_off)) {
> +		verbose(env, "R%d is not a constant address'\n", regno);
> +		return -EACCES;
> +	}
> +
> +	if (!map->ops->map_direct_value_addr) {
> +		verbose(env, "no direct value access support for this map type\n");
> +		return -EACCES;
> +	}
> +
> +	err = check_map_access(env, regno, reg->off,
> +			       map->value_size - reg->off, false,
> +			       ACCESS_HELPER);
> +	if (err)
> +		return err;
> +
> +	map_off = reg->off + reg->var_off.value;
> +	err = map->ops->map_direct_value_addr(map, &map_addr, map_off);
> +	if (err) {
> +		verbose(env, "direct value access on string failed\n");
> +		return err;
> +	}
> +
> +	str_ptr = (char *)(long)(map_addr);
> +	if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) {
> +		verbose(env, "string is not zero-terminated\n");
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
>   static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>   			  struct bpf_call_arg_meta *meta,
>   			  const struct bpf_func_proto *fn,
> @@ -8962,44 +9010,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>   	}
>   	case ARG_PTR_TO_CONST_STR:
>   	{
> -		struct bpf_map *map = reg->map_ptr;
> -		int map_off;
> -		u64 map_addr;
> -		char *str_ptr;
> -
> -		if (!bpf_map_is_rdonly(map)) {
> -			verbose(env, "R%d does not point to a readonly map'\n", regno);
> -			return -EACCES;
> -		}
> -
> -		if (!tnum_is_const(reg->var_off)) {
> -			verbose(env, "R%d is not a constant address'\n", regno);
> -			return -EACCES;
> -		}
> -
> -		if (!map->ops->map_direct_value_addr) {
> -			verbose(env, "no direct value access support for this map type\n");
> -			return -EACCES;
> -		}
> -
> -		err = check_map_access(env, regno, reg->off,
> -				       map->value_size - reg->off, false,
> -				       ACCESS_HELPER);
> +		err = check_reg_const_str(env, reg, regno);
>   		if (err)
>   			return err;
> -
> -		map_off = reg->off + reg->var_off.value;
> -		err = map->ops->map_direct_value_addr(map, &map_addr, map_off);
> -		if (err) {
> -			verbose(env, "direct value access on string failed\n");
> -			return err;
> -		}
> -
> -		str_ptr = (char *)(long)(map_addr);
> -		if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) {
> -			verbose(env, "string is not zero-terminated\n");
> -			return -EINVAL;
> -		}
>   		break;
>   	}
>   	case ARG_PTR_TO_KPTR:

Acked-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>

  reply	other threads:[~2023-11-06 16:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-04  0:13 [PATCH v12 bpf-next 0/9] bpf: File verification with LSM and fsverity Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 1/9] bpf: Add __bpf_dynptr_data* for in kernel use Song Liu
2023-11-06 16:39   ` Vadim Fedorenko
2023-11-06 21:07   ` Andrii Nakryiko
2023-11-06 22:00     ` Song Liu
2023-11-06 22:40       ` Andrii Nakryiko
2023-11-04  0:13 ` [PATCH v12 bpf-next 2/9] bpf: Factor out helper check_reg_const_str() Song Liu
2023-11-06 16:40   ` Vadim Fedorenko [this message]
2023-11-04  0:13 ` [PATCH v12 bpf-next 3/9] bpf: Introduce KF_ARG_PTR_TO_CONST_STR Song Liu
2023-11-06 16:43   ` Vadim Fedorenko
2023-11-04  0:13 ` [PATCH v12 bpf-next 4/9] bpf: Add kfunc bpf_get_file_xattr Song Liu
2023-11-04  9:11   ` Alexei Starovoitov
2023-11-04 14:20     ` Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 5/9] bpf, fsverity: Add kfunc bpf_get_fsverity_digest Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 6/9] Documentation/bpf: Add documentation for filesystem kfuncs Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 7/9] selftests/bpf: Sort config in alphabetic order Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 8/9] selftests/bpf: Add tests for filesystem kfuncs Song Liu
2023-11-04  0:13 ` [PATCH v12 bpf-next 9/9] selftests/bpf: Add test that uses fsverity and xattr to sign a file Song Liu
     [not found] ` <CAADnVQLZ7RkH2ykEohFdDLJkjhmizHUuBakoevjEwvxOFMFjBw@mail.gmail.com>
2023-11-04 14:05   ` [PATCH v12 bpf-next 0/9] bpf: File verification with LSM and fsverity Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6091db94-f730-40e8-a9c4-1ee6a3c56d11@linux.dev \
    --to=vadim.fedorenko@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=ebiggers@kernel.org \
    --cc=fsverity@lists.linux.dev \
    --cc=kernel-team@meta.com \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=roberto.sassu@huaweicloud.com \
    --cc=song@kernel.org \
    --cc=tytso@mit.edu \
    --cc=vadfed@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox